title: OpenCL not allowing me to select a device
body: <p>I have a problem with my code as when I select an option, I cannot seem to be able to select a device from OpenCL platforms. I am able to choose the device TYPE but once I select it, the program skips the whole selection process and goes straight to saying that I have an Invalid Option and that device was not selected. Below shows a code snippet of what my program does and a picture of the output that I currently have.</p>
  
  <p>I coded as such that when I select the device, the program registers and checks that I have entered an integer before referencing the platform device. Any help would be appreciated. Thanks.</p>
  
  <pre><code> bool select_one_device(cl::Platform* platfm, cl::Device* dev)
  {
      std::vector&lt;cl::Platform&gt; platforms;    // available platforms
      std::vector&lt; std::vector&lt;cl::Device&gt; &gt; platformDevices; // devices available for each platform
      std::vector&lt;::size_t&gt; 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(&amp;platforms);
  
          // find and store the devices available to each platform
          for (i = 0; i &lt; platforms.size(); i++)
          {
              std::vector&lt;cl::Device&gt; devices;        // available devices
  
              // get all devices available to the platform
              platforms[i].getDevices(CL_DEVICE_TYPE_ALL, &amp;devices);
  
              // store available devices for the platform
              platformDevices.push_back(devices);
          }
  
          // store options as platform and device indices
          std::vector&lt; std::pair&lt;int, int&gt; &gt; options;
          unsigned int optionCounter = 0; // option counter
  
          unsigned int choice;
          std::cout &lt;&lt; "Do you want to use a CPU or GPU device?" &lt;&lt; std::endl;
          std::cout &lt;&lt; "1. CPU" &lt;&lt; std::endl;
          std::cout &lt;&lt; "2. GPU" &lt;&lt; std::endl;
          std::cout &lt;&lt; "Enter choice: ";
          std::cin &gt;&gt; choice;
  
      // for all platforms
      for (i = 0; i &lt; platforms.size(); i++)
      {
          // for all devices per platform
          for (j = 0; j &lt; platformDevices[i].size(); j++)
          {
              //checks the device type
              cl_device_type type;
              platformDevices[i][j].getInfo(CL_DEVICE_TYPE, &amp;type);
              if (type == CL_DEVICE_TYPE_CPU &amp;&amp; choice == 1)
              {
                  //Option number
                  std::cout &lt;&lt; "Option " &lt;&lt; optionCounter &lt;&lt; std::endl;
                  //outputs the platform and device number
                  std::cout &lt;&lt; "\tPlatform #" &lt;&lt; i &lt;&lt; " - " &lt;&lt; "Device #" &lt;&lt; j &lt;&lt; std::endl;
                  //outputs the device type
                  std::cout &lt;&lt; "\tType: " &lt;&lt; "CPU" &lt;&lt; std::endl;
                  // get and output device name
                  outputString = platformDevices[i][j].getInfo&lt;CL_DEVICE_NAME&gt;();
                  std::cout &lt;&lt; "\tName: " &lt;&lt; outputString &lt;&lt; std::endl;
                  // get and output device vendor
                  outputString = platformDevices[i][j].getInfo&lt;CL_DEVICE_VENDOR&gt;();
                  std::cout &lt;&lt; "\tVendor: " &lt;&lt; outputString &lt;&lt; std::endl;
                  //get and output compute units
                  std::cout &lt;&lt; "\tNumber of compute units: " &lt;&lt; platformDevices[i][j].getInfo&lt;CL_DEVICE_MAX_COMPUTE_UNITS&gt;() &lt;&lt; std::endl;
                  //get and output workgroup size
                  std::cout &lt;&lt; "\tMaximum work group size: " &lt;&lt; platformDevices[i][j].getInfo&lt;CL_DEVICE_MAX_WORK_GROUP_SIZE&gt;() &lt;&lt; std::endl;
                  //get and output workitem size
                  maxWorkItems = platformDevices[i][j].getInfo&lt;CL_DEVICE_MAX_WORK_ITEM_SIZES&gt;();
                  std::cout &lt;&lt; "\tMaximum work item size: " &lt;&lt; maxWorkItems[0] &lt;&lt; std::endl;
                  //get and output local memory size
                  std::cout &lt;&lt; "\tLocal memory size: " &lt;&lt; platformDevices[i][j].getInfo&lt;CL_DEVICE_LOCAL_MEM_SIZE&gt;() &lt;&lt; std::endl;
                  std::cout &lt;&lt; std::endl;
  
                  // store option
                  options.push_back(std::make_pair(i, j));
                  optionCounter++; // increment option counter
              }
  
   std::cout &lt;&lt; "\n--------------------" &lt;&lt; std::endl;
      std::cout &lt;&lt; "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 &gt;&gt; selectedOption)
      {
          char c;
  
          // check if there was anything after the integer
          if (!(stringStream &gt;&gt; c))
          {
              // check if valid option range
              if (selectedOption &gt;= 0 &amp;&amp; selectedOption &lt; 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 &lt;&lt; "\n--------------------" &lt;&lt; std::endl;
      std::cout &lt;&lt; "Invalid option." &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/JleTh.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JleTh.jpg" alt="undesired output"></a></p>
  
  <p>The "device not selected" line comes from my main method's try block.</p>