exchange_Fv.c 10.3 KB
Newer Older
Simone Butzer's avatar
Simone Butzer 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
/*------------------------------------------------------------------------
 * Copyright (C) 2015 For the list of authors, see file AUTHORS.
 *
 * This file is part of IFOS3D.
 * 
 * IFOS3D is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 2.0 of the License only.
 * 
 * IFOS3D is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with IFOS3D. See file COPYING and/or 
 * <http://www.gnu.org/licenses/gpl-2.0.html>.
--------------------------------------------------------------------------*/

/*------------------------------------------------------------------------
 * exchange of wavefield velocities (frequency donain) at grid boundaries between processors
 * when using the standard staggered grid
 *  ----------------------------------------------------------------------*/

#include "fd.h"

double exchange_Fv(float **** vx, float **** vy, float **** vz,int nf,
float *** bufferlef_to_rig, float *** bufferrig_to_lef, 
float *** buffertop_to_bot, float *** bufferbot_to_top, 
float *** bufferfro_to_bac, float *** bufferbac_to_fro, MPI_Request * req_send, MPI_Request * req_rec, int ntr_hess){


	extern int NX, NY, NZ, POS[4], NPROCX, NPROCY, NPROCZ, BOUNDARY, FDORDER, INDEX[7];
	extern const int TAG1,TAG2,TAG3,TAG4,TAG5,TAG6;
	
	MPI_Status status;	
	int i, j, k, l, n, nf1, nf2,m;
	double time=0.0; /*, time1=0.0;*/

        nf1=3*FDORDER/2-1;
	nf2=nf1-1; 
	 
	/*if (LOG){
	if (MYID==0) time1=MPI_Wtime();}*/
	
	for(m=1;m<=nf*(ntr_hess+1);m++){
		/* top-bottom -----------------------------------------------------------*/	

		if (POS[2]!=0)	/* no boundary exchange at top of global grid */
		for (i=1;i<=NX;i++){
			for (k=1;k<=NZ;k++){

				/* storage of top of local volume into buffer */
				n=1;
				for (l=1;l<=FDORDER/2;l++){
				buffertop_to_bot[i][k][n++]  =  vx[m][l][i][k];
				buffertop_to_bot[i][k][n++]  =  vz[m][l][i][k];
				}
				for (l=1;l<=(FDORDER/2-1);l++)
				buffertop_to_bot[i][k][n++]  =  vy[m][l][i][k];			
			}
		}
		


		if (POS[2]!=NPROCY-1)	/* no boundary exchange at bottom of global grid */
		for (i=1;i<=NX;i++){
			for (k=1;k<=NZ;k++){

				/* storage of bottom of local volume into buffer */
				n=1;
				for (l=1;l<=FDORDER/2;l++)
				bufferbot_to_top[i][k][n++]  =  vy[m][NY-l+1][i][k];

				for (l=1;l<=(FDORDER/2-1);l++){
				bufferbot_to_top[i][k][n++]  =  vx[m][NY-l+1][i][k];
				bufferbot_to_top[i][k][n++]  =  vz[m][NY-l+1][i][k];
				}
			}
		}
		
		
		/* persistent communication see comm_ini.c*/
		/*for (i=4;i<=5;i++){*/
			/* send and reveive values at edges of the local grid */
		/*	MPI_Start(&req_send[i]);
			MPI_Wait(&req_send[i],&status);
			MPI_Start(&req_rec[i]);
			MPI_Wait(&req_rec[i],&status);
		}*/


		MPI_Sendrecv_replace(&buffertop_to_bot[1][1][1],NX*NZ*nf1,MPI_FLOAT,INDEX[3],TAG5,INDEX[4],TAG5,MPI_COMM_WORLD,&status);
		MPI_Sendrecv_replace(&bufferbot_to_top[1][1][1],NX*NZ*nf2,MPI_FLOAT,INDEX[4],TAG6,INDEX[3],TAG6,MPI_COMM_WORLD,&status);

	/*	
		MPI_Bsend(&buffertop_to_bot[1][1][1],NX*NZ*nf1,MPI_FLOAT,INDEX[3],TAG5,MPI_COMM_WORLD);
		MPI_Barrier(MPI_COMM_WORLD);
		MPI_Recv(&buffertop_to_bot[1][1][1], NX*NZ*nf1,MPI_FLOAT,INDEX[4],TAG5,MPI_COMM_WORLD,&status);
		MPI_Bsend(&bufferbot_to_top[1][1][1],NX*NZ*nf2,MPI_FLOAT,INDEX[4],TAG6,MPI_COMM_WORLD);
		MPI_Barrier(MPI_COMM_WORLD);
		MPI_Recv(&bufferbot_to_top[1][1][1], NX*NZ*nf2,MPI_FLOAT,INDEX[3],TAG6,MPI_COMM_WORLD,&status);			
	*/          
				
		if (POS[2]!=NPROCY-1)	/* no boundary exchange at bottom of global grid */
		for (i=1;i<=NX;i++){
			for (k=1;k<=NZ;k++){

				n=1;
				for (l=1;l<=FDORDER/2;l++){
				vx[m][NY+l][i][k] = buffertop_to_bot[i][k][n++];
				vz[m][NY+l][i][k] = buffertop_to_bot[i][k][n++];
				}
				
				for (l=1;l<=(FDORDER/2-1);l++)
				vy[m][NY+l][i][k] = buffertop_to_bot[i][k][n++];
				
			
			}
		}
		

		if (POS[2]!=0)	/* no boundary exchange at top of global grid */
		for (i=1;i<=NX;i++){
			for (k=1;k<=NZ;k++){

				n=1;
				for (l=1;l<=FDORDER/2;l++)
				vy[m][1-l][i][k] = bufferbot_to_top[i][k][n++];
				
				for (l=1;l<=(FDORDER/2-1);l++){
				vx[m][1-l][i][k] = bufferbot_to_top[i][k][n++];
				vz[m][1-l][i][k] = bufferbot_to_top[i][k][n++];
				}
			}
		}

		/* left-right -----------------------------------------------------------*/	
		
		
		if ((BOUNDARY) || (POS[1]!=0))	/* no boundary exchange at left edge of global grid */
		for (j=1;j<=NY;j++){
			for (k=1;k<=NZ;k++){

				/* storage of left edge of local volume into buffer */
				n=1;
				for (l=1;l<=FDORDER/2;l++){
				bufferlef_to_rig[j][k][n++]  =  vy[m][j][l][k];
				bufferlef_to_rig[j][k][n++]  =  vz[m][j][l][k];
				}

				for (l=1;l<=(FDORDER/2-1);l++)
				bufferlef_to_rig[j][k][n++]  =  vx[m][j][l][k];
			}
		}


							/* no exchange if periodic boundary condition is applied */
		if ((BOUNDARY) || (POS[1]!=NPROCX-1))	/* no boundary exchange at right edge of global grid */
		for (j=1;j<=NY;j++){
			for (k=1;k<=NZ;k++){
				/* storage of right edge of local volume into buffer */
				n=1;
				for (l=1;l<=FDORDER/2;l++)
				bufferrig_to_lef[j][k][n++] =  vx[m][j][NX-l+1][k];

				for (l=1;l<=(FDORDER/2-1);l++){
				bufferrig_to_lef[j][k][n++] =  vy[m][j][NX-l+1][k];
				bufferrig_to_lef[j][k][n++] =  vz[m][j][NX-l+1][k];
				}
			}
		}


		/* persistent communication see comm_ini.c*/
		/*for (i=0;i<=1;i++){*/
			/* send and reveive values at edges of the local grid */
		/*	MPI_Start(&req_send[i]);
			MPI_Wait(&req_send[i],&status);
			MPI_Start(&req_rec[i]);
			MPI_Wait(&req_rec[i],&status);
		}*/
		
		
		MPI_Sendrecv_replace(&bufferlef_to_rig[1][1][1],NY*NZ*nf1,MPI_FLOAT,INDEX[1],TAG1,INDEX[2],TAG1,MPI_COMM_WORLD,&status);
		MPI_Sendrecv_replace(&bufferrig_to_lef[1][1][1],NY*NZ*nf2,MPI_FLOAT,INDEX[2],TAG2,INDEX[1],TAG2,MPI_COMM_WORLD,&status);
		
		
	/*	MPI_Bsend(&bufferlef_to_rig[1][1][1],NY*NZ*nf1,MPI_FLOAT,INDEX[1],TAG1,MPI_COMM_WORLD);
		MPI_Barrier(MPI_COMM_WORLD);
		MPI_Recv(&bufferlef_to_rig[1][1][1], NY*NZ*nf1,MPI_FLOAT,INDEX[2],TAG1,MPI_COMM_WORLD,&status);
		MPI_Bsend(&bufferrig_to_lef[1][1][1],NY*NZ*nf2,MPI_FLOAT,INDEX[2],TAG2,MPI_COMM_WORLD);
		MPI_Barrier(MPI_COMM_WORLD);
		MPI_Recv(&bufferrig_to_lef[1][1][1], NY*NZ*nf2,MPI_FLOAT,INDEX[1],TAG2,MPI_COMM_WORLD,&status);			
	*/

		if ((BOUNDARY) || (POS[1]!=NPROCX-1))	/* no boundary exchange at right edge of global grid */
		for (j=1;j<=NY;j++){
			for (k=1;k<=NZ;k++){

				n=1;
				for (l=1;l<=FDORDER/2;l++){
				vy[m][j][NX+l][k] = bufferlef_to_rig[j][k][n++];
				vz[m][j][NX+l][k] = bufferlef_to_rig[j][k][n++];
				}

				for (l=1;l<=(FDORDER/2-1);l++)
				vx[m][j][NX+l][k] = bufferlef_to_rig[j][k][n++];		
			}
		}

							/* no exchange if periodic boundary condition is applied */
		if ((BOUNDARY) || (POS[1]!=0))	/* no boundary exchange at left edge of global grid */
		for (j=1;j<=NY;j++){
			for (k=1;k<=NZ;k++){

				n=1;
				for (l=1;l<=FDORDER/2;l++)
				vx[m][j][1-l][k] = bufferrig_to_lef[j][k][n++];

				for (l=1;l<=(FDORDER/2-1);l++){
				vy[m][j][1-l][k] = bufferrig_to_lef[j][k][n++];
				vz[m][j][1-l][k] = bufferrig_to_lef[j][k][n++];
				}

			
			}
		}	
		
			
			/* front-back -----------------------------------------------------------*/	

		
		if ((BOUNDARY) || (POS[3]!=0))	/* no boundary exchange at front side of global grid */
		for (i=1;i<=NX;i++){
			for (j=1;j<=NY;j++){

				/* storage of front side of local volume into buffer */
				n=1;
				for (l=1;l<=FDORDER/2;l++){
				bufferfro_to_bac[j][i][n++]  =  vx[m][j][i][l];
				bufferfro_to_bac[j][i][n++]  =  vy[m][j][i][l];
				}

				for (l=1;l<=(FDORDER/2-1);l++)
				bufferfro_to_bac[j][i][n++]  =  vz[m][j][i][l];			
			}
		}


							/* no exchange if periodic boundary condition is applied */
		if ((BOUNDARY) || (POS[3]!=NPROCZ-1))	/* no boundary exchange at back side of global grid */
		for (i=1;i<=NX;i++){
			for (j=1;j<=NY;j++){
				
				/* storage of back side of local volume into buffer */
				n=1;
				for (l=1;l<=FDORDER/2;l++)
				bufferbac_to_fro[j][i][n++]  =  vz[m][j][i][NZ-l+1];
				
				for (l=1;l<=(FDORDER/2-1);l++){
				bufferbac_to_fro[j][i][n++]  =  vx[m][j][i][NZ-l+1];
				bufferbac_to_fro[j][i][n++]  =  vy[m][j][i][NZ-l+1];
				}
				
			}
		}


		/* persistent communication see comm_ini.c*/
		/*for (i=2;i<=3;i++){*/
			/* send and reveive values at edges of the local grid */
		/*	MPI_Start(&req_send[i]);
			MPI_Wait(&req_send[i],&status);
			MPI_Start(&req_rec[i]);
			MPI_Wait(&req_rec[i],&status);
		}*/
		
		MPI_Sendrecv_replace(&bufferfro_to_bac[1][1][1],NX*NY*nf1,MPI_FLOAT,INDEX[5],TAG3,INDEX[6],TAG3,MPI_COMM_WORLD,&status);
		MPI_Sendrecv_replace(&bufferbac_to_fro[1][1][1],NX*NY*nf2,MPI_FLOAT,INDEX[6],TAG4,INDEX[5],TAG4,MPI_COMM_WORLD,&status);

	/*

		MPI_Bsend(&bufferfro_to_bac[1][1][1],NX*NY*nf1,MPI_FLOAT,INDEX[5],TAG3,MPI_COMM_WORLD);
		MPI_Barrier(MPI_COMM_WORLD);
		MPI_Recv(&bufferfro_to_bac[1][1][1], NX*NY*nf1,MPI_FLOAT,INDEX[6],TAG3,MPI_COMM_WORLD,&status);
		MPI_Bsend(&bufferbac_to_fro[1][1][1],NX*NY*nf2,MPI_FLOAT,INDEX[6],TAG4,MPI_COMM_WORLD);
		MPI_Barrier(MPI_COMM_WORLD);
		MPI_Recv(&bufferbac_to_fro[1][1][1], NX*NY*nf2,MPI_FLOAT,INDEX[5],TAG4,MPI_COMM_WORLD,&status);			
	*/

		/* no exchange if periodic boundary condition is applied */
		if ((BOUNDARY) || (POS[3]!=NPROCZ-1))	/* no boundary exchange at back side of global grid */
		for (i=1;i<=NX;i++){
			for (j=1;j<=NY;j++){

				n=1;
				for (l=1;l<=FDORDER/2;l++){
				vx[m][j][i][NZ+l] = bufferfro_to_bac[j][i][n++];
				vy[m][j][i][NZ+l] = bufferfro_to_bac[j][i][n++];
				}
				
				for (l=1;l<=(FDORDER/2-1);l++)
				vz[m][j][i][NZ+l] = bufferfro_to_bac[j][i][n++];


			}
		}


							/* no exchange if periodic boundary condition is applied */
		if ((BOUNDARY) || (POS[3]!=0))	/* no boundary exchange at front side of global grid */
		for (i=1;i<=NX;i++){
			for (j=1;j<=NY;j++){
				n=1;
				for (l=1;l<=FDORDER/2;l++)
				vz[m][j][i][1-l] = bufferbac_to_fro[j][i][n++];
				
				for (l=1;l<=(FDORDER/2-1);l++){
				vx[m][j][i][1-l] = bufferbac_to_fro[j][i][n++];
				vy[m][j][i][1-l] = bufferbac_to_fro[j][i][n++];
				}
			}
		}

	}
	/*if (LOG)
	if (MYID==0){
		time2=MPI_Wtime();
		time=time2-time1;
		fprintf(FP," Real time for particle velocity exchange: \t %4.2f s.\n",time);
	}*/
	return time;

}