-
Notifications
You must be signed in to change notification settings - Fork 1
/
quad2_v2-1.cu
726 lines (583 loc) · 23.2 KB
/
quad2_v2-1.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
/* Calculates a definite integral by using three different rules.
Compares sequential to parallel implementations. */
/* My code is written in such a way that the block dimension has to be 1024, because of total loop unrolling. */
/* Version 2 calculates the array elements and performs first level of sum reduction at once.
This way it makes only 10^9 / 1024, which is around million, transfers from SM cores to global memory, when n = 10^9.
It also allocates only maximum 512 kB array. It has around million blocks of threads, ie. grid size is around million.
"Dash 1" uses 1D grid and 1D blocks.
For some reason, it doesn't want to work with grid dimension larger than 65535, even though it should.
It says: "Cuda error: kernel invocation: invalid argument.".
That means we can't deploy 10^9 threads in that configuration, and we have to repeat the kernels 15 times,
and do the final reduction on the host.
We will still have around million transfers from registers to global memory, because that is the total number of
blocks, and we perform reduction at the block level. Only, the array will be at most 512 kB.
We will split the region that needs to be calculated in 15 subregions, or whatever is needed. We will work on
the subregions as if they are a complete region, but we will reduce their sums on the host. This way we'll get
the full area under the curve.
Faster than v1, plus works with large arrays. Slightly less accurate, but not much. */
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NUM_OF_GPU_THREADS 1024 // Size of a block of threads
#define BLOCK_DIM NUM_OF_GPU_THREADS
#define GRID_DIM_MAX_X 65535
#define ACCURACY 0.01
typedef struct Results Results;
struct Results {
double valQuad;
double valTrap;
double valSimp;
double timeQuad;
double timeTrap;
double timeSimp;
};
// The function whose integral we calculate
inline __host__ __device__ double f(const double x) {
register const double pi = 3.141592653589793;
double value;
value = 50.0 / ( pi * ( 2500.0 * x * x + 1.0 ) );
return value;
}
void checkCUDAError(const char *msg) {
cudaError_t err = cudaGetLastError();
if (err != cudaSuccess) {
fprintf(stderr, "Cuda error: %s: %s.\n", msg, cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
}
/*************************/
/* SEQUENTIAL ALGORITHMS */
/*************************/
// Quadratic rule
void seqQuad(const unsigned n, const double a, const double b, double *total, double *ExecTime) {
unsigned i;
double x;
double total_q = 0.0;
// Create events for timing execution
cudaEvent_t start = cudaEvent_t();
cudaEvent_t stop = cudaEvent_t();
cudaEventCreate(&start);
cudaEventCreate(&stop);
// Record time into start event
cudaEventRecord(start, 0); // 0 is the default stream id
for (i = 0; i < n; i++) {
x = ((double)(n - 1 - i)*a + (double)(i)*b) / (double)(n - 1);
total_q = total_q + f(x);
}
total_q = (b - a) * total_q / (double)n;
// Record time into stop event
cudaEventRecord(stop, 0);
// Synchronize stop event to wait for end of kernel execution on stream 0
cudaEventSynchronize(stop);
// Compute elapsed time (done by CUDA run-time)
float execTime = 0.f;
cudaEventElapsedTime(&execTime, start, stop);
// Release events
cudaEventDestroy(start);
cudaEventDestroy(stop);
*total = total_q;
*ExecTime = (double)execTime;
}
// Trapezoidal rule
void seqTrap(const unsigned n, const double a, const double b, double *total, double *ExecTime) {
unsigned i;
double x;
const double width = (b - a)/n;
double total_t = 0.0;
// Create events for timing execution
cudaEvent_t start = cudaEvent_t();
cudaEvent_t stop = cudaEvent_t();
cudaEventCreate(&start);
cudaEventCreate(&stop);
// Record time into start event
cudaEventRecord(start, 0); // 0 is the default stream id
{ i = 0; x = a + i*width; total_t += 0.5*f(x); } // loop peeling
for (i = 1; i < n - 1; ++i) {
x = a + i*width;
total_t = total_t + f(x);
}
{ i = n - 1; x = a + i*width; total_t += 0.5*f(x); } // loop peeling
total_t = width * total_t;
// Record time into stop event
cudaEventRecord(stop, 0);
// Synchronize stop event to wait for end of kernel execution on stream 0
cudaEventSynchronize(stop);
// Compute elapsed time (done by CUDA run-time)
float execTime = 0.f;
cudaEventElapsedTime(&execTime, start, stop);
// Release events
cudaEventDestroy(start);
cudaEventDestroy(stop);
*total = total_t;
*ExecTime = (double)execTime;
}
// Simpson 1/3 rule
void seqSimp(const unsigned n, const double a, const double b, double *total, double *ExecTime) {
unsigned i;
double x;
const double width = (b - a)/n;
double total_s = 0.0;
// Create events for timing execution
cudaEvent_t start = cudaEvent_t();
cudaEvent_t stop = cudaEvent_t();
cudaEventCreate(&start);
cudaEventCreate(&stop);
// Record time into start event
cudaEventRecord(start, 0); // 0 is the default stream id
{ i = 0; x = a + i*width; total_s = total_s + f(x); } // loop peeling
for (i = 1; i < n - 2; ++i) {
x = a + i*width;
total_s = total_s + 4*f(x);
++i;
x = a + i*width;
total_s = total_s + 2*f(x);
}
{ i = n - 1; x = a + i*width; total_s = total_s + f(x); } // loop peeling
total_s = width / 3 * total_s;
// Record time into stop event
cudaEventRecord(stop, 0);
// Synchronize stop event to wait for end of kernel execution on stream 0
cudaEventSynchronize(stop);
// Compute elapsed time (done by CUDA run-time)
float execTime = 0.f;
cudaEventElapsedTime(&execTime, start, stop);
// Release events
cudaEventDestroy(start);
cudaEventDestroy(stop);
*total = total_s;
*ExecTime = (double)execTime;
}
Results sequential(const unsigned n, const double a, const double b) {
Results results;
double total_q, total_t, total_s; // the results
double wtime_q, wtime_t, wtime_s; // execution times
seqQuad(n, a, b, &total_q, &wtime_q);
seqTrap(n, a, b, &total_t, &wtime_t);
seqSimp(n, a, b, &total_s, &wtime_s);
results.valQuad = total_q;
results.valTrap = total_t;
results.valSimp = total_s;
results.timeQuad = wtime_q;
results.timeTrap = wtime_t;
results.timeSimp = wtime_s;
return results;
}
/***********************/
/* PARALLEL ALGORITHMS */
/***********************/
// Kernel that performs sum reduction.
__global__ void sumReductionKernel(double *arrayDevice, double *sumDevice, const unsigned dim) {
__shared__ double sdata[BLOCK_DIM];
unsigned tid = threadIdx.x;
unsigned i = blockIdx.x*blockDim.x + tid;
// Load block in the shared memory
if (i < dim) {
sdata[tid] = arrayDevice[i];
}
else {
sdata[tid] = 0.0;
}
// Synchronization is necessary after loading of sdata, to make sure that all threads of the block have loaded their element into sdata.
__syncthreads();
if (blockDim.x >= 1024) {
if (tid < 512) { sdata[tid] += sdata[tid + 512]; } __syncthreads(); }
if (blockDim.x >= 512) {
if (tid < 256) { sdata[tid] += sdata[tid + 256]; } __syncthreads(); }
if (blockDim.x >= 256) {
if (tid < 128) { sdata[tid] += sdata[tid + 128]; } __syncthreads(); }
if (blockDim.x >= 128) {
if (tid < 64) { sdata[tid] += sdata[tid + 64]; } __syncthreads(); }
if (tid < 32) {
// Warp size is 32 threads, so the next instructions don't need synchronization. It's implicitly performed on the warp level. That saves a lot of time.
if (blockDim.x >= 64) sdata[tid] += sdata[tid + 32];
if (blockDim.x >= 32) sdata[tid] += sdata[tid + 16];
if (blockDim.x >= 16) sdata[tid] += sdata[tid + 8];
if (blockDim.x >= 8) sdata[tid] += sdata[tid + 4];
if (blockDim.x >= 4) sdata[tid] += sdata[tid + 2];
if (blockDim.x >= 2) sdata[tid] += sdata[tid + 1];
}
// Thread 0 writes result for this block to global mem
if (tid == 0) {
sumDevice[blockIdx.x] = sdata[0];
}
}
/* Quadratic rule */
// This kernel calculates values of f(x), performs one level of reduction, and puts the partial sums into global memory.
__global__ void parQuadKernel(double *arrayDevice, unsigned n, double a, double b) {
__shared__ double sdata[BLOCK_DIM];
unsigned tid = threadIdx.x;
unsigned i = blockIdx.x*blockDim.x + tid;
// Load block in the shared memory
if (i < n) {
double x = ((double)(n - 1 - i)*a + (double)(i)*b) / (double)(n - 1);
sdata[tid] = f(x);
}
else {
sdata[tid] = 0.0;
}
// Synchronization is necessary after loading of sdata, to make sure that all threads of the block have loaded their element into sdata.
__syncthreads();
if (blockDim.x >= 1024) {
if (tid < 512) { sdata[tid] += sdata[tid + 512]; } __syncthreads(); }
if (blockDim.x >= 512) {
if (tid < 256) { sdata[tid] += sdata[tid + 256]; } __syncthreads(); }
if (blockDim.x >= 256) {
if (tid < 128) { sdata[tid] += sdata[tid + 128]; } __syncthreads(); }
if (blockDim.x >= 128) {
if (tid < 64) { sdata[tid] += sdata[tid + 64]; } __syncthreads(); }
if (tid < 32) {
// Warp size is 32 threads, so the next instructions don't need synchronization. It's implicitly performed on the warp level. That saves a lot of time.
if (blockDim.x >= 64) sdata[tid] += sdata[tid + 32];
if (blockDim.x >= 32) sdata[tid] += sdata[tid + 16];
if (blockDim.x >= 16) sdata[tid] += sdata[tid + 8];
if (blockDim.x >= 8) sdata[tid] += sdata[tid + 4];
if (blockDim.x >= 4) sdata[tid] += sdata[tid + 2];
if (blockDim.x >= 2) sdata[tid] += sdata[tid + 1];
}
// Thread 0 writes result for this block to global mem
if (tid == 0) {
arrayDevice[blockIdx.x] = sdata[0];
}
}
void parQuad(const unsigned n, const double a, const double b, double *total, double *ExecTime) {
const double width = (b - a)/n;
unsigned numBlocksTotal = ceil((double)n / BLOCK_DIM);
const unsigned numGrids = ceil((double)numBlocksTotal / GRID_DIM_MAX_X);
double total_q = 0.0;
// New values for n, a and b - for the subregions.
const unsigned npart = ceil((double)n / numGrids);
const double widthRegion = (b - a)/numGrids;
double apart = a;
double bpart = apart + widthRegion;
// Create events for timing execution
cudaEvent_t start = cudaEvent_t();
cudaEvent_t stop = cudaEvent_t();
cudaEventCreate(&start);
cudaEventCreate(&stop);
// Record time into start event
cudaEventRecord(start, 0); // 0 is the default stream id
while (numBlocksTotal) {
unsigned numBlocks = numBlocksTotal < GRID_DIM_MAX_X ? numBlocksTotal : GRID_DIM_MAX_X;
unsigned newDim = numBlocks;
double *arrayDevice = NULL;
const size_t size = numBlocks * sizeof(double);
double *sumDevice = NULL;
size_t sumSize;
numBlocksTotal -= numBlocks;
// Allocate memory on the GPU for the array that holds values of partial sums, after first level of reduction
cudaMalloc((void **) &arrayDevice, size);
// Launch kernel that calculates values of f(x) and performs one level of reduction
parQuadKernel<<< numBlocks, BLOCK_DIM >>>(arrayDevice, npart, apart, bpart);
apart += (widthRegion + width);
bpart += (widthRegion + width);
numBlocks = ceil((double)newDim / BLOCK_DIM);
sumSize = numBlocks * sizeof(double);
cudaMalloc((void **) &sumDevice, sumSize);
// Launch kernel that performs sum reduction
sumReductionKernel<<< numBlocks, BLOCK_DIM >>>(arrayDevice, sumDevice, newDim);
while (numBlocks > 1) {
newDim = numBlocks;
numBlocks = ceil((double)newDim / BLOCK_DIM);
sumReductionKernel<<< numBlocks, BLOCK_DIM >>>(sumDevice, sumDevice, newDim);
}
// Copy results back to the host
double result;
cudaMemcpy(&result, sumDevice, sizeof(double), cudaMemcpyDeviceToHost);
total_q += result;
// Free CUDA memory
cudaFree(sumDevice);
cudaFree(arrayDevice);
}
total_q = (b - a) * total_q / (double)n;
// Record time into stop event
cudaEventRecord(stop, 0);
// Synchronize stop event to wait for end of kernel execution on stream 0
cudaEventSynchronize(stop);
// Compute elapsed time (done by CUDA run-time)
float elapsed = 0.f;
cudaEventElapsedTime(&elapsed, start, stop);
// Release events
cudaEventDestroy(start);
cudaEventDestroy(stop);
*total = total_q;
*ExecTime = (double)elapsed;
}
/* Trapezoidal rule */
// This kernel calculates values of f(x), performs one level of reduction, and puts the partial sums into global memory.
__global__ void parTrapKernel(double *arrayDevice, unsigned n, double a, double width) {
__shared__ double sdata[BLOCK_DIM];
unsigned tid = threadIdx.x;
unsigned i = blockIdx.x*blockDim.x + tid;
// Load block in the shared memory
if (i < n) {
double x = a + i*width;
if (i == 0 || i == n - 1)
sdata[tid] = 0.5*f(x);
else
sdata[tid] = f(x);
}
else {
sdata[tid] = 0.0;
}
// Synchronization is necessary after loading of sdata, to make sure that all threads of the block have loaded their element into sdata.
__syncthreads();
if (blockDim.x >= 1024) {
if (tid < 512) { sdata[tid] += sdata[tid + 512]; } __syncthreads(); }
if (blockDim.x >= 512) {
if (tid < 256) { sdata[tid] += sdata[tid + 256]; } __syncthreads(); }
if (blockDim.x >= 256) {
if (tid < 128) { sdata[tid] += sdata[tid + 128]; } __syncthreads(); }
if (blockDim.x >= 128) {
if (tid < 64) { sdata[tid] += sdata[tid + 64]; } __syncthreads(); }
if (tid < 32) {
// Warp size is 32 threads, so the next instructions don't need synchronization. It's implicitly performed on the warp level. That saves a lot of time.
if (blockDim.x >= 64) sdata[tid] += sdata[tid + 32];
if (blockDim.x >= 32) sdata[tid] += sdata[tid + 16];
if (blockDim.x >= 16) sdata[tid] += sdata[tid + 8];
if (blockDim.x >= 8) sdata[tid] += sdata[tid + 4];
if (blockDim.x >= 4) sdata[tid] += sdata[tid + 2];
if (blockDim.x >= 2) sdata[tid] += sdata[tid + 1];
}
// Thread 0 writes result for this block to global mem
if (tid == 0) {
arrayDevice[blockIdx.x] = sdata[0];
}
}
void parTrap(const unsigned n, const double a, const double b, double *total, double *ExecTime) {
const double width = (b - a)/n;
unsigned numBlocksTotal = ceil((double)n / BLOCK_DIM);
const unsigned numGrids = ceil((double)numBlocksTotal / GRID_DIM_MAX_X);
double total_t = 0.0;
// New values for n, a and b - for the subregions.
const unsigned npart = ceil((double)n / numGrids);
const double widthRegion = (b - a)/numGrids;
double apart = a;
// Create events for timing execution
cudaEvent_t start = cudaEvent_t();
cudaEvent_t stop = cudaEvent_t();
cudaEventCreate(&start);
cudaEventCreate(&stop);
// Record time into start event
cudaEventRecord(start, 0); // 0 is the default stream id
while (numBlocksTotal) {
unsigned numBlocks = numBlocksTotal < GRID_DIM_MAX_X ? numBlocksTotal : GRID_DIM_MAX_X;
unsigned newDim = numBlocks;
double *arrayDevice = NULL;
const size_t size = numBlocks * sizeof(double);
double *sumDevice = NULL;
size_t sumSize;
numBlocksTotal -= numBlocks;
// Allocate memory on the GPU for the array that holds values of partial sums, after first level of reduction
cudaMalloc((void **) &arrayDevice, size);
// Launch kernel that calculates values of f(x) and performs one level of reduction
parTrapKernel<<< numBlocks, BLOCK_DIM >>>(arrayDevice, npart, apart, width);
apart += (widthRegion + width);
numBlocks = ceil((double)newDim / BLOCK_DIM);
sumSize = numBlocks * sizeof(double);
cudaMalloc((void **) &sumDevice, sumSize);
// Launch kernel that performs sum reduction
sumReductionKernel<<< numBlocks, BLOCK_DIM >>>(arrayDevice, sumDevice, newDim);
while (numBlocks > 1) {
newDim = numBlocks;
numBlocks = ceil((double)newDim / BLOCK_DIM);
sumReductionKernel<<< numBlocks, BLOCK_DIM >>>(sumDevice, sumDevice, newDim);
}
// Copy results back to the host
double result;
cudaMemcpy(&result, sumDevice, sizeof(double), cudaMemcpyDeviceToHost);
total_t += result;
// Free CUDA memory
cudaFree(sumDevice);
cudaFree(arrayDevice);
}
total_t = width * total_t;
// Record time into stop event
cudaEventRecord(stop, 0);
// Synchronize stop event to wait for end of kernel execution on stream 0
cudaEventSynchronize(stop);
// Compute elapsed time (done by CUDA run-time)
float elapsed = 0.f;
cudaEventElapsedTime(&elapsed, start, stop);
// Release events
cudaEventDestroy(start);
cudaEventDestroy(stop);
*total = total_t;
*ExecTime = (double)elapsed;
}
/* Simpson 1/3 rule */
// This kernel calculates values of f(x), and puts them into global memory.
__global__ void parSimpKernel(double *arrayDevice, unsigned n, double a, double width) {
__shared__ double sdata[BLOCK_DIM];
unsigned tid = threadIdx.x;
unsigned i = blockIdx.x*blockDim.x + tid;
// Load block in the shared memory
if (i < n) {
double x = a + i*width;
if (i == 0 || i == n - 1) {
sdata[tid] = f(x);
}
else {
if (i % 2 == 1)
sdata[tid] = 4*f(x);
else
sdata[tid] = 2*f(x);
}
}
else {
sdata[tid] = 0.0;
}
// Synchronization is necessary after loading of sdata, to make sure that all threads of the block have loaded their element into sdata.
__syncthreads();
if (blockDim.x >= 1024) {
if (tid < 512) { sdata[tid] += sdata[tid + 512]; } __syncthreads(); }
if (blockDim.x >= 512) {
if (tid < 256) { sdata[tid] += sdata[tid + 256]; } __syncthreads(); }
if (blockDim.x >= 256) {
if (tid < 128) { sdata[tid] += sdata[tid + 128]; } __syncthreads(); }
if (blockDim.x >= 128) {
if (tid < 64) { sdata[tid] += sdata[tid + 64]; } __syncthreads(); }
if (tid < 32) {
// Warp size is 32 threads, so the next instructions don't need synchronization. It's implicitly performed on the warp level. That saves a lot of time.
if (blockDim.x >= 64) sdata[tid] += sdata[tid + 32];
if (blockDim.x >= 32) sdata[tid] += sdata[tid + 16];
if (blockDim.x >= 16) sdata[tid] += sdata[tid + 8];
if (blockDim.x >= 8) sdata[tid] += sdata[tid + 4];
if (blockDim.x >= 4) sdata[tid] += sdata[tid + 2];
if (blockDim.x >= 2) sdata[tid] += sdata[tid + 1];
}
// Thread 0 writes result for this block to global mem
if (tid == 0) {
arrayDevice[blockIdx.x] = sdata[0];
}
}
void parSimp(const unsigned n, const double a, const double b, double *total, double *ExecTime) {
const double width = (b - a)/n;
unsigned numBlocksTotal = ceil((double)n / BLOCK_DIM);
const unsigned numGrids = ceil((double)numBlocksTotal / GRID_DIM_MAX_X);
double total_s = 0.0;
// New values for n, a and b - for the subregions.
const unsigned npart = ceil((double)n / numGrids);
const double widthRegion = (b - a)/numGrids;
double apart = a;
// Create events for timing execution
cudaEvent_t start = cudaEvent_t();
cudaEvent_t stop = cudaEvent_t();
cudaEventCreate(&start);
cudaEventCreate(&stop);
// Record time into start event
cudaEventRecord(start, 0); // 0 is the default stream id
while (numBlocksTotal) {
unsigned numBlocks = numBlocksTotal < GRID_DIM_MAX_X ? numBlocksTotal : GRID_DIM_MAX_X;
unsigned newDim = numBlocks;
double *arrayDevice = NULL;
const size_t size = numBlocks * sizeof(double);
double *sumDevice = NULL;
size_t sumSize;
numBlocksTotal -= numBlocks;
// Allocate memory on the GPU for the array that holds values of partial sums, after first level of reduction
cudaMalloc((void **) &arrayDevice, size);
// Launch kernel that calculates values of f(x) and performs one level of reduction
parSimpKernel<<< numBlocks, BLOCK_DIM >>>(arrayDevice, npart, apart, width);
apart += (widthRegion + width);
numBlocks = ceil((double)newDim / BLOCK_DIM);
sumSize = numBlocks * sizeof(double);
cudaMalloc((void **) &sumDevice, sumSize);
// Launch kernel that performs sum reduction
sumReductionKernel<<< numBlocks, BLOCK_DIM >>>(arrayDevice, sumDevice, newDim);
while (numBlocks > 1) {
newDim = numBlocks;
numBlocks = ceil((double)newDim / BLOCK_DIM);
sumReductionKernel<<< numBlocks, BLOCK_DIM >>>(sumDevice, sumDevice, newDim);
}
// Copy results back to the host
double result;
cudaMemcpy(&result, sumDevice, sizeof(double), cudaMemcpyDeviceToHost);
total_s += result;
// Free CUDA memory
cudaFree(sumDevice);
cudaFree(arrayDevice);
}
total_s = width / 3 * total_s;
// Record time into stop event
cudaEventRecord(stop, 0);
// Synchronize stop event to wait for end of kernel execution on stream 0
cudaEventSynchronize(stop);
// Compute elapsed time (done by CUDA run-time)
float elapsed = 0.f;
cudaEventElapsedTime(&elapsed, start, stop);
// Release events
cudaEventDestroy(start);
cudaEventDestroy(stop);
*total = total_s;
*ExecTime = (double)elapsed;
}
Results parallel(const unsigned n, const double a, const double b) {
Results results;
parQuad(n, a, b, &results.valQuad, &results.timeQuad);
parTrap(n, a, b, &results.valTrap, &results.timeTrap);
parSimp(n, a, b, &results.valSimp, &results.timeSimp);
return results;
}
void compareAndPrint(const unsigned n, const double a, const double b) {
Results seq, par;
seq = sequential(n, a, b);
par = parallel(n, a, b);
printf(" Sequential estimate quadratic rule = %24.16f\n", seq.valQuad);
printf(" Parallel estimate quadratic rule = %24.16f\n", par.valQuad);
printf("Sequential time quadratic rule = %f ms\n", seq.timeQuad);
printf("Parallel time quadratic rule = %f ms\n", par.timeQuad);
if (fabs(seq.valQuad - par.valQuad) < ACCURACY)
printf("\tTest PASSED!\n");
else
printf("\a\tTest FAILED!!!\n");
printf ("\n");
printf(" Sequential estimate trapezoidal rule = %24.16f\n", seq.valTrap);
printf(" Parallel estimate trapezoidal rule = %24.16f\n", par.valTrap);
printf("Sequential time trapezoidal rule = %f ms\n", seq.timeTrap);
printf("Parallel time trapezoidal rule = %f ms\n", par.timeTrap);
if (fabs(seq.valTrap - par.valTrap) < ACCURACY)
printf("\tTest PASSED!\n");
else
printf("\a\tTest FAILED!!!\n");
printf ("\n");
printf(" Sequential estimate Simpson 1/3 rule = %24.16f\n", seq.valSimp);
printf(" Parallel estimate Simpson 1/3 rule = %24.16f\n", par.valSimp);
printf("Sequential time Simpson 1/3 rule = %f ms\n", seq.timeSimp);
printf("Parallel time Simpson 1/3 rule = %f ms\n", par.timeSimp);
if (fabs(seq.valSimp - par.valSimp) < ACCURACY)
printf("\tTest PASSED!\n");
else
printf("\a\tTest FAILED!!!\n");
printf ("\n");
}
int main(int argc, char *argv[]) {
unsigned n;
double a;
double b;
if (argc != 4) {
n = 10000000;
a = 0.0;
b = 10.0;
}
else {
n = (unsigned)atoi(argv[1]);
a = atof(argv[2]);
b = atof(argv[3]);
}
printf("\n");
printf("QUAD:\n");
printf(" Estimate the integral of f(x) from A to B.\n");
printf(" f(x) = 50 / ( pi * ( 2500 * x * x + 1 ) ).\n");
printf("\n");
printf(" A = %f\n", a);
printf(" B = %f\n", b);
printf(" N = %u\n", n);
printf("\n");
// We can add this for correct time measurement in the nvprof profiler.
cudaDeviceSynchronize();
compareAndPrint(n, a, b);
printf("\n Normal end of execution.\n");
printf("\n");
return 0;
}