-
Notifications
You must be signed in to change notification settings - Fork 0
/
heat.c
268 lines (207 loc) · 7.37 KB
/
heat.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
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
/*
** PROGRAM: heat equation solve
**
** PURPOSE: This program will explore use of an explicit
** finite difference method to solve the heat
** equation under a method of manufactured solution (MMS)
** scheme. The solution has been set to be a simple
** function based on exponentials and trig functions.
**
** A finite difference scheme is used on a 1000x1000 cube.
** A total of 0.5 units of time are simulated.
**
** The MMS solution has been adapted from
** G.W. Recktenwald (2011). Finite difference approximations
** to the Heat Equation. Portland State University.
**
**
** USAGE: Run with two arguments:
** First is the number of cells.
** Second is the number of timesteps.
**
** For example, with 100x100 cells and 10 steps:
**
** ./heat 100 10
**
**
** HISTORY: Written by Tom Deakin, Oct 2018
**
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <omp.h>
// Key constants used in this program
#define PI acos(-1.0) // Pi
#define LINE "--------------------\n" // A line for fancy output
// Function definitions
void initial_value(const int n, const double dx, const double length, double * restrict u);
void zero(const int n, double * restrict u);
void solve(const int n, const double alpha, const double dx, const double dt, const double * restrict u, double * restrict u_tmp);
double solution(const double t, const double x, const double y, const double alpha, const double length);
double l2norm(const int n, const double * restrict u, const int nsteps, const double dt, const double alpha, const double dx, const double length);
// Main function
int main(int argc, char *argv[]) {
// Start the total program runtime timer
double start = omp_get_wtime();
// Problem size, forms an nxn grid
int n = 1000;
// Number of timesteps
int nsteps = 10;
// Check for the correct number of arguments
// Print usage and exits if not correct
if (argc == 3) {
// Set problem size from first argument
n = atoi(argv[1]);
if (n < 0) {
fprintf(stderr, "Error: n must be positive\n");
exit(EXIT_FAILURE);
}
// Set number of timesteps from second argument
nsteps = atoi(argv[2]);
if (nsteps < 0) {
fprintf(stderr, "Error: nsteps must be positive\n");
exit(EXIT_FAILURE);
}
}
//
// Set problem definition
//
double alpha = 0.1; // heat equation coefficient
double length = 1000.0; // physical size of domain: length x length square
double dx = length / (n+1); // physical size of each cell (+1 as don't simulate boundaries as they are given)
double dt = 0.5 / nsteps; // time interval (total time of 0.5s)
// Stability requires that dt/(dx^2) <= 0.5,
double r = alpha * dt / (dx * dx);
// Print message detailing runtime configuration
printf("\n");
printf(" MMS heat equation\n\n");
printf(LINE);
printf("Problem input\n\n");
printf(" Grid size: %d x %d\n", n, n);
printf(" Cell width: %E\n", dx);
printf(" Grid length: %lf x %lf\n", length, length);
printf("\n");
printf(" Alpha: %E\n", alpha);
printf("\n");
printf(" Steps: %d\n", nsteps);
printf(" Total time: %E\n", dt*(double)nsteps);
printf(" Time step: %E\n", dt);
printf(LINE);
// Stability check
printf("Stability\n\n");
printf(" r value: %lf\n", r);
if (r > 0.5)
printf(" Warning: unstable\n");
printf(LINE);
// Allocate two nxn grids
double *u = malloc(sizeof(double)*n*n);
double *u_tmp = malloc(sizeof(double)*n*n);
double *tmp;
// Set the initial value of the grid under the MMS scheme
initial_value(n, dx, length, u);
zero(n, u_tmp);
//
// Run through timesteps under the explicit scheme
//
// Start the solve timer
double tic = omp_get_wtime();
// Initializing Data on GPU from host
#pragma omp target enter data map(to: u[0:n*n], u_tmp[0:n*n])
for (int t = 0; t < nsteps; ++t) {
// Call the solve kernel
// Computes u_tmp at the next timestep
// given the value of u at the current timestep
solve(n, alpha, dx, dt, u, u_tmp);
// Pointer swap
tmp = u;
u = u_tmp;
u_tmp = tmp;
}
#pragma omp target exit data map(from: u[0:n*n])
// Stop solve timer
double toc = omp_get_wtime();
//
// Check the L2-norm of the computed solution
// against the *known* solution from the MMS scheme
//
double norm = l2norm(n, u, nsteps, dt, alpha, dx, length);
// Stop total timer
double stop = omp_get_wtime();
// Print results
printf("Results\n\n");
printf("Error (L2norm): %E\n", norm);
printf("Solve time (s): %lf\n", toc-tic);
printf("Total time (s): %lf\n", stop-start);
printf(LINE);
// Free the memory
free(u);
free(u_tmp);
}
// Sets the mesh to an initial value, determined by the MMS scheme
void initial_value(const int n, const double dx, const double length, double * restrict u) {
double y = dx;
for (int j = 0; j < n; ++j) {
double x = dx; // Physical x position
for (int i = 0; i < n; ++i) {
u[i+j*n] = sin(PI * x / length) * sin(PI * y / length);
x += dx;
}
y += dx; // Physical y position
}
}
// Zero the array u
void zero(const int n, double * restrict u) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
u[i+j*n] = 0.0;
}
}
}
// Compute the next timestep, given the current timestep
void solve(const int n, const double alpha, const double dx, const double dt, const double * restrict u, double * restrict u_tmp) {
// Finite difference constant multiplier
const double r = alpha * dt / (dx * dx);
const double r2 = 1.0 - 4.0*r;
// Loop over the nxn grid
// Map clause to handle data transfer from CPU to GPU
// #pragma omp target map(tofrom u:[0:n*n], u_tmp:[0:n*n])
// You can initialize data on GPU instead by using #pragma omp target enter data map(to: u[0:n*n], u_tmp[0:n*n]) and then removing the map clause here as shown below
#pragma omp target
#pragma omp loop collapse(2)
for (int j = 0; j < n; ++j) {
for (int i = 0; i < n; ++i) {
// Update the 5-point stencil, using boundary conditions on the edges of the domain.
// Boundaries are zero because the MMS solution is zero there.
u_tmp[i+j*n] = r2 * u[i+j*n] +
r * ((i < n-1) ? u[i+1+j*n] : 0.0) +
r * ((i > 0) ? u[i-1+j*n] : 0.0) +
r * ((j < n-1) ? u[i+(j+1)*n] : 0.0) +
r * ((j > 0) ? u[i+(j-1)*n] : 0.0);
}
}
}
// True answer given by the manufactured solution
double solution(const double t, const double x, const double y, const double alpha, const double length) {
return exp(-2.0*alpha*PI*PI*t/(length*length)) * sin(PI*x/length) * sin(PI*y/length);
}
// Computes the L2-norm of the computed grid and the MMS known solution
// The known solution is the same as the boundary function.
double l2norm(const int n, const double * restrict u, const int nsteps, const double dt, const double alpha, const double dx, const double length) {
// Final (real) time simulated
double time = dt * (double)nsteps;
// L2-norm error
double l2norm = 0.0;
// Loop over the grid and compute difference of computed and known solutions as an L2-norm
double y = dx;
for (int j = 0; j < n; ++j) {
double x = dx;
for (int i = 0; i < n; ++i) {
double answer = solution(time, x, y, alpha, length);
l2norm += (u[i+j*n] - answer) * (u[i+j*n] - answer);
x += dx;
}
y += dx;
}
return sqrt(l2norm);
}