-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkd_tree_with_3_process.cpp
267 lines (204 loc) · 6.2 KB
/
kd_tree_with_3_process.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
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
#include<iostream>
#include<algorithm> // for sort
#include<vector>
#include<bits/stdc++.h> // for 2D vector and sort function
#include<random>
#include<mpi.h>
using namespace std;
const int NDIM= 2;
// a structure to represent node ok kd tree
struct Node
{
int point[NDIM]; // to store the dimensional point
Node *left,*right;
};
Node *kd_tree( std::vector<std::vector<double>> vect, bool myaxis, int* compt){
struct Node *newnode = new Node;
if (vect.size()==1){
newnode-> point[0] = vect[0][0];
newnode->point[1] =vect[0][1];
newnode->right = newnode->left=NULL;
*compt = *compt+1;
return newnode;
}
else{
// let's find the median,
int m=vect.size(); //number of row
int l= m/2;
//1. sort vect according to axis myaxis
if(myaxis==true){ // we sort according to y axis
// #pragma omp parallel shared(vect,l,m)
{
// 1. Let's swap the vector
// #pragma omp for ordered
for(int i=0; i<m; i++){
// #pragma omp odered
swap(vect[i][0],vect[i][1]);}
// 2. sort the swap vector
sort(vect.begin(),vect.end());
// 3. swap again the vector
// #pragma omp for ordered
for(int i=0; i<m; i++){
// #pragma omp ordered
swap(vect[i][0],vect[i][1]);}
} // close pragma
}
else{ // we sort according to x axis
sort(vect.begin(),vect.end());
}
newnode->point[0]=vect[l][0];
newnode->point[1] = vect[l][1];
vector<vector<double>> left;
vector<vector<double>> right;
//#pragma omp parellel shared (vect,l,m,left,right)
//{
for (int i=0; i<l; i++)
{
left.push_back(vect[i]);}
for(int i=l+1; i<m;i++)
{
right.push_back(vect[i]);}
// std:: cout<< " out "<< left.size()<<right.size() <<std::endl;
// #pragma omp parallel
{
// #pragma omp single nowait // Due to nowait claude, all the threads skip the
// implied barrier at the end of single region and wait here for being assigned a task
{
// #pragma omp task
newnode->left = kd_tree(left,!myaxis, compt);
// #pragma omp task
{
if(right.size()>0) // this condition is use to avoid dumped core because, for 2 data, right=empty
newnode->right= kd_tree(right,!myaxis, compt);
}
}
}
return newnode;
}
}
int main(int argc, char **argv){
MPI_Init(&argc,&argv);
int rank =MPI::COMM_WORLD.Get_rank();
int nprc= MPI::COMM_WORLD.Get_size();
MPI_Status stats[nprc];
for(int m =4 ; m<5;m++){
int n= pow(10,m);
struct Node* root= new Node;
vector<vector<double>> vect{};
int compt{0};
double tab[2];
double starttime{0}, endtime{0};
// we want to performe 10^m iterrations
// #pragma omp parallel shared(vect,n)
// {
// int rank= omp_get_thread_num(); // this return the id of the thread
// int nthrds = omp_get_num_threads(); // return the number of thread
int count= n/nprc;
int start = rank*count;
int stop = start + count;
bool myaxis=true;
if(rank==0){
// Now let's perform the for loop
// #pragma omp for
for(int i=0; i<n; i++){
srand(i);
double a = double( rand())/double(RAND_MAX);
srand(i+10);
double b = double( rand())/double(RAND_MAX);
// #pragma omp critical
// { // we used critical to allow each thread to write on the shared
//memory, whithout its we will ends with dump core, because all the thread
// will try to write on the same memory at the same times.
// so, this imply only one thread can be at the critical region, other
// wait their turn.
vect.push_back({a,b});
}
// }
// }
starttime = MPI_Wtime();
sort(vect.begin(),vect.end()); // sort according to x axis.
int l= vect.size()/2;
MPI_Send(&l,1,MPI_INT,1,1,MPI_COMM_WORLD);
for(int i=l+1;i<n;i++)
{
tab[0] = vect[i][0];
tab[1] = vect[i][1];
MPI_Send(&tab,2,MPI_DOUBLE,1,1,MPI_COMM_WORLD); // the second proc is the tag
}
std::vector<std::vector<double>> vet{};
for(int i=0;i<l;i++)
vet.push_back(vect[i]);
bool myaxis=false;
root = kd_tree(vet,myaxis, &compt);
cout<< " Number of leaves " << compt<<endl;
endtime = MPI_Wtime();
cout<< rank<<","<<m<<","<<endtime-starttime<<endl;
}
if(rank ==1){
starttime = MPI_Wtime();
int l;
MPI_Recv(&l,1,MPI_INT,rank-1,rank,MPI_COMM_WORLD,&stats[rank]);// receive from previous rank
for(int i=1; i<l; i++){
MPI_Recv(&tab,2,MPI_DOUBLE,rank-1,rank,MPI_COMM_WORLD,&stats[rank]);
vect.push_back({tab[0],tab[1]});
}
myaxis= true;
// #pragma omp for ordered
for(int i=0; i<vect.size(); i++){
// #pragma omp odered
swap(vect[i][0],vect[i][1]);}
// 2. sort the swap vector
sort(vect.begin(),vect.end());
// 3. swap again the vector
// #pragma omp for ordered
for(int i=0; i<vect.size(); i++){
// #pragma omp ordered
swap(vect[i][0],vect[i][1]);}
int h= vect.size()/2;
MPI_Send(&h,1,MPI_INT,2,2,MPI_COMM_WORLD);
for(int i=h+1;i<vect.size();i++)
{
tab[0] = vect[i][0];
tab[1] = vect[i][1];
MPI_Send(&tab,2,MPI_DOUBLE,2,2,MPI_COMM_WORLD); // the second proc is the tag
}
std::vector<std::vector<double>> vet{};
for(int i=0;i<h;i++)
vet.push_back(vect[i]);
root = kd_tree(vet,myaxis, &compt);
cout<< " Number of leaves " << compt<<endl;
endtime = MPI_Wtime();
cout<< rank<<","<<m<<","<<endtime-starttime<<endl;
}// close if
if(rank ==2){
starttime = MPI_Wtime();
int h;
MPI_Recv(&h,1,MPI_INT,rank-1,rank,MPI_COMM_WORLD,&stats[rank]);// receive from previous rank
for(int i=1; i<h; i++){
MPI_Recv(&tab,2,MPI_DOUBLE,1,2,MPI_COMM_WORLD,&stats[rank]);
vect.push_back({tab[0],tab[1]});
}
myaxis = false;
sort(vect.begin(),vect.end());
// l= vect.size()/2;
//MPI_Send(&l,1,MPI_INT,3,3,MPI_COMM_WORLD);
// for(int i=l+1;i<n;i++)
// {
// tab[0] = vect[i][0];
// tab[1] = vect[i][1];
// MPI_Send(&tab,2,MPI_DOUBLE,3,3,MPI_COMM_WORLD); // the second proc is the tag
// }
//
// std::vector<std::vector<double>> vet{};
// for(int i=0;i<l;i++)
// vet.push_back(vect[i]);
root = kd_tree(vect,myaxis, &compt);
cout<< " Number of leaves " << compt<<endl;
endtime = MPI_Wtime();
cout<< rank<<","<<m<<","<<endtime-starttime<<endl;
}
} // for loop
MPI::Finalize();
cout<< " Done .."<<endl;
return 0;
}