Add example floating point calculation

This commit is contained in:
Mario Hüttel 2019-03-11 21:58:38 +01:00
parent 9bfe5e0b30
commit 2ac2de2ad0
4 changed files with 75 additions and 22 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
*.o *.o
build build
*.user.*
*.user *.user

View File

@ -12,5 +12,5 @@ message(STATUS "OpenCL libraries: ${OPENCL_LIBRARIES}")
add_executable(${PROJECT_NAME} "main.c") add_executable(${PROJECT_NAME} "main.c")
configure_file("saxpy.cl" "cl_kernels/saxpy.cl" COPYONLY) configure_file("saxpy.cl" "cl_kernels/saxpy.cl" COPYONLY)
target_link_libraries(${PROJECT_NAME} ${OPENCL_LIBRARIES}) target_link_libraries(${PROJECT_NAME} m ${OPENCL_LIBRARIES})
include_directories(${PROJECT_NAME} ${OPENCL_INCLUDE_DIR}) include_directories(${PROJECT_NAME} ${OPENCL_INCLUDE_DIR})

92
main.c
View File

@ -3,7 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <math.h>
void check_error(cl_int error) void check_error(cl_int error)
{ {
@ -42,23 +42,43 @@ cl_program create_program(char *src, cl_context context)
return program; return program;
} }
float test_a[] = {21.321, 213.213, 3213.23}; float *test_a, *test_b;
float test_b[] = {21.21, 13.213, 17.2553};
static void prepare_data()
{
int i;
test_a = malloc(sizeof(float)*1000000000);
test_b = malloc(sizeof(float)*1000000000);
for (i = 0; i < 1000000000; i++) {
test_a[i] = (float)i;
test_b[i] = 0.0f;
}
}
float mult = 1.0f;
int main(void) int main(void)
{ {
int i;
cl_int error; cl_int error;
cl_int ret;
cl_platform_id *platform_ids = NULL; cl_platform_id *platform_ids = NULL;
cl_device_id *device_ids = NULL; cl_device_id *device_ids = NULL;
cl_uint platform_id_count = 0; cl_uint platform_id_count = 0;
cl_uint device_id_count = 0; cl_uint device_id_count = 0;
cl_context_properties context_properties[4];
cl_context context; cl_context context;
cl_program program; cl_program program;
cl_kernel kernel; cl_kernel kernel;
cl_command_queue queue;
cl_mem a_buff, b_buff; cl_mem a_buff, b_buff;
char *source_code = NULL; char *source_code = NULL;
char temp_buff[100];
size_t data_size = 1000000000;
size_t work_size = 64;
prepare_data();
clGetPlatformIDs(0, NULL, &platform_id_count); clGetPlatformIDs(0, NULL, &platform_id_count);
if (platform_id_count == 0) if (platform_id_count == 0)
@ -73,6 +93,11 @@ int main(void)
printf("Platforms available: %u\n", (unsigned int)platform_id_count); printf("Platforms available: %u\n", (unsigned int)platform_id_count);
for (i = 0; i < platform_id_count; i++) {
temp_buff[0] = '\0';
clGetPlatformInfo(platform_ids[i], CL_PLATFORM_NAME, sizeof(temp_buff), temp_buff, NULL);
printf("Platform ID %d: %s\n", i, temp_buff);
}
clGetDeviceIDs(platform_ids[0], CL_DEVICE_TYPE_ALL, 0, NULL, clGetDeviceIDs(platform_ids[0], CL_DEVICE_TYPE_ALL, 0, NULL,
&device_id_count); &device_id_count);
@ -88,16 +113,32 @@ int main(void)
clGetDeviceIDs(platform_ids[0], CL_DEVICE_TYPE_ALL, device_id_count, clGetDeviceIDs(platform_ids[0], CL_DEVICE_TYPE_ALL, device_id_count,
device_ids, NULL); device_ids, NULL);
context_properties[0] = CL_CONTEXT_PLATFORM; for (i = 0; i < device_id_count; i++) {
context_properties[1] = (cl_context_properties)platform_ids[0]; temp_buff[0] = '\0';
context_properties[2] = context_properties[3] = 0; clGetDeviceInfo(device_ids[i], CL_DEVICE_NAME, sizeof(temp_buff), temp_buff, NULL);
printf("Device ID %d: %s\n", i, temp_buff);
}
context = clCreateContext(context_properties, device_id_count, context = clCreateContext(NULL, 1, &device_ids[0], NULL, NULL, &error);
device_ids, NULL,
NULL, &error);
check_error(error); check_error(error);
queue = clCreateCommandQueue(context, device_ids[0], 0, &error);
/* Create memory buffers for data */
a_buff = clCreateBuffer(context, CL_MEM_READ_ONLY |
CL_MEM_COPY_HOST_PTR,
sizeof(test_a[0])*data_size, test_a, &error);
check_error(error);
b_buff = clCreateBuffer(context, CL_MEM_READ_WRITE |
CL_MEM_COPY_HOST_PTR,
sizeof(test_b[0])*data_size, test_b, &error);
check_error(error);
load_kernel_from_file("cl_kernels/saxpy.cl", &source_code); load_kernel_from_file("cl_kernels/saxpy.cl", &source_code);
program = create_program(source_code, context); program = create_program(source_code, context);
@ -108,17 +149,28 @@ int main(void)
kernel = clCreateKernel(program, "SAXPY", &error); kernel = clCreateKernel(program, "SAXPY", &error);
/* Setting the arguments */
clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&a_buff);
clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&b_buff);
clSetKernelArg(kernel, 2, sizeof(float), (void *)&mult);
/* Create memory buffers for data */ ret = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &data_size, &work_size, 0, NULL, NULL);
a_buff = clCreateBuffer(context, CL_MEM_READ_ONLY | check_error(ret);
CL_MEM_COPY_HOST_PTR,
sizeof(test_a), test_a, &error);
check_error(error);
b_buff = clCreateBuffer(context, CL_MEM_READ_WRITE | clEnqueueReadBuffer(queue, b_buff, CL_TRUE, 0, 1000000000*sizeof(float), test_b, 0, NULL, NULL);
CL_MEM_COPY_HOST_PTR,
sizeof(test_b), test_b, &error); ret = clFlush(queue);
check_error(error); ret = clFinish(queue);
ret = clReleaseKernel(kernel);
ret = clReleaseProgram(program);
ret = clReleaseMemObject(a_buff);
ret = clReleaseMemObject(b_buff);
ret = clReleaseCommandQueue(queue);
ret = clReleaseContext(context);
free(test_a);
free(test_b);
free(platform_ids);
free(device_ids);
return 0; return 0;
} }