-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpi_2dmesh.cpp
770 lines (645 loc) · 25.7 KB
/
mpi_2dmesh.cpp
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
//
// (C) 2021, E. Wes Bethel
// mpi_2dmesh.cpp
// usage:
// mpi_2dmesh [args as follows]
//
// command line arguments:
// -g 1|2|3 : domain decomp method, where 1=row-slab decomp, 2=column-slab decomp,
// 3=tile decomp (OPTIONAL, default is -g 1, row-slab decomp)
// -i filename : name of datafile containing raw unsigned bytes
// (OPTIONAL, default input filename in mpi_2dmesh.hpp)
// -x XXX : specify the number of columns in the mesh, the width (REQUIRED)
// -y YYY : specify the number of rows in the mesh, the height (REQUIRED)
// -o filename : where output results will be written (OPTIONAL, default output
// filename in mpi_2dmesh.hpp)
// -a 1|2 : the action to perform: 1 means perform per-tile processing then write
// output showing results, 2 means generate an output file with cells labeled as to
// which rank they belong to (depends on -g 1|2|3 setting)
// (OPTIONAL, default: -a 1, perform the actual processing)
// -v : a flag that will trigger printing out the 2D vector array of Tile2D (debug)
// (OPTIONAL, default value is no verbose debug output)
//
// Assumptions:
//
// Grid decompositions:
// When creating tile-based decomps, will compute the number of tiles in each
// dimension as sqrt(nranks); please use a number of ranks that is the square
// of an integer, e.g., 4, 9, 16, 25, etc.
#include <iostream>
#include <vector>
#include <stdio.h>
#include <unistd.h>
#include <mpi.h>
#include <math.h>
#include <algorithm>
#include <omp.h>
#include <chrono>
#include "mpi_2dmesh.hpp" // for AppState and Tile2D class
#define DEBUG_TRACE 0
int
parseArgs(int ac, char *av[], AppState *as)
{
int rstat = 0;
int c;
while ( (c = getopt(ac, av, "va:g:x:y:i:")) != -1) {
switch(c) {
case 'a': {
int action = std::atoi(optarg == NULL ? "-1" : optarg);
if (action != MESH_PROCESSING && action != MESH_LABELING_ONLY)
{
printf("Error parsing command line: -a %s is an undefined action \n",optarg);
rstat = 1;
}
else
as->action=action;
break;
}
case 'g': {
int decomp = std::atoi(optarg == NULL ? "-1" : optarg);
if (decomp != ROW_DECOMP && decomp != COLUMN_DECOMP && decomp != TILE_DECOMP)
{
printf("Error parsing command line: -g %s is an undefined decomposition\n",optarg);
rstat = 1;
}
else
as->decomp=decomp;
break;
}
case 'x' : {
int xval = std::atoi(optarg == NULL ? "-1" : optarg);
if (xval < 0 )
{
printf(" Error parsing command line: %d is not a valid mesh width \n",xval);
rstat = 1;
}
else
as->global_mesh_size[0] = xval;
break;
}
case 'y' : {
int yval = std::atoi(optarg == NULL ? "-1" : optarg);
if (yval < 0 )
{
printf(" Error parsing command line: %d is not a valid mesh height \n",yval);
rstat = 1;
}
else
as->global_mesh_size[1] = yval;
break;
}
case 'i' : {
strcpy(as->input_filename, optarg);
break;
}
case 'o' : {
strcpy(as->output_filename, optarg);
break;
}
case 'v' : {
as->debug = 1;
break;
}
} // end switch
} // end while
return rstat;
}
//
// computeMeshDecomposition:
// input: AppState *as - nranks, mesh size, etc.
// computes: tileArray, a 2D vector of Tile2D objects
//
// assumptions:
// - For tile decompositions, will use sqrt(nranks) tiles per axis
//
void
computeMeshDecomposition(AppState *as, vector < vector < Tile2D > > *tileArray) {
int xtiles, ytiles;
int ntiles;
if (as->decomp == ROW_DECOMP) {
// in a row decomposition, each tile width is the same as the mesh width
// the mesh is decomposed along height
xtiles = 1;
// set up the y coords of the tile boundaries
ytiles = as->nranks;
int ylocs[ytiles+1];
int ysize = as->global_mesh_size[1] / as->nranks; // size of each tile in y
int yval=0;
for (int i=0; i<ytiles; i++, yval+=ysize) {
ylocs[i] = yval;
}
ylocs[ytiles] = as->global_mesh_size[1];
// then, create tiles along the y axis
for (int i=0; i<ytiles; i++)
{
vector < Tile2D > tiles;
int width = as->global_mesh_size[0];
int height = ylocs[i+1]-ylocs[i];
Tile2D t = Tile2D(0, ylocs[i], width, height, i);
tiles.push_back(t);
tileArray->push_back(tiles);
}
}
else if (as->decomp == COLUMN_DECOMP) {
// in a columne decomposition, each tile height is the same as the mesh height
// the mesh is decomposed along width
ytiles = 1;
// set up the x coords of the tile boundaries
xtiles = as->nranks;
int xlocs[xtiles+1];
int xsize = as->global_mesh_size[0] / as->nranks; // size of each tile in x
int xval=0;
for (int i=0; i<xtiles; i++, xval+=xsize) {
xlocs[i] = xval;
}
xlocs[xtiles] = as->global_mesh_size[0];
// then, create tiles along the x axis
vector < Tile2D > tile_row;
for (int i=0; i<xtiles; i++)
{
int width = xlocs[i+1]-xlocs[i];
int height = as->global_mesh_size[1];
Tile2D t = Tile2D(xlocs[i], 0, width, height, i);
tile_row.push_back(t);
}
tileArray->push_back(tile_row);
}
else // assume as->decom == TILE_DECOMP
{
// to keep things simple, we will assume sqrt(nranks) tiles in each of x and y axes.
// if sqrt(nranks) is not an even integer, then this approach will result in some
// ranks without tiles/work to do
double root = sqrt(as->nranks);
int nranks_per_axis = (int) root;
xtiles = ytiles = nranks_per_axis;
// set up x coords for tile boundaries
int xlocs[xtiles+1];
int xsize = as->global_mesh_size[0] / nranks_per_axis; // size of each tile in x
int xval=0;
for (int i=0; i<xtiles; i++, xval+=xsize) {
xlocs[i] = xval;
}
xlocs[xtiles] = as->global_mesh_size[0];
// set up y coords for tile boundaries
int ylocs[ytiles+1];
int ysize = as->global_mesh_size[1] / nranks_per_axis; // size of each tile in y
int yval=0;
for (int i=0; i<ytiles; i++, yval+=ysize) {
ylocs[i] = yval;
}
ylocs[ytiles] = as->global_mesh_size[1];
// now, build 2D array of tiles
int rank=0;
for (int j = 0; j < ytiles; j++) { // fix me
vector < Tile2D > tile_row;
for (int i=0; i < xtiles; i++) {
int width, height;
width = xlocs[i+1]-xlocs[i];
height = ylocs[j+1]-ylocs[j];
Tile2D t = Tile2D(xlocs[i], ylocs[j], width, height, rank++);
tile_row.push_back(t);
}
tileArray->push_back(tile_row);
}
}
}
void
write_output_labels(AppState as,
vector < vector < Tile2D > >tileArray)
{
// create a buffer of ints, we will set buf[i,j] to be a value
// in the range 0..nranks-1 to reflect which rank is owner of
// a particular buf[i,j] location.
size_t xsize = as.global_mesh_size[0];
size_t ysize = as.global_mesh_size[1];
printf("\n\nWriting out mesh labels to a file \n");
int meshRankLabels[as.global_mesh_size[0]*as.global_mesh_size[1]];
for (off_t i = 0; i < xsize*ysize; i++)
meshRankLabels[i] = -1;
for (int row=0; row < tileArray.size(); row++)
{
printf(" Row %d of the tileArray is of length %d \n", row, tileArray[row].size());
for (int col=0;col < tileArray[row].size(); col++)
{
Tile2D t = tileArray[row][col];
t.print(row, col); // row, col
// for each tile, paint the meshRankLabels array with the tile's rank
// at the mesh locations owned by the tile
// base offset into the output buffer for this tile
size_t bufOffset = t.yloc * xsize + t.xloc;
for (int jj = 0; jj < t.height; jj++, bufOffset += xsize)
{
for (int ii = 0; ii < t.width; ii++)
meshRankLabels[bufOffset+ii] = t.tileRank;
}
xsize = as.global_mesh_size[0]; // breakpoint anchor
} // end loop over tileArray columns
ysize = as.global_mesh_size[1]; // breakpoint anchor
} // end loop over tileArray rows
FILE *f = fopen(as.output_filename, "w");
if (f == NULL) {
perror(" Error opening output file ");
}
fwrite((void *)meshRankLabels, sizeof(int), xsize*ysize, f);
fclose(f);
} // end writing an output buffer
void
printTileArray(vector < vector < Tile2D > > & tileArray)
{
printf("---- Contents of the tileArray, which is of length %d \n", tileArray.size());
for (int row=0;row<tileArray.size(); row++)
{
printf(" Row %d of the tileArray is of length %d \n", row, tileArray[row].size());
for (int col=0; col<tileArray[row].size(); col++)
{
Tile2D t = tileArray[row][col];
t.print(row, col);
}
}
}
float byteNormalize(unsigned char i)
{
#define ONE_OVER_255 0.003921568627451
return ((float)i * ONE_OVER_255);
}
void
loadInputFile(AppState *as)
{
// open the input file
FILE *f = fopen(as->input_filename, "r");
if (f == NULL)
{
perror(" Problem opening input file ");
return ;
}
// create the landing buffer for the input data
size_t nitems = as->global_mesh_size[0] * as->global_mesh_size[1];
vector<unsigned char> buf(nitems);
// read the input into the buffer
fread((void *)buf.data(), sizeof(unsigned char), nitems, f);
fclose(f);
// create space for the float-converted buffer
as->input_data_floats.resize(nitems);
// now convert from byte, in range 0..255, to float, in range 0..1
transform(buf.begin(), buf.end(), as->input_data_floats.begin(), byteNormalize);
}
unsigned char floatNormalize(float t)
{
// assume t in range 0..1
return ((unsigned char)(t*255.0));
}
void
writeOutputFile(AppState &as)
{
// open the input file
FILE *f = fopen(as.output_filename, "w");
if (f == NULL)
{
perror(" Problem opening output file ");
return ;
}
#if 0
// this code writes out the output as floats rather than converting to ubyte
size_t nitems = as.global_mesh_size[0] * as.global_mesh_size[1];
fwrite((const void *)as.output_data_floats.data(), sizeof(float), nitems, f);
#endif
#if 1
// create the landing buffer for the input data
size_t nitems = as.output_data_floats.size();
vector<unsigned char> buf(nitems);
// now convert from byte, in range 0..255, to float, in range 0..1
transform(as.output_data_floats.begin(), as.output_data_floats.end(), buf.begin(), floatNormalize);
// write out the byte buffer
fwrite((const void *)buf.data(), sizeof(unsigned char), nitems, f);
#endif
fclose(f);
}
void
sendStridedBuffer(float *srcBuf,
int srcWidth, int srcHeight,
int srcOffsetColumn, int srcOffsetRow,
int sendWidth, int sendHeight,
int fromRank, int toRank )
{
int msgTag = 0;
//
// ADD YOUR CODE HERE
// That performs sending of data using MPI_Send(), going "fromRank" and to "toRank". The
// data to be sent is in srcBuf, which has width srcWidth, srcHeight.
// Your code needs to send a subregion of srcBuf, where the subregion is of size
// sendWidth by sendHeight values, and the subregion is offset from the origin of
// srcBuf by the values specificed by srcOffsetColumn, srcOffsetRow.
int globalSize[2] = {srcHeight, srcWidth};
int startOffset[2] = {srcOffsetRow, srcOffsetColumn};
int subRegionSize[2] = {sendHeight, sendWidth};
MPI_Datatype subRegionType;
MPI_Type_create_subarray(2, globalSize, subRegionSize, startOffset, MPI_ORDER_C, MPI_FLOAT, &subRegionType);
MPI_Type_commit(&subRegionType);
MPI_Send(srcBuf, 1, subRegionType, toRank, msgTag, MPI_COMM_WORLD);
MPI_Type_free(&subRegionType);
}
void
recvStridedBuffer(float *dstBuf,
int dstWidth, int dstHeight,
int dstOffsetColumn, int dstOffsetRow,
int expectedWidth, int expectedHeight,
int fromRank, int toRank ) {
int msgTag = 0;
int recvSize[2];
MPI_Status stat;
//
// ADD YOUR CODE HERE
// That performs receiving of data using MPI_Recv(), coming "fromRank" and destined for
// "toRank". The size of the data that arrives will be of size expectedWidth by expectedHeight
// values. This incoming data is to be placed into the subregion of dstBuf that has an origin
// at dstOffsetColumn, dstOffsetRow, and that is expectedWidth, expectedHeight in size.
//
int globalSize[2] = {dstHeight, dstWidth};
int startOffset[2] = {dstOffsetRow, dstOffsetColumn};
int subRegionSize[2] = {expectedHeight, expectedWidth};
MPI_Datatype subRegionType;
MPI_Type_create_subarray(2, globalSize, subRegionSize, startOffset, MPI_ORDER_C, MPI_FLOAT, &subRegionType);
MPI_Type_commit(&subRegionType);
MPI_Recv(dstBuf, 1, subRegionType, fromRank, msgTag, MPI_COMM_WORLD, &stat);
MPI_Type_free(&subRegionType);
}
//
// ADD YOUR CODE HERE
// that performs sobel filtering
// suggest using your cpu code from HW5, no OpenMP parallelism
//
float sobel_filtered_pixel(float *s, int i, int j, int ncols, int nrows, float *gx, float *gy){
float t=0.0;
// ADD CODE HERE: add your code here for computing the sobel stencil computation at location (i,j)
// of input s, returning a float
float gradx = ((j - 1 >= 0)? s[i * ncols + j - 1] * gx[3] : 0.0) +
((i - 1 >=0 && j -1 >=0) ? s[i * ncols + j - 1 - ncols] * gx[0] : 0.0) +
((i + 1 < nrows && j -1 >=0)? s[i * ncols + j - 1 + ncols] * gx[6] : 0.0) +
((j + 1 <ncols)? s[i * ncols + j + 1] * gx[5] : 0.0) +
((i + 1 < nrows && j + 1 <ncols)? s[i * ncols + j + 1 + ncols] * gx[8] : 0.0) +
((i - 1 >=0 && j + 1 <ncols)? s[i * ncols + j + 1 - ncols] * gx[2]: 0.0);
float grady = ((i - 1 >=0) ? s[i * ncols + j - ncols] * gy[1] : 0.0) +
((i + 1 < nrows)? s[i * ncols + j + ncols] * gy[7] : 0.0) +
((i - 1 >=0 && j - 1 >=0)? s[i * ncols + j - 1 - ncols] * gy[0] : 0.0) +
((i + 1 < nrows && j -1 >=0)? s[i * ncols + j - 1 + ncols] * gy[6] : 0.0) +
((i + 1 < nrows && j + 1 <ncols)? s[i * ncols + j + 1 + ncols] * gy[8] : 0.0) +
((i - 1 >=0 && j + 1 <ncols)? s[i * ncols + j + 1 - ncols] * gy[2] : 0.0);
t = sqrt((gradx*gradx) + (grady*grady));
return t;
}
void do_sobel_filtering(float *in, float *out, int ncols, int nrows)
{
float Gx[] = {1.0, 0.0, -1.0, 2.0, 0.0, -2.0, 1.0, 0.0, -1.0};
float Gy[] = {1.0, 2.0, 1.0, 0.0, 0.0, 0.0, -1.0, -2.0, -1.0};
// ADD CODE HERE: insert your code here that iterates over every (i,j) of input, makes a call
// to sobel_filtered_pixel, and assigns the resulting value at location (i,j) in the output.
for(int i=0;i<nrows;i++){
for(int j=0;j<ncols;j++){
out[i*ncols+j] = sobel_filtered_pixel(in, i, j, ncols, nrows, Gx, Gy);
}
}
}
void
sobelAllTiles(int myrank, vector < vector < Tile2D > > & tileArray) {
for (int row=0;row<tileArray.size(); row++)
{
for (int col=0; col<tileArray[row].size(); col++)
{
Tile2D *t = &(tileArray[row][col]);
if (t->tileRank == myrank)
{
#if 0
// debug code
// v1: fill the output buffer with the value of myrank
// printf(" sobelAllTiles(): filling the output buffer of size=%d with myrank=%d\n:", t->outputBuffer.size(), myrank);
//std::fill(t->outputBuffer.begin(), t->outputBuffer.end(), myrank);
// v2. copy the input to the output, umodified
// std::copy(t->inputBuffer.begin(), t->inputBuffer.end(), t->outputBuffer.begin());
#endif
// ADD YOUR CODE HERE
// to call your sobel filtering code on each tile
if(t->tileRank==0)
{
printf("%f\t %f\t\n %f\t %f\t\n", t->inputBuffer[0], t->inputBuffer[1], t->inputBuffer[t->width], t->inputBuffer[t->width+1]);
printf("\n");
printf("%f\t %f\t\n %f\t %f\t\n", t->inputBuffer[t->width-2], t->inputBuffer[t->width-1], t->inputBuffer[t->width+t->width-2], t->inputBuffer[t->width+t->width-1]);
}
do_sobel_filtering(t->inputBuffer.data(), t->outputBuffer.data(), t->width, t->height);
}
}
}
}
void
scatterAllTiles(int myrank, vector < vector < Tile2D > > & tileArray, float *s, int global_width, int global_height, int &numMessage, double &messageSize)
{
#if DEBUG_TRACE
printf(" Rank %d is entering scatterAllTiles \n", myrank);
#endif
for (int row=0;row<tileArray.size(); row++)
{
for (int col=0; col<tileArray[row].size(); col++)
{
Tile2D *t = &(tileArray[row][col]);
if (myrank != 0 && t->tileRank == myrank)
{
int fromRank=0;
// receive a tile's buffer
t->inputBuffer.resize(t->width*t->height);
t->outputBuffer.resize(t->width*t->height);
#if DEBUG_TRACE
printf("scatterAllTiles() receive side:: t->tileRank=%d, myrank=%d, t->inputBuffer->size()=%d, t->outputBuffersize()=%d \n", t->tileRank, myrank, t->inputBuffer.size(), t->outputBuffer.size());
#endif
recvStridedBuffer(t->inputBuffer.data(), t->width, t->height,
0, 0, // offset into the tile buffer: we want the whole thing
t->width, t->height, // how much data coming from this tile
fromRank, myrank);
}
else if (myrank == 0)
{
if (t->tileRank != 0) {
#if DEBUG_TRACE
printf("scatterAllTiles() send side: t->tileRank=%d, myrank=%d, t->inputBuffer->size()=%d \n", t->tileRank, myrank, t->inputBuffer.size());
#endif
sendStridedBuffer(s, // ptr to the buffer to send
global_width, global_height, // size of the src buffer
t->xloc, t->yloc, // offset into the send buffer
t->width, t->height, // size of the buffer to send,
myrank, t->tileRank);
numMessage++;
messageSize += t->width*t->height*sizeof(float);
}
else // rather then have rank 0 send to rank 0, just do a strided copy into a tile's input buffer
{
t->inputBuffer.resize(t->width*t->height);
t->outputBuffer.resize(t->width*t->height);
off_t s_offset=0, d_offset=0;
float *d = t->inputBuffer.data();
for (int j=0;j<t->height;j++, s_offset+=global_width, d_offset+=t->width)
{
memcpy((void *)(d+d_offset), (void *)(s+s_offset), sizeof(float)*t->width);
}
}
}
}
} // loop over 2D array of tiles
#if DEBUG_TRACE
MPI_Barrier(MPI_COMM_WORLD);
if (myrank == 1){
printf("\n\n ----- rank=%d, inside scatterAllTiles debug printing of the tile array \n", myrank);
printTileArray(tileArray);
}
MPI_Barrier(MPI_COMM_WORLD);
#endif
}
void
gatherAllTiles(int myrank, vector < vector < Tile2D > > & tileArray, float *d, int global_width, int global_height, int &numMessage, double &messageSize)
{
for (int row=0;row<tileArray.size(); row++)
{
for (int col=0; col<tileArray[row].size(); col++)
{
Tile2D *t = &(tileArray[row][col]);
#if DEBUG_TRACE
printf("gatherAllTiles(): t->tileRank=%d, myrank=%d, t->outputBuffer->size()=%d \n", t->tileRank, myrank, t->outputBuffer.size());
#endif
if (myrank != 0 && t->tileRank == myrank)
{
// send the tile's output buffer to rank 0
sendStridedBuffer(t->outputBuffer.data(), // ptr to the buffer to send
t->width, t->height, // size of the src buffer
0, 0, // offset into the send buffer
t->width, t->height, // size of the buffer to send,
t->tileRank, 0); // from rank, to rank
numMessage++;
messageSize += t->width*t->height*sizeof(float);
}
else if (myrank == 0)
{
if (t->tileRank != 0) {
// receive a tile's buffer and copy back into the output buffer d
recvStridedBuffer(d, global_width, global_height,
t->xloc, t->yloc, // offset of this tile
t->width, t->height, // how much data coming from this tile
t->tileRank, myrank);
if(t->tileRank==1){
int count=0;
printf("First element is %f\n", d[0]);
printf("\n");
printf("last element is %f\n", d[t->width-1]);
}
}
else // copy from a tile owned by rank 0 back into the main buffer
{
float *s = t->outputBuffer.data();
off_t s_offset=0, d_offset=0;
d_offset = t->yloc * global_width + t->xloc;
for (int j=0;j<t->height;j++, s_offset+=t->width, d_offset+=global_width)
{
memcpy((void *)(d+d_offset), (void *)(s+s_offset), sizeof(float)*t->width);
}
}
}
}
} // loop over 2D array of tiles
}
int main(int ac, char *av[]) {
AppState as;
vector < vector < Tile2D > > tileArray;
std::chrono::time_point<std::chrono::high_resolution_clock> start_time, end_time;
std::chrono::duration<double> elapsed_scatter_time, elapsed_sobel_time, elapsed_gather_time;
MPI_Init(&ac, &av);
int myrank, nranks;
MPI_Comm_rank( MPI_COMM_WORLD, &myrank);
MPI_Comm_size( MPI_COMM_WORLD, &nranks);
int numMessage=0;
double messageSize=0.0;
as.myrank = myrank;
as.nranks = nranks;
if (parseArgs(ac, av, &as) != 0)
{
MPI_Finalize();
return 1;
}
char hostname[256];
gethostname(hostname, sizeof(hostname));
printf("Hello world, I'm rank %d of %d total ranks running on <%s>\n", as.myrank, as.nranks, hostname);
MPI_Barrier(MPI_COMM_WORLD);
#if DEBUG_TRACE
if (as.myrank == 0)
printf("\n\n ----- All ranks will computeMeshDecomposition \n");
#endif
computeMeshDecomposition(&as, &tileArray);
if (as.myrank == 0 && as.debug==1) // print out the AppState and tileArray
{
as.print();
printTileArray(tileArray);
}
MPI_Barrier(MPI_COMM_WORLD);
if (as.action == MESH_LABELING_ONLY) {
if (as.myrank==0) {
printf("\n\n Rank 0 is writing out mesh labels to a file \n");
write_output_labels(as, tileArray);
}
}
else
{
// === rank 0 loads the input file
if (as.myrank == 0)
{
#if DEBUG_TRACE
printf("\n\n Rank 0 is loading input \n");
#endif
loadInputFile(&as);
}
MPI_Barrier(MPI_COMM_WORLD);
// ----------- scatter phase of processing
// start the timer
start_time = std::chrono::high_resolution_clock::now();
scatterAllTiles(as.myrank, tileArray, as.input_data_floats.data(), as.global_mesh_size[0], as.global_mesh_size[1], numMessage, messageSize);
// end the timer
MPI_Barrier(MPI_COMM_WORLD);
end_time = std::chrono::high_resolution_clock::now();
elapsed_scatter_time = end_time - start_time;
// ----------- the actual processing
MPI_Barrier(MPI_COMM_WORLD);
// start the timer
start_time = std::chrono::high_resolution_clock::now();
sobelAllTiles(as.myrank, tileArray);
// end the timer
MPI_Barrier(MPI_COMM_WORLD);
end_time = std::chrono::high_resolution_clock::now();
elapsed_sobel_time = end_time - start_time;
// ----------- gather processing
// create output buffer space on rank 0
if (as.myrank == 0) {
as.output_data_floats.resize(as.global_mesh_size[0]*as.global_mesh_size[1]);
// initialize to a known value outside the range of expected values
std::fill(as.output_data_floats.begin(), as.output_data_floats.end(), -1.0);
}
MPI_Barrier(MPI_COMM_WORLD);
// start the timer
start_time = std::chrono::high_resolution_clock::now();
gatherAllTiles(as.myrank, tileArray, as.output_data_floats.data(), as.global_mesh_size[0], as.global_mesh_size[1], numMessage, messageSize);
// end the timer
MPI_Barrier(MPI_COMM_WORLD);
end_time = std::chrono::high_resolution_clock::now();
elapsed_gather_time = end_time - start_time;
// === write output
if (as.myrank == 0) // only rank 0 writes data to disk
{
writeOutputFile(as);
}
}
MPI_Barrier(MPI_COMM_WORLD);
if (as.myrank == 0) {
printf("\n\nTiming results from rank 0: \n");
printf("\tScatter time:\t%6.4f (ms) \n", elapsed_scatter_time*1000.0);
printf("\tSobel time:\t%6.4f (ms) \n", elapsed_sobel_time*1000.0);
printf("\tGather time:\t%6.4f (ms) \n", elapsed_gather_time*1000.0);
printf("\tNumber of messages sent:\t%d \n", numMessage);
printf("\tTotal message size:\t%6.4f (MB) \n", messageSize/(1024.0*1024.0));
}
MPI_Finalize();
return 0;
}
// EOF