All pastes #2113435 Raw Edit

Mine

public text v1 · immutable
#2113435 ·published 2012-02-08 22:39 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>

#include <CL/cl.h>

#define CASE_ERR(ec) case ec: return #ec;

const char * err_string(cl_int error)
{
   switch(error) {

   CASE_ERR(CL_DEVICE_NOT_AVAILABLE);
   CASE_ERR(CL_DEVICE_NOT_FOUND);
   CASE_ERR(CL_INVALID_DEVICE);
   CASE_ERR(CL_INVALID_DEVICE_TYPE);
   CASE_ERR(CL_INVALID_OPERATION);
   CASE_ERR(CL_INVALID_PLATFORM);
   CASE_ERR(CL_INVALID_PROPERTY);
   CASE_ERR(CL_INVALID_VALUE);
   CASE_ERR(CL_OUT_OF_HOST_MEMORY);
   CASE_ERR(CL_OUT_OF_RESOURCES);
   CASE_ERR(CL_SUCCESS);

   }
}

int main(int argc, char ** argv)
{
   cl_int error;

   cl_uint total_platforms;
   cl_platform_id platform_id;

   cl_uint total_gpu_devices;
   cl_device_id device_id;

   cl_context context;

   cl_command_queue command_queue;

   cl_mem out_buffer;
   float out_value = 0.0;

   error = clGetPlatformIDs(
                           1, /* Max number of platform IDs to return */
                           &platform_id, /* Pointer to platform_id */
                           &total_platforms); /* Total number of platforms
                                               * found on the system */

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clGetPlatformIDs() failed: %s\n", err_string(error));
      return EXIT_FAILURE;
   }

   fprintf(stderr, "There are %u platforms.\n", total_platforms);



   error = clGetDeviceIDs(platform_id,
                          CL_DEVICE_TYPE_GPU,
                          1,
                          &device_id,
                          &total_gpu_devices);

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clGetDeviceIDs() failed: %s\n", err_string(error));
      return EXIT_FAILURE;
   }

   fprintf(stderr, "There are %u GPU devices.\n", total_gpu_devices);

   context = clCreateContext(NULL, /* Properties */
                           1, /* Number of devices */
                           &device_id, /* Device pointer */
                           NULL, /* Callback for reporting errors */
                           NULL, /* User data to pass to error callback */
                           &error); /* Error code */

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clCreateContext() failed: %s\n", err_string(error));
      return EXIT_FAILURE;
   }

   fprintf(stderr, "clCreateContext() succeeded.\n");

   command_queue = clCreateCommandQueue(context,
                                        device_id,
                                        0, /* Command queue properties */
                                        &error); /* Error code */

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clCreateCommandQueue() failed: %s\n", err_string(error));
      return EXIT_FAILURE;
   }

   fprintf(stderr, "clCreateCommandQueue() succeeded.\n");

   out_buffer = clCreateBuffer(context,
                               CL_MEM_WRITE_ONLY, /* Flags */
                               sizeof(float), /* Size of buffer */
                               NULL, /* Pointer to the data */
                               &error); /* error code */

   if (error != CL_SUCCESS) {
      fprintf(stderr, "clCreateBuffer() failed: %s\n", err_string(error));
      return EXIT_FAILURE;
   }

   fprintf(stderr, "clCreateBuffer() succeeded.\n");

   return EXIT_SUCCESS;
}