简体   繁体   中英

How to specify particular GPU Device to be used at the time of running a program in SYCL/DPC++?

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!

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.

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. Eg, 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM