-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW1_101062141_basic.c
360 lines (352 loc) · 9.79 KB
/
HW1_101062141_basic.c
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
// Sample MPI Program for Parallel Programming 2015 Lab1
// You can reuse this code as a template for homework 1.
// Or you can write your own from scratch!
// It's up to you.
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h> // memcmp
#define ROOT 0
#define NOTSORTED 444
#define IS_QS 100
inline void printall(int *array, int length){
int i;
for(i=0;i<length;i++){
printf("%d ",array[i]);
}
putchar('\n');
}
inline void swap(int* a, int* b){
*a = *a ^ *b;
*b = *a ^ *b;
*a = *a ^ *b;
}
void singleOESort(int* array, int length){
int i,sorted=0;
while(!sorted){
sorted = 1;
for(i=0;i+1<length;i+=2){
if(array[i]>array[i+1]){
swap(&array[i],&array[i+1]);
sorted = 0;
}
}
for(i=1;i+1<length;i+=2){
if(array[i]>array[i+1]){
swap(&array[i],&array[i+1]);
sorted = 0;
}
}
}
}
int main (int argc, char *argv[]) {
int rank, size;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Status status;
if (argc < 4) {
if (rank == ROOT) {
fprintf(stderr, "Insufficient args\n");
fprintf(stderr, "Usage: %s N input_file output_file", argv[0]);
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}
int N = atoi(argv[1]), alloc_num, former_alloc_num=0, last_alloc_num=0;
const char *inName = argv[2];
const char *outName = argv[3];
double start, finish, iotime = 0, commtime = 0, io_all, comm_all, cpu_all, cputime = 0, cpustart, cpufinish;
int *root_ptr; // for root process (which rank == 0) only
// Part 1: Read file
/* Note: You should deal with cases where (N < size) in Homework 1 */
int rc, i, trend = NOTSORTED;
int *array;
MPI_File fp;
MPI_File fh;
MPI_Offset my_offset;
MPI_Request req;
rc = MPI_File_open(MPI_COMM_WORLD, inName, MPI_MODE_RDONLY, MPI_INFO_NULL, &fp);
if(rc != MPI_SUCCESS){
MPI_Abort(MPI_COMM_WORLD, rc);
}
MPI_Offset total_number_of_bytes;
MPI_File_get_size(fp, &total_number_of_bytes);
if(total_number_of_bytes/sizeof(int)<N){
if(rank==ROOT)
puts("N is bigger than testcase in input file, read to the end");
N = total_number_of_bytes/sizeof(int);
}
// sheu
// todo
// detect whether the file is sorted
//--------------------------------------------------------------------------------
if(rank!=ROOT){
MPI_File_open(MPI_COMM_WORLD, outName, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
}
else{
alloc_num = N;
MPI_File_seek(fp,(MPI_Offset)0, MPI_SEEK_SET);
array = (int*)malloc(sizeof(int)*alloc_num);
start = MPI_Wtime();
MPI_File_read(fp, array, alloc_num, MPI_INT, &status);
finish = MPI_Wtime();
iotime += finish-start;
MPI_File_open(MPI_COMM_WORLD, outName, MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &fh);
if(N>=2){
#ifdef DEBUG
printall(array, alloc_num);
#endif
if(array[0]>array[1])
trend = -1;
else if(array[0]==array[1])
trend = 0;
else
trend = 1;
for(i=2;i<alloc_num;i++){
if(array[i-1]>array[i]&&(trend==-1||trend==0)){
trend = -1;
continue;
}
else if(array[i-1]==array[i]&&trend==0)
continue;
else if(array[i-1]<array[i]&&(trend==1||trend==0)){
trend = 1;
continue;
}
else{
trend = NOTSORTED;
break;
}
}
#ifdef DEBUG
printf("%d\n", trend);
#endif
if(trend==1||trend==0){
printf("sorted file\n");
my_offset = 0;
printall(array, alloc_num);
start = MPI_Wtime();
MPI_File_write_at(fh, my_offset, array, alloc_num, MPI_INT, &status);
finish = MPI_Wtime();
iotime += finish - start;
printf("iotime : %8.5lf\ncommtime : %8.5lf\n",iotime,commtime);
}
else if(trend==-1){
printf("descending sorted file\n");
root_ptr = (int*)malloc(sizeof(int)*alloc_num);
for(i=0;i<alloc_num;i++){
root_ptr[i] = array[alloc_num-i-1];
}
my_offset = 0;
start = MPI_Wtime();
MPI_File_write_at(fh, my_offset, root_ptr, alloc_num, MPI_INT, &status);
finish = MPI_Wtime();
iotime += finish - start;
printf("iotime : %8.5lf\ncommtime : %8.5lf\n",iotime,commtime);
}
}
else{
// N==1
trend = 0;
my_offset = 0;
start = MPI_Wtime();
MPI_File_write_at(fh, my_offset, array, alloc_num, MPI_INT, &status);
finish = MPI_Wtime();
iotime += finish - start;
printf("iotime : %8.5lf\ncommtime : %8.5lf\n",iotime,commtime);
}
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Bcast(&trend, 1, MPI_INT, ROOT, MPI_COMM_WORLD);
if(trend==-1||trend==0||trend==1){
MPI_Finalize();
exit(0);
}
// read data in memory
// sheu if N < 2 x # of processes, root take over all computation
alloc_num = N/size;
if(2*size>N){
// if N < 2 x size, root take over
if(rank!=ROOT){
MPI_Finalize();
exit(0);
}
else{
alloc_num = N;
MPI_File_seek(fp,(MPI_Offset)0, MPI_SEEK_SET);
array = (int*)malloc(sizeof(int)*alloc_num);
start = MPI_Wtime();
MPI_File_read(fp, array, alloc_num, MPI_INT, &status);
finish = MPI_Wtime();
iotime = finish-start;
cpustart = MPI_Wtime();
singleOESort(array, alloc_num);
cpufinish = MPI_Wtime();
cputime = cpufinish - cpustart;
#ifdef DEBUG
printall(array, alloc_num);
#endif
my_offset = 0;
start = MPI_Wtime();
MPI_File_write_at(fh, my_offset, array, alloc_num, MPI_INT, &status);
finish = MPI_Wtime();
iotime += finish - start;
printf("iotime : %8.5lfs\ncommtime : %8.5lfs\ncputime : %8.5fs\n",iotime,commtime,cputime);
MPI_Finalize();
exit(0);
}
}
else if((alloc_num)%2){
// if alloc_num is odd number
// every process alloc_num-- to guarantee every process has even elements
// except last node
alloc_num--;
former_alloc_num = alloc_num;
MPI_File_seek(fp,(MPI_Offset)alloc_num*rank*sizeof(int), MPI_SEEK_SET);
if(rank==size-1){
alloc_num = N - alloc_num * rank;
}
last_alloc_num = N - alloc_num * rank;
array = (int*)malloc(sizeof(int)*alloc_num);
start = MPI_Wtime();
MPI_File_read(fp, array, alloc_num, MPI_INT, &status);
finish = MPI_Wtime();
iotime += finish-start;
}
else{
// last process need deal with more inputs
former_alloc_num = alloc_num;
MPI_File_seek(fp, (MPI_Offset)rank*alloc_num*sizeof(int), MPI_SEEK_SET);
if(rank==size-1){
alloc_num = N - alloc_num * rank;
}
last_alloc_num = N - alloc_num * rank;
array = (int*)malloc(sizeof(int)*alloc_num);
start = MPI_Wtime();
MPI_File_read(fp, array, alloc_num, MPI_INT, &status);
finish = MPI_Wtime();
iotime += finish-start;
}
// sheu
// todo
// sort and communicate with other
//--------------------------------------------------------------------------------
int tmp1,tmp2,sorted_temp,count=0;
int sorted=0;
#ifdef DEBUG
int *num_ptr, *pos_ptr;
if(rank==ROOT){
root_ptr = (int*)calloc(0,sizeof(int)*N);
num_ptr = (int*)malloc(sizeof(int)*size);
pos_ptr = (int*)malloc(sizeof(int)*size);
for(i=0;i<size;i++){
num_ptr[i] = former_alloc_num;
pos_ptr[i] = i * former_alloc_num;
}
num_ptr[size-1] = last_alloc_num;
}
#endif
cpustart = MPI_Wtime();
while(!sorted){
sorted=1;
for(i=0;i+1<alloc_num;i+=2){
if(array[i]>array[i+1]){
swap(&array[i],&array[i+1]);
sorted = 0;
}
}
for(i=1;i<alloc_num;i+=2){
if(i==alloc_num-1){
start = MPI_Wtime();
// has even elements, which is guaranteed but last process
// send to rank+1, last node do nothing
if(rank!=size-1){
MPI_Isend(&array[i],1,MPI_INT,rank+1,0,MPI_COMM_WORLD,&req);
}
// receive from rank-1, root node do nothing
if(rank!=ROOT){
MPI_Recv(&tmp1,1,MPI_INT,rank-1,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
}
if(array[0]<tmp1&&rank!=ROOT){
swap(&array[0],&tmp1);
sorted = 0;
}
// tmp1 will always be smaller
if(rank!=size-1)
MPI_Wait(&req, MPI_STATUS_IGNORE);
// send to rank-1, root node do nothing
if(rank!=ROOT){
MPI_Isend(&tmp1,1,MPI_INT,rank-1,0,MPI_COMM_WORLD, &req);
}
// receive from rank+1, last node do nothing
if(rank!=size-1){
MPI_Recv(&tmp2,1,MPI_INT,rank+1,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
}
if(array[i]>tmp2&&rank!=size-1){
swap(&array[i],&tmp2);
sorted = 0;
}
if(rank!=ROOT)
MPI_Wait(&req, MPI_STATUS_IGNORE);
finish = MPI_Wtime();
commtime += finish - start;
continue;
}
else if(array[i]>array[i+1]){
swap(&array[i],&array[i+1]);
sorted = 0;
}
if(rank==size-1&&alloc_num%2&&i==alloc_num-2){
start = MPI_Wtime();
// has odd elements, only in last process
MPI_Recv(&tmp1,1,MPI_INT,rank-1,MPI_ANY_TAG,MPI_COMM_WORLD,&status);
if(array[0]<tmp1){
swap(&array[0],&tmp1);
sorted = 0;
}
MPI_Isend(&tmp1,1,MPI_INT,rank-1,0,MPI_COMM_WORLD,&req);
finish = MPI_Wtime();
commtime += finish - start;
}
}
start = MPI_Wtime();
MPI_Allreduce(&sorted,&sorted_temp,1,MPI_INT,MPI_LAND,MPI_COMM_WORLD);
finish = MPI_Wtime();
commtime += finish - start;
sorted = sorted_temp;
count++;
/*if(count>2*N){
printf("quite weird, I can't finish");
break;
}*/
}
cpufinish = MPI_Wtime();
cputime = cpufinish - cpustart - commtime;
#ifdef DEBUG
MPI_Barrier(MPI_COMM_WORLD);
printall(array,alloc_num);
MPI_Gatherv(array, alloc_num, MPI_INT, root_ptr, num_ptr, pos_ptr, MPI_INT, ROOT, MPI_COMM_WORLD);
if(rank==ROOT){
printall(root_ptr, N);
}
#endif
my_offset = rank*former_alloc_num*sizeof(int);
start = MPI_Wtime();
MPI_File_write_at(fh, my_offset, array, alloc_num, MPI_INT, &status);
finish = MPI_Wtime();
iotime += finish - start;
MPI_Allreduce(&iotime,&io_all,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD);
MPI_Allreduce(&commtime,&comm_all,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD);
MPI_Allreduce(&cputime,&cpu_all,1,MPI_DOUBLE,MPI_SUM,MPI_COMM_WORLD);
io_all /= size;
comm_all /= size;
if(rank==ROOT)
printf("iotime : %8.5lfs\ncommtime : %8.5lfs\ncputime : %8.5lfs(sum)\n",io_all ,comm_all, cpu_all);
MPI_Finalize();
return 0;
}