find_global_min_and_max_MASKED.F90 24.1 KB
Newer Older
Femi Kadri's avatar
Femi Kadri committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
SUBROUTINE find_global_min_and_max_MASKED(&
& My_MPI_Process_ID,&
& local_x_min_real,local_x_max_real,&
& local_y_min_real,local_y_max_real,&
& local_z_min_real,local_z_max_real,&
& local_x_min_ghost,local_x_max_ghost,&
& local_y_min_ghost,local_y_max_ghost,&
& local_z_min_ghost,local_z_max_ghost,&
& original_cartesian_communicator,&
& my_mpi_real_data_type,&
& local_walls,&
& LST_local_vol_of_int_user_mask,&
& local_3d_array,&
& global_3d_array_IN_MASK_min,&
& global_3d_array_IN_MASK_max,&
& global_3d_array_OUT_MASK_min,&
& global_3d_array_OUT_MASK_max)

!THIS SUBROUTINE SEARCHES FOR THE LOCAL MIN AND MAX AT THE SAME TIME,
! AND THEN COMMUNICATES THE LOCAL VALUES TO OBTAIN THE GLOBAL EXTREMAS

!THIS SUBROUTINE ASSUMES THAT PROCESS RANKS ARE NONNEGATIVE
!IT TRIES TO FIND THE LOCAL MIN AND MAX OF AN ARRAY, 
!BUT WHEN IT FAILS TO DO SO (FOR EXAMPLE WHEN ONLY WALLS BELONG TO THIS PROCESS)
!THE PROCESS WILL OPT OUT OF COMMUNICATION.  AFTER THE GLOBAL MIN AND MAX ARE DETERMINED
!THEY ARE COMMUNICATED TO THE PROCESSES THAT INITIALLY OPTED OUT OF COMMUNICATION.


