コード例 #1
0
ファイル: OpenCLWrappers.cpp プロジェクト: billw2012/BGalaxy1
std::vector<cl_platform_id> get_platform_ids()
{
	cl_uint numPlatforms;
	::clGetPlatformIDs(0, NULL, &numPlatforms);
	std::vector<cl_platform_id> platformIDs(numPlatforms);
	::clGetPlatformIDs(numPlatforms, &platformIDs[0], NULL);
	return platformIDs;
	//std::vector<cl_platform_id> platformIDs;
	//for(PlatformMap::const_iterator pItr = gPlatforms.begin(); pItr != gPlatforms.end(); ++pItr)
	//{
	//	platformIDs.push_back(pItr->first);
	//}
	//return platformIDs;
}
//------------------------------------------------------------------------------
// returns context associated with single device only,
// to make it support multiple devices, a list of
// <device type, device num> pairs is required
cl_context create_cl_context(const std::string& platformName,
                             const std::string& deviceTypeName,
                             int deviceNum) {
    cl_int status = 0;
    //1) get platfors and search for platform matching platformName
    cl_uint numPlatforms = 0;
    status = clGetPlatformIDs(0, 0, &numPlatforms);
    check_cl_error(status, "clGetPlatformIDs");
    if(numPlatforms < 1) {
        std::cout << "No OpenCL platforms found" << std::endl;
        exit(EXIT_SUCCESS);
    }
    typedef std::vector< cl_platform_id > PlatformIDs;
    PlatformIDs platformIDs(numPlatforms);
    status = clGetPlatformIDs(numPlatforms, &platformIDs[0], 0);
    check_cl_error(status, "clGetPlatformIDs");
    std::vector< char > buf(0x10000, char(0));
    cl_platform_id platformID;
    PlatformIDs::const_iterator pi = platformIDs.begin();
    for(; pi != platformIDs.end(); ++pi) {
        status = clGetPlatformInfo(*pi, CL_PLATFORM_NAME,
                                 buf.size(), &buf[0], 0);
        check_cl_error(status, "clGetPlatformInfo");
        if(platformName == &buf[0]) {
            platformID = *pi;
            break; 
        }
    } 
    if(pi == platformIDs.end()) {
        std::cerr << "ERROR - Couldn't find platform " 
                  << platformName << std::endl;
        exit(EXIT_FAILURE);
    }
    //2) get devices of deviceTypeName type and store their ids into
    //   an array then select device id at position deviceNum
    cl_device_type deviceType;
    if(deviceTypeName == "default") 
        deviceType = CL_DEVICE_TYPE_DEFAULT;
    else if(deviceTypeName == "cpu")
        deviceType = CL_DEVICE_TYPE_CPU;
    else if(deviceTypeName == "gpu")
        deviceType = CL_DEVICE_TYPE_GPU;
    else if(deviceTypeName == "acc")
        deviceType = CL_DEVICE_TYPE_ACCELERATOR; 
    else if(deviceTypeName == "all")
        deviceType = CL_DEVICE_TYPE_CPU;
    else {
        std::cerr << "ERROR - device type " << deviceTypeName << " unknown"
                  << std::endl;
        exit(EXIT_FAILURE);          
    }                      
    cl_uint numDevices = 0; 
    status = clGetDeviceIDs(platformID, deviceType, 0, 0, &numDevices);
    check_cl_error(status, "clGetDeviceIDs");
    if(numDevices < 1) {
        std::cerr << "ERROR - Cannot find device of type " 
                  << deviceTypeName << std::endl;
        exit(EXIT_FAILURE);          
    }
    typedef std::vector< cl_device_id > DeviceIDs;
    DeviceIDs deviceIDs(numDevices);
    status = clGetDeviceIDs(platformID, deviceType, numDevices,
                            &deviceIDs[0], 0);
    check_cl_error(status, "clGetDeviceIDs");
    if(deviceNum < 0 || deviceNum >= numDevices) {
        std::cerr << "ERROR - device number out of range: [0," 
                  << (numDevices - 1) << ']' << std::endl;
        exit(EXIT_FAILURE);
    }
    cl_device_id deviceID = deviceIDs[deviceNum]; 
    //3) create and return context
    cl_context_properties ctxProps[] = {
        CL_CONTEXT_PLATFORM,
        cl_context_properties(platformID),
        0
    };
    //only a single device supported
    cl_context ctx = clCreateContext(ctxProps, 1, &deviceID,
                                     &context_callback, 0, &status);
    check_cl_error(status, "clCreateContext");
    return ctx;
}
コード例 #3
0
	int XdevLComputeDeviceCL::init() {
		cl_int ret;

		cl_uint numPlatforms;
		ret = clGetPlatformIDs(1, &m_platformID, &numPlatforms);
		if(CL_SUCCESS != ret) {
			XDEVL_MODULEX_ERROR(XdevLComputeDeviceCL, "clGetPlatformIDs failed: " << clErrorAsString(ret) << std::endl);
			return -1;
		}

		std::vector<cl_platform_id> platformIDs(numPlatforms);

		ret = clGetPlatformIDs(1, platformIDs.data(), nullptr);

		for(auto platform : platformIDs) {
			XdevLPlatformInfo info;
			info.id = platform;

			std::size_t size;
			ret = clGetPlatformInfo(platform, CL_PLATFORM_PROFILE, sizeof(info.profile), (void*)info.profile, &size);
			ret = clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(info.version), (void*)info.version, &size);
			ret = clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(info.name), (void*)info.name, &size);
			ret = clGetPlatformInfo(platform, CL_PLATFORM_VENDOR, sizeof(info.vendor), (void*)info.vendor, &size);

			if(CL_SUCCESS != ret) {
				XDEVL_MODULEX_ERROR(XdevLComputeDeviceCL, "clGetPlatformInfo failed: " << clErrorAsString(ret) << std::endl);
				return -1;
			}

			std::cout << "Platform Name       : " << info.name << std::endl;
			std::cout << "Platform Profile    : " << info.profile << std::endl;
			std::cout << "Platform Version    : " << info.version << std::endl;
			std::cout << "Platform Vendor     : " << info.vendor << std::endl;


			m_platforms.push_back(std::move(info));
		}

		for(auto platform : m_platforms) {
			cl_uint numDevices;
			ret = clGetDeviceIDs(platform.id, CL_DEVICE_TYPE_ALL, 1, &m_deviceID, &numDevices);
			if(CL_SUCCESS != ret) {
				XDEVL_MODULEX_ERROR(XdevLComputeDeviceCL, "clGetDeviceIDs failed: " << clErrorAsString(ret) << std::endl);
				return -1;
			}
			std::cout << "OpenCL Number of devices: " << numDevices << ", ID: " << m_deviceID << std::endl;

			XdevLDeviceInfo device;
			device.id = m_deviceID;

			std::size_t size;
			ret = clGetDeviceInfo(m_deviceID, CL_DEVICE_NAME, sizeof(device.name), (void*)device.name, &size);
			ret = clGetDeviceInfo(m_deviceID, CL_DEVICE_VENDOR, sizeof(device.vendor), (void*)device.vendor, &size);
			ret = clGetDeviceInfo(m_deviceID, CL_DRIVER_VERSION, sizeof(device.version), (void*)device.version, &size);
			ret = clGetDeviceInfo(m_deviceID, CL_DEVICE_PROFILE, sizeof(device.profile), (void*)device.profile, &size);

			if(CL_SUCCESS != ret) {
				return -1;
			}

			std::cout << "Device Profile    : " << device.profile << std::endl;
			std::cout << "Device Name       : " << device.name << std::endl;
			std::cout << "Device Version    : " << device.version << std::endl;
			std::cout << "Device Vendor     : " << device.vendor << std::endl;


			m_devices.push_back(std::move(device));
		}
		return 0;
	}