forked from alexdevonport/pfaces-pirk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpirk_ctmm.cpp
386 lines (322 loc) · 19.5 KB
/
pirk_ctmm.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <pfaces-sdk.h>
#include "pirk.h"
namespace pirk{
size_t ctmm_saveData(
const pfaces2DKernel& thisKernel,
const pfacesParallelProgram& thisParallelProgram,
std::vector<std::shared_ptr<void>>& postExecuteParamsList) {
(void)thisKernel;
(void)postExecuteParamsList;
//pfacesTerminal::showInfoMessage("This is where I'd write to a save file... IF I HAD ONE\nCTMM");
//pirk* knl = ((pirk*)(&thisKernel));
//float* A = (float*)(thisParallelProgram.m_dataPool[1].first);
//// pfacesTerminal::showMessage("Successor lower\n-------------------");
//// for(int i = 0; i < 2 * knl->states_dim; i++) {
//// pfacesTerminal::showMessage(std::to_string(A[i]));
//// if(i == knl->states_dim -1){
//// pfacesTerminal::showMessage("Successor upper\n-------------------");
//// }
//// }
saveBufferToFile(thisParallelProgram, thisParallelProgram.m_dataPool[1].first, thisParallelProgram.m_dataPool[1].second, "result_ctmm_radius");
saveBufferToFile(thisParallelProgram, thisParallelProgram.m_dataPool[9].first, thisParallelProgram.m_dataPool[9].second, "result_ctmm_center");
return 0;
}
void pirk::initializeCTMM(const std::shared_ptr<pfacesKernelLaunchState>& spLaunchState){
std::string mem_fingerprint_file =
spLaunchState->getKernelPackPath() + std::string("ctmm") +
std::string(PFACES_PATH_SPLITTER) + std::string("ctmm.mem");
/* ----------------------------------------------------------------------------------------------------------------------------- */
/* begin code for creating the "initialize" kernel function (function 10) */
pfacesKernelFunction function_ctmm_initialize(
"ctmm_initialize", /* name of the function to add */
{"initial_state", "final_state", "input", "k0","k1","k2","k3","tmp", "t"} /* list of the names of its args */
);
pfacesKernelFunctionArguments args_ctmm_initialize = pfacesKernelFunctionArguments::loadFromFile(
mem_fingerprint_file, /* name of the file to load the fingerprint from */
"ctmm_initialize", /* name of the function to add */
{"initial_state", "final_state", "input", "k0","k1","k2","k3","tmp", "t"} /* list of the names of its args */
);
function_ctmm_initialize.setArguments(args_ctmm_initialize);
addKernelFunction(function_ctmm_initialize);
/* end code for creating the "initialize_center" kernel function */
/* ----------------------------------------------------------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------------------------------------------------------------- */
/* begin code for creating the "integrate_1" kernel function (function 11) */
pfacesKernelFunction function_ctmm_integrate_1(
"ctmm_integrate_1", /* name of the function to add */
{"initial_state", "final_state", "input", "k0","k1","k2","k3","tmp", "t"} /* list of the names of its args */
);
pfacesKernelFunctionArguments args_ctmm_integrate_1 = pfacesKernelFunctionArguments::loadFromFile(
mem_fingerprint_file, /* name of the file to load the fingerprint from */
"ctmm_integrate_1", /* name of the function to add */
{"initial_state", "final_state", "input", "k0","k1","k2","k3","tmp", "t"} /* list of the names of its args */
);
function_ctmm_integrate_1.setArguments(args_ctmm_integrate_1);
addKernelFunction(function_ctmm_integrate_1);
/* end code for creating the "integrate_1_center" kernel function */
/* ----------------------------------------------------------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------------------------------------------------------------- */
/* begin code for creating the "integrate_2" kernel function (function 12) */
pfacesKernelFunction function_ctmm_integrate_2(
"ctmm_integrate_2", /* name of the function to add */
{"initial_state", "final_state", "input", "k0","k1","k2","k3","tmp", "t"} /* list of the names of its args */
);
pfacesKernelFunctionArguments args_ctmm_integrate_2 = pfacesKernelFunctionArguments::loadFromFile(
mem_fingerprint_file, /* name of the file to load the fingerprint from */
"ctmm_integrate_2", /* name of the function to add */
{"initial_state", "final_state", "input", "k0","k1","k2","k3","tmp", "t"} /* list of the names of its args */
);
function_ctmm_integrate_2.setArguments(args_ctmm_integrate_2);
addKernelFunction(function_ctmm_integrate_2);
/* end code for creating the "integrate_2_center" kernel function */
/* ----------------------------------------------------------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------------------------------------------------------------- */
/* begin code for creating the "integrate_3" kernel function (function 13) */
pfacesKernelFunction function_ctmm_integrate_3(
"ctmm_integrate_3", /* name of the function to add */
{"initial_state", "final_state", "input", "k0","k1","k2","k3","tmp", "t"} /* list of the names of its args */
);
pfacesKernelFunctionArguments args_ctmm_integrate_3 = pfacesKernelFunctionArguments::loadFromFile(
mem_fingerprint_file, /* name of the file to load the fingerprint from */
"ctmm_integrate_3", /* name of the function to add */
{"initial_state", "final_state", "input", "k0","k1","k2","k3","tmp", "t"} /* list of the names of its args */
);
function_ctmm_integrate_3.setArguments(args_ctmm_integrate_3);
addKernelFunction(function_ctmm_integrate_3);
/* end code for creating the "integrate_3_center" kernel function */
/* ----------------------------------------------------------------------------------------------------------------------------- */
/* ----------------------------------------------------------------------------------------------------------------------------- */
/* begin code for creating the "integrate_4" kernel function (function 14) */
pfacesKernelFunction function_ctmm_integrate_4(
"ctmm_integrate_4", /* name of the function to add */
{"initial_state", "final_state", "input", "k0","k1","k2","k3","tmp", "t"} /* list of the names of its args */
);
pfacesKernelFunctionArguments args_ctmm_integrate_4 = pfacesKernelFunctionArguments::loadFromFile(
mem_fingerprint_file, /* name of the file to load the fingerprint from */
"ctmm_integrate_4", /* name of the function to add */
{"initial_state", "final_state", "input", "k0","k1","k2","k3","tmp", "t"} /* list of the names of its args */
);
function_ctmm_integrate_4.setArguments(args_ctmm_integrate_4);
addKernelFunction(function_ctmm_integrate_4);
/* end code for creating the "integrate_4_center" kernel function */
/* ----------------------------------------------------------------------------------------------------------------------------- */
}
void pirk::configureParallelProgramCTMM(pfacesParallelProgram& parallelProgram){
// A parallel advisor used for task scheduling
pfacesParallelAdvisor parallelAdvisor(parallelProgram.getMachine(), parallelProgram.getTargetDevicesIndicies());
/* This range specifies how the task is going to be split up. */
pfacesTerminal::showInfoMessage(std::to_string(states_dim));
cl::NDRange ndUniversalRangeStateDim(
pfacesBigInt::getPrimitiveValue(2*states_dim),
pfacesBigInt::getPrimitiveValue(1) /* dummy value, no need for second dimension */
);
/* note: if you want to use more or fewer dimensions than 2, you can still use this function.
* you just give it as many arguments as you want dimensions. */
std::pair<cl::NDRange, cl::NDRange> stateDimProcessRangeAndOffset = parallelAdvisor.getProcessNDRangeAndOffset(ndUniversalRangeStateDim);
cl::NDRange ndProcessRangeStateDim = stateDimProcessRangeAndOffset.first;
cl::NDRange ndProcessOffsetStateDim = stateDimProcessRangeAndOffset.second;
cl::NDRange ndUniversalOffsetStateDim = cl::NullRange;
std::vector<std::shared_ptr<pfacesDeviceExecuteJob>> jobsPerDev_ctmm_initialize;
jobsPerDev_ctmm_initialize = parallelAdvisor.distributeJob(
*this,
0, /* function index */
ndProcessRangeStateDim, /* process range */
ndProcessOffsetStateDim, /* process offset */
parallelProgram.m_isFixedJobDistribution, /* whether or not to use a fixed job distribution, or to tune automatically */
parallelProgram.m_fixedJobDistribution, /* the fixed distribution, if one is being used */
true, false, false /* some additional flags */
);
std::vector<std::shared_ptr<pfacesDeviceExecuteJob>> jobsPerDev_ctmm_integrate_1;
jobsPerDev_ctmm_integrate_1 = parallelAdvisor.distributeJob(
*this,
1, /* function index */
ndProcessRangeStateDim, /* process range */
ndProcessOffsetStateDim, /* process offset */
parallelProgram.m_isFixedJobDistribution, /* whether or not to use a fixed job distribution, or to tune automatically */
parallelProgram.m_fixedJobDistribution, /* the fixed distribution, if one is being used */
true, false, false /* some additional flags */
);
std::vector<std::shared_ptr<pfacesDeviceExecuteJob>> jobsPerDev_ctmm_integrate_2;
jobsPerDev_ctmm_integrate_2 = parallelAdvisor.distributeJob(
*this,
2, /* function index */
ndProcessRangeStateDim, /* process range */
ndProcessOffsetStateDim, /* process offset */
parallelProgram.m_isFixedJobDistribution, /* whether or not to use a fixed job distribution, or to tune automatically */
parallelProgram.m_fixedJobDistribution, /* the fixed distribution, if one is being used */
true, false, false /* some additional flags */
);
std::vector<std::shared_ptr<pfacesDeviceExecuteJob>> jobsPerDev_ctmm_integrate_3;
jobsPerDev_ctmm_integrate_3 = parallelAdvisor.distributeJob(
*this,
3, /* function index */
ndProcessRangeStateDim, /* process range */
ndProcessOffsetStateDim, /* process offset */
parallelProgram.m_isFixedJobDistribution, /* whether or not to use a fixed job distribution, or to tune automatically */
parallelProgram.m_fixedJobDistribution, /* the fixed distribution, if one is being used */
true, false, false /* some additional flags */
);
std::vector<std::shared_ptr<pfacesDeviceExecuteJob>> jobsPerDev_ctmm_integrate_4;
jobsPerDev_ctmm_integrate_4 = parallelAdvisor.distributeJob(
*this,
4, /* function index */
ndProcessRangeStateDim, /* process range */
ndProcessOffsetStateDim, /* process offset */
parallelProgram.m_isFixedJobDistribution, /* whether or not to use a fixed job distribution, or to tune automatically */
parallelProgram.m_fixedJobDistribution, /* the fixed distribution, if one is being used */
true, false, false /* some additional flags */
);
parallelAdvisor.printTaskSchedulingReport(
parallelProgram.getMachine(),
{
"ctmm_initialize",
"ctmm_integrate_1",
"ctmm_integrate_2",
"ctmm_integrate_3",
"ctmm_integrate_4",
},
{
jobsPerDev_ctmm_initialize,
jobsPerDev_ctmm_integrate_1,
jobsPerDev_ctmm_integrate_2,
jobsPerDev_ctmm_integrate_3,
jobsPerDev_ctmm_integrate_4,
},
ndUniversalRangeStateDim[0]
);
// ---------------------------------------------------------
// MEMORY ALLOCATION
// ---------------------------------------------------------
/**/
std::vector<std::pair<char*, size_t>> dataPool;
pFacesMemoryAllocationReport memReport;
memReport = allocateMemory(dataPool, parallelProgram.getMachine(), parallelProgram.getTargetDevicesIndicies(),
pfacesUtils::oclGetRangeVolume(ndProcessRangeStateDim), false);
memReport.PrintReport();
// ---------------------------------------------------------
// Creating the parallel program
// ---------------------------------------------------------
std::vector<std::shared_ptr<pfacesInstruction>> instrList;
const cl::Device& dataAccessDevice = parallelProgram.getTargetDevices()[0];
// preparing some commonly used instructions
std::shared_ptr<pfacesDeviceReadJob> jobReadAllData;
std::shared_ptr<pfacesInstruction> instr_readAllData = std::make_shared<pfacesInstruction>();
jobReadAllData = std::make_shared<pfacesDeviceReadJob>(dataAccessDevice);
jobReadAllData->setKernelFunctionIdx( /* this function tells the instruction where to read the data from */
1, /* index of the kernel function we're to read data from (0 in this case, since matrixAdd is index 0 */
8 /* how many arguments it has (matrixAdd takes 3; the two matrices to add, and the result */
);
instr_readAllData->setAsReadAllDeviceData(jobReadAllData);
std::shared_ptr<pfacesDeviceWriteJob> jobWriteAllData;
std::shared_ptr<pfacesInstruction> instr_writeAllData = std::make_shared<pfacesInstruction>();
jobWriteAllData = std::make_shared<pfacesDeviceWriteJob>(dataAccessDevice);
jobWriteAllData->setKernelFunctionIdx(
1, /* index of the kernel function we're to write data to */
8 /* how many arguments it has */
);
instr_writeAllData->setAsWriteAllDeviceData(jobWriteAllData);
std::shared_ptr<pfacesInstruction> instrSyncPoint = std::make_shared<pfacesInstruction>();
instrSyncPoint->setAsBlockingSyncPoint();
std::shared_ptr<pfacesInstruction> instrLogOn = std::make_shared<pfacesInstruction>();
instrLogOn->setAsLogOn();
std::shared_ptr<pfacesInstruction> instrLogOff = std::make_shared<pfacesInstruction>();
instrLogOff->setAsLogOff();
// INSTRUCTION: a start message
std::shared_ptr<pfacesInstruction> instrMsg_start = std::make_shared<pfacesInstruction>();
instrMsg_start->setAsMessage("The program has started (CTMM Method).");
instrList.push_back(instrMsg_start);
// INSTRUCTION: write memory bags to devices
// if not using th direct access to host memory, we write the data to the device memory
// and followed by a barrier to sync among all device threads
if (!parallelProgram.m_useHostMemory) {
instrList.push_back(instr_writeAllData);
if (parallelProgram.countTargetDevices() > 1)
instrList.push_back(instrSyncPoint);
}
std::shared_ptr<pfacesInstruction> instrMsg0 = std::make_shared<pfacesInstruction>();
instrMsg0->setAsMessage("Initializing integration...");
instrList.push_back(instrMsg0);
// INSTRUCTIONS: initializing for center integration
for (size_t i = 0; i < jobsPerDev_ctmm_initialize.size(); i++) {
std::shared_ptr<pfacesInstruction> tmpExecuteInstr = std::make_shared<pfacesInstruction>();
tmpExecuteInstr->setAsDeviceExecute(jobsPerDev_ctmm_initialize[i]);
instrList.push_back(tmpExecuteInstr);
}
// INSTRUCTION: a sync point after initialization
instrList.push_back(instrSyncPoint);
std::shared_ptr<pfacesInstruction> instrMsg1 = std::make_shared<pfacesInstruction>();
instrMsg1->setAsMessage("Performing integration...");
instrList.push_back(instrMsg1);
size_t rk4_nint = 5;
size_t total_steps = nsteps * rk4_nint;
int multiple_devices = parallelProgram.countTargetDevices() > 1;
for (size_t w = 0; w < total_steps; w++) {
// INSTRUCTIONS: Perform center integration
for (size_t i = 0; i < jobsPerDev_ctmm_integrate_1.size(); i++) {
std::shared_ptr<pfacesInstruction> tmpExecuteInstr = std::make_shared<pfacesInstruction>();
tmpExecuteInstr->setAsDeviceExecute(jobsPerDev_ctmm_integrate_1[i]);
instrList.push_back(tmpExecuteInstr);
}
if (multiple_devices){
instrList.push_back(instrSyncPoint);
}
// INSTRUCTIONS: Perform center integration
for (size_t i = 0; i < jobsPerDev_ctmm_integrate_2.size(); i++) {
std::shared_ptr<pfacesInstruction> tmpExecuteInstr = std::make_shared<pfacesInstruction>();
tmpExecuteInstr->setAsDeviceExecute(jobsPerDev_ctmm_integrate_2[i]);
instrList.push_back(tmpExecuteInstr);
}
if (multiple_devices){
instrList.push_back(instrSyncPoint);
}
// INSTRUCTIONS: Perform center integration
for (size_t i = 0; i < jobsPerDev_ctmm_integrate_3.size(); i++) {
std::shared_ptr<pfacesInstruction> tmpExecuteInstr = std::make_shared<pfacesInstruction>();
tmpExecuteInstr->setAsDeviceExecute(jobsPerDev_ctmm_integrate_3[i]);
instrList.push_back(tmpExecuteInstr);
}
if (multiple_devices){
instrList.push_back(instrSyncPoint);
}
// INSTRUCTIONS: Perform center integration
for (size_t i = 0; i < jobsPerDev_ctmm_integrate_4.size(); i++) {
std::shared_ptr<pfacesInstruction> tmpExecuteInstr = std::make_shared<pfacesInstruction>();
tmpExecuteInstr->setAsDeviceExecute(jobsPerDev_ctmm_integrate_4[i]);
instrList.push_back(tmpExecuteInstr);
}
if (multiple_devices){
instrList.push_back(instrSyncPoint);
}
}
// INSTRUCTION: a sync point after center integration
instrList.push_back(instrSyncPoint);
std::shared_ptr<pfacesInstruction> instrMsg2 = std::make_shared<pfacesInstruction>();
instrMsg2->setAsMessage("Initializing radius integration...");
instrList.push_back(instrMsg2);
// INSTRUCTION: read memory bags from devices
// if not using direct access to host memory, we write the data to the device memory
// and followed by a barrier to sync among all device threads
if (!parallelProgram.m_useHostMemory) {
instrList.push_back(instr_readAllData);
if (parallelProgram.countTargetDevices() > 1)
instrList.push_back(instrSyncPoint);
}
// INSTRUCTION: a sync point after the matrix elements have all been added together
instrList.push_back(instrSyncPoint);
// INSTRUCTION: a message
// Notify the user that abstraction is complete
std::shared_ptr<pfacesInstruction> instr_MsgAddComplete = std::make_shared<pfacesInstruction>();
instr_MsgAddComplete->setAsMessage("program complete.");
instrList.push_back(instr_MsgAddComplete);
// INSTRUCTION: last instructin is a sync point !
instrList.push_back(instrSyncPoint);
// ---------------------------------------------------------
// Finalize !
// ---------------------------------------------------------
// setting the params back to the parallel program
parallelProgram.m_Universal_globalNDRange = ndUniversalRangeStateDim;
parallelProgram.m_Universal_offsetNDRange = ndUniversalOffsetStateDim;
parallelProgram.m_dataPool = dataPool;
parallelProgram.m_spInstructionList = instrList;
}
} /* end of pirk namespace */