USE debug_module


	IMPLICIT NONE

	INCLUDE "mpif.h"

	!-----------INTENT(IN)------------------------------------------
	INTEGER,INTENT(IN) :: My_MPI_Process_ID

	INTEGER,INTENT(IN) :: local_x_min_real,local_x_max_real
	INTEGER,INTENT(IN) :: local_y_min_real,local_y_max_real
	INTEGER,INTENT(IN) :: local_z_min_real,local_z_max_real

	INTEGER,INTENT(IN) :: local_x_min_ghost,local_x_max_ghost
	INTEGER,INTENT(IN) :: local_y_min_ghost,local_y_max_ghost
	INTEGER,INTENT(IN) :: local_z_min_ghost,local_z_max_ghost

	INTEGER,INTENT(IN) :: original_cartesian_communicator

	INTEGER,INTENT(IN) :: my_mpi_real_data_type

	REAL,INTENT(IN),DIMENSION(&
	& local_x_min_ghost:local_x_max_ghost,&
	& local_y_min_ghost:local_y_max_ghost,&
	& local_z_min_ghost:local_z_max_ghost) :: local_3d_array

	LOGICAL*4,DIMENSION(&
	& local_x_min_ghost:local_x_max_ghost,&
	& local_y_min_ghost:local_y_max_ghost,&
	& local_z_min_ghost:local_z_max_ghost),&
	& INTENT(IN) :: local_walls,LST_local_vol_of_int_user_mask

	LOGICAL*4,PARAMETER :: debug = GLOBAL_DEBUG
	!-----------INTENT(OUT)------------------------------------------

	REAL,INTENT(OUT) :: global_3d_array_IN_MASK_min,global_3d_array_IN_MASK_max
	REAL,INTENT(OUT) :: global_3d_array_OUT_MASK_min,global_3d_array_OUT_MASK_max
	!-----------INTENT(INOUT)------------------------------------------	
	!-----------NO INTENT------------------------------------------
	INTEGER :: I,J,K
	INTEGER :: mpi_error_code,mpi_abort_error
	
	INTEGER :: new_split_cartesian_IN_MASK_comm,new_split_cartesian_OUT_MASK_com

	INTEGER :: this_process_comm_IN_MASK_color,this_process_comm_OUT_MASK_color

	INTEGER :: local_comm_IN_MASK_leader_rank,global_comm_IN_MASK_leader_rank
	INTEGER :: local_comm_OUT_MASK_leader_rank,global_comm_OUT_MASK_leader_rank


	REAL :: local_3d_array_IN_MASK_min,local_3d_array_IN_MASK_max
	REAL :: local_3d_array_OUT_MASK_min,local_3d_array_OUT_MASK_max
	REAL :: local_current_3d_array_value

	LOGICAL:: set_min_and_max_vals_IN_MASK,set_min_and_max_vals_OUT_MASK


	IF(debug) WRITE (0,*) My_MPI_Process_ID,'entered SUBROUTINE find_global_min_and_max_MASKED'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'entered SUBROUTINE find_global_min_and_max_MASKED'



	local_comm_IN_MASK_leader_rank  = - 1  !INITIALLY SET TO SOME NEGATIVE VALUE
	global_comm_IN_MASK_leader_rank = - 1  !INITIALLY SET TO SOME NEGATIVE VALUE

	local_comm_OUT_MASK_leader_rank  = - 2  !INITIALLY SET TO SOME NEGATIVE VALUE
	global_comm_OUT_MASK_leader_rank = - 2  !INITIALLY SET TO SOME NEGATIVE VALUE


	!FIND LOCAL MIN AND MAX IN ONE LOOP
	set_min_and_max_vals_IN_MASK = .FALSE.
	set_min_and_max_vals_OUT_MASK = .FALSE.


	DO K= local_z_min_real, local_z_max_real

		DO J= local_y_min_real, local_y_max_real

			DO I = local_x_min_real, local_x_max_real

				IF( .NOT.local_walls(I,J,K) ) THEN

					local_current_3d_array_value = &
					& local_3d_array(I,J,K)

					IF(	LST_local_vol_of_int_user_mask(I,J,K) ) THEN  !CHECK TO SEE IF THIS VALUE BELONGS TO THE USER MASK
							
						IF(.NOT.set_min_and_max_vals_IN_MASK) THEN  !CHECK TO SEE IF THE INITIAL MIN AND MAX VALUES HAVE BEEN SET													

							local_3d_array_IN_MASK_min = &
							& local_current_3d_array_value

							local_3d_array_IN_MASK_max = &
							& local_current_3d_array_value

							set_min_and_max_vals_IN_MASK = .TRUE.

						ELSE  !set_min_and_max_vals_IN_MASK.EQ..TRUE.

							IF(local_current_3d_array_value.LT.local_3d_array_IN_MASK_min) &
							& local_3d_array_IN_MASK_min = local_current_3d_array_value 

							IF(local_current_3d_array_value.GT.local_3d_array_IN_MASK_max) &
							& local_3d_array_IN_MASK_max = local_current_3d_array_value 

						ENDIF !!set_min_and_max_vals_IN_MASK

					ELSE !.NOT.LST_local_vol_of_int_user_mask(I,J,K)

						IF(.NOT.set_min_and_max_vals_OUT_MASK) THEN	 !CHECK TO SEE IF THE INITIAL MIN AND MAX VALUES HAVE BEEN SET												

							local_3d_array_OUT_MASK_min = &
							& local_current_3d_array_value

							local_3d_array_OUT_MASK_max = &
							& local_current_3d_array_value

							set_min_and_max_vals_OUT_MASK = .TRUE.

						ELSE  !set_min_and_max_vals_OUT_MASK.EQ..TRUE.

							IF(local_current_3d_array_value.LT.local_3d_array_OUT_MASK_min) &
							& local_3d_array_OUT_MASK_min = local_current_3d_array_value 

							IF(local_current_3d_array_value.GT.local_3d_array_OUT_MASK_max) &
							& local_3d_array_OUT_MASK_max = local_current_3d_array_value 

						ENDIF !!set_min_and_max_vals_OUT_MASK
							
					ENDIF !!LST_local_vol_of_int_user_mask(I,J,K)							

				ENDIF !! .NOT.local_walls(I,J,K)

			END DO !I = local_x_min_real, local_x_max_real

		END DO	!J= local_y_min_real, local_y_max_real
			
	END DO !K= local_z_min_real, local_z_max_real


	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: set_min_and_max_vals_IN_MASK',set_min_and_max_vals_IN_MASK
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: set_min_and_max_vals_IN_MASK',set_min_and_max_vals_IN_MASK


	IF(.NOT.set_min_and_max_vals_IN_MASK) THEN

		this_process_comm_IN_MASK_color = MPI_UNDEFINED !OPT OUT OF COMMUNICATION IF THIS PROCESS DOESN'T CONTAIN LOCAL MIN AND MAX VALUES

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: this_process_comm_IN_MASK_color = MPI_UNDEFINED'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: this_process_comm_IN_MASK_color = MPI_UNDEFINED'

	ELSE

		this_process_comm_IN_MASK_color = 96  !CHOSE SOME POSITIVE INTEGER FOR COMM COLOR (USED TO SPLIT THE COMMUNICATOR)

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: local_3d_array_IN_MASK_min',local_3d_array_IN_MASK_min
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: local_3d_array_IN_MASK_min',local_3d_array_IN_MASK_min

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: local_3d_array_IN_MASK_max',local_3d_array_IN_MASK_max
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: local_3d_array_IN_MASK_max',local_3d_array_IN_MASK_max
	
	ENDIF !! local_non_masked_value_counter.EQ.0


	IF(.NOT.set_min_and_max_vals_OUT_MASK) THEN

		this_process_comm_OUT_MASK_color = MPI_UNDEFINED !OPT OUT OF COMMUNICATION IF THIS PROCESS DOESN'T CONTAIN LOCAL MIN AND MAX VALUES

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: this_process_comm_OUT_MASK_color = MPI_UNDEFINED'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: this_process_comm_OUT_MASK_color = MPI_UNDEFINED'

	ELSE
        
		this_process_comm_OUT_MASK_color = 13  !CHOSE SOME POSITIVE INTEGER FOR COMM COLOR (USED TO SPLIT THE COMMUNICATOR)

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: local_3d_array_OUT_MASK_min',local_3d_array_OUT_MASK_min
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: local_3d_array_OUT_MASK_min',local_3d_array_OUT_MASK_min

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: local_3d_array_OUT_MASK_max',local_3d_array_OUT_MASK_max
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: local_3d_array_OUT_MASK_max',local_3d_array_OUT_MASK_max
	
	ENDIF !! local_non_masked_value_counter.EQ.0

	!----------------------------------------------------
	!SPLIT THE original_cartesian_communicator COMMUNICATOR ACCORDING TO LOCAL IN_MASK PROCESS COLOR
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: about to CALL MPI_Comm_split ->this_process_comm_IN_MASK_color'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: about to CALL MPI_Comm_split ->this_process_comm_IN_MASK_color'

	CALL MPI_Comm_split(original_cartesian_communicator,this_process_comm_IN_MASK_color,&
	& 0,new_split_cartesian_IN_MASK_comm,mpi_error_code) !ZERO MEANS WE DON'T CARE ABOUT THE MY RANK WITHIN NEW COMMUNICATOR

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED to CALL MPI_Comm_split ->this_process_comm_IN_MASK_color'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED to CALL MPI_Comm_split ->this_process_comm_IN_MASK_color'


	!SPLIT THE original_cartesian_communicator COMMUNICATOR ACCORDING TO LOCAL OUT_MASK PROCESS COLOR
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: about to CALL MPI_Comm_split ->this_process_comm_OUT_MASK_color'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: about to CALL MPI_Comm_split ->this_process_comm_OUT_MASK_color'

	CALL MPI_Comm_split(original_cartesian_communicator,this_process_comm_OUT_MASK_color,&
	& 0,new_split_cartesian_OUT_MASK_com,mpi_error_code) !ZERO MEANS WE DON'T CARE ABOUT THE MY RANK WITHIN NEW COMMUNICATOR

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED to CALL MPI_Comm_split ->this_process_comm_OUT_MASK_color'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED to CALL MPI_Comm_split ->this_process_comm_OUT_MASK_color'
	!----------------------------------------------------


	IF(new_split_cartesian_IN_MASK_comm /= MPI_COMM_NULL ) THEN !COMMUNICATE local -> global _IN_MASK min & max AMONG PROCESSES THAT HAVE THEM
		
		!IN MASK:  COMMUNICATE LOCAL MIN TO GET THE GLOBAL MIN
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: about to CALL MPI_Allreduce:local_3d_array_IN_MASK_min'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: about to CALL MPI_Allreduce:local_3d_array_IN_MASK_min'

		CALL MPI_Allreduce(local_3d_array_IN_MASK_min,global_3d_array_IN_MASK_min,1,&
		& my_mpi_real_data_type,MPI_MIN,new_split_cartesian_IN_MASK_comm,mpi_error_code)
			
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: finished to CALL MPI_Allreduce:local_3d_array_IN_MASK_min'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: finished to CALL MPI_Allreduce:local_3d_array_IN_MASK_min'

		WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: global_3d_array_IN_MASK_min',global_3d_array_IN_MASK_min
		WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: global_3d_array_IN_MASK_min',global_3d_array_IN_MASK_min


		!IN MASK:  COMMUNICATE LOCAL MAX TO GET THE GLOBAL MAX
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: about to CALL MPI_Allreduce:local_3d_array_IN_MASK_max'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: about to CALL MPI_Allreduce:local_3d_array_IN_MASK_max'

		CALL MPI_Allreduce(local_3d_array_IN_MASK_max,global_3d_array_IN_MASK_max,1,&
		& my_mpi_real_data_type,MPI_MAX,new_split_cartesian_IN_MASK_comm,mpi_error_code)

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: finished to CALL MPI_Allreduce:local_3d_array_IN_MASK_max'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: finished to CALL MPI_Allreduce:local_3d_array_IN_MASK_max'

		WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: global_3d_array_IN_MASK_max',global_3d_array_IN_MASK_max
		WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: global_3d_array_IN_MASK_max',global_3d_array_IN_MASK_max



		!ARBITRARILY PICK AN MPI PROCESS AS THE "LEADER", AMONG THOSE THAT HAVE MIN AND MAX VALUES (THOSE THAT DO NOT HAVE VALUES WILL HAVE A NEGATIVE VALUE FOR local_comm_IN_MASK_leader_rank)
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_ALLreduce My_MPI_Process_ID ->local_comm_IN_MASK_leader_rank'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_ALLreduce My_MPI_Process_ID ->local_comm_IN_MASK_leader_rank'

		CALL MPI_ALLreduce(My_MPI_Process_ID,local_comm_IN_MASK_leader_rank,1,&
		& MPI_INTEGER,MPI_MIN,new_split_cartesian_IN_MASK_comm,mpi_error_code)  

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_ALLreduce My_MPI_Process_ID ->local_comm_IN_MASK_leader_rank'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_ALLreduce My_MPI_Process_ID ->local_comm_IN_MASK_leader_rank'


		!RELEASE THE new_split_cartesian_IN_MASK_comm AS IT IS NO LONGER NEEDED
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_COMM_FREE: new_split_cartesian_IN_MASK_comm'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_COMM_FREE: new_split_cartesian_IN_MASK_comm'

		CALL MPI_COMM_FREE(new_split_cartesian_IN_MASK_comm,mpi_error_code)

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED CALLING MPI_COMM_FREE: new_split_cartesian_IN_MASK_comm'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED CALLING MPI_COMM_FREE: new_split_cartesian_IN_MASK_comm'

	ENDIF !!new_split_cartesian_IN_MASK_comm /= MPI_COMM_NULL

	!SINCE PROCESSES THAT DONT' HAVE MIN AND MAX VALUES CONTAIN A NEGATIVE VALUE FOR local_comm_IN_MASK_leader_rank, THEY WILL PICK THE NONZERO global_comm_IN_MASK_leader_rank
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_ALLreduce local_comm_IN_MASK_leader_rank ->global_comm_IN_MASK_leader_rank'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_ALLreduce local_comm_IN_MASK_leader_rank ->global_comm_IN_MASK_leader_rank'

	CALL MPI_ALLreduce(local_comm_IN_MASK_leader_rank,global_comm_IN_MASK_leader_rank,&
	& 1,MPI_INTEGER,MPI_MAX,original_cartesian_communicator,mpi_error_code) 

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_ALLreduce local_comm_IN_MASK_leader_rank ->global_comm_IN_MASK_leader_rank'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_ALLreduce local_comm_IN_MASK_leader_rank ->global_comm_IN_MASK_leader_rank'


	IF(global_comm_IN_MASK_leader_rank.LT.0) THEN  !CHECK TO MAKE SURE THAT global_comm_IN_MASK_leader_rank HAS BEEN SET
		
		PRINT*,My_MPI_Process_ID,'find_global_min_and_max_MASKED: ERROR:  global_comm_IN_MASK_leader_rank < 0',global_comm_IN_MASK_leader_rank
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ERROR:  global_comm_IN_MASK_leader_rank < 0',global_comm_IN_MASK_leader_rank
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ERROR:  global_comm_IN_MASK_leader_rank < 0',global_comm_IN_MASK_leader_rank

		CALL MPI_Abort(original_cartesian_communicator,mpi_abort_error,mpi_error_code)	

	ENDIF !global_comm_IN_MASK_leader_rank

	!BROADCAST THE _IN_MASK GLOBAL MIN AND MAX VALUES FROM COMMUNICATION LEADER WHO ACTUALLY DOES HAVE THEM TO EVERYONE ELSE
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_Bcast global_3d_array_IN_MASK_min'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_Bcast global_3d_array_IN_MASK_min'

	CALL MPI_Bcast(global_3d_array_IN_MASK_min,1,my_mpi_real_data_type,&
	& global_comm_IN_MASK_leader_rank,original_cartesian_communicator,mpi_error_code )

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_Bcast global_3d_array_IN_MASK_min'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_Bcast global_3d_array_IN_MASK_min'

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_Bcast global_3d_array_IN_MASK_max'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_Bcast global_3d_array_IN_MASK_max'

	CALL MPI_Bcast(global_3d_array_IN_MASK_max,1,my_mpi_real_data_type,&
	& global_comm_IN_MASK_leader_rank,original_cartesian_communicator,mpi_error_code )

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_Bcast global_3d_array_IN_MASK_max'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_Bcast global_3d_array_IN_MASK_max'



	!===================== OUT_MASK =======================================
	IF(new_split_cartesian_OUT_MASK_com /= MPI_COMM_NULL ) THEN !COMMUNICATE local -> global _OUT_MASK min & max AMONG PROCESSES THAT HAVE THEM
		
		!OUT OF MASK:  COMMUNICATE LOCAL MIN TO GET THE GLOBAL MIN 
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: about to CALL MPI_Allreduce:local_3d_array_OUT_MASK_min'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: about to CALL MPI_Allreduce:local_3d_array_OUT_MASK_min'

		CALL MPI_Allreduce(local_3d_array_OUT_MASK_min,global_3d_array_OUT_MASK_min,1,&
		& my_mpi_real_data_type,MPI_MIN,new_split_cartesian_OUT_MASK_com,mpi_error_code)
			
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: finished to CALL MPI_Allreduce:local_3d_array_OUT_MASK_min'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: finished to CALL MPI_Allreduce:local_3d_array_OUT_MASK_min'

		WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: global_3d_array_OUT_MASK_min',global_3d_array_OUT_MASK_min
		WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: global_3d_array_OUT_MASK_min',global_3d_array_OUT_MASK_min

		!OUT OF MASK:  COMMUNICATE LOCAL MAX TO GET THE GLOBAL MAX 
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: about to CALL MPI_Allreduce:local_3d_array_OUT_MASK_max'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: about to CALL MPI_Allreduce:local_3d_array_OUT_MASK_max'

		CALL MPI_Allreduce(local_3d_array_OUT_MASK_max,global_3d_array_OUT_MASK_max,1,&
		& my_mpi_real_data_type,MPI_MAX,new_split_cartesian_OUT_MASK_com,mpi_error_code)

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: finished to CALL MPI_Allreduce:local_3d_array_OUT_MASK_max'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: finished to CALL MPI_Allreduce:local_3d_array_OUT_MASK_max'

		WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: global_3d_array_OUT_MASK_max',global_3d_array_OUT_MASK_max
		WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: global_3d_array_OUT_MASK_max',global_3d_array_OUT_MASK_max



		!ARBITRARILY PICK AN MPI PROCESS AS THE "LEADER", AMONG THOSE THAT HAVE MIN AND MAX VALUES (THOSE THAT DO NOT HAVE VALUES WILL HAVE A NEGATIVE VALUE FOR local_comm_OUT_MASK_leader_rank)
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_ALLreduce My_MPI_Process_ID ->local_comm_OUT_MASK_leader_rank'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_ALLreduce My_MPI_Process_ID ->local_comm_OUT_MASK_leader_rank'

		CALL MPI_ALLreduce(My_MPI_Process_ID,local_comm_OUT_MASK_leader_rank,1,&
		& MPI_INTEGER,MPI_MIN,new_split_cartesian_OUT_MASK_com,mpi_error_code)  

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_ALLreduce My_MPI_Process_ID ->local_comm_OUT_MASK_leader_rank'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_ALLreduce My_MPI_Process_ID ->local_comm_OUT_MASK_leader_rank'


		!RELEASE THE new_split_cartesian_OUT_MASK_com AS IT IS NO LONGER NEEDED
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_COMM_FREE: new_split_cartesian_OUT_MASK_com'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_COMM_FREE: new_split_cartesian_OUT_MASK_com'

		CALL MPI_COMM_FREE(new_split_cartesian_OUT_MASK_com,mpi_error_code)

		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED CALLING MPI_COMM_FREE: new_split_cartesian_OUT_MASK_com'
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED CALLING MPI_COMM_FREE: new_split_cartesian_OUT_MASK_com'

	ENDIF !!new_split_cartesian_OUT_MASK_com /= MPI_COMM_NULL


	!SINCE PROCESSES THAT DONT' HAVE MIN AND MAX VALUES CONTAIN A NEGATIVE VALUE FOR local_comm_OUT_MASK_leader_rank, THEY WILL PICK THE NONZERO global_comm_OUT_MASK_leader_rank
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_ALLreduce local_comm_OUT_MASK_leader_rank ->global_comm_OUT_MASK_leader_rank'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_ALLreduce local_comm_OUT_MASK_leader_rank ->global_comm_OUT_MASK_leader_rank'

	CALL MPI_ALLreduce(local_comm_OUT_MASK_leader_rank,global_comm_OUT_MASK_leader_rank,&
	& 1,MPI_INTEGER,MPI_MAX,original_cartesian_communicator,mpi_error_code) 

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_ALLreduce local_comm_OUT_MASK_leader_rank ->global_comm_OUT_MASK_leader_rank'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_ALLreduce local_comm_OUT_MASK_leader_rank ->global_comm_OUT_MASK_leader_rank'

	IF(global_comm_OUT_MASK_leader_rank.LT.0) THEN  !CHECK TO MAKE SURE THAT global_comm_OUT_MASK_leader_rank HAS BEEN SET
		
		PRINT*,My_MPI_Process_ID,'find_global_min_and_max_MASKED: ERROR:  global_comm_OUT_MASK_leader_rank < 0',global_comm_OUT_MASK_leader_rank
		IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ERROR:  global_comm_OUT_MASK_leader_rank < 0',global_comm_OUT_MASK_leader_rank
		IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ERROR:  global_comm_OUT_MASK_leader_rank < 0',global_comm_OUT_MASK_leader_rank

		CALL MPI_Abort(original_cartesian_communicator,mpi_abort_error,mpi_error_code)	

	ENDIF !global_comm_OUT_MASK_leader_rank

	!BROADCAST THE _OUT_MASK GLOBAL MIN AND MAX VALUES FROM COMMUNICATION LEADER WHO ACTUALLY DOES HAVE THEM TO EVERYONE ELSE
	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_Bcast global_3d_array_OUT_MASK_min'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_Bcast global_3d_array_OUT_MASK_min'

	CALL MPI_Bcast(global_3d_array_OUT_MASK_min,1,my_mpi_real_data_type,&
	& global_comm_OUT_MASK_leader_rank,original_cartesian_communicator,mpi_error_code )

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_Bcast global_3d_array_OUT_MASK_min'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_Bcast global_3d_array_OUT_MASK_min'

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_Bcast global_3d_array_OUT_MASK_max'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: ABOUT TO CALL MPI_Bcast global_3d_array_OUT_MASK_max'

	CALL MPI_Bcast(global_3d_array_OUT_MASK_max,1,my_mpi_real_data_type,&
	& global_comm_OUT_MASK_leader_rank,original_cartesian_communicator,mpi_error_code )

	IF(debug) WRITE (0,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_Bcast global_3d_array_OUT_MASK_max'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'find_global_min_and_max_MASKED: FINISHED TO CALL MPI_Bcast global_3d_array_OUT_MASK_max'


	IF(debug) WRITE (0,*) My_MPI_Process_ID,'about to END SUBROUTINE find_global_min_and_max_MASKED'
	IF(debug) WRITE (9,*) My_MPI_Process_ID,'about to END SUBROUTINE find_global_min_and_max_MASKED'

RETURN
Femi Kadri's avatar
Femi Kadri committed
430
END SUBROUTINE find_global_min_and_max_MASKED