diff --git a/catch/unit/device/getDeviceCount_exe.cc b/catch/unit/device/getDeviceCount_exe.cc index 1da5f7c3a9..bf9951b6d4 100644 --- a/catch/unit/device/getDeviceCount_exe.cc +++ b/catch/unit/device/getDeviceCount_exe.cc @@ -22,6 +22,27 @@ THE SOFTWARE. #include #include +#include + +bool UNSETENV(std::string var) { + int result = -1; + #ifdef __unix__ + result = unsetenv(var.c_str()); + #else + result = _putenv((var + '=').c_str()); + #endif + return (result == 0) ? true: false; +} + +bool SETENV(std::string var, std::string value, int overwrite) { + int result = -1; + #ifdef __unix__ + result = setenv(var.c_str(), value.c_str(), overwrite); + #else + result = _putenv((var + '=' + value).c_str()); + #endif + return (result == 0) ? true: false; +} // Expects 1 command line arg, which is the Device Visible String int main(int argc, char** argv) { @@ -37,18 +58,18 @@ int main(int argc, char** argv) { // disable visible_devices env from shell #ifdef __HIP_PLATFORM_NVCC__ - unsetenv("CUDA_VISIBLE_DEVICES"); - setenv("CUDA_VISIBLE_DEVICES", argv[1], 1); + UNSETENV("CUDA_VISIBLE_DEVICES"); + SETENV("CUDA_VISIBLE_DEVICES", argv[1], 1); auto init_res = hipInit(0); if (hipSuccess != init_res) { std::cerr << "CUDA INIT API returned : " << hipGetErrorString(init_res) << std::endl; return -1; } #else - unsetenv("ROCR_VISIBLE_DEVICES"); - unsetenv("HIP_VISIBLE_DEVICES"); - setenv("ROCR_VISIBLE_DEVICES", argv[1], 1); - setenv("HIP_VISIBLE_DEVICES", argv[1], 1); + UNSETENV("ROCR_VISIBLE_DEVICES"); + UNSETENV("HIP_VISIBLE_DEVICES"); + SETENV("ROCR_VISIBLE_DEVICES", argv[1], 1); + SETENV("HIP_VISIBLE_DEVICES", argv[1], 1); #endif int count = 0; @@ -59,10 +80,10 @@ int main(int argc, char** argv) { } #ifdef __HIP_PLATFORM_NVCC__ - unsetenv("CUDA_VISIBLE_DEVICES"); + UNSETENV("CUDA_VISIBLE_DEVICES"); #else - unsetenv("ROCR_VISIBLE_DEVICES"); - unsetenv("HIP_VISIBLE_DEVICES"); + UNSETENV("ROCR_VISIBLE_DEVICES"); + UNSETENV("HIP_VISIBLE_DEVICES"); #endif return count; } \ No newline at end of file