SegFault at clGetDeviceIDs?

Hi,

I am only beginning to practice OpenCL, and am writing my first full program on my own. This is what I have so far.

#include <stdio.h>
#include <stdlib.h>
#include <CL/cl.h>

main()
{
        cl_int error = 0;
        cl_platform_id platform;
        cl_context context;
        cl_command_queue queue;
        cl_device_id* device;
        cl_uint* num_devices;

        cl_int num_platforms;
        char platform_name[100];

        // Platform
        error = clGetPlatformIDs(1, &platform, &num_platforms);
        if(error != CL_SUCCESS) { printf("Error getting platform id
"); return 0; }
        printf("The platform address is: %p
", platform);
        printf("The number of platforms is: %d
", num_platforms);

        error = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(platform_name), platform_name, NULL);
        if(error != CL_SUCCESS) { printf("Error getting platform name
"); return 0; }
        printf("The platform name is: %s
", platform_name);

        device = malloc(sizeof(cl_device_id));
//      error = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, device, num_devices);
        printf("Hello!
");
        return 0;
}

The commented-out line,

error = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, device, num_devices);

results in a segfault error. However, the strange thing is that it is not fatal, and the line “Hello!” prints just fine as per the printf statement. Does anyone know why the segfault is happening and how I can fix it? Thank you very much!

The problem fixed (?) itself. I decided to go ahead and finish the part of the program that I was working on, and then when I re-compiled and ran it, it would no longer segfault.

Here is the full code, for reference.

#include <stdio.h>
#include <stdlib.h>
#include <CL/cl.h>

main()
{
        cl_int error = 0;
        cl_platform_id platform;
        cl_context context;
        cl_command_queue queue;
        cl_device_id* device;
        cl_uint* num_devices;

        cl_int num_platforms;
        char platform_name[100];

        // Platform, Devices, Context, Command Queue
        error = clGetPlatformIDs(1, &platform, &num_platforms);
        if(error != CL_SUCCESS) { printf("Error getting platform id
"); return 0; }
        printf("The platform address is: %p
", platform);
        printf("The number of platforms is: %d
", num_platforms);

        error = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(platform_name), platform_name, NULL);
        if(error != CL_SUCCESS) { printf("Error getting platform name
"); return 0; }
        printf("The platform name is: %s
", platform_name);

        device = malloc(sizeof(cl_device_id));
        error = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, device, num_devices);
        printf("Hello!
");

        context = clCreateContext(NULL, 1, device, NULL, NULL, &error);
        if(error != CL_SUCCESS) { printf("Error getting context
"); return 0; }

        queue = clCreateCommandQueue(context, *device, 0, &error);
        if(error != CL_SUCCESS) { printf("Error getting queue
"); return 0; }

        // TODO: Kernel

return 0;
}

You are getting exception because you are using uninitialized pointers
cl_device_id* device;
cl_uint* num_devices;
I had same Segmentation Fault in my very first OpenCL program on Ubuntu but from different reason.I did not have check for success of clGetPlatformInfo function before calling clGetDeviceIDs which was failing. It was failing because I did not have installed driver.

1 Like