diff options
author | Torsten Keßler | 2022-12-12 14:18:22 +0100 |
---|---|---|
committer | Torsten Keßler | 2022-12-12 14:18:22 +0100 |
commit | e2184849d6138a29f15df867d7f531db5f9a761a (patch) | |
tree | 1faa9d872771c8a32be2098265af10a899c45f32 /test.c | |
parent | 3a918bf5ee45ad5ca1b3641d7b05143a9c8a3125 (diff) | |
download | aur-e2184849d6138a29f15df867d7f531db5f9a761a.tar.gz |
upgpkg: rocm-opencl-runtime 5.4.0-2
* Remove check() as tests depend on installed hardware,
* instead add simple test case to be run after package installation to
check if it is correctly configured and installed;
* Overwrite default cmake build type to comply with arch's cmake policy.
Diffstat (limited to 'test.c')
-rw-r--r-- | test.c | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/test.c b/test.c new file mode 100644 index 000000000000..1ad3dd203df6 --- /dev/null +++ b/test.c @@ -0,0 +1,136 @@ +#include <stdio.h> +#include <stdlib.h> +#define CL_TARGET_OPENCL_VERSION 300 +#include <CL/cl.h> +#include <math.h> + +static const char *kernel_source="\n" +"__kernel\n" +"void square(__global float *input, __global float *output, uint n)\n" +"{\n" +" uint id = get_global_id(0);\n" +" if(id < n){\n" +" output[id] = input[id]*input[id];\n" +" }\n" +"}\n"; + +int main(int argc, char *argv[]) +{ + size_t n = 1024; + + float *xin = malloc(sizeof *xin * n); + for(size_t i = 0; i < n; i++){ + xin[i] = -1.0f + 2.0f * i / n; + } + + cl_platform_id platform_id; + cl_uint n_platforms; + cl_int err = clGetPlatformIDs(1,&platform_id, &n_platforms); + if(err != CL_SUCCESS){ + fprintf(stderr, "Unable to get platforms\n"); + return 1; + } + + cl_device_id device_id; + cl_uint n_devs; + err = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &n_devs); + if(err != CL_SUCCESS){ + fprintf(stderr, "Unable to get device id\n"); + return 1; + } + + cl_context_properties properties[3]; + properties[0] = CL_CONTEXT_PLATFORM; + properties[1] = (cl_context_properties) platform_id; + properties[2] = 0; + + cl_context context = clCreateContext(properties, 1, &device_id, NULL, NULL, &err); + if(err != CL_SUCCESS){ + fprintf(stderr, "Creating context failed with error %d\n", err); + return 1; + } + + cl_command_queue command_queue = clCreateCommandQueueWithProperties(context, device_id, NULL, &err); + if(err != CL_SUCCESS){ + fprintf(stderr, "Creating command queue failed with error %d\n", err); + return 1; + } + + cl_program program = clCreateProgramWithSource(context, 1, &kernel_source, NULL, &err); + if(err != CL_SUCCESS){ + fprintf(stderr, "Creating program from source failed with error %d\n", err); + return 1; + } + + err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); + if(err != CL_SUCCESS){ + fprintf(stderr, "Compiling program failed with error %d\n", err); + return 1; + } + + cl_kernel kernel = clCreateKernel(program, "square", &err); + if(err != CL_SUCCESS){ + fprintf(stderr, "Creating kernel failed with error %d\n", err); + return 1; + } + + cl_mem x = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof *xin * n, NULL, &err); + if(err != CL_SUCCESS){ + fprintf(stderr, "Allocating read-only memory failed with error code %d\n", err); + return 1; + } + cl_mem y = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof *xin * n, NULL, &err); + if(err != CL_SUCCESS){ + fprintf(stderr, "Allocating write-only memory failed with error code %d\n", err); + return 1; + } + + err = clEnqueueWriteBuffer(command_queue, x, CL_TRUE, 0, sizeof *xin * n, xin, 0, NULL, NULL); + if(err != CL_SUCCESS){ + fprintf(stderr, "Command queue finished with error code: %d\n", err); + return 1; + } + + cl_uint dim = n; + clSetKernelArg(kernel, 0, sizeof x, &x); + clSetKernelArg(kernel, 1, sizeof y, &y); + clSetKernelArg(kernel, 2, sizeof dim, &dim); + + err = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &n, NULL, 0, NULL, NULL); + if(err != CL_SUCCESS){ + fprintf(stderr, "Cannot queue kernel for execution. Error code: %d\n", err); + return 1; + } + err = clFinish(command_queue); + if(err != CL_SUCCESS){ + fprintf(stderr, "Command queue finished with error code: %d\n", err); + return 1; + } + + float *yout = malloc(sizeof *yout * n); + err = clEnqueueReadBuffer(command_queue, y, CL_TRUE, 0, sizeof *yout * n, yout, 0, NULL, NULL); + if(err != CL_SUCCESS){ + fprintf(stderr, "Could not copy memory from device: %d\n", err); + return 1; + } + + float tol = 0.001f; + for(size_t i = 0; i < n; i++){ + if(fabsf(yout[i] - xin[i] * xin[i]) > tol){ + printf("Mismatch at index %zu:\n", i); + printf("Desired: %f\n", xin[i] * xin[i]); + printf("Actual : %f\n", yout[i]); + return 1; + } + } + printf("TESTS PASSED!\n"); + + free(xin); + free(yout); + clReleaseMemObject(x); + clReleaseMemObject(y); + clReleaseProgram(program); + clReleaseKernel(kernel); + clReleaseCommandQueue(command_queue); + clReleaseContext(context); +} |