PDA

View Full Version : SegFault at clGetDeviceIDs?



ds789
12-27-2012, 02:37 PM
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\n"); return 0; }
printf("The platform address is: %p\n", platform);
printf("The number of platforms is: %d\n", num_platforms);

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

device = malloc(sizeof(cl_device_id));
// error = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 1, device, num_devices);
printf("Hello!\n");
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!

ds789
12-28-2012, 03:48 PM
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\n"); return 0; }
printf("The platform address is: %p\n", platform);
printf("The number of platforms is: %d\n", num_platforms);

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

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

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

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

// TODO: Kernel

return 0;
}