OpenCL不允许我选择设备

时间:2019-07-18 14:47:47

标签: c++ opencl

我的代码有问题,因为当我选择一个选项时,似乎无法从OpenCL平台中选择设备。我可以选择设备类型,但是一旦选择它,程序将跳过整个选择过程,直接说我有一个无效的选件,而该设备未被选中。下面显示了我的程序的代码片段以及当前输出的图片。

我的编码方式是,当我选择设备时,程序会注册并检查我是否在引用平台设备之前输入了整数。任何帮助,将不胜感激。谢谢。

 bool select_one_device(cl::Platform* platfm, cl::Device* dev)
{
    std::vector<cl::Platform> platforms;    // available platforms
    std::vector< std::vector<cl::Device> > platformDevices; // devices available for each platform
    std::vector<::size_t> maxWorkItems;     // vector for workitem size
    std::string outputString;               // string for output
    std::string choice;                     // user input choice
    unsigned int i, j;                              // counters

    try {
        // get the number of available OpenCL platforms
        cl::Platform::get(&platforms);

        // find and store the devices available to each platform
        for (i = 0; i < platforms.size(); i++)
        {
            std::vector<cl::Device> devices;        // available devices

            // get all devices available to the platform
            platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &devices);

            // store available devices for the platform
            platformDevices.push_back(devices);
        }

        // store options as platform and device indices
        std::vector< std::pair<int, int> > options;
        unsigned int optionCounter = 0; // option counter

        unsigned int choice;
        std::cout << "Do you want to use a CPU or GPU device?" << std::endl;
        std::cout << "1. CPU" << std::endl;
        std::cout << "2. GPU" << std::endl;
        std::cout << "Enter choice: ";
        std::cin >> choice;

    // for all platforms
    for (i = 0; i < platforms.size(); i++)
    {
        // for all devices per platform
        for (j = 0; j < platformDevices[i].size(); j++)
        {
            //checks the device type
            cl_device_type type;
            platformDevices[i][j].getInfo(CL_DEVICE_TYPE, &type);
            if (type == CL_DEVICE_TYPE_CPU && choice == 1)
            {
                //Option number
                std::cout << "Option " << optionCounter << std::endl;
                //outputs the platform and device number
                std::cout << "\tPlatform #" << i << " - " << "Device #" << j << std::endl;
                //outputs the device type
                std::cout << "\tType: " << "CPU" << std::endl;
                // get and output device name
                outputString = platformDevices[i][j].getInfo<CL_DEVICE_NAME>();
                std::cout << "\tName: " << outputString << std::endl;
                // get and output device vendor
                outputString = platformDevices[i][j].getInfo<CL_DEVICE_VENDOR>();
                std::cout << "\tVendor: " << outputString << std::endl;
                //get and output compute units
                std::cout << "\tNumber of compute units: " << platformDevices[i][j].getInfo<CL_DEVICE_MAX_COMPUTE_UNITS>() << std::endl;
                //get and output workgroup size
                std::cout << "\tMaximum work group size: " << platformDevices[i][j].getInfo<CL_DEVICE_MAX_WORK_GROUP_SIZE>() << std::endl;
                //get and output workitem size
                maxWorkItems = platformDevices[i][j].getInfo<CL_DEVICE_MAX_WORK_ITEM_SIZES>();
                std::cout << "\tMaximum work item size: " << maxWorkItems[0] << std::endl;
                //get and output local memory size
                std::cout << "\tLocal memory size: " << platformDevices[i][j].getInfo<CL_DEVICE_LOCAL_MEM_SIZE>() << std::endl;
                std::cout << std::endl;

                // store option
                options.push_back(std::make_pair(i, j));
                optionCounter++; // increment option counter
            }

 std::cout << "\n--------------------" << std::endl;
    std::cout << "Select a device: ";

    std::string inputString;
    unsigned int selectedOption;    // option that was selected

    std::getline(std::cin, inputString);
    std::istringstream stringStream(inputString);

    // check whether valid option was selected
    // check if input was an integer
    if (stringStream >> selectedOption)
    {
        char c;

        // check if there was anything after the integer
        if (!(stringStream >> c))
        {
            // check if valid option range
            if (selectedOption >= 0 && selectedOption < optionCounter)
            {
                // return the platform and device
                int platformNumber = options[selectedOption].first;
                int deviceNumber = options[selectedOption].second;

                *platfm = platforms[platformNumber];
                *dev = platformDevices[platformNumber][deviceNumber];

                return true;
            }
        }
    }
    // if invalid option selected
    std::cout << "\n--------------------" << std::endl;
    std::cout << "Invalid option." << std::endl;
}

undesired output

“未选择设备”行来自我主要方法的try块。

1 个答案:

答案 0 :(得分:2)

  1. 问题与OpenCL无关。

  2. 问题是您通过运算符>>从cin读取的内容混合在一起,并使用std :: getline()。 “ cin >> variable”不会占用尾随的换行符,因此以后调用getline()时,您会立即得到一个空字符串。

显而易见的解决方案是,当您要求用户在CPU和GPU设备之间进行选择时,使用std :: readline()。