func getDevicesList() []deviceInfo { var deviceIds = make([]cl.DeviceId, 100) var numDevices uint32 err := cl.GetDeviceIDs(nil, cl.DEVICE_TYPE_ALL, uint32(len(deviceIds)), &deviceIds[0], &numDevices) if err != cl.SUCCESS { log.Fatal("Failed to create device group") } var allDevices = make([]deviceInfo, numDevices) for i := uint32(0); i < numDevices; i++ { allDevices[i].deviceId = deviceIds[i] var responseLength uint64 var responseData = make([]byte, BufferSize) cl.GetDeviceInfo(deviceIds[i], cl.DEVICE_NAME, BufferSize, unsafe.Pointer(&responseData[0]), &responseLength) allDevices[i].name = string(responseData[:responseLength]) cl.GetDeviceInfo(deviceIds[i], cl.DEVICE_VENDOR, BufferSize, unsafe.Pointer(&responseData[0]), &responseLength) allDevices[i].vendor = string(responseData[:responseLength]) cl.GetDeviceInfo(deviceIds[i], cl.DEVICE_TYPE, BufferSize, unsafe.Pointer(&responseData[0]), &responseLength) allDevices[i].deviceType = string(responseData[:responseLength]) } return allDevices }
// StatInfo prints a bunch of usefull information about the currently available // CL devices func StatInfo() { ids := make([]cl.PlatformID, 100) actual := uint32(0) cl.GetPlatformIDs(uint32(len(ids)), &ids[0], &actual) for x := 0; x < int(actual); x++ { data := make([]byte, DataSize) size := uint64(0) cl.GetPlatformInfo(ids[x], cl.PLATFORM_PROFILE, DataSize, unsafe.Pointer(&data[0]), &size) profilestring := string(data[0:size]) cl.GetPlatformInfo(ids[x], cl.PLATFORM_VERSION, DataSize, unsafe.Pointer(&data[0]), &size) versionstring := string(data[0:size]) cl.GetPlatformInfo(ids[x], cl.PLATFORM_NAME, DataSize, unsafe.Pointer(&data[0]), &size) namestring := string(data[0:size]) cl.GetPlatformInfo(ids[x], cl.PLATFORM_VENDOR, DataSize, unsafe.Pointer(&data[0]), &size) vendorstring := string(data[0:size]) cl.GetPlatformInfo(ids[x], cl.PLATFORM_EXTENSIONS, DataSize, unsafe.Pointer(&data[0]), &size) extensionsstring := string(data[0:size]) log.Print("PLATFORM_PROFILE:\t\t", profilestring) log.Print("PLATFORM_VERSION:\t\t", versionstring) log.Print("PLATFORM_NAME:\t\t", namestring) log.Print("PLATFORM_VENDOR:\t\t", vendorstring) log.Print("PLATFORM_EXTENSIONS:\t", extensionsstring) devices := make([]cl.DeviceId, 100) actualDid := uint32(0) cl.GetDeviceIDs(ids[x], cl.DEVICE_TYPE_ALL, uint32(len(devices)), &devices[0], &actualDid) log.Println("Devices: ") for y := 0; y < int(actualDid); y++ { cl.GetDeviceInfo(devices[y], cl.DEVICE_NAME, DataSize, unsafe.Pointer(&data[0]), &size) deviceName := string(data[0:size]) log.Print("\tname: "+deviceName+" @ ", &devices[y]) } } }
func main() { data := make([]float32, DataSize) for x := 0; x < len(data); x++ { data[x] = rand.Float32()*99 + 1 } //Get Device var device cl.DeviceId err := cl.GetDeviceIDs(nil, cl.DEVICE_TYPE_GPU, 1, &device, nil) if err != cl.SUCCESS { log.Fatal("Failed to create device group") } var errptr *cl.ErrorCode //Create Computer Context context := cl.CreateContext(nil, 1, &device, nil, nil, errptr) if errptr != nil && cl.ErrorCode(*errptr) != cl.SUCCESS { log.Fatal("couldnt create context") } defer cl.ReleaseContext(context) //Create Command Queue cq := cl.CreateCommandQueue(context, device, 0, errptr) if errptr != nil && cl.ErrorCode(*errptr) != cl.SUCCESS { log.Fatal("couldnt create command queue") } defer cl.ReleaseCommandQueue(cq) //Create program srcptr := cl.Str(KernelSource) program := cl.CreateProgramWithSource(context, 1, &srcptr, nil, errptr) if errptr != nil && cl.ErrorCode(*errptr) != cl.SUCCESS { log.Fatal("couldnt create program") } defer cl.ReleaseProgram(program) err = cl.BuildProgram(program, 1, &device, nil, nil, nil) if err != cl.SUCCESS { var length uint64 buffer := make([]byte, DataSize) log.Println("Error: Failed to build program executable!") cl.GetProgramBuildInfo(program, device, cl.PROGRAM_BUILD_LOG, uint64(len(buffer)), unsafe.Pointer(&buffer[0]), &length) log.Fatal(string(buffer[0:length])) } //Get Kernel kernel := cl.CreateKernel(program, cl.Str("square"+"\x00"), errptr) if errptr != nil && cl.ErrorCode(*errptr) != cl.SUCCESS { log.Fatal("couldnt create compute kernel") } defer cl.ReleaseKernel(kernel) //Create buffers input := cl.CreateBuffer(context, cl.MEM_READ_ONLY, 4*DataSize, nil, errptr) if errptr != nil && cl.ErrorCode(*errptr) != cl.SUCCESS { log.Fatal("couldnt create input buffer") } defer cl.ReleaseMemObject(input) output := cl.CreateBuffer(context, cl.MEM_WRITE_ONLY, 4*DataSize, nil, errptr) if errptr != nil && cl.ErrorCode(*errptr) != cl.SUCCESS { log.Fatal("couldnt create output buffer") } defer cl.ReleaseMemObject(output) //Write data err = cl.EnqueueWriteBuffer(cq, input, cl.TRUE, 0, 4*DataSize, unsafe.Pointer(&data[0]), 0, nil, nil) if err != cl.SUCCESS { log.Fatal("Failed to write to source array") } //Set kernel args count := uint32(DataSize) err = cl.SetKernelArg(kernel, 0, 8, unsafe.Pointer(&input)) if err != cl.SUCCESS { log.Fatal("Failed to write kernel arg 0") } err = cl.SetKernelArg(kernel, 1, 8, unsafe.Pointer(&output)) if err != cl.SUCCESS { log.Fatal("Failed to write kernel arg 1") } err = cl.SetKernelArg(kernel, 2, 4, unsafe.Pointer(&count)) if err != cl.SUCCESS { log.Fatal("Failed to write kernel arg 2") } local := uint64(0) err = cl.GetKernelWorkGroupInfo(kernel, device, cl.KERNEL_WORK_GROUP_SIZE, 8, unsafe.Pointer(&local), nil) if err != cl.SUCCESS { log.Fatal("Failed to get kernel work group info") } global := local err = cl.EnqueueNDRangeKernel(cq, kernel, 1, nil, &global, &local, 0, nil, nil) if err != cl.SUCCESS { log.Fatal("Failed to execute kernel!") } cl.Finish(cq) results := make([]float32, DataSize) err = cl.EnqueueReadBuffer(cq, output, cl.TRUE, 0, 4*1024, unsafe.Pointer(&results[0]), 0, nil, nil) if err != cl.SUCCESS { log.Fatal("Failed to read buffer!") } success := 0 notzero := 0 for i, x := range data { if math.Abs(float64(x*x-results[i])) < 0.5 { success++ } if results[i] > 0 { notzero++ } log.Printf("I/O: %f\t%f", x, results[i]) } log.Printf("%d/%d success", success, DataSize) log.Printf("values not zero: %d", notzero) }