6 Replies Latest reply on Mar 21, 2011 12:07 PM by irnn

    clCreateCommandQueue segfaults x86

    irnn
      Weird bug

      I am having an issue which I think is exactly the same as the one in this thread:

      http://forums.amd.com/devforum/messageview.cfm?catid=390&threadid=142326

      The machine I am running on has an 8 core Intel(R) Xeon(R) CPU E5620  @ 2.40GHz, and 2x Nvidia Tesla M2050. I have some code which runs kernels on the GPUs and the Intel processor. To get OpenCL x86 I am using the AMD APP SDK version 2.3 32bit. *ninja edit, On 64bit Linux

      At runtime the program segfaults on a call to clCreateCommandQueue(). When I run the program with strace this crash no longer happens and it runs fine. I have run the code on computers with different CPUs and it also runs ok.

      Does anyone have any ideas as to what is causing this crash? I'm tearing my hair out here!

      Thanks in advance

        • clCreateCommandQueue segfaults x86
          genaganna

           

          Originally posted by: irnn I am having an issue which I think is exactly the same as the one in this thread:

           

          http://forums.amd.com/devforum/messageview.cfm?catid=390&threadid=142326

           

          The machine I am running on has an 8 core Intel(R) Xeon(R) CPU E5620  @ 2.40GHz, and 2x Nvidia Tesla M2050. I have some code which runs kernels on the GPUs and the Intel processor. To get OpenCL x86 I am using the AMD APP SDK version 2.3 32bit. *ninja edit, On 64bit Linux

           

          At runtime the program segfaults on a call to clCreateCommandQueue(). When I run the program with strace this crash no longer happens and it runs fine. I have run the code on computers with different CPUs and it also runs ok.

           

          Does anyone have any ideas as to what is causing this crash? I'm tearing my hair out here!

           

          Thanks in advance

           

          Irnn,

                 Are you able to run any SDK sample on CPU?  Please run any SDK sample with "--device cpu -e" options and let us know what output you are getting.

           Which linux are you using?

           Please also paste your code here.

            • clCreateCommandQueue segfaults x86
              irnn

              FFT sample from SDK:

               


               

              $ ./FFT --device cpu -e

              Original Input Real
              56.77 16.2255 45.2166 169.329 100.9 145.976 251.169 100.16 71.5089 54.6652


              Original Input Img
              0.221758 0.0633808 0.176627 0.66144 0.394139 0.570218 0.981128 0.391251 0.279332 0.213536


              Platform Vendor : Advanced Micro Devices, Inc.
              Device 0 : Intel(R) Xeon(R) CPU E5620 @ 2.40GHz
              Segmentation fault

               


               

              As for the linux I am using the output from uname -a is:

               


               

              $ uname -a
              Linux u04n035 2.6.18-194.32.1.el5 #1 SMP Tue Jan 4 12:47:36 EST 2011 x86_64 x86_64 x86_64 GNU/Linux

               


               

              I believe it is ClusterVisionOS although I am not certain. The code that I am trying to run is a slightly modified version of apples hello world OpenCL code from their website, compiled with --std=gnu99

              #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <CL/cl.h> #define DATA_SIZE (1024) // Device associated data typedef struct { cl_context context; cl_command_queue queue; cl_program program; cl_kernel kernel; cl_device_id device; } device; // Prototypes char *clErr(cl_int err); // Main Program int main(int argc, char* argv[]) { // Read in the cl kernel file FILE *fp; long kernelSize; char *kernelSource; fp = fopen("testkernel.cl","r"); fseek(fp,0,SEEK_END); kernelSize = ftell(fp); fseek(fp,0,SEEK_SET); kernelSource = (char *)malloc(sizeof(char)*kernelSize); fread(kernelSource,1,kernelSize,fp); fclose(fp); // OpenCL variables cl_int err; cl_platform_id platforms[32]; char platform_data[1024]; cl_device_id *device_id; cl_uint pnum; cl_uint dnum; cl_uint totalDevices = 0; // Get the platforms err = clGetPlatformIDs((cl_uint)32, platforms, &pnum); printf("\nAvailable platforms %d:\n",pnum); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); if(pnum==0) { // No platforms available so bail fprintf(stderr,"\nCRITICAL: No available OpenCL platforms\n"); return EXIT_SUCCESS; } for(int i=0;(unsigned)i<pnum;i++) { memset(platform_data,0,1024); err = clGetPlatformInfo(platforms[i],CL_PLATFORM_NAME, 1024, platform_data, NULL); if(err!=CL_SUCCESS) { printf("Platform num: %d\n",i); printf("%d: %s\n",__LINE__,clErr(err)); return EXIT_FAILURE; } printf("%d: %s\n",i,platform_data); err = clGetPlatformIDs(pnum,&platforms[0],NULL); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); // Get the devices for this platform err = clGetDeviceIDs(platforms[i],CL_DEVICE_TYPE_ALL,0,NULL,&dnum); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); device_id = (cl_device_id*)malloc(sizeof(cl_device_id)*dnum); err = clGetDeviceIDs(platforms[i],CL_DEVICE_TYPE_ALL,dnum,device_id,NULL); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); // Printf out info on the devices and increment the total device counter printf("\nPlatform %d devices:\n",i); for(unsigned int ii=0;ii<dnum;++ii) { totalDevices++; size_t size; err = clGetDeviceInfo(device_id[i],CL_DEVICE_NAME,0,NULL,&size); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); char* string = (char*)malloc(sizeof(char)*size); err = clGetDeviceInfo(device_id[i],CL_DEVICE_NAME,size,string,NULL); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); printf("%s\n",string); } printf("\n"); } // Create data structure to hold all our device info device *devices; devices = malloc(sizeof(device)*totalDevices); unsigned int currentDeviceNum = 0; for(unsigned int plat=0;plat<pnum;plat++) { // Getting the devices for the platform we are on err = clGetDeviceIDs(platforms[plat],CL_DEVICE_TYPE_ALL,0,NULL,&dnum); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); device_id = (cl_device_id*)malloc(sizeof(cl_device_id)*dnum); err = clGetDeviceIDs(platforms[plat],CL_DEVICE_TYPE_ALL,dnum,device_id,NULL); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); // Loop through devices and create all the jazz we need for(unsigned int dev=0;dev<dnum;dev++) { device *thisDevice = malloc(sizeof(device)); thisDevice->device = device_id[dev]; // Create context thisDevice->context = clCreateContext(0,1,&device_id[dev],NULL,NULL,&err); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); // Create command q thisDevice->queue = clCreateCommandQueue(thisDevice->context,device_id[dev],0,&err); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); // Create program thisDevice->program = clCreateProgramWithSource(thisDevice->context,1,(const char **)&kernelSource,NULL,&err); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); // Build program err = clBuildProgram(thisDevice->program,0,NULL,"",NULL,NULL); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); printf("Device %d Kernel file build ouput:\n",currentDeviceNum); size_t clcb; char* string; clGetProgramBuildInfo(thisDevice->program,device_id[dev],CL_PROGRAM_BUILD_LOG,0,NULL,&clcb); string = (char*)malloc(sizeof(char)*clcb); clGetProgramBuildInfo(thisDevice->program,device_id[dev],CL_PROGRAM_BUILD_LOG,clcb,string,NULL); printf("\n%s\n\n",string); if(err!=CL_SUCCESS) return EXIT_FAILURE; // Create kernel thisDevice->kernel = clCreateKernel(thisDevice->program, "square", &err); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); // Save device devices[currentDeviceNum] = *thisDevice; currentDeviceNum++; } } // Make our data float data[DATA_SIZE]; // Data to send to our device float results[DATA_SIZE]; // Data we get back from the device unsigned int correct; // Number of correct results size_t global; // global domain size for our calculation size_t local[totalDevices]; // local domain size for our calculation cl_mem input[totalDevices]; // Input buffer for each device cl_mem output[totalDevices]; // Output buffer for each device int i = 0; unsigned int count = DATA_SIZE; for(i=0;i<count;i++) data[i] = rand()/(float)RAND_MAX; for(unsigned int dev=0;dev<totalDevices;dev++) { // Create the input and output arrays on the device input[dev] = clCreateBuffer(devices[dev].context,CL_MEM_READ_ONLY,sizeof(float)*count,NULL,&err); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); output[dev] = clCreateBuffer(devices[dev].context,CL_MEM_WRITE_ONLY,sizeof(float)*count,NULL,&err); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); // Write the data set into the input array on device memory err = clEnqueueWriteBuffer(devices[dev].queue,input[dev],CL_TRUE,0,sizeof(float)*count,data,0,NULL,NULL); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); // Set the kernel arguments err = 0; err = clSetKernelArg(devices[dev].kernel,0,sizeof(cl_mem),&input[dev]); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); err = clSetKernelArg(devices[dev].kernel,1,sizeof(cl_mem),&output[dev]); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); err = clSetKernelArg(devices[dev].kernel,2,sizeof(unsigned int),&count); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); // Get maximum workgroup size for executing kernel on device err = clGetKernelWorkGroupInfo(devices[dev].kernel,devices[dev].device,CL_KERNEL_WORK_GROUP_SIZE,sizeof(local[dev]),&local[dev],NULL); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); } // Execute kernels global = count; for(unsigned int dev=0;dev<totalDevices;dev++) { err = clEnqueueNDRangeKernel(devices[dev].queue,devices[dev].kernel,1,NULL,&global,&local[dev],0,NULL,NULL); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); } // Make sure we are all finished for(unsigned int dev=0;dev<totalDevices;dev++) { clFinish(devices[dev].queue); // Read back results to verify output err = clEnqueueReadBuffer(devices[dev].queue,output[dev],CL_TRUE,0,sizeof(float)*count,results,0,NULL,NULL); if(err!=CL_SUCCESS) printf("%d: %s\n",__LINE__,clErr(err)); // Validate our results correct = 0; for(unsigned int blarg=0;blarg<count;blarg++) { if(fabs(results[blarg]-(data[blarg]*data[blarg]))<0.0001) correct++; } // Print results printf("Device %d computed: %d/%d correct values!\n",dev,correct,count); // Cleanup clReleaseMemObject(input[dev]); clReleaseMemObject(output[dev]); clReleaseProgram(devices[dev].program); clReleaseKernel(devices[dev].kernel); clReleaseCommandQueue(devices[dev].queue); clReleaseContext(devices[dev].context); } // All done printf("Done.\n"); return EXIT_SUCCESS; } char *clErr(cl_int err) { switch (err) { case CL_SUCCESS: return strdup("OK"); case CL_DEVICE_NOT_FOUND: return strdup("Device not found."); case CL_DEVICE_NOT_AVAILABLE: return strdup("Device not available"); case CL_COMPILER_NOT_AVAILABLE: return strdup("Compiler not available"); case CL_MEM_OBJECT_ALLOCATION_FAILURE: return strdup("Memory object allocation failure"); case CL_OUT_OF_RESOURCES: return strdup("Out of resources"); case CL_OUT_OF_HOST_MEMORY: return strdup("Out of host memory"); case CL_PROFILING_INFO_NOT_AVAILABLE: return strdup("Profiling information not available"); case CL_MEM_COPY_OVERLAP: return strdup("Memory copy overlap"); case CL_IMAGE_FORMAT_MISMATCH: return strdup("Image format mismatch"); case CL_IMAGE_FORMAT_NOT_SUPPORTED: return strdup("Image format not supported"); case CL_BUILD_PROGRAM_FAILURE: return strdup("Program build failure"); case CL_MAP_FAILURE: return strdup("Map failure"); case CL_INVALID_VALUE: return strdup("Invalid value"); case CL_INVALID_DEVICE_TYPE: return strdup("Invalid device type"); case CL_INVALID_PLATFORM: return strdup("Invalid platform"); case CL_INVALID_DEVICE: return strdup("Invalid device"); case CL_INVALID_CONTEXT: return strdup("Invalid context"); case CL_INVALID_QUEUE_PROPERTIES: return strdup("Invalid queue properties"); case CL_INVALID_COMMAND_QUEUE: return strdup("Invalid command queue"); case CL_INVALID_HOST_PTR: return strdup("Invalid host pointer"); case CL_INVALID_MEM_OBJECT: return strdup("Invalid memory object"); case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: return strdup("Invalid image format descriptor"); case CL_INVALID_IMAGE_SIZE: return strdup("Invalid image size"); case CL_INVALID_SAMPLER: return strdup("Invalid sampler"); case CL_INVALID_BINARY: return strdup("Invalid binary"); case CL_INVALID_BUILD_OPTIONS: return strdup("Invalid build options"); case CL_INVALID_PROGRAM: return strdup("Invalid program"); case CL_INVALID_PROGRAM_EXECUTABLE: return strdup("Invalid program executable"); case CL_INVALID_KERNEL_NAME: return strdup("Invalid kernel name"); case CL_INVALID_KERNEL_DEFINITION: return strdup("Invalid kernel definition"); case CL_INVALID_KERNEL: return strdup("Invalid kernel"); case CL_INVALID_ARG_INDEX: return strdup("Invalid argument index"); case CL_INVALID_ARG_VALUE: return strdup("Invalid argument value"); case CL_INVALID_ARG_SIZE: return strdup("Invalid argument size"); case CL_INVALID_KERNEL_ARGS: return strdup("Invalid kernel arguments"); case CL_INVALID_WORK_DIMENSION: return strdup("Invalid work dimension"); case CL_INVALID_WORK_GROUP_SIZE: return strdup("Invalid work group size"); case CL_INVALID_WORK_ITEM_SIZE: return strdup("Invalid work item size"); case CL_INVALID_GLOBAL_OFFSET: return strdup("Invalid global offset"); case CL_INVALID_EVENT_WAIT_LIST: return strdup("Invalid event wait list"); case CL_INVALID_EVENT: return strdup("Invalid event"); case CL_INVALID_OPERATION: return strdup("Invalid operation"); case CL_INVALID_GL_OBJECT: return strdup("Invalid OpenGL object"); case CL_INVALID_BUFFER_SIZE: return strdup("Invalid buffer size"); case CL_INVALID_MIP_LEVEL: return strdup("Invalid mip-map level"); default: return strdup("Unknown"); } }