Reputation: 11
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!
Upvotes: 1
Views: 1477
Reputation: 5219
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:
Starting with oneAPI 2023.1, you can set ONEAPI_DEVICE_SELECTOR
environment variable to control device detection results. E.g., ONEAPI_DEVICE_SELECTOR=opencl:1
will make it so only the second device in the OpenCL backend is visible by the application (this can be either CPU or GPU). The syntax is very flexible, you can control how sub-devices are exposed (when working with multi-tile cards), filter by device type, combine several filters etc.
In oneAPI 2023.0 or earlier, the environment variable was called SYCL_DEVICE_FILTER
, and it had slightly different syntax. E.g., SYCL_DEVICE_FILTER=opencl:gpu:0
will make it so only the first GPU in the OpenCL backend is visible by the application.
That is DPC++-specific, and will not work with other implementations. But, for example, with AdaptiveCpp (previously called hipSYCL) you can use CUDA_VISIBLE_DEVICES
or HIP_VISIBLE_DEVICES
to achieve similar results.
Upvotes: 2
Reputation: 95
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.
Upvotes: 1