-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlake.cu
More file actions
206 lines (159 loc) · 4.84 KB
/
lake.cu
File metadata and controls
206 lines (159 loc) · 4.84 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/time.h>
#define _USE_MATH_DEFINES
#define XMIN 0.0
#define XMAX 1.0
#define YMIN 0.0
#define YMAX 1.0
#define MAX_PSZ 10
#define TSCALE 1.0
#define VSQR 0.1
void init(double *u, double *pebbles, int n);
void evolve(double *un, double *uc, double *uo, double *pebbles, int n, double h, double dt, double t);
int tpdt(double *t, double dt, double end_time);
void print_heatmap(const char *filename, double *u, int n, double h);
void init_pebbles(double *p, int pn, int n);
void run_cpu(double *u, double *u0, double *u1, double *pebbles, int n, double h, double end_time);
extern void run_gpu(double *u, double *u0, double *u1, double *pebbles, int n, double h, double end_time, int nthreads);
int main(int argc, char *argv[])
{
if(argc != 5)
{
printf("Usage: %s npoints npebs time_finish nthreads \n",argv[0]);
return 0;
}
int npoints = atoi(argv[1]);
int npebs = atoi(argv[2]);
double end_time = (double)atof(argv[3]);
int nthreads = atoi(argv[4]);
int narea = npoints * npoints;
double *u_i0, *u_i1;
double *u_cpu, *u_gpu, *pebs;
double h;
double elapsed_cpu, elapsed_gpu;
struct timeval cpu_start, cpu_end, gpu_start, gpu_end;
u_i0 = (double*)malloc(sizeof(double) * narea);
u_i1 = (double*)malloc(sizeof(double) * narea);
pebs = (double*)malloc(sizeof(double) * narea);
u_cpu = (double*)malloc(sizeof(double) * narea);
u_gpu = (double*)malloc(sizeof(double) * narea);
printf("Running %s with (%d x %d) grid, until %f, with %d threads\n", argv[0], npoints, npoints, end_time, nthreads);
h = (XMAX - XMIN)/npoints;
init_pebbles(pebs, npebs, npoints);
init(u_i0, pebs, npoints);
init(u_i1, pebs, npoints);
print_heatmap("lake_i.dat", u_i0, npoints, h);
gettimeofday(&cpu_start, NULL);
run_cpu(u_cpu, u_i0, u_i1, pebs, npoints, h, end_time);
gettimeofday(&cpu_end, NULL);
elapsed_cpu = ((cpu_end.tv_sec + cpu_end.tv_usec * 1e-6)-(
cpu_start.tv_sec + cpu_start.tv_usec * 1e-6));
printf("CPU took %f seconds\n", elapsed_cpu);
gettimeofday(&gpu_start, NULL);
run_gpu(u_gpu, u_i0, u_i1, pebs, npoints, h, end_time, nthreads);
gettimeofday(&gpu_end, NULL);
elapsed_gpu = ((gpu_end.tv_sec + gpu_end.tv_usec * 1e-6)-(
gpu_start.tv_sec + gpu_start.tv_usec * 1e-6));
printf("GPU took %f seconds\n", elapsed_gpu);
print_heatmap("lake_f.dat", u_cpu, npoints, h);
free(u_i0);
free(u_i1);
free(pebs);
free(u_cpu);
free(u_gpu);
return 1;
}
void run_cpu(double *u, double *u0, double *u1, double *pebbles, int n, double h, double end_time)
{
double *un, *uc, *uo;
double t, dt;
un = (double*)malloc(sizeof(double) * n * n);
uc = (double*)malloc(sizeof(double) * n * n);
uo = (double*)malloc(sizeof(double) * n * n);
memcpy(uo, u0, sizeof(double) * n * n);
memcpy(uc, u1, sizeof(double) * n * n);
t = 0.;
dt = h / 2.;
while(1)
{
evolve(un, uc, uo, pebbles, n, h, dt, t);
memcpy(uo, uc, sizeof(double) * n * n);
memcpy(uc, un, sizeof(double) * n * n);
if(!tpdt(&t,dt,end_time)) break;
}
memcpy(u, un, sizeof(double) * n * n);
}
void init_pebbles(double *p, int pn, int n)
{
int i, j, k, idx;
int sz;
srand( time(NULL) );
memset(p, 0, sizeof(double) * n * n);
for( k = 0; k < pn ; k++ )
{
i = rand() % (n - 4) + 2;
j = rand() % (n - 4) + 2;
sz = rand() % MAX_PSZ;
idx = j + i * n;
p[idx] = (double) sz;
}
}
double f(double p, double t)
{
return -expf(-TSCALE * t) * p;
}
int tpdt(double *t, double dt, double tf)
{
if((*t) + dt > tf) return 0;
(*t) = (*t) + dt;
return 1;
}
void init(double *u, double *pebbles, int n)
{
int i, j, idx;
for(i = 0; i < n ; i++)
{
for(j = 0; j < n ; j++)
{
idx = j + i * n;
u[idx] = f(pebbles[idx], 0.0);
}
}
}
void evolve(double *un, double *uc, double *uo, double *pebbles, int n, double h, double dt, double t)
{
int i, j, idx;
for( i = 0; i < n; i++)
{
for( j = 0; j < n; j++)
{
idx = j + i * n;
if( i == 0 || i == n - 1 || j == 0 || j == n - 1)
{
un[idx] = 0.;
}
else
{
un[idx] = 2*uc[idx] - uo[idx] + VSQR *(dt * dt) *((uc[idx-1] + uc[idx+1] +
uc[idx + n] + uc[idx - n] - 4 * uc[idx])/(h * h) + f(pebbles[idx],t));
}
}
}
}
void print_heatmap(const char *filename, double *u, int n, double h)
{
int i, j, idx;
FILE *fp = fopen(filename, "w");
for( i = 0; i < n; i++ )
{
for( j = 0; j < n; j++ )
{
idx = j + i * n;
fprintf(fp, "%f %f %f\n", i*h, j*h, u[idx]);
}
}
fclose(fp);
}