-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
348 lines (268 loc) · 8.07 KB
/
main.cpp
File metadata and controls
348 lines (268 loc) · 8.07 KB
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
#include "DivideAndConquer.h"
#include <string>
#include <cstring>
#define TYPE int
using namespace dac;
/*************************************
* main utilized for testing
*************************************/
class MSDivide : public Divide<TYPE>
{
public:
virtual std::vector<Chunk<TYPE> > divide(Chunk<TYPE> &chunk) { //divide works well
//dbgShowMsg("DIVIDING")
std::vector<Chunk<TYPE> > ret;
int size1 = chunk.size / 2, size2 = chunk.size - size1;
Chunk<TYPE> temp[2];
temp[0].size=size1;
temp[0].buf = (TYPE*)calloc(size1,sizeof(TYPE));
temp[1].size=size2;
temp[1].buf = (TYPE*)calloc(size2,sizeof(TYPE));
if(temp[0].buf == NULL || temp[1].buf == NULL) throw NoMemoryException();
int i;
for(i=0;i<size1;i++)
temp[0].buf[i] = chunk.buf[i];
for(int j=0;j<size2;j++,i++)
temp[1].buf[j] = chunk.buf[i];
// for(unsigned i=0; i<2500;i++)
// for(unsigned j=0; j<500;j++) ;
ret.push_back(temp[0]);
ret.push_back(temp[1]);
return ret;
}
};
class MSCombine : public Combine<TYPE>
{
public:
virtual Chunk<TYPE> combine(std::vector<Chunk<TYPE> > chunks) { //now it works!!
// dbgShowMsg("COMBINING")
Chunk<TYPE> ret;
unsigned i=0, j=0, k=0;
TYPE *v1=chunks[0].buf, *v2=chunks[1].buf;
//
// dbgShowMsg("chunks[0]");
// for(int l=0;l<chunks[0].size;l++)
// std::cout<<chunks[0].buf[l]<<", ";
// std::cout<<std::endl;
ret.size = chunks[0].size + chunks[1].size;
ret.buf = (TYPE*)calloc(ret.size, sizeof(TYPE));
if(ret.buf == NULL) throw NoMemoryException();
while(j<chunks[0].size && k<chunks[1].size) {
int min = (v1[j]<=v2[k]) ? v1[j++] : v2[k++];
ret.buf[i++] = min;
}
if(j==chunks[0].size) {
while(k<chunks[1].size)
ret.buf[i++] = v2[k++];
}
if(k==chunks[1].size) {
while(j<chunks[0].size)
ret.buf[i++] = v1[j++];
}
// for(unsigned i=0; i<2500;i++)
// for(unsigned j=0; j<500;j++) ;
return ret;
}
};
class MSBaseCase : public BaseCase<TYPE>
{
public:
virtual bool isBaseCase(Chunk<TYPE> chunk) { //baseSolve works well
//std::cout<<"IS BASE? returning"<<(chunk.size == 2)<<std::endl;
return (chunk.size <= 2);
}
virtual Chunk<TYPE> baseSolve(Chunk<TYPE> chunk) { //baseSolve works well
// dbgShowMsg("SOLVING BASE")
Chunk<TYPE> ret;
ret.size=chunk.size;
ret.buf=(TYPE*)calloc(ret.size, sizeof(TYPE));
if(ret.buf == NULL) throw NoMemoryException();
if(ret.size == 1) { ret.buf[0] = chunk.buf[0]; return ret; }
if(chunk.buf[0]>chunk.buf[1]) {ret.buf[0]=chunk.buf[1]; ret.buf[1]=chunk.buf[0];}
else {ret.buf[0]=chunk.buf[0]; ret.buf[1]=chunk.buf[1];}
// for (int i = 0; i < 5000; i++);
// for (int j = 0 ; j < 500; j++) ;
return ret;
}
};
void provaSendRecv(void);
int main(int argc, char **argv)
{
DivideAndConquer<TYPE> dac(2);
MSDivide d;
MSCombine c;
MSBaseCase b;
dac.setDivider(&d);
dac.setCombiner(&c);
dac.setBaseHandler(&b);
dac.setInputFile("/tmp/input.bin");
dac.setOutputFile("/tmp/output.bin");
dac.setDivisionDegree(2);
// dac.setThreshold(100);
// dac.setThreadNum(1);
try {
dac.start(argc, argv);
} catch (MPI::Exception e) {
std::cout << e.Get_error_string() << std::endl;
}
catch (NoResourcesException e) {
std::cout << e.suggestion() << std::endl;
}
// MPI::Init(argc, argv);
// provaSendRecv();
// MPI::COMM_WORLD.Barrier();
// MPI::Finalize();
return 0;
}
class QDivide : public Divide<TYPE>
{
public:
virtual std::vector<Chunk<TYPE> > divide(Chunk<TYPE> &chunk) {
std::vector<Chunk<TYPE> > ret;
int pivot;
pivot = chunk.size / 2;
TYPE *groupl = NULL, *groupg = NULL, *groupe = NULL;
int count1 = 0, count2 = 0, count3 = 0;
for (unsigned i = 0; i < chunk.size; i++) { // creating groups
if (chunk.buf[i] < chunk.buf[pivot]) {
count1++;
groupl = (TYPE *) realloc (groupl, sizeof(TYPE) * count1);
groupl[count1-1] = chunk.buf[i];
}
if (chunk.buf[i] > chunk.buf[pivot]) {
count2++;
groupg = (TYPE *) realloc (groupl, sizeof(TYPE) * count2);
groupg[count2-1] = chunk.buf[i];
}
if (chunk.buf[i] == chunk.buf[pivot]) {
count3++;
groupe = (TYPE *) realloc (groupl, sizeof(TYPE) * count3);
groupe[count3-1] = chunk.buf[i];
}
}
Chunk<TYPE> lesser, equal, greater;
lesser.buf = groupl;
lesser.size = count1;
equal.buf = groupe;
equal.size = count2;
greater.buf = groupg;
greater.size = count3;
ret.push_back(lesser);
ret.push_back(equal);
ret.push_back(greater);
return ret;
} // end divide
};
class QCombine : public Combine<TYPE>
{
public:
virtual Chunk<TYPE> combine(std::vector< Chunk<TYPE> > chunks){
Chunk<TYPE> ret;
ret.buf = (TYPE *) malloc (sizeof(TYPE) * (chunks[0].size + chunks[1].size + chunks[2].size));
memcpy(ret.buf, chunks[0].buf, chunks[0].size);
memcpy(&ret.buf[chunks[0].size], chunks[1].buf, chunks[1].size);
memcpy(&ret.buf[chunks[1].size], chunks[2].buf, chunks[2].size);
ret.size = chunks[0].size + chunks[1].size + chunks[2].size;
return ret;
}
};
class QBaseCase : public BaseCase<TYPE> {
public:
virtual bool isBaseCase(Chunk<TYPE> chunk){
if (chunk.size > 1) return false;
else return true;
}
virtual Chunk< TYPE> baseSolve(Chunk<TYPE> chunk){ return chunk; }
};
/*
class MyClass : public SerializableData {
private:
double f1;
int f2;
char f3;
//char *f3;
public:
MyClass() {
f1=3.1416;
f2=22;
f3='l';
}
virtual ~MyClass() {}
virtual int getSize() {
return (sizeof(double)+sizeof(int)+sizeof(char));
}
virtual bool serialize(void* buffer, int bufSize) {
char *buf = (char*)buffer;
int pos=0;
memcpy(buf,&f1,sizeof(double));
pos+=sizeof(double);
memcpy(buf+pos,&f2,sizeof(int));
pos+=sizeof(int);
memcpy(buf+pos,&f3,sizeof(char));
return true;
}
virtual bool deSerialize(void* buffer, int bufSize) {
char *buf=(char*)buffer;
memcpy(&f1,buf,sizeof(double));
memcpy(&f2,buf+sizeof(double),sizeof(int));
memcpy(&f3,buf+sizeof(double)+sizeof(int),sizeof(char));
//buffer=buf;
return true;
}
friend std::ostream& operator<<(std::ostream& out, MyClass& c) {
out << c.f1 << ", " << c.f2 << ", " << c.f3 << std::endl;
return out;
}
};
#define N 20
#define TYPE double
void provaSendRecv(void) {
Chunk<TYPE> chunk;
int myRank = MPI::COMM_WORLD.Get_rank();
//int procN = MPI::COMM_WORLD.Get_size();
if(myRank == 0) {
srand(time(NULL));
chunk.size = N;
chunk.buf = (TYPE *)malloc(N*sizeof(TYPE));
std::cout << "IsSer=" << IS_SERIALIZABLE(TYPE)<<std::endl;
std::cout.flush();
for(int i=0; i<N; i++) {
chunk.buf[i] = (TYPE)i/(rand()%N+1);
std::cout << "chunk.buf["<<i<<"]="<<chunk.buf[i]<<std::endl;
std::cout.flush();
}
std::cout<<std::endl<<"PROC 0 - CALL TO SEND"<<std::endl<<std::endl;
std::cout.flush();
send(1,chunk,MPI::COMM_WORLD,2,Bool2Type<IS_SERIALIZABLE(TYPE)>());
}
if(myRank == 1) {
MPI::Status status;
MPI::COMM_WORLD.Probe(MPI::ANY_SOURCE, 2, status);
std::cout<<std::endl<<"PROC 1 - CALL TO RECV"<<std::endl;
std::cout.flush();
recv(0,&chunk,status.Get_count(MPI::BYTE),MPI::COMM_WORLD,2,Bool2Type<IS_SERIALIZABLE(TYPE)>() );
std::cout<<"Chunk size="<<chunk.size<<std::endl;
for(int i=0; i<chunk.size; i++) {
std::cout << "chunk.buf["<<i<<"]="<<chunk.buf[i]<<std::endl;
std::cout.flush();
}
}
std::cout<<"PROCESS "<<myRank<<" - Finito"<<std::endl<<std::endl;
std::cout.flush();
}//*/
/*
class Prova : public SerializableData
{
public:
int getSize() {return 0;}
bool serialize(void* buffer, int* bufSize) {return true;}
bool deSerialize(void* buffer, int bufSize) {return true;}
};
class Prova2
{
public:
virtual ~Prova2() {}
int getSize() {return 0;}
bool serialize(void* buffer, int* bufSize) {return true;}
bool deSerialize(void* buffer, int bufSize) {return true;}
};//*/