Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix device selection logic #402

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 35 additions & 20 deletions clic/src/openclbackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,29 +179,40 @@ auto
OpenCLBackend::getDevice(const std::string & name, const std::string & type) const -> Device::Pointer
{
#if USE_OPENCL
auto devices = getDevices(type);

// if no device in system, return nullptr
if (device_list_.empty())
{
std::cerr << "Warning: Fail to find any OpenCL compatible devices." << std::endl;
return nullptr;
}

// if no device of the specified type, return the last device from system
const auto devices = getDevices(type);
if (devices.empty())
{
return device_list_.back();
}
if (!name.empty())

// if no specific device name, return the last device of the specified type
if (name.empty())
{
auto lower_case_name = to_lower(name);
auto ite = std::find_if(devices.begin(), devices.end(), [&lower_case_name](const Device::Pointer & dev) {
return (to_lower(dev->getName()).find(lower_case_name) != std::string::npos);
});
if (ite != devices.end())
{
return *ite;
}
return devices.back();
}
if (!devices.empty())

// search for the device by name
auto lower_case_name = to_lower(name);
auto ite = std::find_if(devices.begin(), devices.end(), [&lower_case_name](const Device::Pointer & dev) {
return (to_lower(dev->getName()).find(lower_case_name) != std::string::npos);
});
if (ite != devices.end())
{
return devices.back();
return *ite;
}

std::cerr << "Warning: Fail to find any OpenCL compatible devices." << std::endl;
return nullptr;
// if no device found, return the last device of the specified type
return devices.back();

#else
throw std::runtime_error("Error: OpenCL is not enabled");
#endif
Expand All @@ -211,21 +222,25 @@ auto
OpenCLBackend::getDeviceFromIndex(size_t index, const std::string & type) const -> Device::Pointer
{
#if USE_OPENCL
// if no device in system, return nullptr
if (device_list_.empty())
{
std::cerr << "Warning: Fail to find any OpenCL compatible devices." << std::endl;
return nullptr;
}

auto devices = getDevices(type);
if (devices.empty())
{
return device_list_.back();
}

if (index < devices.size())
{
return devices[index];
}
if (!devices.empty())
{
return devices.back();
}
std::cerr << "Warning: Fail to find any OpenCL compatible devices." << std::endl;
return nullptr;

return devices.back();
#else
throw std::runtime_error("Error: OpenCL is not enabled");
#endif
Expand Down
Loading