int main(void){ cl_int err; cl_context context; cl_device_id did; cl_command_queue queue; CHECK_CL_ERROR(poclu_get_any_device(&context, &did, &queue)); TEST_ASSERT( context ); TEST_ASSERT( did ); TEST_ASSERT( queue ); size_t program_size = strlen(program_src); char* program_buffer = program_src; cl_program program = clCreateProgramWithSource(context, 1, (const char**)&program_buffer, &program_size, &err); //clCreateProgramWithSource for the program with #include failed CHECK_OPENCL_ERROR_IN("clCreateProgramWithSource"); err = clBuildProgram(program, 1, &did, NULL, NULL, NULL); TEST_ASSERT(err == CL_BUILD_PROGRAM_FAILURE); CHECK_CL_ERROR (clReleaseCommandQueue (queue)); CHECK_CL_ERROR (clReleaseProgram (program)); CHECK_CL_ERROR (clReleaseContext (context)); CHECK_CL_ERROR (clUnloadCompiler ()); return EXIT_SUCCESS; }
enum piglit_result piglit_cl_test(const int argc, const char** argv, const struct piglit_cl_api_test_config* config, const struct piglit_cl_api_test_env* env) { cl_int errNo; cl_program program; /*** Normal usage ***/ program = piglit_cl_build_program_with_source(env->context, 1, &dummy_kernel, ""); /* Always returns CL_SUCCESS */ errNo = clUnloadCompiler(); if(!piglit_cl_check_error(errNo, CL_SUCCESS)) { fprintf(stderr, "Failed (error code: %s): Unload compiler.\n", piglit_cl_get_error_name(errNo)); return PIGLIT_FAIL; } /* Building again reloads compiler */ clReleaseProgram(program); program = piglit_cl_build_program_with_source(env->context, 1, &dummy_kernel, ""); clReleaseProgram(program); return PIGLIT_PASS; }
/// Requests that the platform unload any compiler resources. void unload_compiler() { #ifdef BOOST_COMPUTE_CL_VERSION_1_2 clUnloadPlatformCompiler(m_platform); #else clUnloadCompiler(); #endif }
cl_int WINAPI wine_clUnloadCompiler(void) { cl_int ret; TRACE("()\n"); ret = clUnloadCompiler(); TRACE("()=%d\n", ret); return ret; }
int main(int argc, char **argv) { cl_int err; const char *krn_src; cl_program program; cl_context ctx; cl_command_queue queue; cl_device_id did; cl_kernel kernel; CHECK_CL_ERROR(poclu_get_any_device(&ctx, &did, &queue)); TEST_ASSERT(ctx); TEST_ASSERT(did); TEST_ASSERT(queue); krn_src = poclu_read_file(SRCDIR "/tests/runtime/test_clCreateKernelsInProgram.cl"); TEST_ASSERT(krn_src); program = clCreateProgramWithSource(ctx, 1, &krn_src, NULL, &err); CHECK_OPENCL_ERROR_IN("clCreateProgramWithSource"); CHECK_CL_ERROR(clBuildProgram(program, 0, NULL, NULL, NULL, NULL)); kernel = clCreateKernel(program, NULL, &err); TEST_ASSERT(err == CL_INVALID_VALUE); TEST_ASSERT(kernel == NULL); kernel = clCreateKernel(program, "nonexistent_kernel", &err); TEST_ASSERT(err == CL_INVALID_KERNEL_NAME); TEST_ASSERT(kernel == NULL); CHECK_CL_ERROR (clReleaseCommandQueue (queue)); CHECK_CL_ERROR (clReleaseProgram (program)); CHECK_CL_ERROR (clReleaseContext (ctx)); CHECK_CL_ERROR (clUnloadCompiler ()); free ((void *)krn_src); printf("OK\n"); return 0; }
cl_program get_program_from_file(cl_context context, cl_device_id device, const char *filename) { FILE *fp; int size; char *buffer; cl_int err; cl_program program; char buf[100000]; /* Read file into buffer. */ fp = fopen(filename, "r"); if (fp == NULL) { fprintf(stderr, "Failed to open file: %s\n", filename); exit(1); } fseek(fp, 0, SEEK_END); size = ftell(fp); rewind(fp); buffer = (char *) malloc((size+1) * sizeof(char)); buffer[size] = '\0'; fread(buffer, sizeof(char), size, fp); fclose(fp); /* Create program. */ program = clCreateProgramWithSource(context, 1, &buffer, NULL, &err); CL_CHECK_ERR(err); /* Build program. */ if (clBuildProgram(program, 1, &device, "", NULL, NULL) != CL_SUCCESS) { clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 100000, buf, NULL); fprintf(stderr, "CL Compilation failed:\n%s", buffer); exit(1); } free(buffer); err = clUnloadCompiler(); CL_CHECK_ERR(err); return program; }
int main(int argc, char **argv) { cl_platform_id platforms[100]; cl_uint platforms_n = 0; CL_CHECK(clGetPlatformIDs(100, platforms, &platforms_n)); printf("=== %d OpenCL platform(s) found: ===\n", platforms_n); for (int i=0; i<platforms_n; i++) { char buffer[10240]; printf(" -- %d --\n", i); CL_CHECK(clGetPlatformInfo(platforms[i], CL_PLATFORM_PROFILE, 10240, buffer, NULL)); printf(" PROFILE = %s\n", buffer); CL_CHECK(clGetPlatformInfo(platforms[i], CL_PLATFORM_VERSION, 10240, buffer, NULL)); printf(" VERSION = %s\n", buffer); CL_CHECK(clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, 10240, buffer, NULL)); printf(" NAME = %s\n", buffer); CL_CHECK(clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, 10240, buffer, NULL)); printf(" VENDOR = %s\n", buffer); CL_CHECK(clGetPlatformInfo(platforms[i], CL_PLATFORM_EXTENSIONS, 10240, buffer, NULL)); printf(" EXTENSIONS = %s\n", buffer); } if (platforms_n == 0) return 1; cl_device_id devices[100]; cl_uint devices_n = 0; // CL_CHECK(clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 100, devices, &devices_n)); CL_CHECK(clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_GPU, 100, devices, &devices_n)); printf("=== %d OpenCL device(s) found on platform:\n", platforms_n); for (int i=0; i<devices_n; i++) { char buffer[10240]; cl_uint buf_uint; cl_ulong buf_ulong; printf(" -- %d --\n", i); CL_CHECK(clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(buffer), buffer, NULL)); printf(" DEVICE_NAME = %s\n", buffer); CL_CHECK(clGetDeviceInfo(devices[i], CL_DEVICE_VENDOR, sizeof(buffer), buffer, NULL)); printf(" DEVICE_VENDOR = %s\n", buffer); CL_CHECK(clGetDeviceInfo(devices[i], CL_DEVICE_VERSION, sizeof(buffer), buffer, NULL)); printf(" DEVICE_VERSION = %s\n", buffer); CL_CHECK(clGetDeviceInfo(devices[i], CL_DRIVER_VERSION, sizeof(buffer), buffer, NULL)); printf(" DRIVER_VERSION = %s\n", buffer); CL_CHECK(clGetDeviceInfo(devices[i], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(buf_uint), &buf_uint, NULL)); printf(" DEVICE_MAX_COMPUTE_UNITS = %u\n", (unsigned int)buf_uint); CL_CHECK(clGetDeviceInfo(devices[i], CL_DEVICE_MAX_CLOCK_FREQUENCY, sizeof(buf_uint), &buf_uint, NULL)); printf(" DEVICE_MAX_CLOCK_FREQUENCY = %u\n", (unsigned int)buf_uint); CL_CHECK(clGetDeviceInfo(devices[i], CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(buf_ulong), &buf_ulong, NULL)); printf(" DEVICE_GLOBAL_MEM_SIZE = %llu\n", (unsigned long long)buf_ulong); } if (devices_n == 0) return 1; cl_context context; context = CL_CHECK_ERR(clCreateContext(NULL, 1, devices, &pfn_notify, NULL, &_err)); const char *program_source[] = { "__kernel void simple_demo(__global int *src, __global int *dst, int factor)\n", "{\n", " int i = get_global_id(0);\n", " dst[i] = src[i] * factor;\n", "}\n" }; cl_program program; program = CL_CHECK_ERR(clCreateProgramWithSource(context, sizeof(program_source)/sizeof(*program_source), program_source, NULL, &_err)); if (clBuildProgram(program, 1, devices, "", NULL, NULL) != CL_SUCCESS) { char buffer[10240]; clGetProgramBuildInfo(program, devices[0], CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, NULL); fprintf(stderr, "CL Compilation failed:\n%s", buffer); abort(); } CL_CHECK(clUnloadCompiler()); cl_mem input_buffer; input_buffer = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int)*NUM_DATA, NULL, &_err)); cl_mem output_buffer; output_buffer = CL_CHECK_ERR(clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int)*NUM_DATA, NULL, &_err)); int factor = 2; cl_kernel kernel; kernel = CL_CHECK_ERR(clCreateKernel(program, "simple_demo", &_err)); CL_CHECK(clSetKernelArg(kernel, 0, sizeof(input_buffer), &input_buffer)); CL_CHECK(clSetKernelArg(kernel, 1, sizeof(output_buffer), &output_buffer)); CL_CHECK(clSetKernelArg(kernel, 2, sizeof(factor), &factor)); cl_command_queue queue; queue = CL_CHECK_ERR(clCreateCommandQueue(context, devices[0], 0, &_err)); for (int i=0; i<NUM_DATA; i++) { CL_CHECK(clEnqueueWriteBuffer(queue, input_buffer, CL_TRUE, i*sizeof(int), sizeof(int), &i, 0, NULL, NULL)); } cl_event kernel_completion; size_t global_work_size[1] = { NUM_DATA }; CL_CHECK(clEnqueueNDRangeKernel(queue, kernel, 1, NULL, global_work_size, NULL, 0, NULL, &kernel_completion)); CL_CHECK(clWaitForEvents(1, &kernel_completion)); CL_CHECK(clReleaseEvent(kernel_completion)); printf("Result:"); for (int i=0; i<NUM_DATA; i++) { int data; CL_CHECK(clEnqueueReadBuffer(queue, output_buffer, CL_TRUE, i*sizeof(int), sizeof(int), &data, 0, NULL, NULL)); printf(" %d", data); } printf("\n"); CL_CHECK(clReleaseMemObject(input_buffer)); CL_CHECK(clReleaseMemObject(output_buffer)); CL_CHECK(clReleaseKernel(kernel)); CL_CHECK(clReleaseProgram(program)); CL_CHECK(clReleaseContext(context)); return 0; }
int main(void){ cl_int err; cl_platform_id platforms[MAX_PLATFORMS]; cl_uint nplatforms; cl_device_id devices[MAX_DEVICES + 1]; // + 1 for duplicate test cl_device_id device_id0; cl_uint num_devices; size_t i; size_t num_binaries; const unsigned char **binaries = NULL; size_t *binary_sizes = NULL; size_t num_bytes_copied; cl_int binary_statuses[MAX_BINARIES]; cl_int binary_statuses2[MAX_BINARIES]; cl_program program = NULL; cl_program program_with_binary = NULL; err = clGetPlatformIDs(MAX_PLATFORMS, platforms, &nplatforms); CHECK_OPENCL_ERROR_IN("clGetPlatformIDs"); if (!nplatforms) return EXIT_FAILURE; err = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_ALL, MAX_DEVICES, devices, &num_devices); CHECK_OPENCL_ERROR_IN("clGetDeviceIDs"); cl_context context = clCreateContext(NULL, num_devices, devices, NULL, NULL, &err); CHECK_OPENCL_ERROR_IN("clCreateContext"); size_t kernel_size = strlen(kernel); char* kernel_buffer = kernel; program = clCreateProgramWithSource(context, 1, (const char**)&kernel_buffer, &kernel_size, &err); CHECK_OPENCL_ERROR_IN("clCreateProgramWithSource"); err = clBuildProgram(program, num_devices, devices, NULL, NULL, NULL); CHECK_OPENCL_ERROR_IN("clBuildProgram"); err = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, 0, 0, &num_binaries); CHECK_OPENCL_ERROR_IN("clGetProgramInfo"); num_binaries = num_binaries/sizeof(size_t); binary_sizes = (size_t*)malloc(num_binaries * sizeof(size_t)); binaries = (const unsigned char**)calloc(num_binaries, sizeof(unsigned char*)); err = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, num_binaries*sizeof(size_t), binary_sizes , &num_bytes_copied); CHECK_OPENCL_ERROR_IN("clGetProgramInfo"); for (i = 0; i < num_binaries; ++i) binaries[i] = (const unsigned char*) malloc(binary_sizes[i] * sizeof(const unsigned char)); err = clGetProgramInfo(program, CL_PROGRAM_BINARIES, num_binaries*sizeof(char*), binaries, &num_bytes_copied); CHECK_OPENCL_ERROR_IN("clGetProgramInfo"); cl_uint num = num_binaries < num_devices ? num_binaries : num_devices; if (num == 0) { err = !CL_SUCCESS; goto FREE_AND_EXIT; } program_with_binary = clCreateProgramWithBinary(context, num, devices, binary_sizes, binaries, binary_statuses, &err); CHECK_OPENCL_ERROR_IN("clCreateProgramWithBinary"); for (i = 0; i < num; ++i) { cl_program_binary_type bin_type = 0; err = clGetProgramBuildInfo(program_with_binary, devices[i], CL_PROGRAM_BINARY_TYPE, sizeof(bin_type), (void *)&bin_type, NULL); CHECK_OPENCL_ERROR_IN("get program binary type"); /* cl_program_binary_type */ switch(bin_type) { case CL_PROGRAM_BINARY_TYPE_NONE: /*0x0*/ fprintf(stderr, "program binary type: CL_PROGRAM_BINARY_TYPE_NONE\n"); break; case CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT: /*0x1*/ fprintf(stderr, "program binary type: CL_PROGRAM_BINARY_TYPE_COMPILED_OBJECT\n"); break; case CL_PROGRAM_BINARY_TYPE_LIBRARY: /*0x2*/ fprintf(stderr, "program binary type: CL_PROGRAM_BINARY_TYPE_LIBRARY\n"); break; case CL_PROGRAM_BINARY_TYPE_EXECUTABLE: /*0x4*/ fprintf(stderr, "program binary type: CL_PROGRAM_BINARY_TYPE_EXECUTABLE\n"); break; } } err = clReleaseProgram(program_with_binary); CHECK_OPENCL_ERROR_IN("clReleaseProgram"); for (i = 0; i < num; i++) { if (binary_statuses[i] != CL_SUCCESS) { err = !CL_SUCCESS; goto FREE_AND_EXIT; } } // negative test1: invalid device device_id0 = devices[0]; devices[0] = NULL; // invalid device program_with_binary = clCreateProgramWithBinary(context, num, devices, binary_sizes, binaries, binary_statuses, &err); if (err != CL_INVALID_DEVICE || program_with_binary != NULL) { err = !CL_SUCCESS; goto FREE_AND_EXIT; } err = CL_SUCCESS; devices[0] = device_id0; for (i = 0; i < num_binaries; ++i) free((void*)binaries[i]); free(binary_sizes); free(binaries); // negative test2: duplicate device num_binaries = 2; devices[1] = devices[0]; // duplicate binary_sizes = (size_t*)malloc(num_binaries * sizeof(size_t)); binaries = (const unsigned char**)calloc(num_binaries, sizeof(unsigned char*)); err = clGetProgramInfo(program, CL_PROGRAM_BINARY_SIZES, 1*sizeof(size_t), binary_sizes , &num_bytes_copied); CHECK_OPENCL_ERROR_IN("clGetProgramInfo"); binary_sizes[1] = binary_sizes[0]; binaries[0] = (const unsigned char*) malloc(binary_sizes[0] * sizeof(const unsigned char)); binaries[1] = (const unsigned char*) malloc(binary_sizes[1] * sizeof(const unsigned char)); err = clGetProgramInfo(program, CL_PROGRAM_BINARIES, 1 * sizeof(char*), binaries, &num_bytes_copied); CHECK_OPENCL_ERROR_IN("clGetProgramInfo"); memcpy((void*)binaries[1], (void*)binaries[0], binary_sizes[0]); program_with_binary = clCreateProgramWithBinary(context, 2, devices, binary_sizes, binaries, binary_statuses2, &err); if (err != CL_INVALID_DEVICE || program_with_binary != NULL) { err = !CL_SUCCESS; goto FREE_AND_EXIT; } err = CL_SUCCESS; FREE_AND_EXIT: // Free resources for (i = 0; i < num_binaries; ++i) if (binaries) if(binaries[i]) free((void*)binaries[i]); if (binary_sizes) free(binary_sizes); if (binaries) free(binaries); if (program) CHECK_CL_ERROR (clReleaseProgram (program)); if (program_with_binary) CHECK_CL_ERROR (clReleaseProgram (program_with_binary)); if (context) CHECK_CL_ERROR (clReleaseContext (context)); CHECK_CL_ERROR (clUnloadCompiler ()); return err == CL_SUCCESS ? EXIT_SUCCESS : EXIT_FAILURE; }
void __attribute__((__constructor__)) _libstdcl_init() #endif { int i; int n; cl_platform_id platformid; int enable; cl_uint ndev; char env_max_ndev[256]; int lock_key; DEBUG(__FILE__,__LINE__,"_libstdcl_init() called"); /* * set _proc_cl struct */ #ifndef _WIN64 pid_t pid = getpid(); DEBUG(__FILE__,__LINE__,"_libstdcl_init: pid=%d\n",pid); char procexe[256]; snprintf(procexe,256,"/proc/%d/exe",pid); struct stat st; if (stat(procexe,&st)) ERROR(__FILE__,__LINE__,"stat procexe failed"); procelf_fd = open(procexe,O_RDONLY); if (procelf_fd < 0) { ERROR(__FILE__,__LINE__,"opening procexe failed"); } else { procelf = mmap(0,st.st_size,PROT_READ,MAP_PRIVATE,procelf_fd,0); procelf_sz = st.st_size; DEBUG(__FILE__,__LINE__,"_libstdcl_init: procelf size %d bytes\n", st.st_size); // printf("procelf ptr %p %d\n",procelf,errno); fflush(stdout); #if defined(__x86_64__) Elf64_Ehdr* elf = (Elf64_Ehdr*)procelf; Elf64_Shdr* p_shdr = procelf + elf->e_shoff; #elif defined(__i386__) Elf32_Ehdr* elf = (Elf32_Ehdr*)procelf; Elf32_Shdr* p_shdr = procelf + elf->e_shoff; #endif char buf[EI_NIDENT+1]; strncpy(buf,elf->e_ident,EI_NIDENT); DEBUG(__FILE__,__LINE__,"_libstdcl_init: e_ident|%s|\n",buf); // printf("number of section headers %d\n",elf->e_shnum); char* shstr = (char*)procelf + p_shdr[elf->e_shstrndx].sh_offset; // printf("sh str table index %d\n",elf->e_shstrndx); // p_shdr += 1; /* skip first section */ for(n=1;n<elf->e_shnum;n++) { DEBUG(__FILE__,__LINE__, "section offset in img %d bytes (%s) size %d\n", p_shdr->sh_offset, shstr+p_shdr->sh_name,p_shdr->sh_size ); if (!strncmp(shstr+p_shdr->sh_name,".clprgs",7)) { _proc_cl.clprgs=(struct clprgs_entry*)(procelf+p_shdr->sh_offset); _proc_cl.clprgs_n=p_shdr->sh_size/__clprgs_entry_sz; } else if (!strncmp(shstr+p_shdr->sh_name,".cltexts",8)) { _proc_cl.cltexts = (char*)(procelf + p_shdr->sh_offset); _proc_cl.cltexts_sz = p_shdr->sh_size; } else if (!strncmp(shstr+p_shdr->sh_name,".clprgb",7)) { _proc_cl.clprgb=(struct clprgb_entry*)(procelf+p_shdr->sh_offset); _proc_cl.clprgb_n=p_shdr->sh_size/__clprgb_entry_sz; } else if (!strncmp(shstr+p_shdr->sh_name,".cltextb",8)) { _proc_cl.cltextb = (char*)(procelf + p_shdr->sh_offset); _proc_cl.cltextb_sz = p_shdr->sh_size; } else if (!strncmp(shstr+p_shdr->sh_name,".clstrtab",9)) { _proc_cl.clstrtab = (char*)(procelf + p_shdr->sh_offset); _proc_cl.clstrtab_sz = p_shdr->sh_size; } p_shdr += 1; } } DEBUG(__FILE__,__LINE__,"_libstdcl_init: procelf cl sections:" " %p %p %p %p %p\n", _proc_cl.clprgs, _proc_cl.cltexts, _proc_cl.clprgb, _proc_cl.cltextb,_proc_cl.clstrtab ); #endif #if(0) /* * get platform information */ cl_platform_id* platforms = 0; cl_uint nplatforms; char info[1024]; clGetPlatformIDs(0,0,&nplatforms); //printf("XXX %d\n",nplatforms); if (nplatforms) { platforms = (cl_platform_id*)malloc(nplatforms*sizeof(cl_platform_id)); clGetPlatformIDs(nplatforms,platforms,0); for(i=0;i<nplatforms;i++) { char info[1024]; DEBUG(__FILE__,__LINE__,"_libstdcl_init: available platform:"); clGetPlatformInfo(platforms[i],CL_PLATFORM_PROFILE,1024,info,0); DEBUG(__FILE__,__LINE__, "_libstdcl_init: [%p]CL_PLATFORM_PROFILE=%s",platforms[i],info); clGetPlatformInfo(platforms[i],CL_PLATFORM_VERSION,1024,info,0); DEBUG(__FILE__,__LINE__, "_libstdcl_init: [%p]CL_PLATFORM_VERSION=%s",platforms[i],info); clGetPlatformInfo(platforms[i],CL_PLATFORM_NAME,1024,info,0); DEBUG(__FILE__,__LINE__, "_libstdcl_init: [%p]CL_PLATFORM_NAME=%s",platforms[i],info); clGetPlatformInfo(platforms[i],CL_PLATFORM_VENDOR,1024,info,0); DEBUG(__FILE__,__LINE__, "_libstdcl_init: [%p]CL_PLATFORM_VENDOR=%s",platforms[i],info); clGetPlatformInfo(platforms[i],CL_PLATFORM_EXTENSIONS,1024,info,0); DEBUG(__FILE__,__LINE__, "_libstdcl_init: [%p]CL_PLATFORM_EXTENSIONS=%s",platforms[i],info); } } else { WARN(__FILE__,__LINE__, "_libstdcl_init: no platforms found, continue and hope for the best"); } #endif /* * initialize stddev (all CL devices) */ DEBUG(__FILE__,__LINE__,"clinit: initialize stddev"); stddev = 0; ndev = 0; /* this is a special case that implies all available -DAR */ enable = 1; lock_key = 0; if (getenv("STDDEV")) enable = atoi(getenv("STDDEV")); if (enable) { char name[256]; if (getenv("STDDEV_PLATFORM_NAME")) strncpy(name,getenv("STDDEV_PLATFORM_NAME"),256); else name[0]='\0'; if (getenv("STDDEV_MAX_NDEV")) ndev = atoi(getenv("STDDEV_MAX_NDEV")); if (getenv("STDDEV_LOCK")) lock_key = atoi(getenv("STDDEV_LOCK")); stddev = clcontext_create(name,CL_DEVICE_TYPE_ALL,ndev,0,lock_key); } DEBUG(__FILE__,__LINE__,"back from clcontext_create\n"); /* * initialize stdcpu (all CPU CL devices) */ DEBUG(__FILE__,__LINE__,"clinit: initialize stdcpu"); stdcpu = 0; ndev = 0; /* this is a special case that implies all available -DAR */ enable = 1; lock_key = 0; if (getenv("STDCPU")) enable = atoi(getenv("STDCPU")); if (enable) { char name[256]; if (getenv("STDCPU_PLATFORM_NAME")) strncpy(name,getenv("STDCPU_PLATFORM_NAME"),256); else name[0]='\0'; if (getenv("STDCPU_MAX_NDEV")) ndev = atoi(getenv("STDCPU_MAX_NDEV")); if (getenv("STDCPU_LOCK")) lock_key = atoi(getenv("STDCPU_LOCK")); stdcpu = clcontext_create(name,CL_DEVICE_TYPE_CPU,ndev,0,lock_key); } DEBUG(__FILE__,__LINE__,"back from clcontext_create\n"); /* * initialize stdgpu (all GPU CL devices) */ DEBUG(__FILE__,__LINE__,"clinit: initialize stdgpu"); /* if (!__getenv_token("STDGPU",0,env_max_ndev,256)) { enable = ndev = atoi(env_max_ndev); } else { ndev = 0; enable = 1; } */ stdgpu = 0; ndev = 0; /* this is a special case that implies all available -DAR */ enable = 1; lock_key = 0; if (getenv("STDGPU")) enable = atoi(getenv("STDGPU")); if (enable) { char name[256]; if (getenv("STDGPU_PLATFORM_NAME")) strncpy(name,getenv("STDGPU_PLATFORM_NAME"),256); else name[0]='\0'; if (getenv("STDGPU_MAX_NDEV")) ndev = atoi(getenv("STDGPU_MAX_NDEV")); if (getenv("STDGPU_LOCK")) lock_key = atoi(getenv("STDGPU_LOCK")); stdgpu = clcontext_create(name,CL_DEVICE_TYPE_GPU,ndev,0,lock_key); } DEBUG(__FILE__,__LINE__,"back from clcontext_create\n"); /* * initialize stdrpu (all RPU CL devices) */ /* XXX old style, need to update -DAR if (!__getenv_token("STDRPU",0,env_max_ndev,256)) { enable = ndev = atoi(env_max_ndev); } else { ndev = 0; enable = 1; } stdrpu = 0; if (enable) { platformid = _select_platformid(nplatforms,platforms,"STDRPU"); if (platformid != (cl_platform_id)(-1)) { DEBUG(__FILE__,__LINE__, "_libstdcl_init: stdrpu platformid %p",platformid); stdrpu = clcontext_create(platformid,CL_DEVICE_TYPE_RPU,ndev,0); } } */ /* char buf[256]; if (!__getenv_token("COPRTHR","log_automatic_kernels",buf,256)) { __log_automatic_kernels_filename = (char*)malloc(256+6); if (!strncasecmp(buf,"log_automatic_kernels",256)) { snprintf( __log_automatic_kernels_filename,256+6, "coprthr.autokern.log.%d",getpid()); } else { snprintf(__log_automatic_kernels_filename,256+6,"%s.%d",buf,getpid()); } DEBUG(__FILE__,__LINE__,"log_automatic_kernels written to %s", __log_automatic_kernels_filename); } */ // char buf[256]; if (getenv("COPRTHR_LOG_AUTOKERN")) { __log_automatic_kernels_filename = (char*)malloc(256+6); // if (!strncasecmp(buf,"log_automatic_kernels",256)) { snprintf( __log_automatic_kernels_filename,256+6, "coprthr.autokern.log.%d",getpid()); // } else { // snprintf(__log_automatic_kernels_filename,256+6,"%s.%d",buf,getpid()); // } DEBUG(__FILE__,__LINE__,"log_automatic_kernels written to %s", __log_automatic_kernels_filename); } clUnloadCompiler(); }
int main(int argc, char **argv) { /* test name */ char name[] = "test_image_query_funcs"; size_t global_work_size[1] = { 1 }, local_work_size[1]= { 1 }; size_t srcdir_length, name_length, filename_size; char *filename = NULL; char *source = NULL; cl_device_id devices[1]; cl_context context = NULL; cl_command_queue queue = NULL; cl_program program = NULL; cl_kernel kernel = NULL; cl_int err; /* image parameters */ cl_uchar4 *imageData; cl_image_format image_format; cl_image_desc image2_desc, image3_desc; printf("Running test %s...\n", name); memset(&image2_desc, 0, sizeof(cl_image_desc)); image2_desc.image_type = CL_MEM_OBJECT_IMAGE2D; image2_desc.image_width = 2; image2_desc.image_height = 4; memset(&image3_desc, 0, sizeof(cl_image_desc)); image3_desc.image_type = CL_MEM_OBJECT_IMAGE3D; image3_desc.image_width = 2; image3_desc.image_height = 4; image3_desc.image_depth = 8; image_format.image_channel_order = CL_RGBA; image_format.image_channel_data_type = CL_UNSIGNED_INT8; imageData = (cl_uchar4*)malloc (4 * 4 * sizeof(cl_uchar4)); TEST_ASSERT (imageData != NULL && "out of host memory\n"); memset (imageData, 1, 4*4*sizeof(cl_uchar4)); /* determine file name of kernel source to load */ srcdir_length = strlen(SRCDIR); name_length = strlen(name); filename_size = srcdir_length + name_length + 16; filename = (char *)malloc(filename_size + 1); TEST_ASSERT (filename != NULL && "out of host memory\n"); snprintf(filename, filename_size, "%s/%s.cl", SRCDIR, name); /* read source code */ source = poclu_read_file (filename); TEST_ASSERT (source != NULL && "Kernel .cl not found."); /* setup an OpenCL context and command queue using default device */ context = poclu_create_any_context(); TEST_ASSERT (context != NULL && "clCreateContextFromType call failed\n"); cl_sampler external_sampler = clCreateSampler ( context, CL_FALSE, CL_ADDRESS_NONE, CL_FILTER_NEAREST, &err); CHECK_OPENCL_ERROR_IN ("clCreateSampler"); CHECK_CL_ERROR (clGetContextInfo (context, CL_CONTEXT_DEVICES, sizeof (cl_device_id), devices, NULL)); queue = clCreateCommandQueue (context, devices[0], 0, &err); CHECK_OPENCL_ERROR_IN ("clCreateCommandQueue"); /* Create image */ cl_mem image2 = clCreateImage (context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, &image_format, &image2_desc, imageData, &err); CHECK_OPENCL_ERROR_IN ("clCreateImage image2"); cl_mem image3 = clCreateImage (context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, &image_format, &image3_desc, imageData, &err); CHECK_OPENCL_ERROR_IN ("clCreateImage image3"); unsigned color[4] = { 2, 9, 11, 7 }; size_t orig[3] = { 0, 0, 0 }; size_t reg[3] = { 2, 4, 1 }; err = clEnqueueFillImage (queue, image2, color, orig, reg, 0, NULL, NULL); CHECK_OPENCL_ERROR_IN ("clCreateImage image3"); /* create and build program */ program = clCreateProgramWithSource (context, 1, (const char **)&source, NULL, &err); CHECK_OPENCL_ERROR_IN ("clCreateProgramWithSource"); err = clBuildProgram (program, 0, NULL, NULL, NULL, NULL); CHECK_OPENCL_ERROR_IN ("clBuildProgram"); /* execute the kernel with give name */ kernel = clCreateKernel (program, name, NULL); CHECK_OPENCL_ERROR_IN ("clCreateKernel"); err = clSetKernelArg (kernel, 0, sizeof (cl_mem), &image2); CHECK_OPENCL_ERROR_IN ("clSetKernelArg 0"); err = clSetKernelArg (kernel, 1, sizeof (cl_mem), &image3); CHECK_OPENCL_ERROR_IN ("clSetKernelArg 1"); err = clSetKernelArg (kernel, 2, sizeof (cl_sampler), &external_sampler); CHECK_OPENCL_ERROR_IN ("clSetKernelArg 2"); err = clEnqueueNDRangeKernel (queue, kernel, 1, NULL, global_work_size, local_work_size, 0, NULL, NULL); CHECK_OPENCL_ERROR_IN ("clEnqueueNDRangeKernel"); err = clFinish (queue); CHECK_OPENCL_ERROR_IN ("clFinish"); clReleaseMemObject (image2); clReleaseMemObject (image3); clReleaseKernel (kernel); clReleaseProgram (program); clReleaseCommandQueue (queue); clReleaseSampler (external_sampler); clUnloadCompiler (); clReleaseContext (context); free (source); free (filename); free (imageData); printf("OK\n"); return 0; }
int main(int argc, char **argv) { cl_context ctx; cl_command_queue q; // root device, all devices #define NUMDEVS 6 cl_device_id rootdev, alldevs[NUMDEVS]; // pointers to the sub devices of the partitions EQUALLY and BY_COUNTS // respectively cl_device_id *eqdev = alldevs + 1, *countdev = alldevs + 4; cl_uint max_cus, max_subs, split; cl_uint i, j; cl_int err = poclu_get_any_device(&ctx, &rootdev, &q); CHECK_OPENCL_ERROR_IN("poclu_get_any_device"); TEST_ASSERT( ctx ); TEST_ASSERT( rootdev ); TEST_ASSERT( q ); alldevs[0] = rootdev; err = clGetDeviceInfo(rootdev, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(max_cus), &max_cus, NULL); CHECK_OPENCL_ERROR_IN("CL_DEVICE_MAX_COMPUTE_UNITS"); if (max_cus < 2) { printf("This test requires a cl device with at least 2 compute units" " (a dual-core or better CPU)\n"); return 1; } err = clGetDeviceInfo(rootdev, CL_DEVICE_PARTITION_MAX_SUB_DEVICES, sizeof(max_subs), &max_subs, NULL); CHECK_OPENCL_ERROR_IN("CL_DEVICE_PARTITION_MAX_SUB_DEVICES"); // test fails without possible sub-devices, e.g. with basic pocl device TEST_ASSERT(max_subs > 1); cl_device_partition_property *dev_pt; size_t dev_pt_size; err = clGetDeviceInfo(rootdev, CL_DEVICE_PARTITION_PROPERTIES, 0, NULL, &dev_pt_size); CHECK_OPENCL_ERROR_IN("CL_DEVICE_PARTITION_PROPERTIES size"); dev_pt = malloc(dev_pt_size); TEST_ASSERT(dev_pt); err = clGetDeviceInfo(rootdev, CL_DEVICE_PARTITION_PROPERTIES, dev_pt_size, dev_pt, NULL); CHECK_OPENCL_ERROR_IN("CL_DEVICE_PARTITION_PROPERTIES"); j = dev_pt_size / sizeof (*dev_pt); // number of partition types // check that partition types EQUALLY and BY_COUNTS are supported int found = 0; for (i = 0; i < j; ++i) { if (dev_pt[i] == CL_DEVICE_PARTITION_EQUALLY || dev_pt[i] == CL_DEVICE_PARTITION_BY_COUNTS) ++found; } TEST_ASSERT(found == 2); // here we will store the partition types returned by the subdevices cl_device_partition_property *ptype = NULL; size_t ptype_size; cl_uint numdevs = 0; cl_device_id parent; cl_uint sub_cus; /* CL_DEVICE_PARTITION_EQUALLY */ printf("Max CUs: %u\n", max_cus); /* if the device has 3 CUs, 3 subdevices will be created, otherwise 2. */ if (max_cus == 3) split = 3; else split = 2; const cl_device_partition_property equal_splitter[] = { CL_DEVICE_PARTITION_EQUALLY, max_cus/split, 0 }; err = clCreateSubDevices(rootdev, equal_splitter, 0, NULL, &numdevs); CHECK_OPENCL_ERROR_IN("count sub devices"); TEST_ASSERT(numdevs == split); err = clCreateSubDevices(rootdev, equal_splitter, split, eqdev, NULL); CHECK_OPENCL_ERROR_IN("partition equally"); if (split == 2) eqdev[2] = NULL; cl_uint refc; err = clGetDeviceInfo (eqdev[0], CL_DEVICE_REFERENCE_COUNT, sizeof (refc), &refc, NULL); CHECK_OPENCL_ERROR_IN ("get refcount"); TEST_ASSERT (refc == 1); /* First, check that the root device is untouched */ err = clGetDeviceInfo(rootdev, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(sub_cus), &sub_cus, NULL); CHECK_OPENCL_ERROR_IN("parenty CU"); TEST_ASSERT(sub_cus == max_cus); err = clGetDeviceInfo(rootdev, CL_DEVICE_PARENT_DEVICE, sizeof(parent), &parent, NULL); CHECK_OPENCL_ERROR_IN("root parent device"); TEST_ASSERT(parent == NULL); /* partition type may either be NULL or contain a 0 entry */ err = clGetDeviceInfo(rootdev, CL_DEVICE_PARTITION_TYPE, 0, NULL, &ptype_size); CHECK_OPENCL_ERROR_IN("root partition type"); if (ptype_size != 0) { /* abuse dev_pt which should be large enough */ TEST_ASSERT(ptype_size == sizeof(cl_device_partition_property)); TEST_ASSERT(ptype_size <= dev_pt_size); err = clGetDeviceInfo(rootdev, CL_DEVICE_PARTITION_TYPE, ptype_size, dev_pt, NULL); CHECK_OPENCL_ERROR_IN("root partition type #2"); TEST_ASSERT(dev_pt[0] == 0); } /* now test the subdevices */ for (i = 0; i < split; ++i) { err = clGetDeviceInfo(eqdev[i], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(sub_cus), &sub_cus, NULL); CHECK_OPENCL_ERROR_IN("sub CU"); TEST_ASSERT(sub_cus == max_cus/split); err = clGetDeviceInfo(eqdev[i], CL_DEVICE_PARENT_DEVICE, sizeof(parent), &parent, NULL); CHECK_OPENCL_ERROR_IN("sub parent device"); TEST_ASSERT(parent == rootdev); err = clGetDeviceInfo(eqdev[i], CL_DEVICE_PARTITION_TYPE, 0, NULL, &ptype_size); CHECK_OPENCL_ERROR_IN("sub partition type"); TEST_ASSERT(ptype_size == sizeof(equal_splitter)); ptype = malloc(ptype_size); TEST_ASSERT(ptype); err = clGetDeviceInfo(eqdev[i], CL_DEVICE_PARTITION_TYPE, ptype_size, ptype, NULL); CHECK_OPENCL_ERROR_IN("sub partition type #2"); TEST_ASSERT(memcmp(ptype, equal_splitter, ptype_size) == 0); /* free the partition type */ free(ptype) ; ptype = NULL; } /* CL_DEVICE_PARTITION_BY_COUNTS */ /* Note that the platform will only read this to the first 0, * which is actually CL_DEVICE_PARTITION_BY_COUNTS_LIST_END; * the test is structured with an additional final 0 intentionally, * to follow the Khoronos doc example */ const cl_device_partition_property count_splitter[] = { CL_DEVICE_PARTITION_BY_COUNTS, 1, max_cus - 1, CL_DEVICE_PARTITION_BY_COUNTS_LIST_END, 0 }; err = clCreateSubDevices(rootdev, count_splitter, 0, NULL, &numdevs); CHECK_OPENCL_ERROR_IN("count sub devices"); TEST_ASSERT(numdevs == 2); err = clCreateSubDevices(rootdev, count_splitter, 2, countdev, NULL); CHECK_OPENCL_ERROR_IN("partition by counts"); /* First, check that the root device is untouched */ err = clGetDeviceInfo(rootdev, CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(sub_cus), &sub_cus, NULL); CHECK_OPENCL_ERROR_IN("parenty CU"); TEST_ASSERT(sub_cus == max_cus); err = clGetDeviceInfo(rootdev, CL_DEVICE_PARENT_DEVICE, sizeof(parent), &parent, NULL); CHECK_OPENCL_ERROR_IN("root parent device"); TEST_ASSERT(parent == NULL); /* partition type may either be NULL or contain a 0 entry */ err = clGetDeviceInfo(rootdev, CL_DEVICE_PARTITION_TYPE, 0, NULL, &ptype_size); CHECK_OPENCL_ERROR_IN("root partition type"); if (ptype_size != 0) { /* abuse dev_pt which should be large enough */ TEST_ASSERT(ptype_size == sizeof(cl_device_partition_property)); TEST_ASSERT(ptype_size <= dev_pt_size); err = clGetDeviceInfo(rootdev, CL_DEVICE_PARTITION_TYPE, ptype_size, dev_pt, NULL); CHECK_OPENCL_ERROR_IN("root partition type #2"); TEST_ASSERT(dev_pt[0] == 0); } // devices might be returned in different order than the counts // in the count_splitter int found_cus[2] = {0, 0}; /* now test the subdevices */ for (i = 0; i < 2; ++i) { err = clGetDeviceInfo(countdev[i], CL_DEVICE_MAX_COMPUTE_UNITS, sizeof(sub_cus), &sub_cus, NULL); CHECK_OPENCL_ERROR_IN("sub CU"); if (sub_cus == count_splitter[1]) found_cus[0] += 1; else if (sub_cus == count_splitter[2]) found_cus[1] += 1; err = clGetDeviceInfo(countdev[i], CL_DEVICE_PARENT_DEVICE, sizeof(parent), &parent, NULL); CHECK_OPENCL_ERROR_IN("sub parent device"); TEST_ASSERT(parent == rootdev); /* The partition type returned is up to the first 0, * which happens to be the CL_DEVICE_PARTITION_BY_COUNTS_LIST_END, * not the final terminating 0 in count_splitter, so it has one less * element. It should be otherwise equal */ err = clGetDeviceInfo(countdev[i], CL_DEVICE_PARTITION_TYPE, 0, NULL, &ptype_size); CHECK_OPENCL_ERROR_IN("sub partition type"); TEST_ASSERT(ptype_size == sizeof(count_splitter) - sizeof(*count_splitter)); ptype = malloc(ptype_size); TEST_ASSERT(ptype); err = clGetDeviceInfo(countdev[i], CL_DEVICE_PARTITION_TYPE, ptype_size, ptype, NULL); CHECK_OPENCL_ERROR_IN("sub partition type #2"); TEST_ASSERT(memcmp(ptype, count_splitter, ptype_size) == 0); /* free the partition type */ free(ptype) ; ptype = NULL; } /* the previous loop finds 1+1 subdevices only on >dual core systems; * on dual cores, the count_splitter is [1, 1] and the above * "(sub_cus == count_splitter[x])" results in 2+0 subdevices found */ if (max_cus > 2) TEST_ASSERT(found_cus[0] == 1 && found_cus[1] == 1); else TEST_ASSERT((found_cus[0] + found_cus[1]) == 2); /* So far, so good. Let's now try and use these devices, * by building a program for all of them and launching kernels on them. * * Note that there's a discrepancy in behavior between implementations: * some assume you can treat sub-devices as their parent device, and thus * e.g. using them through any context which includes their parent devices, * other fail miserably if you try this. * * For the time being we will test the stricter behavior, where * sub-devices should be added manually to a context. */ err = clReleaseCommandQueue(q); CHECK_OPENCL_ERROR_IN("clReleaseCommandQueue"); err = clReleaseContext(ctx); CHECK_OPENCL_ERROR_IN("clReleaseContext"); /* if we split into 2 equal parts, third pointer is NULL. Let's copy the * previous device to it */ if (split == 2) eqdev[2] = eqdev[1]; ctx = clCreateContext(NULL, NUMDEVS, alldevs, NULL, NULL, &err); CHECK_OPENCL_ERROR_IN("clCreateContext"); TEST_ASSERT( test_context(ctx, prog_src_all, 1, NUMDEVS, alldevs) == CL_SUCCESS ); ctx = clCreateContext(NULL, NUMDEVS - 1, alldevs + 1, NULL, NULL, &err); CHECK_OPENCL_ERROR_IN("clCreateContext"); TEST_ASSERT( test_context(ctx, prog_src_two, -1, NUMDEVS - 1, alldevs + 1) == CL_SUCCESS ); /* Don't release the same device twice. clReleaseDevice(NULL) should return * an error but not crash. */ if (split == 2) eqdev[2] = NULL; for (i = 0; i < NUMDEVS; i++) clReleaseDevice (alldevs[i]); CHECK_CL_ERROR (clUnloadCompiler ()); free (dev_pt); printf ("OK\n"); return 0; }
int main(int argc, char **argv) { cl_int err; const char *krn_src; cl_program empty, program; cl_context ctx; cl_device_id did; cl_command_queue queue; cl_uint num_krn; cl_kernel kernels[2]; err = poclu_get_any_device(&ctx, &did, &queue); CHECK_OPENCL_ERROR_IN("poclu_get_any_device"); TEST_ASSERT( ctx ); TEST_ASSERT( did ); TEST_ASSERT( queue ); /* Test creating a program from an empty source */ empty = clCreateProgramWithSource(ctx, 1, &empty_src, NULL, &err); CHECK_OPENCL_ERROR_IN("clCreateProgramWithSource"); err = clBuildProgram(empty, 0, NULL, NULL, NULL, NULL); CHECK_OPENCL_ERROR_IN("clBuildProgram"); err = clCreateKernelsInProgram(empty, 0, NULL, &num_krn); CHECK_OPENCL_ERROR_IN("clCreateKernelsInProgram"); TEST_ASSERT(num_krn == 0); krn_src = poclu_read_file(SRCDIR "/tests/runtime/test_clCreateKernelsInProgram.cl"); TEST_ASSERT(krn_src); program = clCreateProgramWithSource(ctx, 1, &krn_src, NULL, &err); CHECK_OPENCL_ERROR_IN("clCreateProgramWithSource"); err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); CHECK_OPENCL_ERROR_IN("clBuildProgram"); err = clCreateKernelsInProgram(program, 0, NULL, &num_krn); CHECK_OPENCL_ERROR_IN("clCreateKernelsInProgram"); // test_clCreateKernelsInProgram.cl has two kernel functions. TEST_ASSERT(num_krn == 2); err = clCreateKernelsInProgram(program, 2, kernels, NULL); CHECK_OPENCL_ERROR_IN("clCreateKernelsInProgram"); // make sure the kernels were actually created // Note: nothing in the specification says which kernel function // is kernels[0], which is kernels[1]. For now assume pocl/LLVM // orders these deterministacally err = clEnqueueTask(queue, kernels[0], 0, NULL, NULL); CHECK_OPENCL_ERROR_IN("clEnqueueTask"); err = clFinish(queue); CHECK_OPENCL_ERROR_IN("clFinish"); err = clEnqueueTask(queue, kernels[1], 0, NULL, NULL); CHECK_OPENCL_ERROR_IN("clEnqueueTask"); err = clFinish(queue); CHECK_OPENCL_ERROR_IN("clFinish"); CHECK_CL_ERROR (clReleaseCommandQueue (queue)); CHECK_CL_ERROR (clReleaseKernel (kernels[0])); CHECK_CL_ERROR (clReleaseKernel (kernels[1])); CHECK_CL_ERROR (clReleaseProgram (program)); CHECK_CL_ERROR (clReleaseProgram (empty)); CHECK_CL_ERROR (clReleaseContext (ctx)); CHECK_CL_ERROR (clUnloadCompiler ()); free ((void *)krn_src); return EXIT_SUCCESS; }
int call_test(const char *name) { size_t global_work_size[1] = { 1 }, local_work_size[1]= { 1 }; size_t srcdir_length, name_length, filename_size; char *filename = NULL; char *source = NULL; cl_device_id devices[1]; cl_context context = NULL; cl_command_queue queue = NULL; cl_program program = NULL; cl_kernel kernel = NULL; cl_int result; int retval = -1; TEST_ASSERT (name != NULL); /* determine file name of kernel source to load */ srcdir_length = strlen(SRCDIR); name_length = strlen(name); filename_size = srcdir_length + name_length + 16; filename = (char *)malloc(filename_size + 1); if (!filename) { puts("out of memory"); goto error; } snprintf(filename, filename_size, "%s/%s.cl", SRCDIR, name); /* read source code */ source = poclu_read_file (filename); TEST_ASSERT (source != NULL && "Kernel .cl not found."); /* setup an OpenCL context and command queue using default device */ context = poclu_create_any_context(); if (!context) { puts("clCreateContextFromType call failed\n"); goto error; } result = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(cl_device_id), devices, NULL); if (result != CL_SUCCESS) { puts("clGetContextInfo call failed\n"); goto error; } queue = clCreateCommandQueue(context, devices[0], 0, NULL); if (!queue) { puts("clCreateCommandQueue call failed\n"); goto error; } /* create and build program */ program = clCreateProgramWithSource (context, 1, (const char **)&source, NULL, NULL); if (!program) { puts("clCreateProgramWithSource call failed\n"); goto error; } result = clBuildProgram(program, 0, NULL, "-I" SRCDIR, NULL, NULL); if (result != CL_SUCCESS) { puts("clBuildProgram call failed\n"); goto error; } /* execute the kernel with give name */ kernel = clCreateKernel(program, name, NULL); if (!kernel) { puts("clCreateKernel call failed\n"); goto error; } result = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, global_work_size, local_work_size, 0, NULL, NULL); if (result != CL_SUCCESS) { puts("clEnqueueNDRangeKernel call failed\n"); goto error; } result = clFinish(queue); if (result == CL_SUCCESS) retval = 0; error: if (kernel) { clReleaseKernel(kernel); } if (program) { clReleaseProgram(program); } if (queue) { clReleaseCommandQueue(queue); } if (context) { clUnloadCompiler (); clReleaseContext (context); } if (source) { free(source); } if (filename) { free(filename); } return retval; }
int main(int argc, char **argv) { /* test name */ char name[] = "test_sampler_address_clamp"; size_t global_work_size[1] = { 1 }, local_work_size[1]= { 1 }; size_t srcdir_length, name_length, filename_size; char *filename = NULL; char *source = NULL; cl_device_id devices[1]; cl_context context = NULL; cl_command_queue queue = NULL; cl_program program = NULL; cl_kernel kernel = NULL; cl_int result; int retval = -1; /* image parameters */ cl_uchar4 *imageData; cl_image_format image_format; cl_image_desc image_desc; printf("Running test %s...\n", name); memset(&image_desc, 0, sizeof(cl_image_desc)); image_desc.image_type = CL_MEM_OBJECT_IMAGE2D; image_desc.image_width = 4; image_desc.image_height = 4; image_format.image_channel_order = CL_RGBA; image_format.image_channel_data_type = CL_UNSIGNED_INT8; imageData = (cl_uchar4*)malloc (4 * 4 * sizeof(cl_uchar4)); if (imageData == NULL) { puts("out of host memory\n"); goto error; } memset (imageData, 1, 4*4*sizeof(cl_uchar4)); /* determine file name of kernel source to load */ srcdir_length = strlen(SRCDIR); name_length = strlen(name); filename_size = srcdir_length + name_length + 16; filename = (char *)malloc(filename_size + 1); if (!filename) { puts("out of memory"); goto error; } snprintf(filename, filename_size, "%s/%s.cl", SRCDIR, name); /* read source code */ source = poclu_read_file (filename); TEST_ASSERT (source != NULL && "Kernel .cl not found."); /* setup an OpenCL context and command queue using default device */ context = poclu_create_any_context(); if (!context) { puts("clCreateContextFromType call failed\n"); goto error; } result = clGetContextInfo(context, CL_CONTEXT_DEVICES, sizeof(cl_device_id), devices, NULL); if (result != CL_SUCCESS) { puts("clGetContextInfo call failed\n"); goto error; } queue = clCreateCommandQueue(context, devices[0], 0, NULL); if (!queue) { puts("clCreateCommandQueue call failed\n"); goto error; } /* Create image */ cl_mem image = clCreateImage (context, CL_MEM_READ_ONLY | CL_MEM_USE_HOST_PTR, &image_format, &image_desc, imageData, &result); if (result != CL_SUCCESS) { puts("image creation failed\n"); goto error; } /* create and build program */ program = clCreateProgramWithSource (context, 1, (const char **)&source, NULL, NULL); if (!program) { puts("clCreateProgramWithSource call failed\n"); goto error; } result = clBuildProgram(program, 0, NULL, NULL, NULL, NULL); if (result != CL_SUCCESS) { puts("clBuildProgram call failed\n"); goto error; } /* execute the kernel with give name */ kernel = clCreateKernel(program, name, NULL); if (!kernel) { puts("clCreateKernel call failed\n"); goto error; } result = clSetKernelArg( kernel, 0, sizeof(cl_mem), &image); if (result) { puts("clSetKernelArg failed\n"); goto error; } result = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, global_work_size, local_work_size, 0, NULL, NULL); if (result != CL_SUCCESS) { puts("clEnqueueNDRangeKernel call failed\n"); goto error; } result = clFinish(queue); if (result == CL_SUCCESS) retval = 0; error: if (image) { clReleaseMemObject (image); } if (kernel) { clReleaseKernel(kernel); } if (program) { clReleaseProgram(program); } if (queue) { clReleaseCommandQueue(queue); } if (context) { clUnloadCompiler (); clReleaseContext (context); } if (source) { free(source); } if (filename) { free(filename); } if (imageData) { free(imageData); } if (retval) { printf("FAIL\n"); return 1; } printf("OK\n"); return 0; }