-
Notifications
You must be signed in to change notification settings - Fork 2
/
MLSOMA.cpp
312 lines (277 loc) · 7.42 KB
/
MLSOMA.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
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
#include "MLSOMA.h"
#include "SerializedSharedMemory.h"
#include "TimeAnalysis.h"
using namespace std;
MLSOMA::MLSOMA()
{
is_first_try = true;
Init_MetaInformation();
SOMAs = new SOMA_Sequence();
SHARED_MEMORY_size = GRID_N*(DIMENSION + 1);
BOUNDS_GLOBAL = (float*)malloc(DIMENSION * 2 * sizeof(float));
BOUNDS = (float*)malloc(DIMENSION * 2 * sizeof(float));
#ifdef MPI3
MPI_Win_create(SHARED_MEMORY, SHARED_MEMORY_size * sizeof(int), sizeof(float), MPI_INFO_NULL, MPI_COMM_WORLD, &win);
MPI_Win_fence(0, win);
MPI_Win_fence(0, win);
#endif // MPI3
//if (PROCESS_ID == 0)
SHARED_MEMORY = (float*)malloc(SHARED_MEMORY_size * sizeof(float));
for (int i = 0; i < SHARED_MEMORY_size; i++)
SHARED_MEMORY[i] = 0;
SerializedSharedMemory::getInstance().Init();
MPI_Barrier(MPI_COMM_WORLD);
}
MLSOMA::~MLSOMA()
{
#ifdef MPI3
//MPI_Win_free(&win);
#endif // MPI3
}
void MLSOMA::Run()
{
if (WORLD_SIZE == 1)
{
Run2();
}
else
{
if (PROCESS_ID == 0) Master_Process();
else Slave_Process();
}
ShowResults();
}
void MLSOMA::Run2()
{
while (true)
{
int gid_id = FindAGird();
if (gid_id == -1) break;
SetGrid(gid_id);
SOMAs->RanDomIndividuals();
SOMAs->DoSOMAWithSchwefel();
WriteResultToSharedMemory();
}
}
void MLSOMA::Allgather()
{
//MPI_Allgather()
}
void MLSOMA::Init_MetaInformation()
{
GRID_N = (int)pow(GRID_X, DIMENSION);
current_grid_index = (int*)malloc(DIMENSION*sizeof(int));
SelfCalcGridIndex(PROCESS_ID);
}
void MLSOMA::SetBounds(float min, float max)
{
SOMAs->SetBounds(min, max);
for (int i = 0; i<DIMENSION; i++)
{
BOUNDS[i*2] = min;
BOUNDS[i*2 + 1] = max;
}
}
void MLSOMA::SetBounds(float xmin, float xmax, float ymin, float ymax)
{
SOMAs->SetBounds(xmin, xmax, ymin, ymax);
BOUNDS[0] = xmin;
BOUNDS[1] = xmax;
BOUNDS[2] = ymin;
BOUNDS[3] = ymax;
}
void MLSOMA::SetBounds(int dimension, float min, float max)
{
BOUNDS[dimension*2] = min;
BOUNDS[dimension*2+1] = max;
SOMAs->SetBounds(dimension, min, max);
}
void MLSOMA::SetBoundsGlobal(float min, float max)
{
if (WORLD_SIZE > 1 /*&& PROCESS_ID == 0*/)
{
TimeAnalysis::getInstance().SetBound(min, max);
}
for (int i = 0; i<DIMENSION; i++)
{
BOUNDS_GLOBAL[i*2] = min;
BOUNDS_GLOBAL[i*2 + 1] = max;
}
}
void MLSOMA::SelfCalcGridIndex(int index)
{
int index2 = index;
for (int i = 0; i < DIMENSION; i++)
{
current_grid_index[i] = index2%GRID_X;
index2 = index2/ GRID_X;
}
/*printf("AA %d= (( ",index);
for (int i = 0; i < DIMENSION; i++) printf("%d,", current_grid_index[i]);
printf("))\n");*/
//current_grid_index[0] = index%GRID_X;
//current_grid_index[1] = index/GRID_X;
}
void MLSOMA::WriteResultToSharedMemory()
{
int index = (current_grid_index[1] * GRID_X + current_grid_index[0])*(DIMENSION+1);
SHARED_MEMORY[index] = SOMAs->Individuals[SOMAs->LEADER_INDEX*DIMENSION];
SHARED_MEMORY[index+1] = SOMAs->Individuals[SOMAs->LEADER_INDEX*DIMENSION+1];
SHARED_MEMORY[index + 2] = SOMAs->Individuals_cost[SOMAs->LEADER_INDEX];
}
void MLSOMA::ShowResults()
{
if (PROCESS_ID != 0) return; // only rank 0
printf("RESULTs: ");
for (int i = 0; i < SHARED_MEMORY_size; i += DIMENSION + 1)
{
printf("(%f,%f, %f) ", SHARED_MEMORY[i], SHARED_MEMORY[i + 1], SHARED_MEMORY[i + DIMENSION]);
}
printf("\n");
}
void MLSOMA::SetBound(int *current_grid_index)
{
#ifdef OLD
float* step = (float*)malloc(DIMENSION * sizeof(float));
for (int i = 0; i < DIMENSION; i++)
{
step[i] = (BOUNDS_GLOBAL[2 * i + 1] - BOUNDS_GLOBAL[2 * i]) / GRID_X;
}
float xmin = BOUNDS_GLOBAL[2 * 0] + step[0] * current_grid_index[0];
float xmax = BOUNDS_GLOBAL[2 * 0] + step[0] * (current_grid_index[0] + 1);
float ymin = BOUNDS_GLOBAL[2 * 1] + step[1] * current_grid_index[1];
float ymax = BOUNDS_GLOBAL[2 * 1] + step[1] * (current_grid_index[1] + 1);
SetBounds(xmin, xmax);
delete step;
#else
float* step = (float*)malloc(DIMENSION * sizeof(float));
for (int i = 0; i < DIMENSION; i++)
{
step[i] = (BOUNDS_GLOBAL[2 * i + 1] - BOUNDS_GLOBAL[2 * i]) / GRID_X;
float min = BOUNDS_GLOBAL[2 * i] + step[i] * current_grid_index[i];
float max = BOUNDS_GLOBAL[2 * i] + step[i] * (current_grid_index[i] + 1);
SetBounds(i,min, max);
}
delete step;
#endif // OLD
}
bool MLSOMA::IsGirdCompleted()
{
if (SerializedSharedMemory::getInstance().GetCurrentGrid() > GRID_N - 1)
return true;
return false;
}
void MLSOMA::SetGrid(int id)
{
if (GRID_N < 100)
{
printf("PROCESS %d check GriD %d \n", PROCESS_ID, id);
}
SelfCalcGridIndex(id);
SetBound(current_grid_index);
}
bool MLSOMA::IsGridDone(int id)
{
int s = DIMENSION + 1;
int index = id*s;
if (SHARED_MEMORY[id*s] == SHARED_MEMORY[id*s + 1] &&
SHARED_MEMORY[id*s + 2] == SHARED_MEMORY[id*s + 1]) return false;
return true;
}
int MLSOMA::FindAGird()
{
for (int i = 0; i < GRID_N; i++)
{
if (!IsGridDone(i)) return i;
}
return -1;
}
void MLSOMA::Master_Process()
{
int buf_size = (DIMENSION + 1 + 1);
float * buf = (float*)malloc(buf_size * sizeof(float));
MPI_Status status;
int num_receieve = 0;
TimeAnalysis::getInstance().Begin_ProcessCPU();
// set auto
for (int i = 0; i < WORLD_SIZE-1; i++)
{
SHARED_MEMORY[i*(DIMENSION + 1)] = -2;
}
while (true)
{
MPI_Recv(buf, buf_size, MPI_FLOAT, MPI_ANY_SOURCE, 11, MPI_COMM_WORLD, &status);
int index = ((int)buf[0])*(DIMENSION + 1);
for (int i = 0; i <= DIMENSION; i++)
{
SHARED_MEMORY[index + i] = buf[1 + i];
}
int next_grid = FindAGird();
buf[0] = next_grid;
if (next_grid >= 0)
{
SHARED_MEMORY[next_grid*(DIMENSION + 1)] = -2;
}
MPI_Send(buf, 1, MPI_FLOAT, status.MPI_SOURCE, 0, MPI_COMM_WORLD);
num_receieve++;
if (num_receieve >= GRID_N) break;
}
ConclueResults();
for (int d = 0; d < DIMENSION; d++)
{
TimeAnalysis::getInstance().POSITION[d] = GLOBAL_POSITION[d];
}
TimeAnalysis::getInstance().VALUE = GLOBAL_VALUE;
TimeAnalysis::getInstance().End_ProcessCPU();
}
void MLSOMA::Slave_Process()
{
int gid_id = -1;
while (true)
{
if (is_first_try)
{
is_first_try = false;
gid_id = MY_DEVICE_ID;
}
if (gid_id == -1) break;
SetGrid(gid_id);
SOMAs->RanDomIndividuals();
SOMAs->DoSOMAWithSchwefel();
WriteResultToSharedMemory();
int buf_size = (DIMENSION + 1 + 1);
float * buf = (float*)malloc(buf_size * sizeof(float));
int index = (current_grid_index[1] * GRID_X + current_grid_index[0])*(DIMENSION + 1);
buf[0] = gid_id;
for (int i = 0; i < DIMENSION; i++)
{
buf[1 + i] = SOMAs->Individuals[SOMAs->LEADER_INDEX*DIMENSION +i];
}
buf[DIMENSION+1] = SOMAs->Individuals_cost[SOMAs->LEADER_INDEX];
MPI_Send(buf, buf_size, MPI_FLOAT, 0, 11, MPI_COMM_WORLD);
MPI_Status status;
MPI_Recv(buf, 1, MPI_FLOAT, 0, 0, MPI_COMM_WORLD, &status);
if (buf[0] < 0) break;
else gid_id = (int)buf[0];
}
}
void MLSOMA::ConclueResults()
{
int index_max = 0 ;
float value_max = SHARED_MEMORY[DIMENSION];
for (int i = 0; i < GRID_N; i++)
{
int value = SHARED_MEMORY[i*(DIMENSION + 1) + (DIMENSION)];
if (value > value_max)
{
index_max = i;
value_max = value;
}
}
GLOBAL_POSITION = (float*)malloc(DIMENSION * sizeof(float));
for (int i = 0; i < DIMENSION; i++)
{
GLOBAL_POSITION[i] = SHARED_MEMORY[index_max*(DIMENSION + 1) + i];
}
GLOBAL_VALUE = value_max;
}