-
Notifications
You must be signed in to change notification settings - Fork 4
/
laplace_gpu.cu
732 lines (622 loc) · 21.3 KB
/
laplace_gpu.cu
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/** GPU Laplace solver using optimized red-black Gauss–Seidel with SOR solver
* \file main_cpu_opt.c
*
* \author Kyle E. Niemeyer
* \date 09/21/2012
*
* Solves Laplace's equation in 2D (e.g., heat conduction in a rectangular plate)
* on GPU using CUDA with the red-black Gauss–Seidel with sucessive overrelaxation
* (SOR) that has been "optimized". This means that the red and black kernels
* only loop overtheir respective cells, instead of over all cells and skipping
* even/odd cells. This requires separate arrays for red and black cells.
*
* Boundary conditions:
* T = 0 at x = 0, x = L, y = 0
* T = TN at y = H
*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "timer.h"
// CUDA libraries
#include <cuda.h>
#include <helper_cuda.h>
/** Problem size along one side; total number of cells is this squared */
#define NUM 8192
// block size
#define BLOCK_SIZE 128
/** Double precision */
//#define DOUBLE
#ifdef DOUBLE
#define Real double
#define ZERO 0.0
#define ONE 1.0
#define TWO 2.0
/** SOR relaxation parameter */
const Real omega = 1.85;
#else
#define Real float
#define ZERO 0.0f
#define ONE 1.0f
#define TWO 2.0f
/** SOR relaxation parameter */
const Real omega = 1.85f;
#endif
/** Arrange global memory for coalescing */
#define COALESCE
/** Split temperature into red and black arrays */
#define MEMOPT
/** Use shared memory to get residual */
#define SHARED
/** Use texture memory */
//#define TEXTURE
/** Use atomic operations to calculate residual, only for SINGLE PRECISION */
//#define ATOMIC
#if defined (ATOMIC) && defined (DOUBLE)
# error double precision atomic operations not supported
#endif
#ifdef TEXTURE
#ifdef DOUBLE
texture<int2,1> aP_t;
texture<int2,1> aW_t;
texture<int2,1> aE_t;
texture<int2,1> aS_t;
texture<int2,1> aN_t;
texture<int2,1> b_t;
static __inline__ __device__ double get_tex (texture<int2, 1> tex, int i)
{
int2 v = tex1Dfetch(tex, i);
return __hiloint2double(v.y, v.x);
}
#else
texture<float> aP_t;
texture<float> aW_t;
texture<float> aE_t;
texture<float> aS_t;
texture<float> aN_t;
texture<float> b_t;
static __inline__ __device__ float get_tex (texture<float> tex, int i)
{
return tex1Dfetch(tex, i);
}
#endif
#endif
///////////////////////////////////////////////////////////////////////////////
/** Function to evaluate coefficient matrix and right-hand side vector.
*
* \param[in] rowmax number of rows
* \param[in] colmax number of columns
* \param[in] th_cond thermal conductivity
* \param[in] dx grid size in x dimension (uniform)
* \param[in] dy grid size in y dimension (uniform)
* \param[in] width width of plate (z dimension)
* \param[in] TN temperature at top boundary
* \param[out] aP array of self coefficients
* \param[out] aW array of west neighbor coefficients
* \param[out] aE array of east neighbor coefficients
* \param[out] aS array of south neighbor coefficients
* \param[out] aN array of north neighbor coefficients
* \param[out] b right-hand side array
*/
void fill_coeffs (int rowmax, int colmax, Real th_cond, Real dx, Real dy,
Real width, Real TN, Real * aP, Real * aW, Real * aE,
Real * aS, Real * aN, Real * b)
{
int col, row;
for (col = 0; col < colmax; ++col) {
for (row = 0; row < rowmax; ++row) {
int ind = col * rowmax + row;
b[ind] = ZERO;
Real SP = ZERO;
if (col == 0) {
// left BC: temp = 0
aW[ind] = ZERO;
SP = -TWO * th_cond * width * dy / dx;
} else {
aW[ind] = th_cond * width * dy / dx;
}
if (col == colmax - 1) {
// right BC: temp = 0
aE[ind] = ZERO;
SP = -TWO * th_cond * width * dy / dx;
} else {
aE[ind] = th_cond * width * dy / dx;
}
if (row == 0) {
// bottom BC: temp = 0
aS[ind] = ZERO;
SP = -TWO * th_cond * width * dx / dy;
} else {
aS[ind] = th_cond * width * dx / dy;
}
if (row == rowmax - 1) {
// top BC: temp = TN
aN[ind] = ZERO;
b[ind] = TWO * th_cond * width * dx * TN / dy;
SP = -TWO * th_cond * width * dx / dy;
} else {
aN[ind] = th_cond * width * dx / dy;
}
aP[ind] = aW[ind] + aE[ind] + aS[ind] + aN[ind] - SP;
} // end for row
} // end for col
} // end fill_coeffs
///////////////////////////////////////////////////////////////////////////////
/** Function to update temperature for red cells
*
* \param[in] aP array of self coefficients
* \param[in] aW array of west neighbor coefficients
* \param[in] aE array of east neighbor coefficients
* \param[in] aS array of south neighbor coefficients
* \param[in] aN array of north neighbor coefficients
* \param[in] b right-hand side array
* \param[in] temp_black temperatures of black cells, constant in this function
* \param[inout] temp_red temperatures of red cells
* \param[out] bl_norm_L2 array with residual information for blocks
*/
#ifdef TEXTURE
__global__ void red_kernel (const Real * temp_black, Real * temp_red, Real * norm_L2)
#else
__global__ void red_kernel (const Real * aP, const Real * aW, const Real * aE,
const Real * aS, const Real * aN, const Real * b,
const Real * temp_black, Real * temp_red,
Real * norm_L2)
#endif
{
int row = 1 + (blockIdx.x * blockDim.x) + threadIdx.x;
int col = 1 + (blockIdx.y * blockDim.y) + threadIdx.y;
// store residual for block
#ifdef SHARED
__shared__ Real res_cache[BLOCK_SIZE];
res_cache[threadIdx.x] = ZERO;
#endif
#ifdef MEMOPT
int ind_red = col * ((NUM >> 1) + 2) + row; // local (red) index
int ind = 2 * row - (col & 1) - 1 + NUM * (col - 1); // global index
#else
if ((row + col) % 2 == 0) {
int ind_red = (col * (NUM + 2)) + row;
int ind = ((col - 1) * NUM ) + row - 1;
#endif
Real temp_old = temp_red[ind_red];
#if defined(TEXTURE) && defined(MEMOPT)
Real res = get_tex(b_t, ind)
+ (get_tex(aW_t, ind) * temp_black[row + (col - 1) * ((NUM >> 1) + 2)]
+ get_tex(aE_t, ind) * temp_black[row + (col + 1) * ((NUM >> 1) + 2)]
+ get_tex(aS_t, ind) * temp_black[row - (col & 1) + col * ((NUM >> 1) + 2)]
+ get_tex(aN_t, ind) * temp_black[row + ((col + 1) & 1) + col * ((NUM >> 1) + 2)]);
Real temp_new = temp_old * (ONE - omega) + omega * (res / get_tex(aP_t, ind));
#elif defined(TEXTURE) && !defined(MEMOPT)
Real res = get_tex(b_t, ind)
+ (get_tex(aW_t, ind) * temp_black[row + (col - 1) * (NUM + 2)]
+ get_tex(aE_t, ind) * temp_black[row + (col + 1) * (NUM + 2)]
+ get_tex(aS_t, ind) * temp_black[row - 1 + col * (NUM + 2)]
+ get_tex(aN_t, ind) * temp_black[row + 1 + col * (NUM + 2)]);
Real temp_new = temp_old * (ONE - omega) + omega * (res / get_tex(aP_t, ind));
#elif !defined(TEXTURE) && defined(MEMOPT)
Real res = b[ind]
+ (aW[ind] * temp_black[row + (col - 1) * ((NUM >> 1) + 2)]
+ aE[ind] * temp_black[row + (col + 1) * ((NUM >> 1) + 2)]
+ aS[ind] * temp_black[row - (col & 1) + col * ((NUM >> 1) + 2)]
+ aN[ind] * temp_black[row + ((col + 1) & 1) + col * ((NUM >> 1) + 2)]);
Real temp_new = temp_old * (ONE - omega) + omega * (res / aP[ind]);
#else
// neither TEXTURE nor MEMOPT defined
Real res = b[ind]
+ (aW[ind] * temp_black[row + (col - 1) * (NUM + 2)]
+ aE[ind] * temp_black[row + (col + 1) * (NUM + 2)]
+ aS[ind] * temp_black[row - 1 + col * (NUM + 2)]
+ aN[ind] * temp_black[row + 1 + col * (NUM + 2)]);
Real temp_new = temp_old * (ONE - omega) + omega * (res / aP[ind]);
#endif
temp_red[ind_red] = temp_new;
res = temp_new - temp_old;
#ifdef SHARED
// store squared residual from each thread in block
res_cache[threadIdx.x] = res * res;
// synchronize threads in block
__syncthreads();
// add up squared residuals for block
int i = BLOCK_SIZE >> 1;
while (i != 0) {
if (threadIdx.x < i) {
res_cache[threadIdx.x] += res_cache[threadIdx.x + i];
}
__syncthreads();
i >>= 1;
}
// store block's summed residuals
if (threadIdx.x == 0) {
#ifdef ATOMIC
atomicAdd (norm_L2, res_cache[0]);
#else
norm_L2[blockIdx.y + (gridDim.y * blockIdx.x)] = res_cache[0];
#endif
}
#else
norm_L2[ind_red] = res * res;
#endif
#ifndef MEMOPT
}
#endif
} // end red_kernel
///////////////////////////////////////////////////////////////////////////////
/** Function to update temperature for black cells
*
* \param[in] aP array of self coefficients
* \param[in] aW array of west neighbor coefficients
* \param[in] aE array of east neighbor coefficients
* \param[in] aS array of south neighbor coefficients
* \param[in] aN array of north neighbor coefficients
* \param[in] b right-hand side array
* \param[in] temp_red temperatures of red cells, constant in this function
* \param[inout] temp_black temperatures of black cells
* \param[out] bl_norm_L2 array with residual information for blocks
*/
#ifdef TEXTURE
__global__ void black_kernel (const Real * temp_red, Real * temp_black, Real * norm_L2)
#else
__global__ void black_kernel (const Real * aP, const Real * aW, const Real * aE,
const Real * aS, const Real * aN, const Real * b,
const Real * temp_red, Real * temp_black,
Real * norm_L2)
#endif
{
int row = 1 + (blockIdx.x * blockDim.x) + threadIdx.x;
int col = 1 + (blockIdx.y * blockDim.y) + threadIdx.y;
#ifdef SHARED
// store residual for block
__shared__ Real res_cache[BLOCK_SIZE];
res_cache[threadIdx.x] = ZERO;
#endif
#ifdef MEMOPT
int ind_black = col * ((NUM >> 1) + 2) + row; // local (black) index
int ind = 2 * row - ((col + 1) & 1) - 1 + NUM * (col - 1); // global index
#else
if ((row + col) % 2 == 1) {
int ind_black = (col * (NUM + 2)) + row;
int ind = ((col - 1) * NUM ) + row - 1;
#endif
Real temp_old = temp_black[ind_black];
#if defined(TEXTURE) && defined(MEMOPT)
Real res = get_tex(b_t, ind)
+ (get_tex(aW_t, ind) * temp_red[row + (col - 1) * ((NUM >> 1) + 2)]
+ get_tex(aE_t, ind) * temp_red[row + (col + 1) * ((NUM >> 1) + 2)]
+ get_tex(aS_t, ind) * temp_red[row - ((col + 1) & 1) + col * ((NUM >> 1) + 2)]
+ get_tex(aN_t, ind) * temp_red[row + (col & 1) + col * ((NUM >> 1) + 2)]);
Real temp_new = temp_old * (ONE - omega) + omega * (res / get_tex(aP_t, ind));
#elif defined(TEXTURE) && !defined(MEMOPT)
Real res = get_tex(b_t, ind)
+ (get_tex(aW_t, ind) * temp_red[row + (col - 1) * (NUM + 2)]
+ get_tex(aE_t, ind) * temp_red[row + (col + 1) * (NUM + 2)]
+ get_tex(aS_t, ind) * temp_red[row - 1 + col * (NUM + 2)]
+ get_tex(aN_t, ind) * temp_red[row + 1 + col * (NUM + 2)]);
Real temp_new = temp_old * (ONE - omega) + omega * (res / get_tex(aP_t, ind));
#elif !defined(TEXTURE) && defined(MEMOPT)
Real res = b[ind]
+ (aW[ind] * temp_red[row + (col - 1) * ((NUM >> 1) + 2)]
+ aE[ind] * temp_red[row + (col + 1) * ((NUM >> 1) + 2)]
+ aS[ind] * temp_red[row - ((col + 1) & 1) + col * ((NUM >> 1) + 2)]
+ aN[ind] * temp_red[row + (col & 1) + col * ((NUM >> 1) + 2)]);
Real temp_new = temp_old * (ONE - omega) + omega * (res / aP[ind]);
#else
// neither TEXTURE nor MEMOPT defined
Real res = b[ind]
+ (aW[ind] * temp_red[row + (col - 1) * (NUM + 2)]
+ aE[ind] * temp_red[row + (col + 1) * (NUM + 2)]
+ aS[ind] * temp_red[row - 1 + col * (NUM + 2)]
+ aN[ind] * temp_red[row + 1 + col * (NUM + 2)]);
Real temp_new = temp_old * (ONE - omega) + omega * (res / aP[ind]);
#endif
temp_black[ind_black] = temp_new;
res = temp_new - temp_old;
#ifdef SHARED
// store squared residual from each thread in block
res_cache[threadIdx.x] = res * res;
// synchronize threads in block
__syncthreads();
// add up squared residuals for block
int i = BLOCK_SIZE >> 1;
while (i != 0) {
if (threadIdx.x < i) {
res_cache[threadIdx.x] += res_cache[threadIdx.x + i];
}
__syncthreads();
i >>= 1;
}
// store block's summed residuals
if (threadIdx.x == 0) {
#ifdef ATOMIC
atomicAdd (norm_L2, res_cache[0]);
#else
norm_L2[blockIdx.y + (gridDim.y * blockIdx.x)] = res_cache[0];
#endif
}
#else
norm_L2[ind_black] = res * res;
#endif
#ifndef MEMOPT
}
#endif
} // end black_kernel
///////////////////////////////////////////////////////////////////////////////
/** Main function that solves Laplace's equation in 2D (heat conduction in plate)
*
* Contains iteration loop for red-black Gauss-Seidel with SOR GPU kernels
*/
int main (void) {
// size of plate
Real L = 1.0;
Real H = 1.0;
Real width = 0.01;
// thermal conductivity
Real th_cond = 1.0;
// temperature at top boundary
Real TN = 1.0;
// SOR iteration tolerance
Real tol = 1.e-6;
// number of cells in x and y directions
// including unused boundary cells
#ifdef MEMOPT
int num_rows = (NUM / 2) + 2;
#else
int num_rows = NUM + 2;
#endif
int num_cols = NUM + 2;
int size_temp = num_rows * num_cols;
int size = NUM * NUM;
// size of cells
Real dx = L / NUM;
Real dy = H / NUM;
// iterations for Red-Black Gauss-Seidel with SOR
int iter;
int it_max = 1e6;
// allocate memory
Real *aP, *aW, *aE, *aS, *aN, *b;
Real *temp_red, *temp_black;
// arrays of coefficients
aP = (Real *) calloc (size, sizeof(Real));
aW = (Real *) calloc (size, sizeof(Real));
aE = (Real *) calloc (size, sizeof(Real));
aS = (Real *) calloc (size, sizeof(Real));
aN = (Real *) calloc (size, sizeof(Real));
// RHS
b = (Real *) calloc (size, sizeof(Real));
// temperature arrays
temp_red = (Real *) calloc (size_temp, sizeof(Real));
temp_black = (Real *) calloc (size_temp, sizeof(Real));
// set coefficients
fill_coeffs (NUM, NUM, th_cond, dx, dy, width, TN, aP, aW, aE, aS, aN, b);
int i;
for (i = 0; i < size_temp; ++i) {
temp_red[i] = ZERO;
temp_black[i] = ZERO;
}
//////////////////////////////
// block and grid dimensions
//////////////////////////////
#ifdef COALESCE
///////////////////////////////////////
// coalescing
dim3 dimBlock (BLOCK_SIZE, 1);
#ifdef MEMOPT
dim3 dimGrid (NUM / (2 * BLOCK_SIZE), NUM);
#else
dim3 dimGrid (NUM / BLOCK_SIZE, NUM);
#endif
///////////////////////////////////////
#else
///////////////////////////////////////
// naive (no coalescing)
dim3 dimBlock (1, BLOCK_SIZE);
#ifdef MEMOPT
dim3 dimGrid (NUM / 2, NUM / BLOCK_SIZE);
#else
dim3 dimGrid (NUM, NUM / BLOCK_SIZE);
#endif
///////////////////////////////////////
#endif
// residual
Real *bl_norm_L2;
#ifdef SHARED
#ifdef ATOMIC
int size_norm = 1;
// single value, using atomic operations to sum
#else
// one value for each block
int size_norm = dimGrid.x * dimGrid.y;
#endif
#else
// one for each temperature value
int size_norm = size_temp;
#endif
bl_norm_L2 = (Real *) calloc (size_norm, sizeof(Real));
for (i = 0; i < size_norm; ++i) {
bl_norm_L2[i] = ZERO;
}
// set device
checkCudaErrors(cudaSetDevice (1));
// print problem info
printf("Problem size: %d x %d \n", NUM, NUM);
//////////////////////////////
// start timer
//clock_t start_time = clock();
StartTimer();
//////////////////////////////
// allocate device memory
Real *aP_d, *aW_d, *aE_d, *aS_d, *aN_d, *b_d;
Real *temp_red_d;
#ifdef MEMOPT
Real *temp_black_d;
#endif
cudaMalloc ((void**) &aP_d, size * sizeof(Real));
cudaMalloc ((void**) &aW_d, size * sizeof(Real));
cudaMalloc ((void**) &aE_d, size * sizeof(Real));
cudaMalloc ((void**) &aS_d, size * sizeof(Real));
cudaMalloc ((void**) &aN_d, size * sizeof(Real));
cudaMalloc ((void**) &b_d, size * sizeof(Real));
cudaMalloc ((void**) &temp_red_d, size_temp * sizeof(Real));
#ifdef MEMOPT
cudaMalloc ((void**) &temp_black_d, size_temp * sizeof(Real));
#endif
// copy to device memory
cudaMemcpy (aP_d, aP, size * sizeof(Real), cudaMemcpyHostToDevice);
cudaMemcpy (aW_d, aW, size * sizeof(Real), cudaMemcpyHostToDevice);
cudaMemcpy (aE_d, aE, size * sizeof(Real), cudaMemcpyHostToDevice);
cudaMemcpy (aS_d, aS, size * sizeof(Real), cudaMemcpyHostToDevice);
cudaMemcpy (aN_d, aN, size * sizeof(Real), cudaMemcpyHostToDevice);
cudaMemcpy (b_d, b, size * sizeof(Real), cudaMemcpyHostToDevice);
cudaMemcpy (temp_red_d, temp_red, size_temp * sizeof(Real), cudaMemcpyHostToDevice);
#ifdef MEMOPT
cudaMemcpy (temp_black_d, temp_black, size_temp * sizeof(Real), cudaMemcpyHostToDevice);
#endif
#ifdef TEXTURE
// bind to textures
cudaBindTexture (NULL, aP_t, aP_d, size * sizeof(Real));
cudaBindTexture (NULL, aW_t, aW_d, size * sizeof(Real));
cudaBindTexture (NULL, aE_t, aE_d, size * sizeof(Real));
cudaBindTexture (NULL, aS_t, aS_d, size * sizeof(Real));
cudaBindTexture (NULL, aN_t, aN_d, size * sizeof(Real));
cudaBindTexture (NULL, b_t, b_d, size * sizeof(Real));
#endif
// residual
Real *bl_norm_L2_d;
cudaMalloc ((void**) &bl_norm_L2_d, size_norm * sizeof(Real));
#ifndef SHARED
cudaMemcpy (bl_norm_L2_d, bl_norm_L2, size_norm * sizeof(Real), cudaMemcpyHostToDevice);
#endif
// iteration loop
for (iter = 1; iter <= it_max; ++iter) {
Real norm_L2 = ZERO;
#ifdef ATOMIC
// set device value to zero
*bl_norm_L2 = ZERO;
cudaMemcpy (bl_norm_L2_d, bl_norm_L2, sizeof(Real), cudaMemcpyHostToDevice);
#endif
// update red cells
#if defined(TEXTURE) && defined(MEMOPT)
red_kernel <<<dimGrid, dimBlock>>> (temp_black_d, temp_red_d, bl_norm_L2_d);
#elif defined(TEXTURE) && !defined(MEMOPT)
red_kernel <<<dimGrid, dimBlock>>> (temp_red_d, temp_red_d, bl_norm_L2_d);
#elif !defined(TEXTURE) && defined(MEMOPT)
red_kernel <<<dimGrid, dimBlock>>> (aP_d, aW_d, aE_d, aS_d, aN_d, b_d, temp_black_d, temp_red_d, bl_norm_L2_d);
#else // neither defined
red_kernel <<<dimGrid, dimBlock>>> (aP_d, aW_d, aE_d, aS_d, aN_d, b_d, temp_red_d, temp_red_d, bl_norm_L2_d);
#endif
// transfer residual value(s) back to CPU
#if !defined(ATOMIC) && defined(MEMOPT)
cudaMemcpy (bl_norm_L2, bl_norm_L2_d, size_norm * sizeof(Real), cudaMemcpyDeviceToHost);
// add red cell contributions to residual
for (int i = 0; i < size_norm; ++i) {
norm_L2 += bl_norm_L2[i];
}
#endif
#if defined(TEXTURE) && defined(MEMOPT)
black_kernel <<<dimGrid, dimBlock>>> (temp_red_d, temp_black_d, bl_norm_L2_d);
#elif defined(TEXTURE) && !defined(MEMOPT)
black_kernel <<<dimGrid, dimBlock>>> (temp_red_d, temp_red_d, bl_norm_L2_d);
#elif !defined(TEXTURE) && defined(MEMOPT)
black_kernel <<<dimGrid, dimBlock>>> (aP_d, aW_d, aE_d, aS_d, aN_d, b_d, temp_red_d, temp_black_d, bl_norm_L2_d);
#else // neither defined
black_kernel <<<dimGrid, dimBlock>>> (aP_d, aW_d, aE_d, aS_d, aN_d, b_d, temp_red_d, temp_red_d, bl_norm_L2_d);
#endif
// transfer residual value(s) back to CPU and
// add black cell contributions to residual
#ifdef ATOMIC
cudaMemcpy (bl_norm_L2, bl_norm_L2_d, sizeof(Real), cudaMemcpyDeviceToHost);
norm_L2 = *bl_norm_L2;
#else
cudaMemcpy (bl_norm_L2, bl_norm_L2_d, size_norm * sizeof(Real), cudaMemcpyDeviceToHost);
for (int i = 0; i < size_norm; ++i) {
norm_L2 += bl_norm_L2[i];
}
#endif
// calculate residual
norm_L2 = sqrt(norm_L2 / ((Real)size));
if (iter % 100 == 0) printf("%5d, %0.6f\n", iter, norm_L2);
// if tolerance has been reached, end SOR iterations
if (norm_L2 < tol) {
break;
}
}
// transfer final temperature values back
cudaMemcpy (temp_red, temp_red_d, size_temp * sizeof(Real), cudaMemcpyDeviceToHost);
#ifdef MEMOPT
cudaMemcpy (temp_black, temp_red_d, size_temp * sizeof(Real), cudaMemcpyDeviceToHost);
#endif
/////////////////////////////////
// end timer
//time = walltime(&time);
//clock_t end_time = clock();
double runtime = GetTimer();
/////////////////////////////////
printf("GPU\n");
printf("Iterations: %i\n", iter);
//printf("Time: %f\n", (end_time - start_time) / (double)CLOCKS_PER_SEC);
printf("Total time: %f s\n", runtime / 1000.0);
// print temperature data to file
FILE * pfile;
pfile = fopen("temp_gpu.dat", "w");
if (pfile != NULL) {
fprintf(pfile, "#x\ty\ttemp(K)\n");
int row, col;
for (row = 1; row < NUM + 1; ++row) {
for (col = 1; col < NUM + 1; ++col) {
Real x_pos = (col - 1) * dx + (dx / 2);
Real y_pos = (row - 1) * dy + (dy / 2);
if ((row + col) % 2 == 0) {
// even, so red cell
#ifdef MEMOPT
int ind = col * num_rows + (row + (col % 2)) / 2;
#else
int ind = ((col - 1) * NUM ) + row - 1;
#endif
fprintf(pfile, "%f\t%f\t%f\n", x_pos, y_pos, temp_red[ind]);
} else {
// odd, so black cell
#ifdef MEMOPT
int ind = col * num_rows + (row + ((col + 1) % 2)) / 2;
fprintf(pfile, "%f\t%f\t%f\n", x_pos, y_pos, temp_black[ind]);
#else
int ind = ((col - 1) * NUM ) + row - 1;
fprintf(pfile, "%f\t%f\t%f\n", x_pos, y_pos, temp_red[ind]);
#endif
}
}
fprintf(pfile, "\n");
}
}
fclose(pfile);
// free device memory
cudaFree(aP_d);
cudaFree(aW_d);
cudaFree(aE_d);
cudaFree(aS_d);
cudaFree(aN_d);
cudaFree(b_d);
cudaFree(temp_red_d);
#ifdef MEMOPT
cudaFree(temp_black_d);
#endif
cudaFree(bl_norm_L2_d);
#ifdef TEXTURE
// unbind textures
cudaUnbindTexture(aP_t);
cudaUnbindTexture(aW_t);
cudaUnbindTexture(aE_t);
cudaUnbindTexture(aS_t);
cudaUnbindTexture(aN_t);
cudaUnbindTexture(b_t);
#endif
free(aP);
free(aW);
free(aE);
free(aS);
free(aN);
free(b);
free(temp_red);
free(temp_black);
free(bl_norm_L2);
checkCudaErrors (cudaDeviceReset());
return 0;
}