There are many municipal pools with proper infrastructure in Madrid. They usually open around mid-May and work until mid-September. Closer to the city centre you can visit the Canal Isabel II and Casa de Campo sports complexes. The first has a large Olympic swimming pool for adults and a kid's pool. In Casa de Campo there is one indoor and two outdoor pools, as well as a library and vast sunbathing areas.
The upper pool was chosen for recreation by the local gay community. And if you like "au naturel" tan, the municipal pool in Barrio del Pilar has a "corner" for nudists. And do not forget about sunscreen!
If you can not stay in the hot city, you can always leave it and go diving into the cool crystal waters of the Manzanares River in La Pedriza, a rocky desert in the foothills of the Sierra de Guadarrama.