示例#1
0
std::list<OpenCLDevice> OpenCLDevice::getDevices()
{
  cl_uint platform_number, device_number, i, j;
  cl_platform_id * platform_id;
  cl_device_id * device_id;
  cl_int err;
  
  std::list<OpenCLDevice> devices_list;

  //seaching of platforms available
  if ((err = clGetPlatformIDs(0, NULL, &platform_number)) != 0)
  {
    throw OpenCLDeviceException("Can't get number of OpenCL platforms", err);
  }
  
  platform_id = new cl_platform_id[platform_number];

  if ((err = clGetPlatformIDs(platform_number,  platform_id, NULL)) != 0)
  {
    throw OpenCLDeviceException("Can't get OpenCL platforms ids", err);
  }
  //std::cout << "Platforms: " << platform_number << "\n";
  for(i=0;i<platform_number;++i) 
  {
    if((err = clGetDeviceIDs(platform_id[i],CL_DEVICE_TYPE_ALL, 0, NULL, &device_number)) != 0)
    {
      throw OpenCLDeviceException("Can't get number of devices on platform", err);
    }
    
    device_id = new cl_device_id[device_number];
    
    if((err = clGetDeviceIDs(platform_id[i],CL_DEVICE_TYPE_ALL, device_number, device_id, NULL)) != 0)
    {
      throw OpenCLDeviceException("Can't get ids of devices on platform", err);
    }
    //std::cout << "Devices: " << device_number << "\n";
    for(j=0;j<device_number;++j)
    {
      devices_list.push_back(OpenCLDevice(platform_id[i], device_id[j]));
    }
    
    delete[] device_id;
  }
  
  delete[] platform_id;
  return devices_list;
}
示例#2
0
int
OpenCLUtil::init( )
{
  #if OPENCL_TRACE
  std::cerr << "OpenCLUtil::init() begins...\n";
  #endif

  int err;
  char buf[1024];

  if( true == has_init )
    return 0;

  cl_platform_id platform = getPlatform();

  if(platform == NULL)
    return -1;

  // Get device count.
  cl_uint device_count;
#ifdef MAC
  if( CL_SUCCESS != clGetDeviceIDs( platform, CL_DEVICE_TYPE_GPU, 0, NULL, &device_count ) )
    return -1;

  // Get device IDs.
  cl_device_id* device_ids = new cl_device_id[ device_count ];
  if( CL_SUCCESS != clGetDeviceIDs( platform, CL_DEVICE_TYPE_GPU, device_count, device_ids, &device_count ) )
    return -2;
#else
  if( CL_SUCCESS != clGetDeviceIDs( platform, CL_DEVICE_TYPE_ALL, 0, NULL, &device_count ) )
    return -1;

  // Get device IDs.
  cl_device_id* device_ids = new cl_device_id[ device_count ];
  if( CL_SUCCESS != clGetDeviceIDs( platform, CL_DEVICE_TYPE_ALL, device_count, device_ids, &device_count ) )
    return -2;
#endif

  // Create context.
  if( (cl_context)0 == ( context = clCreateContext(0, device_count, device_ids, &pfn_notify, NULL, &err) ) )
    return -3;
  if( CL_SUCCESS != err )
    return -5;

  #if OPENCL_TRACE
  std::cerr << "Created context: " << context << "\n";
  #endif

  // Get device-specific information.
  for( cl_uint i = 0; i < device_count; ++i )
    {
      devices.push_back( OpenCLDevice( device_ids[i] ) );
      #if OPENCL_TRACE
      std::cerr << "Loading device ID: " << device_ids[i] << "\n";
      #endif
      OpenCLDevice* dev_info = &devices.back( );

      // Name
      clGetDeviceInfo( device_ids[i], CL_DEVICE_NAME, sizeof(buf), &buf, NULL );
      dev_info->name = std::string( buf );
      clGetDeviceInfo( device_ids[i], CL_DEVICE_VENDOR, sizeof(buf), &buf, NULL );
      dev_info->vendor = std::string( buf );

      // Compute units & frequency
      clGetDeviceInfo( device_ids[i], CL_DEVICE_MAX_COMPUTE_UNITS,
		       sizeof(dev_info->max_compute_units), &dev_info->max_compute_units, NULL );
      clGetDeviceInfo( device_ids[i], CL_DEVICE_MAX_CLOCK_FREQUENCY,
		       sizeof(dev_info->max_clock_freq), &dev_info->max_clock_freq, NULL );

      // TODO(mangpo):
      // Work-item and work-group properties
      /*std::cerr << "work-item" << std::endl;
      clGetDeviceInfo( device_ids[1], CL_DEVICE_MAX_WORK_ITEM_SIZES,
		       sizeof(dev_info->max_workitem_size), &dev_info->max_workitem_size, NULL );
      std::cerr << "work-group" << std::endl;
      clGetDeviceInfo( device_ids[i], CL_DEVICE_MAX_WORK_GROUP_SIZE,
		       sizeof(dev_info->max_workgroup_size), &dev_info->max_workgroup_size, NULL );*/

      // Memory properties
      clGetDeviceInfo( device_ids[i], CL_DEVICE_GLOBAL_MEM_SIZE,
		       sizeof(dev_info->global_mem_size), &dev_info->global_mem_size, NULL );
      clGetDeviceInfo( device_ids[i], CL_DEVICE_LOCAL_MEM_SIZE,
		       sizeof(dev_info->local_mem_size), &dev_info->local_mem_size, NULL );

      //std::cout << "local mem size = " << dev_info->local_mem_size << std::endl;
      // Queue properties
      cl_command_queue_properties queue_props;
      clGetDeviceInfo( device_ids[i], CL_DEVICE_QUEUE_PROPERTIES,
		       sizeof(queue_props), &queue_props, NULL );
      dev_info->has_queue_outoforder_exec = queue_props & CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE;
      dev_info->has_queue_profiling = queue_props & CL_QUEUE_PROFILING_ENABLE;

      // Create queue
      if( (cl_command_queue)0 == ( dev_info->queue =
				clCreateCommandQueue( context, device_ids[i], 0, &err ) ) )
	      return -4;
      if( CL_SUCCESS != err )
	      return -6;

      #if OPENCL_TRACE
      std::cerr << "Created command queue: " << dev_info->queue << "\n";
      #endif
    }

  // Clean up.
  delete[] device_ids;

  has_init = true;
  #if OPENCL_TRACE
  std::cerr << "OpenCLUtil::init() finishes...\n";
  #endif
  return 0;
}