From 7d667b2831c2fcc536e965a5906c7bdcdbd161ff Mon Sep 17 00:00:00 2001 From: Alan Dayton <6393677+adayton1@users.noreply.github.com> Date: Fri, 30 Aug 2024 11:17:00 -0700 Subject: [PATCH] Fix pre-main crash in CUDA builds (#905) --- .gitignore | 1 + src/umpire/resource/MemoryResourceTypes.hpp | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 097d44045..c0411b1e2 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ CMakeFiles .devcontainer *spack.lock* .spack_env/*/.spack-env +*.orig diff --git a/src/umpire/resource/MemoryResourceTypes.hpp b/src/umpire/resource/MemoryResourceTypes.hpp index 19fb61098..6f0ebb29e 100644 --- a/src/umpire/resource/MemoryResourceTypes.hpp +++ b/src/umpire/resource/MemoryResourceTypes.hpp @@ -8,7 +8,6 @@ #define UMPIRE_MemoryResourceTypes_HPP #include -#include #include #include "umpire/config.hpp" @@ -16,6 +15,8 @@ #if defined(UMPIRE_ENABLE_CUDA) #include +#else +#include #endif /* UMPIRE_ENABLE_CUDA */ #if defined(UMPIRE_ENABLE_HIP) @@ -106,14 +107,23 @@ inline MemoryResourceType string_to_resource(const std::string& resource) inline int resource_to_device_id(const std::string& resource) { + int device_id{0}; + +#if defined(UMPIRE_ENABLE_CUDA) + if (resource.find("::") != std::string::npos) { + device_id = std::stoi(resource.substr(resource.find("::") + 2)); + } +#else const std::regex id_regex{R"(.*::(\d+))", std::regex_constants::ECMAScript | std::regex_constants::optimize}; std::smatch m; - int device_id{0}; if (std::regex_match(resource, m, id_regex)) { device_id = std::stoi(m[1]); - } else { -// get the device bound to the current process + } +#endif + else { + // get the device bound to the current process + #if defined(UMPIRE_ENABLE_CUDA) cudaGetDevice(&device_id); #endif /* UMPIRE_ENABLE_CUDA */ @@ -122,6 +132,7 @@ inline int resource_to_device_id(const std::string& resource) hipGetDevice(&device_id); #endif /* UMPIRE_ENABLE_HIP */ } + return device_id; }