-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUfSaCL.h
519 lines (459 loc) · 22.3 KB
/
UfSaCL.h
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
#pragma once
#include "libGPGPU/gpgpu.hpp"
#include<vector>
#include<string>
#include<cstdint>
#include<iostream>
#include<random>
#include<limits>
namespace UFSACL
{
// abstract solver that takes user algorithm into OpenCL kernel and runs on thousands of (GPU/CPU) threads
// supports maximum 2 billion elements for (num paramters X num objects) [example: 1000 parameters for 2 million objects]
// NumObjects = number of clones of state-machine (that are computed in parallel)
// NumParameters = number of parameters to tune to minimize energy
// ParameterType = float or double
template<int NumParameters, int NumObjects, typename ParameterType = float>
struct UltraFastSimulatedAnnealing
{
private:
std::string kernel;
GPGPU::Computer computer;
GPGPU::HostParameter randomDataIn;
GPGPU::HostParameter energyOut;
GPGPU::HostParameter parameterIn;
GPGPU::HostParameter parameterOut;
GPGPU::HostParameter randomDataOut;
GPGPU::HostParameter temperatureIn;
std::vector<GPGPU::HostParameter> userInputFullAccess;
int numWorkGroupsToRun;
int workGroupThreads;
int numParametersItersPerWorkgroupWithUnused;
std::string constants;
ParameterType currentEnergy;
std::vector<ParameterType> currentParameters;
std::vector<ParameterType> bestParameters;
std::string userInputs;
std::string userInputsWithoutTypes;
std::string userFunction;
std::string funcMin;
public:
UltraFastSimulatedAnnealing(std::string funcToMinimize, int gpuThreadsPerObject = 256, int numGPUsToUse = 16) :computer(GPGPU::Computer::DEVICE_ALL, -1, 1, true, numGPUsToUse)
{
workGroupThreads = gpuThreadsPerObject;
numWorkGroupsToRun = NumObjects;
if (NumParameters % workGroupThreads != 0)
numParametersItersPerWorkgroupWithUnused = (NumParameters / workGroupThreads) + 1;
else
numParametersItersPerWorkgroupWithUnused = NumParameters / workGroupThreads;
currentParameters.resize(NumParameters);
bestParameters.resize(NumParameters);
funcMin = funcToMinimize;
}
void build()
{
constants = std::string(R"(
#define NumItems )") + std::to_string(NumParameters * NumObjects) + std::string(R"(
)");
if constexpr (std::is_floating_point_v<ParameterType> && sizeof(ParameterType) == 4)
constants += std::string(R"(
#define GPGPU_REAL_VAL float
#define GPGPU_ZERO_REAL_VAL (0.0f)
#define UIMAXFLOATINV (2.32830644e-10f)
)");
else if (std::is_floating_point_v<ParameterType> && sizeof(ParameterType) == 8)
constants += std::string(R"(
#define GPGPU_REAL_VAL double
#define GPGPU_ZERO_REAL_VAL (0.0)
#define UIMAXFLOATINV (2.32830644e-10)
)");
constants += std::string(R"(
#define WorkGroupThreads )") + std::to_string(workGroupThreads) + std::string(R"(
)");
constants += std::string(R"(
#define NumParameters )") + std::to_string(NumParameters) + std::string(R"(
)");
constants += std::string(R"(
#define NumParamsPerThread )") + std::to_string(numParametersItersPerWorkgroupWithUnused) + std::string(R"(
)");
constants += userFunction;
kernel = constants + std::string(R"(
const unsigned int rnd(unsigned int seed)
{
seed = (seed ^ 61) ^ (seed >> 16);
seed *= 9;
seed = seed ^ (seed >> 4);
seed *= 0x27d4eb2d;
seed = seed ^ (seed >> 15);
return seed;
}
const GPGPU_REAL_VAL random(unsigned int seed)
{
return seed * UIMAXFLOATINV;
}
#define parallelFor(ITERS,BODY) \
{\
const int numLoopIter = (ITERS / WorkGroupThreads) + 1; \
for(int iGPGPU=0;iGPGPU<numLoopIter;iGPGPU++) \
{ \
const int loopId = threadId + WorkGroupThreads * iGPGPU; \
if(loopId < ITERS) \
{ \
BODY \
} \
} \
}
#define parallelForWithBarrier(ITERS,BODY) \
{\
const int numLoopIter = (ITERS / WorkGroupThreads) + 1; \
for(int iGPGPU=0;iGPGPU<numLoopIter;iGPGPU++) \
{ \
const int loopId = threadId + WorkGroupThreads * iGPGPU; \
if(loopId < ITERS) \
{ \
BODY \
} \
barrier(CLK_LOCAL_MEM_FENCE); \
} \
}
kernel void kernelFunction(global unsigned int * seedIn, global unsigned int * seedOut, global GPGPU_REAL_VAL * tempIn, global GPGPU_REAL_VAL * energyOut, global GPGPU_REAL_VAL * parameterIn, global GPGPU_REAL_VAL * parameterOut )") + userInputs + std::string(R"()
{
const int id = get_global_id(0);
const int groupId = id / WorkGroupThreads;
const int localId = id % WorkGroupThreads;
local GPGPU_REAL_VAL parameters[NumParameters];
local GPGPU_REAL_VAL energies[WorkGroupThreads];
const GPGPU_REAL_VAL temperature = tempIn[0];
const int numLoopIter = (NumParameters / WorkGroupThreads) + 1;
unsigned int tmpRnd = seedIn[id];
const GPGPU_REAL_VAL gpgpuHalf = 0.5;
for(int i=0;i<numLoopIter;i++)
{
const int loopId = localId + WorkGroupThreads * i;
if(loopId < NumParameters)
{
tmpRnd = rnd(tmpRnd);
GPGPU_REAL_VAL randomization = random(tmpRnd);
tmpRnd = rnd(tmpRnd);
GPGPU_REAL_VAL chance = random(tmpRnd)+(GPGPU_REAL_VAL)0.000000001;
tmpRnd = rnd(tmpRnd);
GPGPU_REAL_VAL probability = random(tmpRnd)+(GPGPU_REAL_VAL)0.000000001;
GPGPU_REAL_VAL change = (randomization - gpgpuHalf)*temperature;
if(chance > 0.97f)
change *= 10.0f;
if(chance > 0.99f)
change *= 5.0f;
if(chance > 0.997f)
change *= 5.0f;
if(chance > 0.9992f)
change *= 5.0f;
parameters[loopId] = fmod(parameterIn[loopId] + change + 10000.0,1.0);//fmod((double)parameterIn[loopId] + change + 10000.0,1.0);
}
}
seedOut[id]=tmpRnd;
barrier(CLK_LOCAL_MEM_FENCE);
// objective function by user
GPGPU_REAL_VAL energy = GPGPU_ZERO_REAL_VAL;
const int threadId = localId;
const int objectId = groupId;
)") + funcMin + std::string(R"(
energies[localId] = energy;
// objective function end
barrier(CLK_LOCAL_MEM_FENCE);
for(unsigned int i=WorkGroupThreads/2;i>=1;i>>=1)
{
unsigned int reduceId = i + localId;
if(localId<i)
energies[localId] += energies[reduceId];
barrier(CLK_LOCAL_MEM_FENCE);
}
if(localId == 0)
energyOut[id]=energies[0];
for(int i=0;i<numLoopIter;i++)
{
const int loopId = localId + WorkGroupThreads * i;
const int arrayId = loopId + groupId*WorkGroupThreads*NumParamsPerThread;
if(loopId < NumParameters)
{
parameterOut[arrayId] = parameters[loopId];
}
}
}
)");
computer.compile(kernel, "kernelFunction");
randomDataIn = computer.createArrayInputLoadBalanced<unsigned int>("rndIn", numWorkGroupsToRun * workGroupThreads);
randomDataOut = computer.createArrayOutput<unsigned int>("rndOut", numWorkGroupsToRun * workGroupThreads);
energyOut = computer.createArrayOutput<ParameterType>("energyOut", numWorkGroupsToRun * workGroupThreads);
parameterIn = computer.createArrayInput<ParameterType>("parameterIn", NumParameters);
temperatureIn = computer.createArrayInput<ParameterType>("tempIn", 1);
parameterOut = computer.createArrayOutput<ParameterType>("parameterOut",
numWorkGroupsToRun * numParametersItersPerWorkgroupWithUnused * workGroupThreads, numParametersItersPerWorkgroupWithUnused);
for (int i = 0; i < numWorkGroupsToRun * workGroupThreads; i++)
randomDataIn.access<unsigned int>(i) = i;
}
// declare a function before simulated-annealing-kernel, to improve code reusability
// can be called multiple times or once to add all user-functions
void addFunctionDefinition(std::string userFunctionPrm)
{
userFunction += R"(
)";
userFunction += userFunctionPrm;
userFunction += R"(
)";
}
// use additional buffers from host-environment in simulated-annealing kernel
// use same name with this in kernel when accessing data
// only simulated-annealing (to be minimized) energy function parameters are cached inside local (in-chip) fast memory
// any data added with addUserInput method is directly accessed from video-memory that is likely cached by hardware
// if data with same name exists, it updates the data
template<typename T>
void addUserInput(std::string customInputName, std::vector<T> customInput)
{
const int sz = userInputFullAccess.size();
for (int i = 0; i < sz; i++)
{
if (customInputName == userInputFullAccess[i].getName())
{
userInputFullAccess[i].copyDataFromPtr(customInput.data());
return;
}
}
// fully-copied array (for all GPGPU devices to have all-element random-access in kernel)
userInputFullAccess.emplace_back(computer.createArrayInput<T>(customInputName, customInput.size(), 1));
userInputFullAccess[sz].copyDataFromPtr(customInput.data());
userInputsWithoutTypes += std::string(", ") + customInputName;
if (typeid(T) == typeid(char))
{
userInputs += std::string(", global char * ") + customInputName;
}
else if (typeid(T) == typeid(unsigned char))
{
userInputs += std::string(", global unsigned char * ") + customInputName;
}
else if (typeid(T) == typeid(bool))
{
userInputs += std::string(", global unsigned char * ") + customInputName; // using char instead of bool since opencl not good at that
}
else if (typeid(T) == typeid(short))
{
userInputs += std::string(", global short * ") + customInputName;
}
else if (typeid(T) == typeid(unsigned short))
{
userInputs += std::string(", global unsigned short * ") + customInputName;
}
else if (typeid(T) == typeid(int))
{
userInputs += std::string(", global int * ") + customInputName;
}
else if (typeid(T) == typeid(unsigned int))
{
userInputs += std::string(", global unsigned int * ") + customInputName;
}
else if (typeid(T) == typeid(long long))
{
userInputs += std::string(", global long * ") + customInputName;
}
else if (typeid(T) == typeid(unsigned long long))
{
userInputs += std::string(", global unsigned long * ") + customInputName;
}
else if (typeid(T) == typeid(float))
{
userInputs += std::string(", global float * ") + customInputName;
}
else if (typeid(T) == typeid(double))
{
userInputs += std::string(", global double * ") + customInputName;
}
else if (typeid(T) == typeid(std::int8_t))
{
userInputs += std::string(", global char * ") + customInputName;
}
else if (typeid(T) == typeid(std::int16_t))
{
userInputs += std::string(", global short * ") + customInputName;
}
else if (typeid(T) == typeid(std::int32_t))
{
userInputs += std::string(", global int * ") + customInputName;
}
else if (typeid(T) == typeid(std::int64_t))
{
userInputs += std::string(", global long * ") + customInputName;
}
else if (typeid(T) == typeid(std::uint64_t))
{
userInputs += std::string(", global unsigned long * ") + customInputName;
}
return;
}
std::vector<ParameterType> run(
const ParameterType temperatureStart = 1.0f, const ParameterType temperatureStop = 0.01f, const ParameterType temperatureDivider = 2.0f,
const int numReheats = 5,
const bool debug = false, const bool deviceDebug = false, const bool energyDebug = false,
std::function<void(ParameterType*)> callbackLowerEnergyFound = [](ParameterType*) {},
std::vector<ParameterType> userHintForInitialParametersNormalized = std::vector<ParameterType>()
)
{
std::random_device rd;
std::mt19937 rng{ rd() };
std::uniform_real_distribution<float> uid(0.0f, 1.0f);
int reheat = numReheats;
auto kernelParams = randomDataIn.next(randomDataOut).next(temperatureIn).next(energyOut).next(parameterIn).next(parameterOut);
const int sz = userInputFullAccess.size();
for (int i = 0; i < sz; i++)
{
auto kernelParamsNew = kernelParams.next(userInputFullAccess[i]);
kernelParams = kernelParamsNew;
}
// initial guess for parameters (middle-points for all dimensions or user hint)
if (userHintForInitialParametersNormalized.size() == NumParameters)
{
for (int i = 0; i < NumParameters; i++)
{
parameterIn.access<ParameterType>(i) = userHintForInitialParametersNormalized[i];
}
}
else
{
for (int i = 0; i < NumParameters; i++)
{
parameterIn.access<ParameterType>(i) = 0.5f;
}
}
ParameterType temp = temperatureStart;
ParameterType foundEnergy = std::numeric_limits<ParameterType>::max();
// compute user-hinted parameters first
if (userHintForInitialParametersNormalized.size() == NumParameters)
{
// to compute with hint parameters exactly, set temperature to zero
temperatureIn.access<ParameterType>(0) = 0;
// run all GPUs to iterate random seeds
computer.compute(kernelParams, "kernelFunction", 0, numWorkGroupsToRun * workGroupThreads, workGroupThreads);
randomDataIn.copyDataFromPtr(randomDataOut.accessPtr<unsigned int>(0));
// get energy of hint
foundEnergy = energyOut.access<ParameterType>(0);
}
// initialize temperature
temperatureIn.access<ParameterType>(0) = temperatureStart;
int foundId = -1;
int iter = 0;
int foundIdBest = -1;
std::vector<double> perf;
size_t measuredNanoSecTot = 0;
ParameterType bestEnergy = foundEnergy;
{
GPGPU::Bench benchTot(&measuredNanoSecTot);
while (temp > temperatureStop)
{
if (debug)
std::cout << "iteration-" << iter++ << std::endl;
bool foundBetterEnergy = false;
bool foundBestEnergy = false;
size_t measuredNanoSec = 0;
bool doNotHeat = false;
{
GPGPU::Bench bench(&measuredNanoSec);
perf = computer.compute(kernelParams, "kernelFunction", 0, numWorkGroupsToRun * workGroupThreads, workGroupThreads);
randomDataIn.copyDataFromPtr(randomDataOut.accessPtr<unsigned int>(0));
ParameterType tmpEn = std::numeric_limits<double>::max();
int tmpI = -1;
for (int i = 0; i < NumObjects; i++)
{
const int index = i * workGroupThreads;
const ParameterType energy = energyOut.access<ParameterType>(index);
if (tmpEn > energy)
{
tmpEn = energy;
tmpI = i;
}
}
if (foundEnergy > tmpEn && tmpI >= 0)
{
foundEnergy = tmpEn;
foundId = tmpI;
foundBetterEnergy = true;
if (bestEnergy > tmpEn)
{
bestEnergy = tmpEn;
foundIdBest = tmpI;
foundBestEnergy = true;
}
}
else if (false && tmpI >= 0)
{
doNotHeat = true;
double rnd0 = uid(rng);
double dE = std::abs(foundEnergy - tmpEn) / std::abs(std::numeric_limits<double>::min() + foundEnergy);
if (rnd0 < std::exp(-dE / (temp * 0.1)))
{
foundEnergy = tmpEn;
foundId = tmpI;
foundBetterEnergy = true;
}
}
}
if (debug)
std::cout << "computation-time=" << measuredNanoSec * 0.000000001 << " seconds" << std::endl;
if (foundBetterEnergy)
{
if (!doNotHeat)
temp *= std::pow(temperatureDivider, 2.0); // as long as better states are found, temperature can be kept high
for (int i = 0; i < NumParameters; i++)
{
currentParameters[i] = parameterOut.access<ParameterType>(i + foundId * numParametersItersPerWorkgroupWithUnused * workGroupThreads);
}
if (foundBestEnergy)
{
for (int i = 0; i < NumParameters; i++)
{
bestParameters[i] = parameterOut.access<ParameterType>(i + foundIdBest * numParametersItersPerWorkgroupWithUnused * workGroupThreads);
}
}
// new low-energy point becomes new guess for next iteration
for (int i = 0; i < NumParameters; i++)
{
parameterIn.access<ParameterType>(i) = currentParameters[i];
}
if (energyDebug && foundBestEnergy)
std::cout << "lower energy found: " << bestEnergy << std::endl;
if (foundBestEnergy)
callbackLowerEnergyFound(bestParameters.data());
}
temp /= temperatureDivider;
temperatureIn.access<ParameterType>(0) = temp;
if (!(temp > temperatureStop))
{
reheat--;
if (reheat == 0)
{
break;
}
else
{
if (debug || energyDebug)
std::cout << "reheating. num reheats left=" << reheat << std::endl;
temp = temperatureStart;
iter = 0;
}
}
}
}
if (debug || energyDebug)
std::cout << "total computation-time=" << measuredNanoSecTot * 0.000000001 << " seconds (this includes debugging console-output that is slow)" << std::endl;
if (deviceDebug || energyDebug)
{
std::cout << "---------------" << std::endl;
std::cout << "OpenCL device info:" << std::endl;
auto names = computer.deviceNames(false);
for (int i = 0; i < names.size(); i++)
{
std::cout << names[i] << " computed " << (perf[i] * 100.0) << "% of total work" << std::endl;
}
std::cout << "---------------" << std::endl;
}
return bestParameters;
}
};
}