Beispiel #1
0
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 select1 [4]float32
	var select2 [2]cl.CL_uchar
	var select1_buffer, select2_buffer cl.CL_mem

	/* Create a context */
	device = utils.Create_device()
	context = cl.CLCreateContext(nil, 1, device[:], nil, nil, &err)
	if err < 0 {
		println("Couldn't create a context")
		return
	}

	/* 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 write-only buffer to hold the output data */
	select1_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_WRITE_ONLY,
		cl.CL_size_t(unsafe.Sizeof(select1)), nil, &err)
	if err < 0 {
		println("Couldn't create a buffer")
		return
	}
	select2_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_WRITE_ONLY,
		cl.CL_size_t(unsafe.Sizeof(select2)), nil, &err)

	/* Create kernel argument */
	err = cl.CLSetKernelArg(kernel, 0, cl.CL_size_t(unsafe.Sizeof(select1_buffer)), unsafe.Pointer(&select1_buffer))
	if err < 0 {
		println("Couldn't set a kernel argument")
		return
	}
	cl.CLSetKernelArg(kernel, 1, cl.CL_size_t(unsafe.Sizeof(select2_buffer)), unsafe.Pointer(&select2_buffer))

	/* 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
	}

	/* Read and print the result */
	err = cl.CLEnqueueReadBuffer(queue, select1_buffer, cl.CL_TRUE, 0,
		cl.CL_size_t(unsafe.Sizeof(select1)), unsafe.Pointer(&select1), 0, nil, nil)
	if err < 0 {
		println("Couldn't read the buffer")
		return
	}
	cl.CLEnqueueReadBuffer(queue, select2_buffer, cl.CL_TRUE, 0,
		cl.CL_size_t(unsafe.Sizeof(select2)), unsafe.Pointer(&select2), 0, nil, nil)

	fmt.Printf("select: ")
	for i := 0; i < 3; i++ {
		fmt.Printf("%.2f, ", select1[i])
	}
	fmt.Printf("%.2f\n", select1[3])

	fmt.Printf("bitselect: %X, %X\n", select2[0], select2[1])

	/* Deallocate resources */
	cl.CLReleaseMemObject(select1_buffer)
	cl.CLReleaseMemObject(select2_buffer)
	cl.CLReleaseKernel(kernel)
	cl.CLReleaseCommandQueue(queue)
	cl.CLReleaseProgram(*program)
	cl.CLReleaseContext(context)
}
Beispiel #2
0
func main() {
	var i, j cl.CL_size_t
	// Rows and columns in the input image
	inputFile := "test.png"
	outputFile := "output.png"
	refFile := "ref.png"

	// Homegrown function to read a BMP from file
	inputpixels, imageWidth, imageHeight, err1 := utils.Read_image_data(inputFile)
	if err1 != nil {
		log.Fatal(err1)
		return
	} else {
		fmt.Printf("width=%d, height=%d (%d)\n", imageWidth, imageHeight, inputpixels[0])
	}

	// Output image on the host
	outputpixels := make([]uint16, imageHeight*imageWidth)
	inputImage := make([]float32, imageHeight*imageWidth)
	outputImage := make([]float32, imageHeight*imageWidth)
	refImage := make([]float32, imageHeight*imageWidth)

	for i = 0; i < imageHeight*imageWidth; i++ {
		inputImage[i] = float32(inputpixels[i])
	}

	// 45 degree motion blur
	var filter = [49]float32{0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0,
		0, 0, -1, 0, 1, 0, 0,
		0, 0, -2, 0, 2, 0, 0,
		0, 0, -1, 0, 1, 0, 0,
		0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0}

	// The convolution filter is 7x7
	filterWidth := cl.CL_size_t(7)
	filterSize := cl.CL_size_t(filterWidth * filterWidth) // Assume a square kernel

	// Set up the OpenCL environment
	var status cl.CL_int

	// Discovery platform
	var platform [1]cl.CL_platform_id
	status = cl.CLGetPlatformIDs(1, platform[:], nil)
	chk(status, "clGetPlatformIDs")

	// Discover device
	var device [1]cl.CL_device_id
	cl.CLGetDeviceIDs(platform[0], cl.CL_DEVICE_TYPE_ALL, 1, device[:], nil)
	chk(status, "clGetDeviceIDs")

	// Create context
	//var props =[3]cl.CL_context_properties{cl.CL_CONTEXT_PLATFORM,
	//    (cl.CL_context_properties)(unsafe.Pointer(&platform[0])), 0};

	var context cl.CL_context
	context = cl.CLCreateContext(nil, 1, device[:], nil, nil, &status)
	chk(status, "clCreateContext")

	// Create command queue
	var queue cl.CL_command_queue
	queue = cl.CLCreateCommandQueue(context, device[0], 0, &status)
	chk(status, "clCreateCommandQueue")

	// The image format describes how the data will be stored in memory
	var format cl.CL_image_format
	format.Image_channel_order = cl.CL_R         // single channel
	format.Image_channel_data_type = cl.CL_FLOAT // float data type

	var desc cl.CL_image_desc
	desc.Image_type = cl.CL_MEM_OBJECT_IMAGE2D
	desc.Image_width = imageWidth
	desc.Image_height = imageHeight
	desc.Image_depth = 0
	desc.Image_array_size = 0
	desc.Image_row_pitch = 0
	desc.Image_slice_pitch = 0
	desc.Num_mip_levels = 0
	desc.Num_samples = 0
	desc.Buffer = cl.CL_mem{}

	// Create space for the source image on the device
	d_inputImage := cl.CLCreateImage(context, cl.CL_MEM_READ_ONLY, &format, &desc,
		nil, &status)
	chk(status, "clCreateImage")

	// Create space for the output image on the device
	d_outputImage := cl.CLCreateImage(context, cl.CL_MEM_WRITE_ONLY, &format, &desc,
		nil, &status)
	chk(status, "clCreateImage")

	// Create space for the 7x7 filter on the device
	d_filter := cl.CLCreateBuffer(context, 0, filterSize*cl.CL_size_t(unsafe.Sizeof(filter[0])),
		nil, &status)
	chk(status, "clCreateBuffer")

	// Copy the source image to the device
	var origin = [3]cl.CL_size_t{0, 0, 0}                                                // Offset within the image to copy from
	var region = [3]cl.CL_size_t{cl.CL_size_t(imageWidth), cl.CL_size_t(imageHeight), 1} // Elements to per dimension
	status = cl.CLEnqueueWriteImage(queue, d_inputImage, cl.CL_FALSE, origin, region,
		0, 0, unsafe.Pointer(&inputImage[0]), 0, nil, nil)
	chk(status, "clEnqueueWriteImage")

	// Copy the 7x7 filter to the device
	status = cl.CLEnqueueWriteBuffer(queue, d_filter, cl.CL_FALSE, 0,
		filterSize*cl.CL_size_t(unsafe.Sizeof(filter[0])), unsafe.Pointer(&filter[0]), 0, nil, nil)
	chk(status, "clEnqueueWriteBuffer")

	// Create the image sampler
	sampler := cl.CLCreateSampler(context, cl.CL_FALSE,
		cl.CL_ADDRESS_CLAMP_TO_EDGE, cl.CL_FILTER_NEAREST, &status)
	chk(status, "clCreateSampler")

	// Create a program object with source and build it
	program := utils.Build_program(context, device[:], "convolution.cl", nil)
	kernel := cl.CLCreateKernel(*program, []byte("convolution"), &status)
	chk(status, "clCreateKernel")

	// Set the kernel arguments
	var w, h, f cl.CL_int
	w = cl.CL_int(imageWidth)
	h = cl.CL_int(imageHeight)
	f = cl.CL_int(filterWidth)
	status = cl.CLSetKernelArg(kernel, 0, cl.CL_size_t(unsafe.Sizeof(d_inputImage)), unsafe.Pointer(&d_inputImage))
	status |= cl.CLSetKernelArg(kernel, 1, cl.CL_size_t(unsafe.Sizeof(d_outputImage)), unsafe.Pointer(&d_outputImage))
	status |= cl.CLSetKernelArg(kernel, 2, cl.CL_size_t(unsafe.Sizeof(h)), unsafe.Pointer(&h))
	status |= cl.CLSetKernelArg(kernel, 3, cl.CL_size_t(unsafe.Sizeof(w)), unsafe.Pointer(&w))
	status |= cl.CLSetKernelArg(kernel, 4, cl.CL_size_t(unsafe.Sizeof(d_filter)), unsafe.Pointer(&d_filter))
	status |= cl.CLSetKernelArg(kernel, 5, cl.CL_size_t(unsafe.Sizeof(f)), unsafe.Pointer(&f))
	status |= cl.CLSetKernelArg(kernel, 6, cl.CL_size_t(unsafe.Sizeof(sampler)), unsafe.Pointer(&sampler))
	chk(status, "clSetKernelArg")

	// Set the work item dimensions
	var globalSize = [2]cl.CL_size_t{imageWidth, imageHeight}
	status = cl.CLEnqueueNDRangeKernel(queue, kernel, 2, nil, globalSize[:], nil, 0,
		nil, nil)
	chk(status, "clEnqueueNDRange")

	// Read the image back to the host
	status = cl.CLEnqueueReadImage(queue, d_outputImage, cl.CL_TRUE, origin,
		region, 0, 0, unsafe.Pointer(&outputImage[0]), 0, nil, nil)
	chk(status, "clEnqueueReadImage")

	// Write the output image to file
	for i = 0; i < imageHeight*imageWidth; i++ {
		outputpixels[i] = uint16(outputImage[i])
	}
	utils.Write_image_data(outputFile, outputpixels, imageWidth, imageHeight)

	// Compute the reference image
	for i = 0; i < imageHeight; i++ {
		for j = 0; j < imageWidth; j++ {
			refImage[i*imageWidth+j] = 0
		}
	}

	// Iterate over the rows of the source image
	halfFilterWidth := filterWidth / 2
	var sum float32
	for i = 0; i < imageHeight; i++ {
		// Iterate over the columns of the source image
		for j = 0; j < imageWidth; j++ {
			sum = 0 // Reset sum for new source pixel
			// Apply the filter to the neighborhood
			for k := -halfFilterWidth; k <= halfFilterWidth; k++ {
				for l := -halfFilterWidth; l <= halfFilterWidth; l++ {
					if i+k >= 0 && i+k < imageHeight &&
						j+l >= 0 && j+l < imageWidth {
						sum += inputImage[(i+k)*imageWidth+j+l] *
							filter[(k+halfFilterWidth)*filterWidth+
								l+halfFilterWidth]
					} else {
						i_k := i + k
						j_l := j + l
						if i+k < 0 {
							i_k = 0
						} else if i+k >= imageHeight {
							i_k = imageHeight - 1
						}
						if j+l < 0 {
							j_l = 0
						} else if j+l >= imageWidth {
							j_l = imageWidth - 1
						}
						sum += inputImage[(i_k)*imageWidth+j_l] *
							filter[(k+halfFilterWidth)*filterWidth+
								l+halfFilterWidth]
					}
				}
			}
			refImage[i*imageWidth+j] = sum
		}
	}
	// Write the ref image to file
	for i = 0; i < imageHeight*imageWidth; i++ {
		outputpixels[i] = uint16(refImage[i])
	}
	utils.Write_image_data(refFile, outputpixels, imageWidth, imageHeight)

	failed := 0
	for i = 0; i < imageHeight; i++ {
		for j = 0; j < imageWidth; j++ {
			if math.Abs(float64(outputImage[i*imageWidth+j]-refImage[i*imageWidth+j])) > 0.01 {
				//fmt.Printf("Results are INCORRECT\n");
				//fmt.Printf("Pixel mismatch at <%d,%d> (%f vs. %f) %f\n", i, j,
				//   outputImage[i*imageWidth+j], refImage[i*imageWidth+j], inputImage[i*imageWidth+j]);
				failed++
			}
		}
	}
	fmt.Printf("Mismatch Pixel number/Total pixel number = %d/%d\n", failed, imageWidth*imageHeight)

	// Free OpenCL resources
	cl.CLReleaseKernel(kernel)
	cl.CLReleaseProgram(*program)
	cl.CLReleaseCommandQueue(queue)
	cl.CLReleaseMemObject(d_inputImage)
	cl.CLReleaseMemObject(d_outputImage)
	cl.CLReleaseMemObject(d_filter)
	cl.CLReleaseSampler(sampler)
	cl.CLReleaseContext(context)
}
Beispiel #3
0
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 */
	dim := cl.CL_uint(2)
	var global_offset = [2]cl.CL_size_t{3, 5}
	var global_size = [2]cl.CL_size_t{6, 4}
	var local_size = [2]cl.CL_size_t{3, 2}
	var test [24]float32
	var test_buffer cl.CL_mem

	/* 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 write-only buffer to hold the output data */
	test_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_WRITE_ONLY,
		cl.CL_size_t(unsafe.Sizeof(test)), 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(test_buffer)), unsafe.Pointer(&test_buffer))
	if err < 0 {
		println("Couldn't set a 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.CLEnqueueNDRangeKernel(queue, kernel, dim, global_offset[:],
		global_size[:], local_size[:], 0, nil, nil)
	if err < 0 {
		println("Couldn't enqueue the kernel")
		return
	}

	/* Read and print the result */
	err = cl.CLEnqueueReadBuffer(queue, test_buffer, cl.CL_TRUE, 0,
		cl.CL_size_t(unsafe.Sizeof(test)), unsafe.Pointer(&test), 0, nil, nil)
	if err < 0 {
		println("Couldn't read the buffer")
		return
	}

	for i := 0; i < 24; i += 6 {
		fmt.Printf("%.2f     %.2f     %.2f     %.2f     %.2f     %.2f\n",
			test[i], test[i+1], test[i+2], test[i+3], test[i+4], test[i+5])
	}

	/* Deallocate resources */
	cl.CLReleaseMemObject(test_buffer)
	cl.CLReleaseKernel(kernel)
	cl.CLReleaseCommandQueue(queue)
	cl.CLReleaseProgram(*program)
	cl.CLReleaseContext(context)
}
Beispiel #4
0
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

	var err1 error
	var global_size [2]cl.CL_size_t

	/* Image data */
	var pixels []uint16
	var png_format cl.CL_image_format
	var input_image, output_image cl.CL_mem
	var origin, region [3]cl.CL_size_t
	var width, height cl.CL_size_t

	/* Open input file and read image data */
	pixels, width, height, err1 = utils.Read_image_data(INPUT_FILE)
	if err1 != nil {
		return
	} else {
		fmt.Printf("width=%d, height=%d", width, height)
	}

	/* 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 {
		fmt.Printf("Couldn't create a kernel: %d", err)
		return
	}

	/* Create image object */
	png_format.Image_channel_order = cl.CL_LUMINANCE
	png_format.Image_channel_data_type = cl.CL_UNORM_INT16
	input_image = cl.CLCreateImage2D(context,
		cl.CL_MEM_READ_ONLY|cl.CL_MEM_COPY_HOST_PTR,
		&png_format, width, height, 0, unsafe.Pointer(&pixels[0]), &err)
	output_image = cl.CLCreateImage2D(context,
		cl.CL_MEM_WRITE_ONLY, &png_format, width, height, 0, nil, &err)
	if err < 0 {
		println("Couldn't create the image object")
		return
	}

	/* Create kernel arguments */
	err = cl.CLSetKernelArg(kernel, 0, cl.CL_size_t(unsafe.Sizeof(input_image)), unsafe.Pointer(&input_image))
	err |= cl.CLSetKernelArg(kernel, 1, cl.CL_size_t(unsafe.Sizeof(output_image)), unsafe.Pointer(&output_image))
	if err < 0 {
		println("Couldn't set a 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 */
	global_size[0] = width
	global_size[1] = height
	err = cl.CLEnqueueNDRangeKernel(queue, kernel, 2, nil, global_size[:],
		nil, 0, nil, nil)
	if err < 0 {
		println("Couldn't enqueue the kernel")
		return
	}

	/* Read the image object */
	origin[0] = 0
	origin[1] = 0
	origin[2] = 0
	region[0] = width
	region[1] = height
	region[2] = 1
	err = cl.CLEnqueueReadImage(queue, output_image, cl.CL_TRUE, origin,
		region, 0, 0, unsafe.Pointer(&pixels[0]), 0, nil, nil)
	if err < 0 {
		println("Couldn't read from the image object")
		return
	}

	/* Create output PNG file and write data */
	utils.Write_image_data(OUTPUT_FILE, pixels, width, height)

	/* Deallocate resources */
	cl.CLReleaseMemObject(input_image)
	cl.CLReleaseMemObject(output_image)
	cl.CLReleaseKernel(kernel)
	cl.CLReleaseCommandQueue(queue)
	cl.CLReleaseProgram(*program)
	cl.CLReleaseContext(context)

}
Beispiel #5
0
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 data []float32
	var data_buffer cl.CL_mem
	var user_event, kernel_event, read_event [1]cl.CL_event

	/* Initialize data */
	data = make([]float32, 4)
	for i := 0; i < 4; i++ {
		data[i] = float32(i) * 1.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 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_READ_WRITE|cl.CL_MEM_COPY_HOST_PTR,
		cl.CL_size_t(unsafe.Sizeof(data[0]))*4, unsafe.Pointer(&data[0]), &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
	}

	/* Create a command queue */
	queue = cl.CLCreateCommandQueue(context, device[0],
		cl.CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &err)
	if err < 0 {
		println("Couldn't create a command queue")
		return
	}

	/* Configure events */
	user_event[0] = cl.CLCreateUserEvent(context, &err)
	if err < 0 {
		println("Couldn't enqueue the kernel")
		return
	}

	/* Enqueue kernel */
	err = cl.CLEnqueueTask(queue, kernel, 1, user_event[:], &kernel_event[0])
	if err < 0 {
		println("Couldn't enqueue the kernel")
		return
	}

	/* Read the buffer */
	err = cl.CLEnqueueReadBuffer(queue, data_buffer, cl.CL_FALSE, 0,
		cl.CL_size_t(unsafe.Sizeof(data[0]))*4, unsafe.Pointer(&data[0]), 1, kernel_event[:], &read_event[0])
	if err < 0 {
		println("Couldn't read the buffer")
		return
	}

	/* Set callback for event */
	err = cl.CLSetEventCallback(read_event[0], cl.CL_COMPLETE,
		read_complete, unsafe.Pointer(&data))
	if err < 0 {
		println("Couldn't set callback for event")
		return
	}

	/* Sleep for a second to demonstrate the that commands haven't
	   started executing. Then prompt user */
	time.Sleep(1)
	fmt.Printf("Old data: %4.2f, %4.2f, %4.2f, %4.2f\n",
		data[0], data[1], data[2], data[3])
	fmt.Printf("Press ENTER to continue.\n")
	//getchar();
	reader := bufio.NewReader(os.Stdin)
	reader.ReadString('\n')

	/* Set user event to success */
	cl.CLSetUserEventStatus(user_event[0], cl.CL_SUCCESS)

	/* Deallocate resources */
	cl.CLReleaseEvent(read_event[0])
	cl.CLReleaseEvent(kernel_event[0])
	cl.CLReleaseEvent(user_event[0])
	cl.CLReleaseMemObject(data_buffer)
	cl.CLReleaseKernel(kernel)
	cl.CLReleaseCommandQueue(queue)
	cl.CLReleaseProgram(*program)
	cl.CLReleaseContext(context)
}
Beispiel #6
0
func main() {

	/* Host/device 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 test [16]byte
	var test_buffer cl.CL_mem

	/* Create a 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 write-only buffer to hold the output data */
	test_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_WRITE_ONLY,
		cl.CL_size_t(unsafe.Sizeof(test)), 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(test_buffer)), unsafe.Pointer(&test_buffer))
	if err < 0 {
		println("Couldn't set a 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
	}

	/* Read and print the result */
	err = cl.CLEnqueueReadBuffer(queue, test_buffer, cl.CL_TRUE, 0,
		cl.CL_size_t(unsafe.Sizeof(test)), unsafe.Pointer(&test), 0, nil, nil)
	if err < 0 {
		println("Couldn't read the buffer")
		return
	}
	for i := 0; i < 15; i++ {
		fmt.Printf("0x%X, ", test[i])
	}
	fmt.Printf("0x%X\n", test[15])

	/* Deallocate resources */
	cl.CLReleaseMemObject(test_buffer)
	cl.CLReleaseKernel(kernel)
	cl.CLReleaseCommandQueue(queue)
	cl.CLReleaseProgram(*program)
	cl.CLReleaseContext(context)
}
Beispiel #7
0
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 a float32 = 6.0
	var b float32 = 2.0
	var result float32
	var a_buffer, b_buffer, output_buffer cl.CL_mem

	/* Extension data */
	var sizeofuint cl.CL_uint
	var addr_data interface{}
	var ext_data interface{}
	fp64_ext := "cl_khr_fp64"
	var ext_size cl.CL_size_t
	var options []byte

	/* 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
	}

	/* Obtain the device data */
	if cl.CLGetDeviceInfo(device[0], cl.CL_DEVICE_ADDRESS_BITS,
		cl.CL_size_t(unsafe.Sizeof(sizeofuint)), &addr_data, nil) < 0 {
		println("Couldn't read extension data")
		return
	}
	fmt.Printf("Address width: %v\n", addr_data.(cl.CL_uint))

	/* Define "FP_64" option if doubles are supported */
	cl.CLGetDeviceInfo(device[0], cl.CL_DEVICE_EXTENSIONS,
		0, nil, &ext_size)
	// ext_data = (char*)malloc(ext_size + 1);
	// ext_data[ext_size] = '\0';
	cl.CLGetDeviceInfo(device[0], cl.CL_DEVICE_EXTENSIONS,
		ext_size, &ext_data, nil)
	if strings.Contains(ext_data.(string), fp64_ext) {
		fmt.Printf("The %s extension is supported.\n", fp64_ext)
		options = []byte("-DFP_64 ")
	} else {
		fmt.Printf("The %s extension is not supported. %s\n", fp64_ext, ext_data.(string))
	}

	/* Build the program and create the kernel */
	program = utils.Build_program(context, device[:], PROGRAM_FILE, options)
	kernel = cl.CLCreateKernel(*program, KERNEL_FUNC, &err)
	if err < 0 {
		println("Couldn't create a kernel")
		return
	}

	/* Create CL buffers to hold input and output data */
	a_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_READ_ONLY|
		cl.CL_MEM_COPY_HOST_PTR, cl.CL_size_t(unsafe.Sizeof(a)), unsafe.Pointer(&a), &err)
	if err < 0 {
		println("Couldn't create a memory object")
		return
	}

	b_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_READ_ONLY|
		cl.CL_MEM_COPY_HOST_PTR, cl.CL_size_t(unsafe.Sizeof(b)), unsafe.Pointer(&b), nil)
	output_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_WRITE_ONLY,
		cl.CL_size_t(unsafe.Sizeof(b)), nil, nil)

	/* Create kernel arguments */
	err = cl.CLSetKernelArg(kernel, 0, cl.CL_size_t(unsafe.Sizeof(a_buffer)), unsafe.Pointer(&a_buffer))
	if err < 0 {
		println("Couldn't set a kernel argument")
		return
	}
	cl.CLSetKernelArg(kernel, 1, cl.CL_size_t(unsafe.Sizeof(b_buffer)), unsafe.Pointer(&b_buffer))
	cl.CLSetKernelArg(kernel, 2, cl.CL_size_t(unsafe.Sizeof(output_buffer)), unsafe.Pointer(&output_buffer))

	/* 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
	}

	/* Read and print the result */
	err = cl.CLEnqueueReadBuffer(queue, output_buffer, cl.CL_TRUE, 0,
		cl.CL_size_t(unsafe.Sizeof(result)), unsafe.Pointer(&result), 0, nil, nil)
	if err < 0 {
		println("Couldn't read the output buffer")
		return
	}
	fmt.Printf("The kernel result is %f\n", result)

	/* Deallocate resources */
	cl.CLReleaseMemObject(a_buffer)
	cl.CLReleaseMemObject(b_buffer)
	cl.CLReleaseMemObject(output_buffer)
	cl.CLReleaseKernel(kernel)
	cl.CLReleaseCommandQueue(queue)
	cl.CLReleaseProgram(*program)
	cl.CLReleaseContext(context)
}
Beispiel #8
0
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

	var offset, global_size, local_size [1]cl.CL_size_t

	/* Data and events */
	var data [2]cl.CL_int
	var data_buffer cl.CL_mem

	/* 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]))*2, 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
	}

	/* 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 */
	offset[0] = 0
	global_size[0] = 8
	local_size[0] = 4
	err = cl.CLEnqueueNDRangeKernel(queue, kernel, 1, offset[:], global_size[:], local_size[:], 0, nil, nil)
	if err < 0 {
		println("Couldn't enqueue the kernel")
		return
	}

	/* Read the buffer */
	err = cl.CLEnqueueReadBuffer(queue, data_buffer, cl.CL_TRUE, 0,
		cl.CL_size_t(unsafe.Sizeof(data[0]))*2, unsafe.Pointer(&data[0]), 0, nil, nil)
	if err < 0 {
		println("Couldn't read the buffer")
		return
	}

	fmt.Printf("Increment: %d\n", data[0])
	fmt.Printf("Atomic increment: %d\n", data[1])

	/* Deallocate resources */
	cl.CLReleaseMemObject(data_buffer)
	cl.CLReleaseKernel(kernel)
	cl.CLReleaseCommandQueue(queue)
	cl.CLReleaseProgram(*program)
	cl.CLReleaseContext(context)
}
Beispiel #9
0
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)
}
Beispiel #10
0
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_ints cl.CL_int
	var num_items [1]cl.CL_size_t
	var data [NUM_INTS]cl.CL_int
	var data_buffer cl.CL_mem
	var prof_event cl.CL_event
	var total_time cl.CL_ulong
	var time_start, time_end interface{}

	/* Initialize data */
	for i := 0; i < NUM_INTS; i++ {
		data[i] = cl.CL_int(i)
	}

	/* Set number of data points and work-items */
	num_ints = NUM_INTS
	num_items[0] = NUM_ITEMS

	/* 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_READ_WRITE|cl.CL_MEM_COPY_HOST_PTR,
		cl.CL_size_t(unsafe.Sizeof(data[0]))*NUM_INTS, unsafe.Pointer(&data[0]), &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
	}
	cl.CLSetKernelArg(kernel, 1, cl.CL_size_t(unsafe.Sizeof(num_ints)), unsafe.Pointer(&num_ints))

	/* 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 */
		cl.CLEnqueueNDRangeKernel(queue, kernel, 1, nil, num_items[:],
			nil, 0, nil, &prof_event)
		if err < 0 {
			println("Couldn't enqueue the kernel")
			return
		}

		/* Finish processing the queue and get profiling information */
		cl.CLFinish(queue)
		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)
	}
	fmt.Printf("Average time = %v\n", total_time/NUM_ITERATIONS)

	/* Deallocate resources */
	cl.CLReleaseEvent(prof_event)
	cl.CLReleaseKernel(kernel)
	cl.CLReleaseMemObject(data_buffer)
	cl.CLReleaseCommandQueue(queue)
	cl.CLReleaseProgram(*program)
	cl.CLReleaseContext(context)
}
Beispiel #11
0
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 full_matrix, zero_matrix [80]float32
	var sizeoffloat32 = cl.CL_size_t(unsafe.Sizeof(full_matrix[0]))
	var buffer_origin = [3]cl.CL_size_t{5 * sizeoffloat32, 3, 0}
	var host_origin = [3]cl.CL_size_t{1 * sizeoffloat32, 1, 0}
	var region = [3]cl.CL_size_t{4 * sizeoffloat32, 4, 1}
	var matrix_buffer cl.CL_mem

	/* Initialize data */
	for i := 0; i < 80; i++ {
		full_matrix[i] = float32(i) * 1.0
		zero_matrix[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)
	if program == nil {
		println("Couldn't build program")
		return
	}

	kernel = cl.CLCreateKernel(*program, []byte(KERNEL_FUNC), &err)
	if err < 0 {
		println("Couldn't create a kernel")
		return
	}

	/* Create a buffer to hold 80 floats */
	matrix_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_READ_WRITE|
		cl.CL_MEM_COPY_HOST_PTR, cl.CL_size_t(unsafe.Sizeof(full_matrix)), unsafe.Pointer(&full_matrix[0]), &err)
	if err < 0 {
		println("Couldn't create a buffer object")
		return
	}

	/* Set buffer as argument to the kernel */
	err = cl.CLSetKernelArg(kernel, 0, cl.CL_size_t(unsafe.Sizeof(matrix_buffer)), unsafe.Pointer(&matrix_buffer))
	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 write to buffer */
	err = cl.CLEnqueueWriteBuffer(queue, matrix_buffer, cl.CL_TRUE, 0,
		cl.CL_size_t(unsafe.Sizeof(full_matrix)), unsafe.Pointer(&full_matrix[0]), 0, nil, nil)
	if err < 0 {
		println("Couldn't write to the buffer object")
		return
	}

	/* Enqueue command to read rectangle of data */
	err = cl.CLEnqueueReadBufferRect(queue, matrix_buffer, cl.CL_TRUE,
		buffer_origin, host_origin, region, 10*sizeoffloat32, 0,
		10*sizeoffloat32, 0, unsafe.Pointer(&zero_matrix[0]), 0, nil, nil)
	if err < 0 {
		println("Couldn't read the rectangle from the buffer object")
		return
	}

	/* Display updated buffer */
	for i := 0; i < 8; i++ {
		for j := 0; j < 10; j++ {
			fmt.Printf("%6.1f", zero_matrix[j+i*10])
		}
		fmt.Printf("\n")
	}

	/* Deallocate resources */
	cl.CLReleaseMemObject(matrix_buffer)
	cl.CLReleaseKernel(kernel)
	cl.CLReleaseCommandQueue(queue)
	cl.CLReleaseProgram(*program)
	cl.CLReleaseContext(context)
}
Beispiel #12
0
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)
}
Beispiel #13
0
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 r_coords = [4]float32{2, 1, 3, 4}
	var angles = [4]float32{3 * M_PI / 8, 3 * M_PI / 4, 4 * M_PI / 3, 11 * M_PI / 6}
	var x_coords, y_coords [4]float32
	var r_coords_buffer, angles_buffer,
		x_coords_buffer, y_coords_buffer cl.CL_mem

	/* 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
	}

	/* 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 write-only buffer to hold the output data */
	r_coords_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_READ_ONLY|cl.CL_MEM_COPY_HOST_PTR,
		cl.CL_size_t(unsafe.Sizeof(r_coords)), unsafe.Pointer(&r_coords[0]), &err)
	if err < 0 {
		println("Couldn't create a buffer")
		return
	}
	angles_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_READ_ONLY|cl.CL_MEM_COPY_HOST_PTR,
		cl.CL_size_t(unsafe.Sizeof(angles)), unsafe.Pointer(&angles[0]), &err)
	x_coords_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_READ_WRITE,
		cl.CL_size_t(unsafe.Sizeof(x_coords)), nil, &err)
	y_coords_buffer = cl.CLCreateBuffer(context, cl.CL_MEM_READ_WRITE,
		cl.CL_size_t(unsafe.Sizeof(y_coords)), nil, &err)

	/* Create kernel argument */
	err = cl.CLSetKernelArg(kernel, 0, cl.CL_size_t(unsafe.Sizeof(r_coords_buffer)), unsafe.Pointer(&r_coords_buffer))
	if err < 0 {
		println("Couldn't set a kernel argument")
		return
	}
	cl.CLSetKernelArg(kernel, 1, cl.CL_size_t(unsafe.Sizeof(angles_buffer)), unsafe.Pointer(&angles_buffer))
	cl.CLSetKernelArg(kernel, 2, cl.CL_size_t(unsafe.Sizeof(x_coords_buffer)), unsafe.Pointer(&x_coords_buffer))
	cl.CLSetKernelArg(kernel, 3, cl.CL_size_t(unsafe.Sizeof(y_coords_buffer)), unsafe.Pointer(&y_coords_buffer))

	/* 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
	}

	/* Read and print the result */
	err = cl.CLEnqueueReadBuffer(queue, x_coords_buffer, cl.CL_TRUE, 0,
		cl.CL_size_t(unsafe.Sizeof(x_coords)), unsafe.Pointer(&x_coords), 0, nil, nil)
	if err < 0 {
		println("Couldn't read the buffer")
		return
	}
	cl.CLEnqueueReadBuffer(queue, y_coords_buffer, cl.CL_TRUE, 0,
		cl.CL_size_t(unsafe.Sizeof(y_coords)), unsafe.Pointer(&y_coords), 0, nil, nil)

	/* Display the results */
	for i := 0; i < 4; i++ {
		fmt.Printf("(%6.3f, %6.3f)\n", x_coords[i], y_coords[i])
	}

	/* Deallocate resources */
	cl.CLReleaseMemObject(r_coords_buffer)
	cl.CLReleaseMemObject(angles_buffer)
	cl.CLReleaseMemObject(x_coords_buffer)
	cl.CLReleaseMemObject(y_coords_buffer)
	cl.CLReleaseKernel(kernel)
	cl.CLReleaseCommandQueue(queue)
	cl.CLReleaseProgram(*program)
	cl.CLReleaseContext(context)
}