func (this *buffer) EnqueueMap(queue CommandQueue, blocking_map cl.CL_bool, map_flags cl.CL_map_flags, offset cl.CL_size_t, cb cl.CL_size_t, event_wait_list []Event) (unsafe.Pointer, Event, error) { var errCode cl.CL_int var event_id cl.CL_event numEvents := cl.CL_uint(len(event_wait_list)) events := make([]cl.CL_event, numEvents) for i := cl.CL_uint(0); i < numEvents; i++ { events[i] = event_wait_list[i].GetID() } if mapped_ptr := cl.CLEnqueueMapBuffer(queue.GetID(), this.memory_id, blocking_map, map_flags, offset, cb, numEvents, events, &event_id, &errCode); errCode != cl.CL_SUCCESS { return nil, nil, fmt.Errorf("EnqueueMap failure with errcode_ret %d: %s", errCode, cl.ERROR_CODES_STRINGS[-errCode]) } else { return mapped_ptr, &event{event_id}, nil } }
func main() { /* OpenCL data structures */ var device []cl.CL_device_id var context cl.CL_context var queue cl.CL_command_queue var program *cl.CL_program var kernel cl.CL_kernel var err cl.CL_int /* Data and buffers */ var data_one, data_two, result_array [100]float32 var buffer_one, buffer_two cl.CL_mem var mapped_memory unsafe.Pointer /* Initialize arrays */ for i := 0; i < 100; i++ { data_one[i] = 1.0 * float32(i) data_two[i] = -1.0 * float32(i) result_array[i] = 0.0 } /* Create a device and context */ device = utils.Create_device() context = cl.CLCreateContext(nil, 1, device[:], nil, nil, &err) if err < 0 { println("Couldn't create a context") return } /* Build the program and create the kernel */ program = utils.Build_program(context, device[:], PROGRAM_FILE, nil) kernel = cl.CLCreateKernel(*program, []byte(KERNEL_FUNC), &err) if err < 0 { println("Couldn't create a kernel") return } /* Create buffers */ buffer_one = cl.CLCreateBuffer(context, cl.CL_MEM_READ_WRITE| cl.CL_MEM_COPY_HOST_PTR, cl.CL_size_t(unsafe.Sizeof(data_one)), unsafe.Pointer(&data_one[0]), &err) if err < 0 { println("Couldn't create buffer object 1") return } buffer_two = cl.CLCreateBuffer(context, cl.CL_MEM_READ_WRITE| cl.CL_MEM_COPY_HOST_PTR, cl.CL_size_t(unsafe.Sizeof(data_two)), unsafe.Pointer(&data_two), &err) if err < 0 { println("Couldn't create buffer object 2") return } /* Set buffers as arguments to the kernel */ err = cl.CLSetKernelArg(kernel, 0, cl.CL_size_t(unsafe.Sizeof(buffer_one)), unsafe.Pointer(&buffer_one)) err |= cl.CLSetKernelArg(kernel, 1, cl.CL_size_t(unsafe.Sizeof(buffer_two)), unsafe.Pointer(&buffer_two)) if err < 0 { println("Couldn't set the buffer as the kernel argument") return } /* Create a command queue */ queue = cl.CLCreateCommandQueue(context, device[0], 0, &err) if err < 0 { println("Couldn't create a command queue") return } /* Enqueue kernel */ err = cl.CLEnqueueTask(queue, kernel, 0, nil, nil) if err < 0 { println("Couldn't enqueue the kernel") return } /* Enqueue command to copy buffer one to buffer two */ err = cl.CLEnqueueCopyBuffer(queue, buffer_one, buffer_two, 0, 0, cl.CL_size_t(unsafe.Sizeof(data_one)), 0, nil, nil) if err < 0 { println("Couldn't perform the buffer copy") return } /* Enqueue command to map buffer two to host memory */ mapped_memory = cl.CLEnqueueMapBuffer(queue, buffer_two, cl.CL_TRUE, cl.CL_MAP_READ, 0, cl.CL_size_t(unsafe.Sizeof(data_two)), 0, nil, nil, &err) if err < 0 { println("Couldn't map the buffer to host memory") return } /* Transfer memory and unmap the buffer */ C.memcpy(unsafe.Pointer(&result_array[0]), mapped_memory, C.size_t(unsafe.Sizeof(data_two))) err = cl.CLEnqueueUnmapMemObject(queue, buffer_two, mapped_memory, 0, nil, nil) if err < 0 { println("Couldn't unmap the buffer") return } /* Display updated buffer */ for i := 0; i < 10; i++ { for j := 0; j < 10; j++ { fmt.Printf("%6.1f", result_array[j+i*10]) } fmt.Printf("\n") } /* Deallocate resources */ cl.CLReleaseMemObject(buffer_one) cl.CLReleaseMemObject(buffer_two) cl.CLReleaseKernel(kernel) cl.CLReleaseCommandQueue(queue) cl.CLReleaseProgram(*program) cl.CLReleaseContext(context) }
func main() { /* OpenCL data structures */ var device []cl.CL_device_id var context cl.CL_context var queue cl.CL_command_queue var program *cl.CL_program var kernel cl.CL_kernel var err cl.CL_int /* Data and events */ var num_vectors cl.CL_int var data [NUM_BYTES]byte var data_buffer cl.CL_mem var prof_event cl.CL_event var total_time cl.CL_ulong var time_start, time_end interface{} var mapped_memory unsafe.Pointer /* Create a device and context */ device = utils.Create_device() context = cl.CLCreateContext(nil, 1, device[:], nil, nil, &err) if err < 0 { println("Couldn't create a context") return } /* Build the program and create a kernel */ program = utils.Build_program(context, device[:], PROGRAM_FILE, nil) kernel = cl.CLCreateKernel(*program, KERNEL_FUNC, &err) if err < 0 { println("Couldn't create a kernel") return } /* Create a buffer to hold data */ data_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_WRITE_ONLY, cl.CL_size_t(unsafe.Sizeof(data[0]))*NUM_BYTES, nil, &err) if err < 0 { println("Couldn't create a buffer") return } /* Create kernel argument */ err = cl.CLSetKernelArg(kernel, 0, cl.CL_size_t(unsafe.Sizeof(data_buffer)), unsafe.Pointer(&data_buffer)) if err < 0 { println("Couldn't set a kernel argument") return } /* Tell kernel number of char16 vectors */ num_vectors = NUM_BYTES / 16 cl.CLSetKernelArg(kernel, 1, cl.CL_size_t(unsafe.Sizeof(num_vectors)), unsafe.Pointer(&num_vectors)) /* Create a command queue */ queue = cl.CLCreateCommandQueue(context, device[0], cl.CL_QUEUE_PROFILING_ENABLE, &err) if err < 0 { println("Couldn't create a command queue") return } total_time = 0.0 for i := 0; i < NUM_ITERATIONS; i++ { /* Enqueue kernel */ err = cl.CLEnqueueTask(queue, kernel, 0, nil, nil) if err < 0 { println("Couldn't enqueue the kernel") return } if PROFILE_READ == 1 { /* Read the buffer */ err = cl.CLEnqueueReadBuffer(queue, data_buffer, cl.CL_TRUE, 0, cl.CL_size_t(unsafe.Sizeof(data[0]))*NUM_BYTES, unsafe.Pointer(&data[0]), 0, nil, &prof_event) if err < 0 { println("Couldn't read the buffer") return } } else { /* Create memory map */ mapped_memory = cl.CLEnqueueMapBuffer(queue, data_buffer, cl.CL_TRUE, cl.CL_MAP_READ, 0, cl.CL_size_t(unsafe.Sizeof(data[0]))*NUM_BYTES, 0, nil, &prof_event, &err) if err < 0 { println("Couldn't map the buffer to host memory") return } } /* Get profiling information */ cl.CLGetEventProfilingInfo(prof_event, cl.CL_PROFILING_COMMAND_START, cl.CL_size_t(unsafe.Sizeof(total_time)), &time_start, nil) cl.CLGetEventProfilingInfo(prof_event, cl.CL_PROFILING_COMMAND_END, cl.CL_size_t(unsafe.Sizeof(total_time)), &time_end, nil) total_time += time_end.(cl.CL_ulong) - time_start.(cl.CL_ulong) if PROFILE_READ == 0 { /* Unmap the buffer */ err = cl.CLEnqueueUnmapMemObject(queue, data_buffer, mapped_memory, 0, nil, nil) if err < 0 { println("Couldn't unmap the buffer") return } } } if PROFILE_READ == 1 { fmt.Printf("Average read time: %v\n", total_time/NUM_ITERATIONS) } else { fmt.Printf("Average map time: %v\n", total_time/NUM_ITERATIONS) } /* Deallocate resources */ cl.CLReleaseEvent(prof_event) cl.CLReleaseMemObject(data_buffer) cl.CLReleaseKernel(kernel) cl.CLReleaseCommandQueue(queue) cl.CLReleaseProgram(*program) cl.CLReleaseContext(context) }