-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiGPU_f2d.cu
864 lines (589 loc) · 18.4 KB
/
multiGPU_f2d.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <iostream>
#include <chrono>
#include <memory>
#include <vector>
using namespace std;
using namespace std::chrono;
#define IMUL(a,b) __mul24(a,b)
cudaError_t performMultiGPUJacobi();
/*struct cuda_deleter
{
void operator() (void * p) { cudaFree(p); }
};
template<typename T>
auto make_unique_cuda_array(std::size_t size)
{
T * p = nullptr;
if (auto err = cudaMalloc((void**)&p, size * sizeof(T)))
throw std::bad_alloc();
return std::unique_ptr<T[], cuda_deleter>(p);
}*/
//Support for below c++14 on *nix
template<typename T, typename ...Args>
std::unique_ptr<T> make_unique(Args&& ...args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
struct create_DeviceHalos
{
int deviceID;
vector<float> eHalo;
vector<float> wHalo;
vector<float> nHalo;
vector<float> sHalo;
};
//Simple Jacobi iteration
__global__ void jacobi_Simple(const float *A0, const float *A1, const float *A2, const float *A3, const float *A4, float *x_in, float *x_out, const float *rhs, const float *nhalo, const float *shalo, const int deviceID)
{
int index = threadIdx.x + blockDim.x * blockIdx.x;
float result = rhs[index];
int dim = blockDim.x;// dim across x
int dim_y = gridDim.x;
int x_pos = blockIdx.x;
int y_pos = threadIdx.x;
//result = result+nhalo[y_pos];
//Get the boundaries
int leftBoundaryElem = x_pos * (dim);
int rightBoundaryElem = (x_pos * dim) + (dim - 1);
int topBoundaryElem = y_pos + ((dim_y - 1) * (dim));
int bottomBoundaryElem = y_pos;
//Carry out computations for boundary elements
if ((index == leftBoundaryElem) && (index == bottomBoundaryElem)) // Bottom left Corner Element
{
//Top
result -= A4[index] * x_in[index + dim];
//Right
result -= A3[index] * x_in[index + 1];
result /= A2[index];
x_out[index] = result;
return;
}
else if ((index == rightBoundaryElem) && (index == bottomBoundaryElem)) //Bottom Right Corner Element
{
//Top
result -= A4[index] * x_in[index + dim];
//Left
result -= A1[index] * x_in[index - 1];
result /= A2[index];
x_out[index] = result;
return;
}
else if ((index == leftBoundaryElem) && (index == topBoundaryElem)) //Top left Corner Element
{
//Bottom
result -= A0[index] * x_in[index - dim];
//Right
result -= A3[index] * x_in[index + 1];
result /= A2[index];
x_out[index] = result;
return;
}
else if ((index == rightBoundaryElem) && (index == topBoundaryElem)) //Top Right Corner Element
{
//Bottom
result -= A0[index] * x_in[index - dim];
//Left
result -= A1[index] * x_in[index - 1];
result /= A2[index];
x_out[index] = result;
return;
}
else if (index == leftBoundaryElem)
{
//Bottom
result -= A0[index] * x_in[index - dim];
//Top
result -= A4[index] * x_in[index + dim];
//Right
result -= A3[index] * x_in[index + 1];
result /= A2[index];
x_out[index] = result;
return;
}
else if (index == bottomBoundaryElem) {
//Top
result -= A4[index] * x_in[index + dim];
//Left
result -= A1[index] * x_in[index - 1];
//Right
result -= A3[index] * x_in[index + 1];
result /= A2[index];
x_out[index] = result;
return;
}
else if (index == rightBoundaryElem) {
//Bottom
result -= A0[index] * x_in[index - dim];
//Top
result -= A4[index] * x_in[index + dim];
//Left
result -= A1[index] * x_in[index - 1];
result /= A2[index];
x_out[index] = result;
return;
}
else if (index == topBoundaryElem) {
//Bottom
result -= A0[index] * x_in[index - dim];
//Left
result -= A1[index] * x_in[index - 1];
//Right
result -= A3[index] * x_in[index + 1];
result /= A2[index];
x_out[index] = result;
return;
}
//For every other element not on the boundary
else {
//Bottom
result -= A0[index] * x_in[index - dim];
//Top
result -= A4[index] * x_in[index + dim];
//Left
result -= A1[index] * x_in[index - 1];
//Right
result -= A3[index] * x_in[index + 1];
result /= A2[index];
x_out[index] = result;
return;
}
}
//Init Halos: In 1D decomposition only North and South Halos are used. In 2D decomposition North, South, East and West Halo need to be initialized and computed
//In 3D decomposition North, South, East , West, Top and Bottom needs to be initialized and computed
void initHalos(int numDevices, vector<create_DeviceHalos> &deviceArray, int dim_x, float *vec_in) {
deviceArray.resize(numDevices);
int chunksize = ((dim_x*dim_x) / numDevices);
cout << "chunk size is :"<< chunksize;
for (int i = 0, pos= chunksize;i < numDevices;pos+=chunksize,i++) {
deviceArray[i].deviceID = i;
deviceArray[i].nHalo.resize(dim_x);
//TODO: 2D halo exchange
//TODO: deviceArray[i].eHalo.resize(dim_x);
//TODO: deviceArray[i].wHalo.resize(dim_x);
deviceArray[i].sHalo.resize(dim_x);
if (numDevices == 1)
{
for (int count = 0;count<dim_x;count++)
{
deviceArray[i].nHalo[count] = 1.0f;
deviceArray[i].sHalo[count] = 1.0f;
}
return;
}
//First Device needs only nHalo
if (i == 0)
{
for (int k = pos,count=0;count<dim_x;k++,count++)
{
cout << "Halo nPosition for first Device is : " << k << endl;
deviceArray[i].nHalo[count] = vec_in[k];
}
}
//Last device needs only sHalo
else if (i == (numDevices-1))
{
for (int k = pos-(chunksize+dim_x),count=0;count<dim_x;count++,k++)
{
cout << "Halo sPosition for Last Device is : " << k << endl;
deviceArray[i].sHalo[count] = vec_in[k];
}
}
//All the other devices need both sHalo and nHalo
else
{
for (int k = pos, count=0;count<dim_x;count++,k++)
{
cout << "Halo nPosition for Mid Device "<<i<<" is : " <<k<< endl;
deviceArray[i].nHalo[count] = vec_in[k];
}
for (int k = pos-(chunksize +dim_x),count=0;count<dim_x;count++,k++)
{
cout << "Halo sPosition for Mid Device " << i << " is : " << k << endl;
deviceArray[i].sHalo[count] = vec_in[k];
}
}
}
}
//Init matrix Diagonals A0, A1, A2, A3, A4
void initDiag(float *A0, float *A1, float *A2, float *A3, float *A4, float *rhs, float *vec_in, float *vec_out, int dim)
{
//Not accounted for Obstacles
for (int i = 0; i < dim; ++i)
{
for (int j = 0; j < dim; ++j)
{
int idx = j + dim * i;
//Bottom
if (i == 0) {
A0[idx] = 0.0f;
}
else {
A0[idx] = 1.0f;
}
//Left
if (j == 0)
{
A1[idx] = 0.0f;
}
else {
A1[idx] = 1.0f;
}
//Right
if (j == dim - 1)
{
A3[idx] = 0.0f;
}
else {
A3[idx] = 1.0f;
}
//Top
if (i == dim - 1)
{
A4[idx] = 0.0f;
}
else {
A4[idx] = 1.0f;
}
//Primary Diagonal
A2[idx] = 1.0f; // sum of A0, A1, A3, A4 ... except all 0.0...
//Result(RHS) and Vector_In
rhs[idx] = 1.0f;
vec_in[idx] = 1.0f;
vec_out[idx] = 0.0f;
}
}
// (i1, j1) = dimX / 4, dimY / 2
// (i2, j2) = 3 * dimX / 4, dimY / 2
// rhs[i1, j1] = +5.0
// rhs[i2, j2] = -5.0
}
void getAllDeviceProperties() {
int nDevices;
cudaGetDeviceCount(&nDevices);
for (int i = 0; i < nDevices; i++) {
cudaDeviceProp prop;
cudaGetDeviceProperties(&prop, i);
printf("Device Number: %d\n", i);
printf(" Device name: %s\n", prop.name);
printf(" Memory Clock Rate (KHz): %d\n",
prop.memoryClockRate);
printf(" Memory Bus Width (bits): %d\n",
prop.memoryBusWidth);
printf(" Peak Memory Bandwidth (GB/s): %f\n\n",
2.0*prop.memoryClockRate*(prop.memoryBusWidth / 8) / 1.0e6);
}
}
cudaError_t performMultiGPUJacobi()
{
//Fixed values to be changed later
int dim = 8;
//TODO: write a 2D domain decomposition method for more than 2 GPUs
int size = dim * dim;
//auto result = make_unique<float[]>(size);
//Create Diagonal Vectors
/*auto a0 = make_unique<float[]>(size);
auto a1 = make_unique<float[]>(size);
auto a2 = make_unique<float[]>(size);
auto a3 = make_unique<float[]>(size);
auto a4 = make_unique<float[]>(size);
auto vec_in = make_unique<float[]>(size);
auto rhs = make_unique<float[]>(size);
auto vec_out = make_unique<float[]>(size);*/
std::vector<float> a0(size);
std::vector<float> a1(size);
std::vector<float> a2(size);
std::vector<float> a3(size);
std::vector<float> a4(size);
std::vector<float> vec_in(size);
std::vector<float> vec_out(size);
std::vector<float> rhs(size);
std::vector<float> result(size);
//Get the total number of devices
int numDevices;
cudaGetDeviceCount(&numDevices);
cout << endl << "Total number of Devices in the System are : " << numDevices << endl;
getAllDeviceProperties();
//For both the GPUs - One halo per device(for 2 GPUs down and up halos are needed) : TODO: division when exchanging with more than 2 GPUs
//For 4 GPUs up, down, left right would be needed (1*dim must be changed to 4 * dim)
//1*dim*numDevices calculates total storage space needed for Halos 1:-Total halos needed per device. dim:-number of elements in the dimension
//auto halos = make_unique<float[]>(haloStorage);
//Configuring the number of GPU's manually
//numDevices=2;
initDiag(&a0[0], &a1[0], &a2[0], &a3[0], &a4[0], &rhs[0], &vec_in[0], &vec_out[0], dim);
vector<create_DeviceHalos> deviceArray;
initHalos(numDevices,deviceArray,dim, &vec_in[0]);
if(numDevices>1){
cout << endl << "Halo Init.."<<endl;
for (int i = 0;i < numDevices;i++) {
cout<<"Device ID: "<<deviceArray[i].deviceID;
//First Device needs only nHalo
if (i == 0)
{
cout << "First Device";
for (int k = 0;k<dim;k++)
{
cout<< deviceArray[i].nHalo[k];
}
}
//Last device needs only sHalo
else if (i == (numDevices - 1))
{
cout << "Last Device";
for (int k = 0;k<dim;k++)
{
cout << deviceArray[i].sHalo[k];
}
}
//All the other devices need both sHalo and nHalo
else
{
cout << "Middle Device";
for (int k = 0;k<dim;k++)
{
cout << deviceArray[i].nHalo[k];
}
for (int k = 0;k<dim;k++)
{
cout << deviceArray[i].sHalo[k];
}
}
cout << endl;
}
cout << endl;
cout << endl;
cout << endl;
}
cout << "A0 ....";
for (int i = 0; i < size; i++) {
cout << a0[i] << " ";
}
cout << endl;
cout << "A1 ....";
for (int i = 0; i < size; i++) {
cout << a1[i] << " ";
}
cout << endl;
cout << "A2 ....";
for (int i = 0; i < size; i++) {
cout << a2[i] << " ";
}
cout << endl;
cout << "A3 ....";
for (int i = 0; i < size; i++) {
cout << a3[i] << " ";
}
cout << endl;
cout << "A4 ....";
for (int i = 0; i < size; i++) {
cout << a4[i] << " ";
}
cout << endl;
cout << "RHS ....";
for (int i = 0; i < size; i++) {
cout << rhs[i] << " ";
}
cout << endl;
cout << "Vec In ...." << endl;
for (int i = size - 1; i >= 0; i--) {
if ((i + 1) % dim == 0) { cout << endl; }
cout << vec_in[i] << " ";
}
cout << endl;
cout << "Made it here..";
/* auto d_A0 = make_unique_cuda_array<float>(size);
auto d_A1 = make_unique_cuda_array<float>(size);
auto d_A2 = make_unique_cuda_array<float>(size);
auto d_A3 = make_unique_cuda_array<float>(size);
auto d_A4 = make_unique_cuda_array<float>(size);
auto d_Vec_In = make_unique_cuda_array<float>(size);
auto d_Rhs = make_unique_cuda_array<float>(size);
auto d_Vec_Out = make_unique_cuda_array<float>(size); */
//Allocate memory on the devices
//Let the total number of GPU be 2 : has to be changed later
//Computation divided into (size/2) on first and size-(size/2) on second
int *domainDivision;
domainDivision = new int[numDevices];
//Logic for total chunk per device (Domain distribution)
for (int i = 0; i < numDevices; i++) {
//if(!(i==numDevices-1)){
domainDivision[i] = size / numDevices;
//size = (size - size / numDevices);
//}
}
//For use on Device
float *d_A0[4],
*d_A1[4],
*d_A2[4],
*d_A3[4],
*d_A4[4],
*d_Vec_In[4],
*d_Vec_Out[4],
*d_Rhs[4],
*d_nhalos[4],
*d_shalos[4];
/*d_A0 = new float[numDevices];
d_A1 = new float[numDevices];
d_A2 = new float[numDevices];
d_A3 = new float[numDevices];
d_A4 = new float[numDevices];
d_Vec_In = new float[numDevices];
d_Vec_Out = new float[numDevices];
d_Rhs = new float[numDevices];
d_halos = new float[numDevices];*/
/* The domain division is done in 1D rowise */
for (int dev = 0; dev<numDevices; dev++)
{
//Setting the device before allocation
cudaSetDevice(dev);
//cudamalloc the Diagonals
cudaMalloc((void**)&d_A0[dev], domainDivision[dev] * sizeof(float));
cudaMalloc((void**)&d_A1[dev], domainDivision[dev] * sizeof(float));
cudaMalloc((void**)&d_A2[dev], domainDivision[dev] * sizeof(float));
cudaMalloc((void**)&d_A3[dev], domainDivision[dev] * sizeof(float));
cudaMalloc((void**)&d_A4[dev], domainDivision[dev] * sizeof(float));
//cudamalloc the Input Vector and Result vector
cudaMalloc((void**)&d_Vec_In[dev], domainDivision[dev] * sizeof(float));
cudaMalloc((void**)&d_Vec_Out[dev], domainDivision[dev] * sizeof(float));
cudaMalloc((void**)&d_Rhs[dev], domainDivision[dev] * sizeof(float));
//cudaMalloc Halos: North and South--1D. TODO: East and West for 2D
cudaMalloc((void**)&d_nhalos[dev], dim * sizeof(float));
cudaMalloc((void**)&d_shalos[dev], dim * sizeof(float));
}
/* The transfer of Data from Host to Device */
for (int dev = 0, pos = 0; dev<numDevices; pos += domainDivision[dev], dev++)
{
//Setting the device before allocation
cudaSetDevice(dev);
//Copy the diagonals from host to device
cudaMemcpy(d_A0[dev], &a0[0] + pos, domainDivision[dev] * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_A1[dev], &a1[0] + pos, domainDivision[dev] * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_A2[dev], &a2[0] + pos, domainDivision[dev] * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_A3[dev], &a3[0] + pos, domainDivision[dev] * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_A4[dev], &a4[0] + pos, domainDivision[dev] * sizeof(float), cudaMemcpyHostToDevice);
//Copy in and out vectors and RHS
cudaMemcpy(d_Vec_In[dev], &vec_in[0] + pos, domainDivision[dev] * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_Vec_Out[dev], &vec_out[0] + pos, domainDivision[dev] * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_Rhs[dev], &rhs[0] + pos, domainDivision[dev] * sizeof(float), cudaMemcpyHostToDevice);
//Copy intial Halos in 1D : TODO compute more than 1D
if(dev==0){
cudaMemcpy(d_nhalos[dev], &deviceArray[dev].nHalo, dim * sizeof(float), cudaMemcpyHostToDevice);
}
else if(dev==(numDevices-1)){
cudaMemcpy(d_shalos[dev], &deviceArray[dev].sHalo, dim * sizeof(float), cudaMemcpyHostToDevice);
}
else {
cudaMemcpy(d_nhalos[dev], &deviceArray[dev].nHalo, dim * sizeof(float), cudaMemcpyHostToDevice);
cudaMemcpy(d_shalos[dev], &deviceArray[dev].sHalo, dim * sizeof(float), cudaMemcpyHostToDevice);
}
}
if (auto err = cudaGetLastError())
{
fprintf(stderr, "Jacobi launch failed: %s\n", cudaGetErrorString(err));
return err;
}
if (auto err = cudaGetLastError())
{
fprintf(stderr, "Jacobi launch failed: %s\n", cudaGetErrorString(err));
return err;
}
if (auto err = cudaGetLastError())
{
fprintf(stderr, "Jacobi launch failed: %s\n", cudaGetErrorString(err));
return err;
}
//multMatrix(d_A0, d_A1, d_A2, d_A3, d_A4, myDim, d_vec, d_res);
//Perform one Jacobi Step
int blocksize = dim / numDevices; //TODO: make it to more than 2 GPUs
int threads = dim;
//Call to kernal
int iterations = 4;
for (int i = 0; i<iterations; i++)
{
cout << endl << endl << "Iteration : " << i + 1 << endl << endl << endl;
for (int dev = 0, pos = 0; dev<numDevices; pos += domainDivision[dev], dev++)
{
cout << endl << endl << "Kernal Execution on GPU : " << dev;
cout << endl << "Position :" << pos;
cudaSetDevice(dev);
cout << endl << "Check Intermediate Result before it gets passed to kernal" << endl;
cudaMemcpy(&result[0] + pos, d_Vec_In[dev], domainDivision[dev] * sizeof(float), cudaMemcpyDeviceToHost);
/*for (int i = domainDivision[dev]+pos-1; i>=0; i--) {
if ((i + 1) % dim == 0) { cout << endl; }
cout << "matrix_pos:" << i << " " << result[i] << " ";
}*/
for (int i = size - 1; i >= 0; i--) {
if ((i + 1) % dim == 0) { cout << endl; }
cout << "#pos:" << i << " " << result[i] << " ";
}
jacobi_Simple <<<blocksize, threads >>>(d_A0[dev], d_A1[dev], d_A2[dev], d_A3[dev], d_A4[dev], d_Vec_In[dev], d_Vec_Out[dev], d_Rhs[dev], d_nhalos[dev], d_shalos[dev], deviceArray[dev].deviceID);
//TODO: Currently serial has to be done cudaMemcpyAsync using CUDA Streams
//Copy the intermediate result from Device to Host memory
cudaMemcpy(&result[0] + pos, d_Vec_Out[dev], domainDivision[dev] * sizeof(float), cudaMemcpyDeviceToHost);
//Copy the intermediate result from the Host memory to the Device memory
cudaMemcpy(d_Vec_In[dev], &result[0] + pos, domainDivision[dev] * sizeof(float), cudaMemcpyHostToDevice);
//Print Intermediate result
/* cout << endl <<"Intermediate Result";
for (int i = domainDivision[dev]; i >= 0; i--) {
if ((i + 1) % dim == 0) { cout << endl; }
cout << "position:"<<i<<" "<<result[i] << " ";
}*/
}
cout << endl << "Exchanging Halos";
}
if (auto err = cudaGetLastError())
{
fprintf(stderr, "Jacobi launch failed: %s\n", cudaGetErrorString(err));
return err;
}
cout << endl << "Iterations successful " << endl;
//Copy the final result from all devices
for (int dev = 0, pos = 0; dev < numDevices; pos += domainDivision[dev], dev++)
{
cudaMemcpy(&result[0] + pos, d_Vec_Out[dev], domainDivision[dev] * sizeof(float), cudaMemcpyDeviceToHost);
}
if (auto err = cudaGetLastError())
{
fprintf(stderr, "Jacobi launch failed: %s\n", cudaGetErrorString(err));
return err;
}
//Print result
for (int i = size - 1; i >= 0; i--) {
if ((i + 1) % dim == 0) { cout << endl; }
cout << result[i] << " ";
}
// Freeing memory auto done by cuda deleter
//Free memory on devices
for (int dev = 0; dev<numDevices; dev++)
{
cudaFree(d_A0[dev]);
cudaFree(d_A1[dev]);
cudaFree(d_A2[dev]);
cudaFree(d_A3[dev]);
cudaFree(d_A4[dev]);
cudaFree(d_Vec_In[dev]);
cudaFree(d_Vec_Out[dev]);
cudaFree(d_nhalos[dev]);
cudaFree(d_shalos[dev]);
cudaFree(d_Rhs[dev]);
}
cout << endl << "Device Memory free successful.";
//Take care of dynamic mem location
delete[] domainDivision;
return cudaSuccess;
}
int main()
{
cudaError_t cudaStatus = performMultiGPUJacobi();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "Computation failed! \n");
return 1;
}
cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceReset failed! \n");
return 1;
}
return 0;
}