/
main.go
635 lines (485 loc) · 17.5 KB
/
main.go
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
package main
import (
"flag"
"fmt"
"github.com/Morenim/gom-opencl/bitset"
"github.com/Morenim/gom-opencl/ga"
"github.com/Morenim/gom-opencl/problem"
"github.com/rainliu/gocl/cl"
"io/ioutil"
"log"
"math/rand"
"os"
"time"
"unsafe"
)
var (
useCPU bool
printProblems bool
verbosity int
randomSeed int
populationSize int
numGenerations int
problemLength int
problemIndex int
)
var problems = []struct {
name string
evaluator problem.Problem
clSource string
}{
{"Deceptive Trap", problem.DeceptiveTrap(4), "deceptive_trap.cl"},
{"HIFF", problem.HIFF(0), "hiff.cl"},
}
type byLength [][]int
func (bl byLength) Len() int {
return len(bl)
}
func (bl byLength) Swap(i, j int) {
bl[i], bl[j] = bl[j], bl[i]
}
func (bl byLength) Less(i, j int) bool {
if len(bl[i]) < len(bl[j]) {
return true
}
if len(bl[i]) == len(bl[j]) {
return bl[i][0] < bl[j][0]
}
return false
}
// Find the first device matching the device type from the list of platforms.
func findDevice(platforms []cl.CL_platform_id, deviceType cl.CL_device_type) (platformID cl.CL_platform_id, deviceID cl.CL_device_id) {
// Search all platforms for the first device.
for _, platform := range platforms {
var numDevices cl.CL_uint
// Get the number of matching devices for the platform.
status := cl.CLGetDeviceIDs(platform, deviceType, 0, nil, &numDevices)
// Check for errors, continue to next platform if no matching device was found.
switch status {
case cl.CL_DEVICE_NOT_FOUND:
fallthrough
case cl.CL_SUCCESS:
if numDevices == 0 {
continue
}
default:
log.Printf("OpenCL failed with status code: %s", cl.ERROR_CODES_STRINGS[-status])
log.Fatalf("Fatal error: could not retrieve devices for platform %d", platform)
}
device := make([]cl.CL_device_id, 1)
// Select first device matching the device type.
status = cl.CLGetDeviceIDs(
platform,
cl.CL_DEVICE_TYPE_GPU,
1,
device,
nil)
if status != cl.CL_SUCCESS {
log.Fatalf("Fatal error: could not retrieve GPU device for platform %d", platform)
}
platformID = platform
deviceID = device[0]
break
}
return
}
func printPlatforms(platforms []cl.CL_platform_id) {
log.Printf("Debug: found %d platforms:", len(platforms))
getParam := func(id cl.CL_platform_id, name cl.CL_platform_info) interface{} {
var numChars cl.CL_size_t
var info interface{}
status := cl.CLGetPlatformInfo(id, name, 0, nil, &numChars)
status = cl.CLGetPlatformInfo(id, name, numChars, &info, nil)
if status != cl.CL_SUCCESS {
log.Fatalf("Fatal error: could not retrieve OpenCL platform info for id %d", id)
}
return info.(string)
}
for _, id := range platforms {
log.Printf("%s %d", "PlatformID", id)
log.Printf("\t%-11s: %s", "Name", getParam(id, cl.CL_PLATFORM_NAME))
log.Printf("\t%-11s: %s", "Vendor", getParam(id, cl.CL_PLATFORM_VENDOR))
log.Printf("\t%-11s: %s", "Version", getParam(id, cl.CL_PLATFORM_VERSION))
log.Printf("\t%-11s: %s", "Profile", getParam(id, cl.CL_PLATFORM_PROFILE))
log.Printf("\t%-11s: %s", "Extensions", getParam(id, cl.CL_PLATFORM_EXTENSIONS))
}
}
func printDeviceInfo(device cl.CL_device_id) {
var buffer interface{}
getParam := func(name cl.CL_device_info) interface{} {
requireSuccess(cl.CLGetDeviceInfo(device, name, 128, &buffer, nil),
"could not retrieve work group information for kernel.")
return buffer
}
log.Printf("Chosen Device Info:")
switch getParam(cl.CL_DEVICE_TYPE) {
case cl.CL_DEVICE_TYPE_GPU:
log.Printf("\t%-11s: %v", "Type", "GPU")
case cl.CL_DEVICE_TYPE_CPU:
log.Printf("\t%-11s: %v", "Type", "CPU")
}
log.Printf("\t%-11s: %v", "Max Compute Units", getParam(cl.CL_DEVICE_MAX_COMPUTE_UNITS))
log.Printf("\t%-11s: %v", "Max Work Group Size", getParam(cl.CL_DEVICE_MAX_WORK_GROUP_SIZE))
log.Printf("\t%-11s: %v", "Max Work Item Dimensions", getParam(cl.CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS))
log.Printf("\t%-11s: %v", "Max Mem Alloc Size", getParam(cl.CL_DEVICE_MAX_MEM_ALLOC_SIZE))
log.Printf("\t%-11s: %v", "Local Mem Size", getParam(cl.CL_DEVICE_LOCAL_MEM_SIZE))
//log.Printf("\t%-11s: %v", "Max Write Image Args", getParam(cl.CL_DEVICE_MAX_READ_IMAGE_ARGS))
//log.Printf("\t%-11s: %v", "Max Image2D Width", getParam(cl.CL_DEVICE_IMAGE2D_MAX_WIDTH))
//log.Printf("\t%-11s: %v", "Max Image2D Height", getParam(cl.CL_DEVICE_IMAGE2D_MAX_HEIGHT))
}
func printKernelWorkGroup(kernel cl.CL_kernel, device cl.CL_device_id) {
var buffer interface{}
getParam := func(name cl.CL_kernel_work_group_info) interface{} {
requireSuccess(cl.CLGetKernelWorkGroupInfo(kernel, device, name, 12, &buffer, nil),
"could not retrieve work group information for kernel.")
return buffer
}
log.Printf("Kernel Work Group Information:")
log.Printf("\t%-11s: %v", "Work Group Size", getParam(cl.CL_KERNEL_WORK_GROUP_SIZE))
log.Printf("\t%-11s: %v", "Local Memory Size", getParam(cl.CL_KERNEL_LOCAL_MEM_SIZE))
log.Printf("\t%-11s: %v", "Private Memory Size", getParam(cl.CL_KERNEL_PRIVATE_MEM_SIZE))
log.Printf("\t%-11s: %v", "Preferred Work Group Size Multiple", getParam(cl.CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE))
}
func printGeneration(numGenerations int, pop *ga.Population) {
fmt.Printf("Generation %d\n", numGenerations)
fmt.Println("===============")
for i, solution := range pop.Solutions {
fitness, optimal := problems[problemIndex].evaluator.Evaluate(solution.Bits)
solution.Fitness = fitness
fmt.Printf("x_%-2d: %v %t\n", i, solution, optimal)
}
fmt.Println("===============")
fmt.Println()
}
func flattenIntoSlice(src [][]int, dest []cl.CL_uint) {
dest[0] = cl.CL_uint(len(src))
i := 1
for _, node := range src {
j := 0
numMasks := 0
start := i // Reserve index for number of masks.
i++
for j < len(node) {
maskIndex := uint32(node[j] >> 5) // index of mask to create
mask := uint32(0)
// Create a mask from all indices belonging to the block of bits.
for j < len(node) && (uint32(node[j])>>5) == maskIndex {
mask |= 1 << (uint32(node[j]) & 31)
j++
}
// Append the mask to the flattened FOS.
dest[i] = cl.CL_uint(maskIndex)
dest[i+1] = cl.CL_uint(mask)
i += 2
numMasks++
}
dest[start] = cl.CL_uint(numMasks)
}
}
func populationToSlice(pop *ga.Population, dest []cl.CL_uint) {
destPtr := 0
length := pop.Length()
numBlocks := blocksPerSolution(pop)
for _, solution := range pop.Solutions {
toCopy := length
for j := 0; j < numBlocks; j++ {
// Determine the number of bits to copy.
blockSize := 32
if toCopy < 32 {
blockSize = toCopy
}
toCopy -= blockSize
// Copy the bits into 32-bit integer.
var raw uint32
raw = 0
for k := 0; k < blockSize; k++ {
index := j*32 + k
if solution.Bits.Has(index) {
raw |= (1 << uint32(k))
}
}
dest[destPtr] = cl.CL_uint(raw)
destPtr++
}
}
}
func sliceToPopulation(src []cl.CL_uint, pop *ga.Population) bool {
numBlocks := blocksPerSolution(pop)
buffer := make([]uint32, numBlocks)
foundOptimal := false
for i, _ := range pop.Solutions {
for j := 0; j < numBlocks; j++ {
buffer[j] = uint32(src[i*numBlocks+j])
}
pop.Solutions[i].Bits, _ = bitset.FromUInt32s(buffer, pop.Length())
fitness, optimal := problems[problemIndex].evaluator.Evaluate(pop.Solutions[i].Bits)
pop.Solutions[i].Fitness = fitness
if optimal {
foundOptimal = true
}
}
return foundOptimal
}
func setKernelArg(kernel cl.CL_kernel, pos int, data interface{}) {
var status cl.CL_int
switch data := data.(type) {
case *cl.CL_mem:
status = cl.CLSetKernelArg(
kernel, cl.CL_uint(pos), cl.CL_size_t(unsafe.Sizeof(data)),
unsafe.Pointer(data))
case *cl.CL_uint:
status = cl.CLSetKernelArg(
kernel, cl.CL_uint(pos), cl.CL_size_t(unsafe.Sizeof(*data)),
unsafe.Pointer(data))
default:
log.Fatalf("Fatal error: setting kernel arg for unknown type %t.", data)
}
if status != cl.CL_SUCCESS {
log.Printf("%v", cl.ERROR_CODES_STRINGS[-status])
log.Fatalf("Fatal error: could not set arg %d for OpenCL kernel.", pos)
}
}
func printProblemList() {
fmt.Println("Index 0: Deceptive Trap Function (k = 4)")
fmt.Println("Index 1: HIFF")
os.Exit(0)
}
func printProgramInfo(program cl.CL_program, name cl.CL_program_info) string {
var buffer interface{}
var size cl.CL_size_t
status := cl.CLGetProgramInfo(program, name, 0, nil, &size)
status = cl.CLGetProgramInfo(program, cl.CL_PROGRAM_SOURCE, size, &buffer, nil)
if status != cl.CL_SUCCESS {
log.Printf("%s", cl.ERROR_CODES_STRINGS[-status])
log.Fatal("Fatal error: could not retrieve program info.")
}
return fmt.Sprintf("%s", buffer.(string))
}
func printProgramBuildInfo(program cl.CL_program, device cl.CL_device_id) {
var numChars cl.CL_size_t
var info interface{}
err := "could not retrieve OpenCL program build info."
requireSuccess(cl.CLGetProgramBuildInfo(
program, device, cl.CL_PROGRAM_BUILD_LOG,
0, nil, &numChars), err)
requireSuccess(cl.CLGetProgramBuildInfo(
program, device, cl.CL_PROGRAM_BUILD_LOG,
numChars, &info, nil), err)
// printProgramInfo(program, cl.CL_PROGRAM_SOURCE)
log.Print("Fatal error: could not build OpenCL program.")
log.Fatalf("%s", info.(string))
}
func parseCommandLine() {
flag.BoolVar(&useCPU, "cpu", false, "Whether to use the CPU over the GPU.")
flag.IntVar(&verbosity, "verbosity", 0, "Verbosity of the output.")
flag.IntVar(&randomSeed, "random", 0, "Random seed to use. Defaults to a time-based random seed.")
flag.IntVar(&populationSize, "size", 64, "Number of solutions in the fixed-size population.")
flag.IntVar(&numGenerations, "generations", -1, "Maximum number of generations to perform for GOMEA.")
flag.IntVar(&problemLength, "length", 32, "Length of the optimization problem.")
flag.IntVar(&problemIndex, "index", 0, "Index of the optimization problem to solve.")
flag.BoolVar(&printProblems, "problem-list", false, "Print a list of the available optimization problems and terminate.")
flag.Parse()
}
func blocksPerSolution(pop *ga.Population) int {
return ((pop.Length() - 1) >> 5) + 1
}
func requireSuccess(status cl.CL_int, customError string) {
if status != cl.CL_SUCCESS {
log.Printf("OpenCL failed with status code: %s", cl.ERROR_CODES_STRINGS[-status])
log.Fatalf("Fatal error: %s", customError)
}
}
func runOpenCL() {
var status cl.CL_int
var numPlatforms cl.CL_uint
//---------------------------------------------------
// Step 1: Discover and retrieve OpenCL platforms.
//---------------------------------------------------
status = cl.CLGetPlatformIDs(0, nil, &numPlatforms)
platforms := make([]cl.CL_platform_id, numPlatforms)
requireSuccess(cl.CLGetPlatformIDs(numPlatforms, platforms, nil),
"could not retrieve OpenCL platform IDs.")
if verbosity >= 4 {
printPlatforms(platforms)
}
//---------------------------------------------------
// Step 2: Discover and retrieve OpenCL devices.
//---------------------------------------------------
var preferredType cl.CL_device_type
if useCPU {
preferredType = cl.CL_DEVICE_TYPE_CPU
} else {
preferredType = cl.CL_DEVICE_TYPE_GPU
}
_, gpuDevice := findDevice(platforms, preferredType)
gpuDevices := make([]cl.CL_device_id, 1)
gpuDevices[0] = gpuDevice
if verbosity >= 4 {
printDeviceInfo(gpuDevice)
}
//---------------------------------------------------
// Step 3: Create an OpenCL context.
//---------------------------------------------------
context := cl.CLCreateContext(nil, 1, gpuDevices, nil, nil, &status)
requireSuccess(status, "could not create OpenCL context.")
defer cl.CLReleaseContext(context)
//---------------------------------------------------
// Step 3: Create an OpenCL command queue.
//---------------------------------------------------
commandQueue := cl.CLCreateCommandQueue(context, gpuDevice, 0, &status)
requireSuccess(status, "could not create OpenCL command queue.")
defer cl.CLReleaseCommandQueue(commandQueue)
//---------------------------------------------------
// Step 4: Create OpenCL program and kernel.
//---------------------------------------------------
var clSourceData [3][]byte
var clSourceLengths [3]cl.CL_size_t
var err error
clSourceFiles := []string{
"kernels/" + problems[problemIndex].clSource,
"kernels/rng.cl",
"kernels/gom.cl",
}
for i, s := range clSourceFiles {
clSourceData[i], err = ioutil.ReadFile(s)
if err != nil {
log.Fatalf("Could not read the kernel source file %s.", s)
}
clSourceLengths[i] = cl.CL_size_t(len(clSourceData[i]))
}
program := cl.CLCreateProgramWithSource(context, 3, clSourceData[:], clSourceLengths[:], &status)
requireSuccess(status, "could not compile an OpenCL kernel from source.")
status = cl.CLBuildProgram(program, 1, gpuDevices, nil, nil, nil)
if status != cl.CL_SUCCESS {
printProgramBuildInfo(program, gpuDevice)
}
kernel := cl.CLCreateKernel(program, []byte("gom"), &status)
requireSuccess(status, "could not create OpenCL kernel.")
//---------------------------------------------------
// Step 6: Initialize OpenCL memory.
//---------------------------------------------------
if verbosity >= 4 {
printKernelWorkGroup(kernel, gpuDevice)
}
//---------------------------------------------------
// Step 7: Initialize OpenCL memory.
//---------------------------------------------------
var size cl.CL_uint
length := cl.CL_size_t(problemLength)
pop := ga.NewPopulation(populationSize, problemLength)
numBlocks := blocksPerSolution(pop) * pop.Size()
dataSize := cl.CL_size_t(unsafe.Sizeof(size)) * cl.CL_size_t(numBlocks)
populationData := make([]cl.CL_uint, numBlocks)
offspringData := make([]cl.CL_uint, numBlocks)
populationBuffer := cl.CLCreateBuffer(
context, cl.CL_MEM_READ_ONLY, dataSize, nil, &status)
requireSuccess(status, "could not allocate an OpenCL memory buffer.")
cloneBuffer := cl.CLCreateBuffer(
context, cl.CL_MEM_READ_WRITE, dataSize, nil, &status)
requireSuccess(status, "could not allocate an OpenCL memory buffer.")
// Maximum bound on the number of elements in the LT + node sizes.
boundSum := (length*length+3*length-2)/2 + (2*length - 1) + 1
ltSize := cl.CL_size_t(unsafe.Sizeof(length)) * boundSum
ltData := make([]cl.CL_uint, boundSum)
ltBuffer := cl.CLCreateBuffer(context, cl.CL_MEM_READ_ONLY, ltSize, nil, &status)
requireSuccess(status, "could not allocate an OpenCL memory buffer.")
var dummyCLBool cl.CL_char
improvsSize := cl.CL_size_t(unsafe.Sizeof(dummyCLBool)) * cl.CL_size_t(pop.Size())
improvsData := make([]cl.CL_char, pop.Size())
improvsBuffer := cl.CLCreateBuffer(context, cl.CL_MEM_WRITE_ONLY, improvsSize, nil, &status)
requireSuccess(status, "could not allocate an OpenCL memory buffer.")
offspringBuffer := cl.CLCreateBuffer(
context, cl.CL_MEM_WRITE_ONLY, dataSize, nil, &status)
requireSuccess(status, "could not allocate an OpenCL memory buffer.")
//---------------------------------------------------
// Step 8: Perform GOMEA.
//---------------------------------------------------
if randomSeed == 0 {
rand.Seed(time.Now().Unix())
} else {
rand.Seed(int64(randomSeed))
}
done := false
generationsPassed := 0
if verbosity >= 3 {
printGeneration(0, pop)
}
for !done {
// Build the linkage tree and upload a flattened version to the compute device.
freqs := Frequencies(pop)
lt := LinkageTree(pop, freqs)
flattenIntoSlice(lt, ltData)
requireSuccess(cl.CLEnqueueWriteBuffer(
commandQueue, ltBuffer, cl.CL_TRUE, 0,
ltSize, unsafe.Pointer(<Data[0]), 0, nil, nil),
"could not write data to an OpenCL memory buffer.")
// Store a flattened version of the population on the compute device.
populationToSlice(pop, populationData)
requireSuccess(cl.CLEnqueueWriteBuffer(
commandQueue, populationBuffer, cl.CL_TRUE, 0,
dataSize, unsafe.Pointer(&populationData[0]), 0, nil, nil),
"could not write data to an OpenCL memory buffer.")
// Set the GOM kernel arguments.
popSize := cl.CL_uint(pop.Size())
solLength := cl.CL_uint(pop.Length())
setKernelArg(kernel, 0, &populationBuffer)
setKernelArg(kernel, 1, &popSize)
setKernelArg(kernel, 2, &solLength)
setKernelArg(kernel, 3, &cloneBuffer)
setKernelArg(kernel, 4, <Buffer)
setKernelArg(kernel, 5, &improvsBuffer)
setKernelArg(kernel, 6, &offspringBuffer)
var globalWorkSize [1]cl.CL_size_t
globalWorkSize[0] = cl.CL_size_t(pop.Size())
// Perform GOM crossover.
requireSuccess(cl.CLEnqueueNDRangeKernel(
commandQueue, kernel, 1, nil, globalWorkSize[:],
nil, 0, nil, nil),
"could not enqueue OpenCL kernel.")
requireSuccess(cl.CLFinish(commandQueue), "could not finish command queue.")
// Retrieve the offspring population from the compute device.
requireSuccess(cl.CLEnqueueReadBuffer(
commandQueue, offspringBuffer, cl.CL_TRUE, 0,
dataSize, unsafe.Pointer(&offspringData[0]), 0, nil, nil),
"reading a buffer failed.")
requireSuccess(cl.CLEnqueueReadBuffer(
commandQueue, improvsBuffer, cl.CL_TRUE, 0,
improvsSize, unsafe.Pointer(&improvsData[0]), 0, nil, nil),
"reading improvs buffer failed.")
foundOptimal := sliceToPopulation(offspringData, pop)
generationsPassed++
if (verbosity == 2 && done) || (verbosity == 3) {
printGeneration(generationsPassed, pop)
}
// TODO: Termination Criterion
if generationsPassed == numGenerations {
done = true
}
improved := false
for _, b := range improvsData {
if b > 0 {
improved = true
break
}
}
if !improved {
if verbosity >= 2 {
log.Println("Terminated after the population did not improve for one generation.")
}
done = true
}
if foundOptimal {
if verbosity >= 2 {
log.Printf("Optimal solution found after %d generations.\n", generationsPassed)
}
done = true
}
}
}
func main() {
parseCommandLine()
if printProblems {
printProblemList()
}
runOpenCL()
}