|
从某个教程里面copy 了一个例子
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <alloca.h>
- #ifdef APPLE
- #include <OpenCL/cl.h>
- #else
- #include <CL/cl.h>
- #endif
- void loadProgramSource(const char** files,
- size_t length,
- char** buffer,
- size_t* sizes) {
- /* Read each source file (*.cl) and store the contents into a temporary datastore */
- for(size_t i=0; i < length; i++) {
- FILE* file = fopen(files[i], "r");
- if(file == NULL) {
- perror("Couldn't read the program file");
- exit(1);
- }
- fseek(file, 0, SEEK_END);
- sizes[i] = ftell(file);
- rewind(file); // reset the file pointer so that 'fread' reads from the front
- buffer[i] = (char*)malloc(sizes[i]+1);
- buffer[i][sizes[i]] = '\0';
- fread(buffer[i], sizeof(char), sizes[i], file);
- fclose(file);
- }
- }
- int main(int argc, char** argv) {
- /* OpenCL 1.1 data structures */
- cl_platform_id* platforms;
- cl_program program;
- cl_device_id device;
- cl_context context;
- /* OpenCL 1.1 scalar data types */
- cl_uint numOfPlatforms;
- cl_int error;
- /*
- Get the number of platforms
- Remember that for each vendor's SDK installed on the computer,
- the number of available platform also increased.
- */
- error = clGetPlatformIDs(0, NULL, &numOfPlatforms);
- if(error != CL_SUCCESS) {
- perror("Unable to find any OpenCL platforms");
- exit(1);
- }
- platforms = (cl_platform_id*) alloca(sizeof(cl_platform_id) * numOfPlatforms);
- printf("Number of OpenCL platforms found: %d\n", numOfPlatforms);
- error = clGetPlatformIDs(numOfPlatforms, platforms, NULL);
- if(error != CL_SUCCESS) {
- perror("Unable to find any OpenCL platforms");
- exit(1);
- }
- // Search for a CPU/GPU device through the installed platforms
- // Build a OpenCL program and do not run it.
- for(cl_uint i = 0; i < numOfPlatforms; i++ ) {
- // Get the GPU device
- error = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_GPU, 1, &device, NULL);
- if(error != CL_SUCCESS) {
- // Otherwise, get the CPU
- error = clGetDeviceIDs(platforms[i], CL_DEVICE_TYPE_CPU, 1, &device, NULL);
- }
- if(error != CL_SUCCESS) {
- perror("Can't locate any OpenCL compliant device");
- exit(1);
- }
- /* Create a context */
- context = clCreateContext(NULL, 1, &device, NULL, NULL, &error);
- if(error != CL_SUCCESS) {
- perror("Can't create a valid OpenCL context");
- exit(1);
- }
- /* Load the two source files into temporary datastores */
- const char *file_names[] = {"simple.cl", "simple_2.cl"};
- const int NUMBER_OF_FILES = 2;
- char* buffer[NUMBER_OF_FILES];
- size_t sizes[NUMBER_OF_FILES];
- loadProgramSource(file_names, NUMBER_OF_FILES, buffer, sizes);
- /* Create the OpenCL program object */
- program = clCreateProgramWithSource(context, NUMBER_OF_FILES, (const char**)buffer, sizes, &error);
- if(error != CL_SUCCESS) {
- perror("Can't create the OpenCL program object");
- exit(1);
- }
- /* Build OpenCL program object and dump the error message, if any */
- char *program_log;
- const char options[] = "-cl-finite-math-only -cl-no-signed-zeros";
- size_t log_size;
- printf("build-options:%s\n", argv[1]);
- error = clBuildProgram(program, 1, &device, argv[1], NULL, NULL);
- // Uncomment the line below, comment the line above; re-build the program to use build options statically
- // error = clBuildProgram(program, 1, &device, options, NULL, NULL);
- if(error != CL_SUCCESS) {
- // If there's an error whilst building the program, dump the log
- clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
- program_log = (char*) malloc(log_size+1);
- program_log[log_size] = '\0';
- clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
- log_size+1, program_log, NULL);
- printf("\n=== ERROR ===\n\n%s\n=============\n", program_log);
- free(program_log);
- exit(1);
- }
-
- /* Clean up */
- for(i=0; i< NUMBER_OF_FILES; i++) { free(buffer[i]); }
- clReleaseProgram(program);
- clReleaseContext(context);
- }
- }
编译指令如下
- gcc build_opencl_program.c -o build_opencl_program -lOpenCL
运行错误
- ./build_opencl_program
- Number of OpenCL platforms found: 1
- Failed creating base context during opening of kernel driver.
- Kernel module may not have been loaded
- Failed creating base context during opening of kernel driver.
- Kernel module may not have been loaded
- Can't locate any OpenCL compliant device: Bad address
- enCL @localhost opencl]# ./build_opencl_program e
- Number of OpenCL platforms found: 1
- Failed creating base context during opening of kernel driver.
- Kernel module may not have been loaded
- Failed creating base context during opening of kernel driver.
- Kernel module may not have been loaded
- Can't locate any OpenCL compliant device: Bad address
有相关OpenCL 在3399pro中的使用说明吗?
clinfo 打印出的信息如下
- [root@localhost opencl]# clinfo
- Number of platforms 1
- Platform Name ARM Platform
- Platform Vendor ARM
- Platform Version OpenCL 1.2 v1.r18p0-01rel0.42aa65ae5fdfdec464479cb891075220
- Platform Profile FULL_PROFILE
- Failed creating base context during opening of kernel driver.
- Kernel module may not have been loaded
- Platform Extensions <printPlatformInfo:4: get CL_PLATFORM_EXTENSIONS size : error -6>
- Failed creating base context during opening of kernel driver.
- Kernel module may not have been loaded
- printPlatformInfo:488: number of devices : error -6
|
|