-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainSort.hpp
168 lines (129 loc) · 3.21 KB
/
MainSort.hpp
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
//#include "ttmath/ttmath.h"
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <iostream>
#include "igcl/igcl.hpp"
using namespace std;
#define TEST_READY
//#define NUMSIZE 1024
//typedef ttmath::UInt<NUMSIZE> DATATYPE;
typedef unsigned int DATATYPE;
int ARRAYSIZE = 30000000;
int nTests = 33;
int nParticipants = 4;
void setSize(int val) { ARRAYSIZE = val; }
void setNTests(int val) { nTests = val; }
void setNNodes(int val) { nParticipants = val; }
void fill(DATATYPE * array, uint size)
{
for (uint i = 0; i < size; i++) {
array[i] = (int) (double(rand()) / RAND_MAX * INT_MAX);
}
}
void joinSort(const DATATYPE * arrayA, const uint sizeA, const DATATYPE * arrayB, const uint sizeB, DATATYPE * finalArray)
{
uint a = 0, b = 0, i = 0;
while (a < sizeA and b < sizeB)
{
if (arrayA[a] < arrayB[b])
finalArray[i++] = arrayA[a++];
else
finalArray[i++] = arrayB[b++];
}
while (a < sizeA)
finalArray[i++] = arrayA[a++];
while (b < sizeB)
finalArray[i++] = arrayB[b++];
}
bool confirm(DATATYPE * array)
{
for (int i = 0; i < ARRAYSIZE-1; i++) {
if (array[i] > array[i+1]) {
return false;
}
}
return true;
}
void work(igcl::Node * node)
{
for (int test=0; test<nTests; ++test)
{
//printf("STARTED\n");
igcl::peer_id id = node->getId();
timeval iniTimeGlobal, endTimeGlobal;
DATATYPE * array, * finalArray;
uint originalSize = 0, size;
igcl::peer_id parent;
if (id == 0)
{
array = (DATATYPE*) malloc(ARRAYSIZE*sizeof(DATATYPE));
originalSize = ARRAYSIZE;
srand(0);
fill(array, originalSize);
//cout << "STARTED" << endl;
gettimeofday(&iniTimeGlobal, NULL);
}
if (id > 0)
{
node->recvBranch(array, originalSize, parent);
}
node->branch<2>(array, originalSize, 1, size);
std::sort(array, array+size);
if (node->nDownstreamPeers() > 0)
{
finalArray = (DATATYPE*) malloc(originalSize*sizeof(DATATYPE));
igcl::result_type res = node->merge(finalArray, originalSize, 1, array, size, joinSort);
if (res != igcl::SUCCESS) {
return;
}
free(array);
array = finalArray;
}
if (id > 0)
{
node->returnBranch(array, originalSize, 1, parent);
}
if (id == 0)
{
gettimeofday(&endTimeGlobal, NULL);
printf("Time = %ld ms (parallel)\n", timeDiff(iniTimeGlobal, endTimeGlobal));
if (!confirm(array)) {
printf("ARRAY IS NOT SORTED!!!!!!!\n");
/*for (int i=0; i<ARRAYSIZE; i++) {
std::cout << array[i] << " ";
}
std::cout << std::endl;*/
//} else {
//printf("ARRAY IS SORTED\n");
}
// fill array again and test sequential speed for comparison
/*srand(0);
fill(array, ARRAYSIZE);
timeval iniTime, endTime;
gettimeofday(&iniTime, NULL);
std::sort(array, array+ARRAYSIZE);
gettimeofday(&endTime, NULL);
printf("[%d] time = %ld ms (sequential)\n", id, timeDiff(iniTime, endTime));*/
}
//printf("ENDED\n");
free(array);
}
}
void runCoordinator(igcl::Coordinator * coord)
{
GroupLayout layout = GroupLayout::getSortTreeLayout(nParticipants, 2);
//GroupLayout layout = GroupLayout::getAllToAllLayout(nParticipants);
coord->setLayout(layout);
coord->start();
coord->waitForNodes(nParticipants);
work(coord);
//coord->hang();
coord->terminate();
}
void runPeer(igcl::Peer * peer)
{
peer->start();
work(peer);
peer->hang();
}