Beispiel #1
0
int
main(int argc, char * argv[])
{
    Opts opts = { 0 };
    cl_int status;
    cl_platform_id *platformList;
    cl_uint numPlatforms;
    int ii;

    ParseOpts(&opts, argc, argv);


    if ((status = clGetPlatformIDs(0, NULL, &numPlatforms)) != CL_SUCCESS) {
       Warning("Unable to query the number of platforms: %s\n",
               CLErrString(status));
       exit(1);
    }
    printf("Found %d platform(s).\n", numPlatforms);

    platformList = malloc(sizeof(cl_platform_id) * numPlatforms);
    if ((status = clGetPlatformIDs(numPlatforms, platformList, NULL)) != CL_SUCCESS) {
       Warning("Unable to enumerate the platforms: %s\n",
               CLErrString(status));
       exit(1);
    }

    for (ii = 0; ii < numPlatforms; ii++) {
       PrintPlatform(platformList[ii]);
    }

    free(platformList);
    exit(0);
}
Beispiel #2
0
cl_int InitOpenCLEnvironment( char * device_type, cl_device_id * device, cl_context * context, cl_command_queue * cmdQueue ) {

  cl_int status;
  cl_uint numPlatforms, numDevices;
  cl_device_type device_kind;
  cl_platform_id * platforms_list;
  cl_platform_id platform;

  /* Initialize the Platform. Program considers a single platform. */
  if ( ( status = clGetPlatformIDs( 0, NULL, &numPlatforms ) ) != CL_SUCCESS ) {
    fprintf( stderr, "Unable to query the number of platforms: %s\n", CLErrString(status) );
    exit( 1 );
  }



#ifdef __DEBUG
  fprintf( stdout, "Found %d platform(s).\n", numPlatforms );
#endif

  platforms_list = (cl_platform_id *) malloc( sizeof(cl_platform_id) * numPlatforms );
  if ( (clGetPlatformIDs( numPlatforms, platforms_list, NULL ) ) != CL_SUCCESS ) {
    fprintf( stderr, "Unable to enumerate the platforms: %s\n", CLErrString(status));
    exit( 1 );
  }
  
  if( !strcmp( device_type, "gpu" ) ) {
    fprintf( stdout, "\nUSING GPU" );
    device_kind = CL_DEVICE_TYPE_GPU;
    platform = FindPlatformWithDeviceType(platforms_list, numPlatforms, device_kind);
  }
  else { //cpu
    fprintf( stdout, "\nUSING CPU" );
    device_kind = CL_DEVICE_TYPE_CPU;
    platform = FindPlatformWithDeviceType(platforms_list, numPlatforms, device_kind);
  }

  free(platforms_list);
#ifdef __DEBUG
  PrintPlatform( (* platform) );
#endif 

  /* Initialize the Devices */
  if ((status = clGetDeviceIDs( platform , device_kind, 0, NULL, &numDevices ) ) != CL_SUCCESS) {
    fprintf( stderr, "platform[%p]: Unable to query the number of devices: %s\n", platform, CLErrString( status ) );
    exit( 1 );
   }

#ifdef __DEBUG
  fprintf( stdout, "platform[%p]: Found a device.\n", (* platform) );
#endif

   if ((status = clGetDeviceIDs(  platform, device_kind, 1, device, NULL)) != CL_SUCCESS) {
     fprintf ( stderr, "platform[%p]: Unable to enumerate the devices: %s\n",  platform, CLErrString( status ) );
     exit( 1 );
   }

   (* context) = clCreateContext( NULL, 1, device, NULL, NULL, &status );
  
   if ( status != CL_SUCCESS ) {
     fprintf ( stderr, "platform[%p]: Unable to init OpenCL context: %s\n", platform, CLErrString( status ) );
     exit( 1 );     
   }

   (* cmdQueue) = clCreateCommandQueue( (* context), (* device), 0, &status );
  
   if ( status != CL_SUCCESS ) {
     fprintf ( stderr, "platform[%p]: Unable to init OpenCL command queue: %s\n", platform, CLErrString( status ) );
     exit( 1 );
   }

   return CL_SUCCESS;

}