remove copy from phi_in to psi_out in evolve_wave

Instead of copying phi_in to psi_out value by value in evolve_wave_half we
can redesign how we use our pointers and instead of copying everything we
can simply swap pointers afterwards. In some tests this resulted in a speed
up of ~10%
This commit is contained in:
Julian Kauth
2022-07-17 13:50:13 +02:00
parent d3ec9a57d2
commit 68d776b5e7
5 changed files with 56 additions and 88 deletions

View File

@@ -453,7 +453,7 @@ void init_bc_phase(double left_bc[NY], double top_bc[NX], double bot_bc[NX])
// // printf("phi(0,0) = %.3lg, psi(0,0) = %.3lg\n", phi[NX/2][NY/2], psi[NX/2][NY/2]);
// }
void evolve_wave_half(double *phi_in[NX], double *psi_in[NX], double *phi_out[NX], double *psi_out[NX],
void evolve_wave_half(double *phi_in[NX], double *psi_in[NX], double *phi_out[NX],
short int *xy_in[NX])
/* time step of field evolution */
/* phi is value of field at time t, psi at time t-1 */
@@ -512,7 +512,6 @@ void evolve_wave_half(double *phi_in[NX], double *psi_in[NX], double *phi_out[NX
/* evolve phi */
phi_out[i][j] = -y + 2*x + tcc[i][j]*delta - KAPPA*x - tgamma[i][j]*(x-y);
psi_out[i][j] = x;
}
}
}
@@ -557,7 +556,6 @@ void evolve_wave_half(double *phi_in[NX], double *psi_in[NX], double *phi_out[NX
break;
}
}
psi_out[0][j] = x;
}
}
@@ -593,7 +591,6 @@ void evolve_wave_half(double *phi_in[NX], double *psi_in[NX], double *phi_out[NX
break;
}
}
psi_out[NX-1][j] = x;
}
}
@@ -656,7 +653,6 @@ void evolve_wave_half(double *phi_in[NX], double *psi_in[NX], double *phi_out[NX
break;
}
}
psi_out[i][NY-1] = x;
}
}
@@ -719,7 +715,6 @@ void evolve_wave_half(double *phi_in[NX], double *psi_in[NX], double *phi_out[NX
break;
}
}
psi_out[i][0] = x;
}
}
@@ -737,20 +732,19 @@ void evolve_wave_half(double *phi_in[NX], double *psi_in[NX], double *phi_out[NX
{
if (phi_out[i][j] > VMAX) phi_out[i][j] = VMAX;
if (phi_out[i][j] < -VMAX) phi_out[i][j] = -VMAX;
if (psi_out[i][j] > VMAX) psi_out[i][j] = VMAX;
if (psi_out[i][j] < -VMAX) psi_out[i][j] = -VMAX;
}
}
}
}
void evolve_wave(double *phi[NX], double *psi[NX], double *phi_tmp[NX], double *psi_tmp[NX], short int *xy_in[NX])
void evolve_wave(double *phi[NX], double *psi[NX], double *tmp[NX], short int *xy_in[NX])
/* time step of field evolution */
/* phi is value of field at time t, psi at time t-1 */
{
evolve_wave_half(phi, psi, phi_tmp, psi_tmp, xy_in);
evolve_wave_half(phi_tmp, psi_tmp, phi, psi, xy_in);
evolve_wave_half(phi, psi, tmp, xy_in);
evolve_wave_half(tmp, phi, psi, xy_in);
evolve_wave_half(psi, tmp, phi, xy_in);
}
@@ -848,7 +842,7 @@ void animation()
{
double time, scale, diss, rgb[3], hue, y, dissip, ej, gradient[2], dx, dy, dt, xleft, xright,
length, fx, fy, force[2];
double *phi[NX], *psi[NX], *phi_tmp[NX], *psi_tmp[NX];
double *phi[NX], *psi[NX], *tmp[NX];
short int *xy_in[NX], redraw = 0;
int i, j, k, n, s, ij[2], i0, iplus, iminus, j0, jplus, jminus, p, q;
static int imin, imax;
@@ -862,8 +856,7 @@ void animation()
{
phi[i] = (double *)malloc(NY*sizeof(double));
psi[i] = (double *)malloc(NY*sizeof(double));
phi_tmp[i] = (double *)malloc(NY*sizeof(double));
psi_tmp[i] = (double *)malloc(NY*sizeof(double));
tmp[i] = (double *)malloc(NY*sizeof(double));
xy_in[i] = (short int *)malloc(NY*sizeof(short int));
}
mangrove = (t_mangrove *)malloc(NMAXCIRCLES*sizeof(t_mangrove)); /* mangroves */
@@ -977,7 +970,7 @@ void animation()
for (j=0; j<NVID; j++)
{
// printf("%i ", j);
evolve_wave(phi, psi, phi_tmp, psi_tmp, xy_in);
evolve_wave(phi, psi, tmp, xy_in);
// if (i % 10 == 9) oscillate_linear_wave(0.2*scale, 0.15*(double)(i*NVID + j), -1.5, YMIN, -1.5, YMAX, phi, psi);
}
@@ -1258,8 +1251,7 @@ void animation()
{
free(phi[i]);
free(psi[i]);
free(phi_tmp[i]);
free(psi_tmp[i]);
free(tmp[i]);
free(xy_in[i]);
}
free(mangrove);