-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
101 lines (87 loc) · 2.88 KB
/
main.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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
#include <mpi.h>
#include "graph.h"
#ifndef NODES
#define NODES 9
#endif
#ifndef MAXWEIGHT
#define MAXWEIGHT 20
#endif
#ifndef EDGEODDS
#define EDGEODDS 1 // 1:EDGEODDS probability of an edge during syntehsis.
#endif
int main(int argc, char** argv) {
int n = NODES, colorsize, chunksize;
int i, j, k;
int *Wo, *W, *colchunk, *rowchunk;
int rank,rowrank,colrank,rowcolor,colcolor,root;
int size, rowsize, colsize;
MPI_Status status;
MPI_Comm rowcomm, colcomm;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD,&rank);
MPI_Comm_size(MPI_COMM_WORLD,&size);
chunksize = n / (int)sqrt(size);
colorsize = (int)sqrt(size);
rowcolor = rank/colorsize;
colcolor = rank%colorsize;
MPI_Comm_split(MPI_COMM_WORLD,rowcolor,rank,&rowcomm);
MPI_Comm_split(MPI_COMM_WORLD,colcolor,rank,&colcomm);
MPI_Comm_rank(rowcomm,&rowrank); MPI_Comm_rank(colcomm,&colrank);
#ifdef DEBUG
// rowcolor should match colrank, and visa-versa
fprintf(stderr,"%d: nodes %d\tchunksize %d\trowcolor %d\trowrank %d\tcolcolor %d\tcolrank %d\n",
rank,n,chunksize,rowcolor,rowrank,colcolor,colrank);
#endif
#ifdef FULLGEN
Wo = (int*) calloc(n*n, sizeof(int));
W = (int*) calloc(n*n, sizeof(int));
//graphsynth(Wo, n, time(NULL),MAXWEIGHT,EDGEODDS);
matrixsynth(Wo, n, time(NULL), MAXWEIGHT);
for (i = 0; i < n*n; i++) {
W[i] = 0;
}
#else
Wo = (int*) calloc(chunksize*chunksize,sizeof(int));
W = (int*) calloc(chunksize*chunksize,sizeof(int));
matrixsynth(Wo, chunksize, time(NULL), MAXWEIGHT);
for (i = 0; i < chunksize*chunksize; i++) {
W[i] = 0;
}
#endif
colchunk = (int*) calloc(chunksize,sizeof(int));
rowchunk = (int*) calloc(chunksize,sizeof(int));
for (k = 0; k < n; k++) {
root = k/chunksize;
for (i = 0; i < chunksize; i++) {
int ktrunc = k%chunksize; // an adaptation of k truncated to chunk
rowchunk[i] = Wo[chunksize*ktrunc+i];
colchunk[i] = Wo[chunksize*i+ktrunc];
}
#ifdef DEBUG
fprintf(stderr,"%d: k %d\troot %d\n",
rank,k,root);
printgraph(W,chunksize);
#endif
MPI_Bcast(colchunk,chunksize,MPI_INT,root,colcomm);
MPI_Bcast(rowchunk,chunksize,MPI_INT,root,rowcomm);
for (i = 0; i < chunksize; i++) {
for (j = 0; j < chunksize; j++) {
//W[n*i+j] = min( Wo[n*i+j], Wo[n*i+k] + Wo[n*k+j]);
W[chunksize*i+j] = min( Wo[chunksize*i+j], colchunk[i] + rowchunk[j]);
}
}
for (i = 0; i < chunksize; i++) {
for (j = 0; j < chunksize; j++) {
Wo[chunksize*i+j] = W[chunksize*i+j];
}
}
}
MPI_Comm_free(&colcomm);
MPI_Comm_free(&rowcomm);
MPI_Finalize();
return 0;
}