-
Notifications
You must be signed in to change notification settings - Fork 3
/
Problem-bsfCode.cpp
274 lines (231 loc) · 9.26 KB
/
Problem-bsfCode.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
/*==============================================================================
Project: Bulk Synchronous Farm (BSF)
Theme: BSF Skeleton
Module: Problem-bsfCode.h (Problem-dependent Code)
Prefix: PC
Author: Leonid B. Sokolinsky
This source code is a part of BSF Skeleton (https://github.com/leonid-sokolinsky/BSF-skeleton)
==============================================================================*/
#include "Problem-Data.h" // Problem Types
#include "Problem-Forwards.h" // Problem Function Forwards
#include "Problem-bsfParameters.h" // BSF-skeleton parameters
#include "BSF-SkeletonVariables.h" // Skeleton Variables
using namespace std;
//----------------------- Predefined problem-dependent functions -----------------
void PC_bsf_Init(bool* success) {
};
void PC_bsf_SetListSize(int* listSize) {
*listSize = PP_N;
};
void PC_bsf_CopyParameter(PT_bsf_parameter_T parameterIn, PT_bsf_parameter_T* parameterOutP) {
for (int i = 0; i < PP_N; i++)
parameterOutP->x[i] = parameterIn.x[i];
};
void PC_bsf_MapF(PT_bsf_mapElem_T* mapElem, PT_bsf_reduceElem_T* reduceElem, int* success) {
for (int i = 0; i < PP_N; i++)
reduceElem->column[i] = mapElem->column[i] * BSF_sv_parameter.x[mapElem->columnNo];
};
void PC_bsf_MapF_1(PT_bsf_mapElem_T* mapElem, PT_bsf_reduceElem_T_1* reduceElem, int* success) {
// Optional filling. Do not delete!
};
void PC_bsf_MapF_2(PT_bsf_mapElem_T* mapElem, PT_bsf_reduceElem_T_2* reduceElem, int* success) {
// Optional filling. Do not delete!
};
void PC_bsf_MapF_3(PT_bsf_mapElem_T* mapElem, PT_bsf_reduceElem_T_3* reduceElem, int* success) {
// Optional filling. Do not delete!
};
void PC_bsf_ReduceF(PT_bsf_reduceElem_T* x, PT_bsf_reduceElem_T* y, PT_bsf_reduceElem_T* z) { // z = x (+) y
for (int i = 0; i < PP_N; i++)
z->column[i] = x->column[i] + y->column[i];
};
void PC_bsf_ReduceF_1(PT_bsf_reduceElem_T_1* x, PT_bsf_reduceElem_T_1* y, PT_bsf_reduceElem_T_1* z) {
// Optional filling. Do not delete!
};
void PC_bsf_ReduceF_2(PT_bsf_reduceElem_T_2* x, PT_bsf_reduceElem_T_2* y, PT_bsf_reduceElem_T_2* z) {
// Optional filling. Do not delete!
};
void PC_bsf_ReduceF_3(PT_bsf_reduceElem_T_3* x, PT_bsf_reduceElem_T_3* y, PT_bsf_reduceElem_T_3* z) {
// Optional filling. Do not delete!
};
void PC_bsf_ProcessResults(
PT_bsf_reduceElem_T* reduceResult,
int reduceCounter,
PT_bsf_parameter_T* parameter,
int* newJobCas,
bool* exit
) {
for (int j = 0; j < PP_N; j++) {
PD_prev_x[j] = parameter->x[j];
parameter->x[j] = reduceResult->column[j] + beta(j);
};
if (StopCond(parameter))
*exit = true;
else
*exit = false;
};
void PC_bsf_ProcessResults_1(
PT_bsf_reduceElem_T_1* reduceResult,
int reduceCounter,
PT_bsf_parameter_T* parameter,
int* nextJob,
bool* exit
) {
// Optional filling. Do not delete!
};
void PC_bsf_ProcessResults_2(
PT_bsf_reduceElem_T_2* reduceResult,
int reduceCounter,
PT_bsf_parameter_T* parameter,
int* nextJob,
bool* exit
) {
// Optional filling. Do not delete!
};
void PC_bsf_ProcessResults_3(
PT_bsf_reduceElem_T_3* reduceResult,
int reduceCounter,
PT_bsf_parameter_T* parameter,
int* nextJob,
bool* exit
) {
// Optional filling. Do not delete!
};
void PC_bsf_JobDispatcher(
PT_bsf_parameter_T* parameter, // Current Approximation
int* job,
bool* exit
) {
// Optional filling. Do not delete!
}
void PC_bsf_ParametersOutput(PT_bsf_parameter_T parameter) {
cout << "=================================================== Jacobi MR ====================================================" << endl;
cout << "Number of Workers: " << BSF_sv_numOfWorkers << endl;
#ifdef PP_BSF_OMP
#ifdef PP_BSF_NUM_THREADS
cout << "Number of Threads: " << PP_BSF_NUM_THREADS << endl;
#else
cout << "Number of Threads: " << omp_get_num_procs() << endl;
#endif // PP_BSF_NUM_THREADS
#else
cout << "OpenMP is turned off!" << endl;
#endif // PP_BSF_OMP
cout << "Dimension: N = " << PP_N << endl;
cout << "Eps_Square = " << PP_EPS << endl;
#ifdef PP_MATRIX_OUTPUT
cout << "------- Matrix A & Column b -------" << endl;
for (int i = 0; i < PP_N; i++) {
for (int j = 0; j < PP_N; j++)
cout << setw(7) << A(i,j);
cout << setw(7) << b(i) << endl;
};
cout << endl;
cout << "------- Matrix Alpha & Column Beta -------" << endl;
for (int i = 0; i < PP_N; i++) {
for (int j = 0; j < PP_N; j++)
cout << setw(9) << Alpha(i,j);
cout << setw(9) << beta(i) << endl;
};
cout << endl;
#endif // PP_MATRIX_OUTPUT
cout << "Initial x: "; for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PP_N); j++) cout << setw(7) << parameter.x[j]; cout << (PP_OUTPUT_LIMIT < PP_N ? "..." : "") << endl;
cout << "-------------------------------------------" << endl;
}
void PC_bsf_IterOutput(PT_bsf_reduceElem_T* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double elapsedTime, int jobCase) {
cout << "------------------ " << BSF_sv_iterCounter << " ------------------" << endl;
cout << "Approximation:\t\t"; for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PP_N); j++) cout << setw(12) << parameter.x[j];
cout << (PP_OUTPUT_LIMIT < PP_N ? "..." : "") << endl;/**/
}
void PC_bsf_IterOutput_1(PT_bsf_reduceElem_T_1* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double elapsedTime, int jobCase) {
cout << "------------------ " << BSF_sv_iterCounter << " ------------------" << endl;
// Optional filling. Do not delete!
}
void PC_bsf_IterOutput_2(PT_bsf_reduceElem_T_2* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double elapsedTime, int jobCase) {
cout << "------------------ " << BSF_sv_iterCounter << " ------------------" << endl;
// Optional filling. Do not delete!
}
void PC_bsf_IterOutput_3(PT_bsf_reduceElem_T_3* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double elapsedTime, int jobCase) {
cout << "------------------ " << BSF_sv_iterCounter << " ------------------" << endl;
// Optional filling. Do not delete!
}
void PC_bsf_ProblemOutput(PT_bsf_reduceElem_T* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double t) {
cout << "=============================================" << endl;
cout << "Time: " << t << endl;
cout << "Iterations: " << BSF_sv_iterCounter << endl;
cout << "Solution: "; for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PP_N); j++) cout << setw(12) << parameter.x[j];
cout << (PP_OUTPUT_LIMIT < PP_N ? "..." : "") << endl;
}
void PC_bsf_ProblemOutput_1(PT_bsf_reduceElem_T_1* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double t) {
// Optional filling. Do not delete!
}
void PC_bsf_ProblemOutput_2(PT_bsf_reduceElem_T_2* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double t) {
// Optional filling. Do not delete!
};
void PC_bsf_ProblemOutput_3(PT_bsf_reduceElem_T_3* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double t) {
// Optional filling. Do not delete!
}
void PC_bsf_SetInitParameter(PT_bsf_parameter_T* parameter) {
for (int i = 0; i < PP_N; i++) // Generating coordinates of initial appriximation
parameter->x[i] = beta(i);
}
void PC_bsf_SetMapListElem(PT_bsf_mapElem_T* elem, int j) {
elem->columnNo = j;
for (int i = 0; i < PP_N; i++)
elem->column[i] = Alpha(i, j);
}
//----------------------- Assigning Values to BSF-skeleton Variables (Do not modify!) -----------------------
void PC_bsfAssignAddressOffset(int value) { BSF_sv_addressOffset = value; }
void PC_bsfAssignIterCounter(int value) { BSF_sv_iterCounter = value; }
void PC_bsfAssignJobCase(int value) { BSF_sv_jobCase = value; }
void PC_bsfAssignMpiMaster(int value) { BSF_sv_mpiMaster = value; };
void PC_bsfAssignMpiRank(int value) { BSF_sv_mpiRank = value; }
void PC_bsfAssignNumberInSublist(int value) { BSF_sv_numberInSublist = value; }
void PC_bsfAssignNumOfWorkers(int value) { BSF_sv_numOfWorkers = value; }
void PC_bsfAssignParameter(PT_bsf_parameter_T parameter) { PC_bsf_CopyParameter(parameter, &BSF_sv_parameter); }
void PC_bsfAssignSublistLength(int value) { BSF_sv_sublistLength = value; }
//----------------------------- User functions -----------------------------
static bool StopCond(PT_bsf_parameter_T* parameter) {// Calculates the stop condition
PT_vector_T difference; // Difference between current and previous approximations
#ifdef PP_MAX_ITER_COUNT
if (BSF_sv_iterCounter > PP_MAX_ITER_COUNT) {
cout << "Acceptable maximum number of iterations is exceeded: PP_MAX_ITER_COUNT = " << PP_MAX_ITER_COUNT << endl;
return true;
};
#endif // PP_MAX_ITER_COUNT
for (int j = 0; j < PP_N; j++)
difference[j] = PD_prev_x[j] - parameter->x[j];
if (Norm(difference) < PP_EPS)
return true;
else
return false;
}
static double Norm(PT_vector_T x) {// Calculates the square of the vector norm
double sum = 0;
for (int j = 0; j < PP_N; j++)
sum += x[j] * x[j];
return sqrt(sum);
}
inline PT_float_T A(int i, int j) {
if (j > i) return 0;
if (j < i) return 1;
if (j == 0) return 1;
return 2 * i;
}
inline PT_float_T b(int i) {
if (i == 0) return 1;
return i + 2 * i;
}
inline PT_float_T Alpha(int i, int j) { // Reduced matrix Alpha
if (j == i) return 0;
return -A(i, j) / A(i, i);
}
inline PT_float_T beta(int i) { // Reduced colunm beta
return b(i) / A(i, i);
}