1

I was trying the code with SYCL/DPC++. I have two GPUs present on my device. How can I specify that my code needs to run on a particular GPU device? When I am trying to run my code using "gpu-selector" only one default one is used to run. How can I use the other GPU device in order to run my code?

Here is my code.

#include <iostream>
#include <CL/sycl.hpp>
using namespace sycl;
using namespace std;
int main() {
queue my_gpu( gpu_selector{} );
cout << "My GPU Device: " <<
my_gpu.get_device().get_info<info::device::name>() << "\n";
return 0;
}

Can someone help me out with how can I run my code on a particular GPU device?

Thanks in Advance!

sv6
  • 11
  • 4

2 Answers2

1

Yes, it is possible to select a particular GPU device. Please find the below code to get the results from a specific GPU device.

class my_selector : public device_selector {
public:
int operator()(const device &dev) const override {
if (
dev.get_info<info::device::name>().find("gpu_vendor_name")
!= std::string::npos &&
dev.get_info<info::device::vendor>().find("gpu_device_name")
!= std::string::npos)
return 1;
}
return -1;
}
};

In this code, we can specify the name of the GPU vendor in ("gpu_vendor_name") as per your requirement. If we have two GPU devices with the same vendor then we can also specify the one we want to run code in the GPU device("gpu_device_name").

The highest return value will be selected to run the code on the GPU device which we want.

0

The answer by Varsha is a good general solution.

But since your question is tagged with DPC++, I think it is worth mentioning an alternative approach:

You can set SYCL_DEVICE_FILTER environment variable to control device detection results. E.g., SYCL_DEVICE_FILTER=opencl:gpu:1 will make it so only the second GPU in the OpenCL backend is visible by the application. It will even hide the Host device.

That is DPC++-specific, and will not work with other implementations. But, for example, with hipSYCL you can use CUDA_VISIBLE_DEVICES or HIP_VISIBLE_DEVICES to achieve similar results.

aland
  • 4,829
  • 2
  • 24
  • 42