-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeUtils.cpp
More file actions
165 lines (133 loc) · 4.23 KB
/
TreeUtils.cpp
File metadata and controls
165 lines (133 loc) · 4.23 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
#include <ctime>
#include <fstream>
#include <vector>
#include <mpi.h>
#include "TreeUtils.h"
tnode* makeRandomTree() {
tnode* tree = new tnode;
tree->value = 5;
srand(1);
for (int i = 0; i < GENERATE_COUNT; i++) {
long random = rand()%10;
addNode(random, 0, tree);
}
return tree;
}
void prepareSubTreesForMPI(tnode* tree, int level, std::vector<tnode*> *buf) {
if (tree != NULL){
if(level > tree->level) {
prepareSubTreesForMPI(tree->left, level, buf);
prepareSubTreesForMPI(tree->right, level, buf);
}
if(level == tree->level) {
buf->push_back(tree);
}
} else {printf("lol");}
}
long getSumOfAllChilds(tnode* tree) {
if (tree != NULL){
long leftSum = 0;
long rightSum = 0;
if (tree->left != NULL) {
tree->left->sum = getSumOfAllChilds(tree->left);
leftSum = tree->left->sum + tree->left->value;
}
if (tree->right != NULL)
{
tree->right->sum = getSumOfAllChilds(tree->right);
rightSum = tree->right->sum + tree->right->value;
}
return leftSum + rightSum;
}
return 0;
}
long getSumOfAllChilds_MPI(tnode* tree, int level, int rank, int numprocs) {
MPI_Request request;
long Result = 0;
MPI_Status status;
int tag = 50;
std::vector<tnode*> buf;
double starttime, endtime = 0.0;
prepareSubTreesForMPI(tree, level, &buf);
if (rank == 0) {
starttime= MPI_Wtime();
}
if (rank != 0) {
tnode* subTree = buf.at(rank-1);
long drobSum = getSumOfAllChilds(subTree) + subTree->value;
MPI_Isend(&drobSum, 1, MPI_LONG, 0, tag, MPI_COMM_WORLD, &request);
}
if (rank == 0) {
for (int i = 1; i < numprocs; i++) {
long res = 0;
MPI_Recv(&res, 1, MPI_LONG, i, tag, MPI_COMM_WORLD, &status);
Result += res;
}
Result = Result + getLastNodes(level, tree) - tree->value;
endtime = MPI_Wtime();
printf("[MPI]: Time %lf\n", endtime - starttime);
printf("[MPI]: Sum %llu\n", Result);
}
return 0;
}
long getLastNodes(int level, tnode* tree) {
if (tree != NULL && level > tree->level) {
return getLastNodes(level, tree->left) + getLastNodes(level, tree->right) + tree->value;
}
return 0;
}
void* getSumOfAllChilds_Pthread(void *args){
pthreadArg *arg = (pthreadArg *)args;
if (arg->tree != NULL){
long leftSum = 0;
long rightSum = 0;
if (arg->threadCount <= 1){
arg->tree->sum = getSumOfAllChilds(arg->tree);
return 0;
}
int leftJoinStatus, rightJoinStatus;
int leftCreateStatus, rightCreateStatus;
pthread_t leftThread;
pthreadArg leftArg;
if (arg->tree->left != NULL){
leftArg.tree = arg->tree->left;
leftArg.threadCount = arg->threadCount/2;
leftCreateStatus = pthread_create(&leftThread, NULL, getSumOfAllChilds_Pthread, (void*) &leftArg);
if (leftCreateStatus != 0) {
printf("[ERROR] Can't create thread. Status: %d\n", leftCreateStatus);
exit(ERROR_CREATE_THREAD);
}
}
pthread_t rightThread;
pthreadArg rightArg;
if (arg->tree->right != NULL){
rightArg.tree = arg->tree->right;
rightArg.threadCount = arg->threadCount/2;
rightCreateStatus = pthread_create(&rightThread, NULL, getSumOfAllChilds_Pthread, (void*) &rightArg);
if (rightCreateStatus != 0) {
printf("[ERROR] Can't create thread. Status: %d\n", rightCreateStatus);
exit(ERROR_CREATE_THREAD);
}
}
leftCreateStatus = pthread_join(leftThread, (void**)&leftJoinStatus);
if (leftCreateStatus != SUCCESS) {
printf("[ERROR] Can't join thread. Status: %d\n", leftCreateStatus);
exit(ERROR_JOIN_THREAD);
}
if (arg->tree->left != NULL){
arg->tree->left->sum = leftArg.tree->sum;
leftSum = arg->tree->left->sum + arg->tree->left->value;
}
rightCreateStatus = pthread_join(rightThread, (void**)&rightJoinStatus);
if (rightCreateStatus != SUCCESS) {
printf("[ERROR] Can't join thread. Status: %d\n", rightCreateStatus);
exit(ERROR_JOIN_THREAD);
}
if (arg->tree->right != NULL){
arg->tree->right->sum = rightArg.tree->sum;
rightSum = arg->tree->right->sum + arg->tree->right->value;
}
arg->tree->sum = leftSum + rightSum;
}
return 0;
}