-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem-bsfCode.cpp
292 lines (250 loc) · 10.6 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*==============================================================================
Project: Bulk Synchronous Farm (BSF)
Theme: BSF Skeleton
Module: Problem-bsfCode.cpp (Problem-dependent Code)
Prefix: PI
Author: Leonid B. Sokolinsky
This source code is a part of 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) { // success=false if initialization is unsuccessful
PD_g = (double*)malloc(PP_N * sizeof(double));
if (PD_g == NULL) {
*success = false;
return;
}
}
void PC_bsf_SetListSize(int* listSize) {
*listSize = PP_N;
};
void PC_bsf_MapF(PT_bsf_mapElem_T* mapElem, PT_bsf_reduceElem_T* reduceElem, int* success // 1 - reduceElem was produced successfully (default); 0 - otherwise
){
int mapIndex = BSF_sv_numberInSublist;
if (mapIndex == 0)
for (int i = 0; i < PP_N; i++)
PD_g[i] = 0;
for (int j = 0; j < PP_N; j++)
PD_g[mapIndex + BSF_sv_addressOffset] += mapElem->row[j] * BSF_sv_parameter.approximation[j];
if (mapIndex != BSF_sv_sublistLength - 1)
*success = 0;
else
for (int j = 0; j < PP_N; j++)
reduceElem->g[j] = PD_g[j];
};
void PC_bsf_MapF_1(PT_bsf_mapElem_T* mapElem, PT_bsf_reduceElem_T_1* reduceElem,
int* success // 1 - reduceElem was produced successfully (default); 0 - otherwise
) {
/* not used */
};
void PC_bsf_MapF_2(PT_bsf_mapElem_T* mapElem, PT_bsf_reduceElem_T_2* reduceElem,
int* success // 1 - reduceElem was produced successfully (default); 0 - otherwise
) {
/* not used */
};
void PC_bsf_MapF_3(PT_bsf_mapElem_T* mapElem, PT_bsf_reduceElem_T_3* reduceElem,
int* success // 1 - reduceElem was produced successfully (default); 0 - otherwise
) {
// optional filling
};
void PC_bsf_ReduceF(PT_bsf_reduceElem_T* x, PT_bsf_reduceElem_T* y, PT_bsf_reduceElem_T* z) { // z = x + y
for (int j = 0; j < PP_N; j++)
z->g[j] = x->g[j] + y->g[j];
};
void PC_bsf_ReduceF_1(PT_bsf_reduceElem_T_1* x, PT_bsf_reduceElem_T_1* y, PT_bsf_reduceElem_T_1* z) {/* not used */ };
void PC_bsf_ReduceF_2(PT_bsf_reduceElem_T_2* x, PT_bsf_reduceElem_T_2* y, PT_bsf_reduceElem_T_2* z) {/* not used */ };
void PC_bsf_ReduceF_3(PT_bsf_reduceElem_T_3* x, PT_bsf_reduceElem_T_3* y, PT_bsf_reduceElem_T_3* z) {/* not used */ }
void PC_bsf_ProcessResults(
PT_bsf_reduceElem_T* reduceResult,
int reduceCounter, // Number of successfully produced Elrments of Reduce List
PT_bsf_parameter_T* parameter, // Current Approximation
int* nextJob,
bool* exit // "true" if Stopping Criterion is satisfied, and "false" otherwise
) {
for (int j = 0; j < PP_N; j++) {
PD_prevApproximation[j] = parameter->approximation[j];
parameter->approximation[j] = reduceResult->g[j] + beta(j);
};
/*debug*//*
int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank);
cout << rank << ":=========>PC_bsf_ProcessResults: parameter->approximation:" << endl;
for (int j = 0; j < PP_N; j++)
cout << setw(8) << parameter->approximation[j];
cout << endl;
/*end debug*/
if (ExitCondition(parameter))
*exit = true;
else
*exit = false;
};
void PC_bsf_ProcessResults_1(
PT_bsf_reduceElem_T_1* reduceResult,
int reduceCounter, // Number of successfully produced Elrments of Reduce List
PT_bsf_parameter_T* parameter, // Current Approximation
int* nextJob,
bool* exit // "true" if Stopping Criterion is satisfied, and "false" otherwise
) {
// not used
};
void PC_bsf_ProcessResults_2(
PT_bsf_reduceElem_T_2* reduceResult,
int reduceCounter, // Number of successfully produced Elrments of Reduce List
PT_bsf_parameter_T* parameter, // Current Approximation
int* nextJob,
bool* exit // "true" if Stopping Criterion is satisfied, and "false" otherwise
) {
// not used
};
void PC_bsf_ProcessResults_3(
PT_bsf_reduceElem_T_3* reduceResult,
int reduceCounter, // Number of successfully produced Elrments of Reduce List
PT_bsf_parameter_T* parameter, // Current Approximation
int* nextJob,
bool* exit // "true" if Stopping Criterion is satisfied, and "false" otherwise
) {
// optional filling
};
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 M ====================================================" << 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(7) << Alpha(i, j);
cout << setw(7) << beta(i) << endl;
};
cout << endl;
#endif // PP_MATRIX_OUTPUT
cout << "Initial approximation: "; for (int j = 0; j < PF_MIN(PP_OUTPUT_LIMIT, PP_N); j++) cout << setw(7) << parameter.approximation[j]; cout << (PP_OUTPUT_LIMIT < PP_N ? "..." : "") << endl;
cout << "-------------------------------------------" << endl;
};
void PC_bsf_CopyParameter(PT_bsf_parameter_T parameterIn, PT_bsf_parameter_T* parameterOutP) {
for (int i = 0; i < PP_N; i++)
parameterOutP->approximation[i] = parameterIn.approximation[i];
};
void PC_bsf_IterOutput(PT_bsf_reduceElem_T* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double elapsedTime, int nextJob)
{
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.approximation[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 nextJob)
{
cout << "------------------ " << BSF_sv_iterCounter << " ------------------" << endl;
/* not used */
};
void PC_bsf_IterOutput_2(PT_bsf_reduceElem_T_2* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double elapsedTime, int nextJob)
{
cout << "------------------ " << BSF_sv_iterCounter << " ------------------" << endl;
/* not used */
};
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
};
void PC_bsf_ProblemOutput(PT_bsf_reduceElem_T* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter, double t) {// Output Function
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.approximation[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) {// Output Function
// optional filling
};
void PC_bsf_ProblemOutput_2(PT_bsf_reduceElem_T_2* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter, double t) {// Output Function
// optional filling
};
void PC_bsf_SetInitParameter(PT_bsf_parameter_T* parameter) {
for (int i = 0; i < PP_N; i++) // Generating coordinates of initial appriximation
parameter->approximation[i] = beta(i);
};
void PC_bsf_ProblemOutput_3(PT_bsf_reduceElem_T_3* reduceResult, int reduceCounter, PT_bsf_parameter_T parameter,
double t) {// Output Function
// optional filling
};
void PC_bsf_SetMapListElem(PT_bsf_mapElem_T* elem, int i) {
for (int j = 0; j < PP_N; j++)
elem->row[j] = 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 double Norm(PT_vector_T x) {
double sum = 0;
for (int j = 0; j < PP_N; j++)
sum += x[j] * x[j];
return sqrt(sum);
};
static bool ExitCondition(PT_bsf_parameter_T* parameter) {
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_prevApproximation[j] - parameter->approximation[j];
if (Norm(difference) < PP_EPS)
return true;
else
return false;
};
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);
}