forked from csc-training/mpi-introduction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.c
71 lines (58 loc) · 2.19 KB
/
core.c
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
/* Main solver routines for heat equation solver */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <mpi.h>
#include "heat.h"
/* Exchange the boundary values */
void exchange(field *temperature, parallel_data *parallel)
{
double *data;
double *sbuf_up, *sbuf_down, *rbuf_up, *rbuf_down;
data = temperature->data;
// Send to the up, receive from down
sbuf_up = data + temperature->ny + 2; // upper data
rbuf_down = data + (temperature->nx + 1) * (temperature->ny + 2); // lower halo
MPI_Sendrecv(sbuf_up, temperature->ny + 2, MPI_DOUBLE,
parallel->nup, 11,
rbuf_down, temperature->ny + 2, MPI_DOUBLE,
parallel->ndown, 11, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
// Send to the down, receive from up
sbuf_down = data + temperature->nx * (temperature->ny + 2); // lower data
rbuf_up = data; // upper halo
MPI_Sendrecv(sbuf_down, temperature->ny + 2, MPI_DOUBLE,
parallel->ndown, 12,
rbuf_up, temperature->ny + 2, MPI_DOUBLE,
parallel->nup, 12, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
/* Update the temperature values using five-point stencil */
void evolve(field *curr, field *prev, double a, double dt)
{
double dx2, dy2;
int nx, ny;
double *currdata, *prevdata;
/* HINT: to help the compiler do not access members of structures
* within OpenACC parallel regions */
currdata = curr->data;
prevdata = prev->data;
nx = curr->nx;
ny = curr->ny;
/* Determine the temperature field at next time step
* As we have fixed boundary conditions, the outermost gridpoints
* are not updated. */
dx2 = prev->dx * prev->dx;
dy2 = prev->dy * prev->dy;
for (int i = 1; i < nx + 1; i++) {
for (int j = 1; j < ny + 1; j++) {
int ind = i * (ny + 2) + j;
int ip = (i + 1) * (ny + 2) + j;
int im = (i - 1) * (ny + 2) + j;
int jp = i * (ny + 2) + j + 1;
int jm = i * (ny + 2) + j - 1;
currdata[ind] = prevdata[ind] + a * dt *
((prevdata[ip] -2.0 * prevdata[ind] + prevdata[im]) / dx2 +
(prevdata[jp] - 2.0 * prevdata[ind] + prevdata[jm]) / dy2);
}
}
}