-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathColumnBlock.cpp
219 lines (184 loc) · 5.34 KB
/
ColumnBlock.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
#include <map>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <mpi.h>
#include <string.h>
#include "ColumnBlock.h"
#include "constants.h"
using namespace std;
block::block(int column_size, int partition_mode)
{
this->column_size = column_size;
pivot_array = new int[column_size];
this->partition_mode = partition_mode;
rank_id = MPI::COMM_WORLD.Get_rank();
proc_num = MPI::COMM_WORLD.Get_size();
k_column = new float[column_size];
// Initializing of pivot_array
for (int i = 0; i<column_size; i++)
pivot_array[i] = i;
}
block::~block()
{
for ( map<int,float*>::iterator it = column.begin(); it != column.end(); ++it )
delete[] it->second; //delete column data
delete[] k_column;
}
bool block::add_column(int id, float* data)
{
column[id] = data;
return true;
}
// find the max of column k
// then return the id of the row that have the max elem.
int block::find_pivot(int k)
{
float* current_column = column[k];
float max_val = current_column[pivot_array[k]];
int max_val_id = k;
int i;
for ( i = k + 1; i < column_size; i++ )
{
if ( current_column[pivot_array[i]] > max_val )
{
max_val = current_column[pivot_array[i]];
max_val_id = i;
}
}
return max_val_id;
}
//swap k-th element of pivot array with max_val_id-th
bool block::update_pivot(int k, int max_val_id)
{
int tmp = pivot_array[k];
pivot_array[k] = pivot_array[max_val_id];
pivot_array[max_val_id] = tmp;
return true;
}
bool block::compute_values(int k)
{
float akk = k_column[pivot_array[k]];
float* Ak = k_column;
for ( map<int,float*>::iterator it = column.begin(); it != column.end(); ++it )
{
if ( it->first < k + 1 )
continue;
float* Aj = it->second;
Aj[pivot_array[k]] = Aj[pivot_array[k]]/akk;
for ( int i = 0; i<column_size; i++)
{
if( i != k )
Aj[pivot_array[i]] = Aj[pivot_array[i]] - Ak[pivot_array[i]]*Aj[pivot_array[k]];
}
}
}
bool block::sync(int max_val_id, int k)
{
int max_val_id_local = max_val_id;
if ( local_column(k) )
{ //must send max_val_id and colum k
MPI_Bcast((void*)(&max_val_id), 1, MPI_INT, rank_id, MPI_COMM_WORLD);
MPI_Bcast((void*)column[k], column_size, MPI_FLOAT, rank_id, MPI_COMM_WORLD);
memcpy(k_column, column[k], column_size*sizeof(float));
}
else
{ //receive
int root = get_root(k);
MPI_Bcast((void*)(&max_val_id_local), 1, MPI_INT, root, MPI_COMM_WORLD);
MPI_Bcast((void*)k_column, column_size, MPI_FLOAT, root, MPI_COMM_WORLD);
}
update_pivot(k, max_val_id_local);
return true;
}
bool block::sync2(int max_val_id, int k)
{
int max_val_id_local = max_val_id;
float* tmpbuffer = new float[column_size+1];
if ( local_column(k) )
{ //must send max_val_id and colum k
MPI_Request req;
memcpy(tmpbuffer + 1, column[k], column_size*sizeof(float));
memcpy(tmpbuffer, &max_val_id, sizeof(int));
for ( int j = 1; j < proc_num; j++)
{
for ( int i = k + 1; i < column_size; i++ )
{
int dest = get_root(i);
if ( dest == rank_id)
continue;
if( dest == j)
{
MPI_Isend((void*)(tmpbuffer), column_size + 1, MPI_FLOAT, dest,
rank_id, MPI_COMM_WORLD, &req);
break;
}
}
}
//always send to master because he has the b array
if ( rank_id != 0 )
{
MPI_Isend((void*)(tmpbuffer), column_size + 1, MPI_FLOAT, 0,
rank_id, MPI_COMM_WORLD, &req);
}
memcpy(k_column, column[k], column_size*sizeof(float));
}
else
{ //receive
//if I have a column with id > k
bool must_receive = false;
for ( map<int,float*>::iterator it = column.begin(); it != column.end(); ++it )
{
if ( it->first > k)
must_receive = true;
}
if(must_receive)
{
int root = get_root(k);
MPI_Request req;
MPI_Status stat;
MPI_Irecv((void*)tmpbuffer, column_size + 1, MPI_FLOAT, root,
root, MPI_COMM_WORLD, &req);
MPI_Wait(&req, &stat);
memcpy(&max_val_id_local, tmpbuffer, sizeof(int));
memcpy(k_column, tmpbuffer+1, column_size*sizeof(float));
}
}
update_pivot(k, max_val_id_local);
delete []tmpbuffer;
return true;
}
int block::get_root(int k)
{
if ( partition_mode == distribution::GROUPED )
{
return k/(column_size/proc_num);
}
else if( partition_mode == distribution::ROUNDROBIN )
{
return k%proc_num;
}
}
bool block::local_column(int k)
{
if ( column.count(k) != 0 )
return true;
else
return false;
}
void block::print_pivot()
{
for (int i = 0; i<column_size; i++)
cout<<"piv["<<i<<"] = "<<pivot_array[i]<<endl;
}
void block::print_solution()
{
if ( rank_id != 0 )
return;
cout<<"SOLUTION:"<<endl;
for ( int i = 0; i < column_size; i++ )
{
cout<<"x"<<pivot_array[i]<< " = "<<(column[column_size])[i]<<endl;
}
return;
}