-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
202 lines (152 loc) · 5.32 KB
/
main.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
#include <iostream>
#include <map>
#include "harp.h"
#include <time.h>
#include <TableInfo.h>
#include "DataTypes.h"
using namespace std;
using namespace harp::ds;
using namespace harp::comm;
template<class TYPE>
void printTable(int worker, Table<TYPE> *tab) {
string pri = "\n" + to_string(worker) + " [";
for (const auto f: *tab->getPartitions()) {
auto data = f.second->getData();
for (int i = 0; i< f.second->getSize(); i++) {
pri += to_string(data[i]) + ",";
}
pri += "|";
}
pri += "]";
cout << pri << endl;
}
/**
* create a Table with given properties
* all properties shows max value
* all properties are assigned random values
* @param tableId
* @param maxPartitions
* @param maxPartitionSize
* @param maxValue
* @return
*/
template<class TYPE>
Table<TYPE> * createTable(int tableId, int maxPartitions, int maxPartitionSize, int maxValue, int workerId=0) {
auto *tab = new Table<TYPE>(tableId);
srand(workerId + time(NULL));
// int numOfPartitions = 3;
int numOfPartitions = rand() % maxPartitions;
// cout << "number of partitions: " << numOfPartitions << endl;
for (int p = 0; p < numOfPartitions; p++) {
int partitionSize = rand() % maxPartitionSize + 1;
TYPE *data = new TYPE[partitionSize];
for (int j = 0; j < partitionSize; j++) {
data[j] = rand() % maxValue;
}
auto *partition = new Partition<TYPE>(p, data, partitionSize);
tab->addPartition(partition);
}
return tab;
}
class MyWorker : public harp::worker::Worker {
void execute(Communicator *comm, int argc = 0, char *argv[] = NULL) override {
// testBroadcast(comm);
// testAllReduce(comm);
// testGather(comm);
testAllGather(comm);
}
void testBroadcast(Communicator *comm) {
Table<int> * tab;
// populate the table for the first worker
// this table will be broadcasted to all
if (this->workerId == 0) {
tab = createTable<int>(0, 5, 20, 1000);
cout << "original table: " << endl;
printTable(workerId, tab);
// tables of other workers will be empty
} else {
tab = new Table<int>(0);
}
// broadcast from 0 to all
// comm->broadcastAsOneArray(tab, 0);
comm->broadcastAsPartitions(tab, 0);
printTable(workerId, tab);
tab->clear();
}
void testAllReduce(Communicator *comm) {
Table<int> * tab;
tab = createTable<int>(0, 3, 10, 1000);
printTable(workerId, tab);
// broadcast from 0 to all
// comm->allReduceAsPartitions(tab, MPI_SUM);
comm->allReduceAsOneArray(tab, MPI_SUM);
cout << "table after allReduce" << endl;
printTable(workerId, tab);
tab->clear();
}
void testGather(Communicator *comm) {
Table<double> * tab = createTable<double>(0, 4, 10, 1000, workerId);
printTable(workerId, tab);
int rootWorkerId = 3;
// auto tables = comm->gatherAsPartitions(tab, rootWorkerId);
auto tables = comm->gatherAsOneArray(tab, rootWorkerId);
if (workerId == rootWorkerId) {
cout << "tables after gather at worker: " << workerId << endl;
for (auto * table : *tables) {
printTable(table->getId(), table);
// harp::util::print::printTable(table);
}
}
tab->clear();
}
void testAllGather(Communicator *comm) {
Table<double> * tab = createTable<double>(0, 4, 10, 1000, workerId);
printTable(workerId, tab);
// broadcast from 0 to all
auto tables = comm->allGatherAsOneArray(tab);
// auto tables = comm->allGatherAsPartitions(tab);
if (workerId == 1) {
cout << "tables after all gather at worker: " << workerId << endl;
for (auto * table : *tables) {
printTable(table->getId(), table);
// harp::util::print::printTable(table);
}
}
cout << "table after allGather" << endl;
// printTable(workerId, tab);
tab->clear();
}
void testRotate(Communicator *comm) {
auto *tab = new Table<int>(1);
srand(workerId + time(NULL));
int numOfPartitions = rand() % 40;
for (int p = 0; p < numOfPartitions; p++) {
int partitionSize = rand() % 1000000;
int *data = new int[partitionSize];
for (int j = 0; j < partitionSize; j++) {
data[j] = rand() % 100;
}
auto *partition = new Partition<int>(p, data, partitionSize);
tab->addPartition(partition);
}
//printPartitions(workerId, tab);
cout << this->workerId << " arrived at the barrier" << endl;
comm->barrier();
cout << this->workerId << " released from the barrier" << endl;
comm->rotate(tab);
// comm->barrier();
// printPartitions(workerId, &tab);
// comm->broadcast(tab, 0);
//comm->allReduce(&tab, MPI_SUM);
cout << this->workerId << " bcast complete" << endl;
comm->barrier();
//printPartitions(workerId, tab);
tab->clear();
}
}; // end of MyWorker
int main() {
MyWorker worker;
worker.init(0, nullptr);
worker.start();
return 0;
}