From ee53ef507f3d56aed3186e431fe67dc18e08fe2f Mon Sep 17 00:00:00 2001 From: pensun Date: Sat, 27 Feb 2016 14:48:00 -0600 Subject: [PATCH] Test cases for HIP_VISIBLE_DEVICES/CUDA_VISIBLE_DEVICES. hipEnvVar is the base test case, to be called by hipEnvVarDriver at the run time. Test case includes tests for normal use case of the environment variable, invalid value/sequence and use CUDA_VISIBLE_DEVICES as a alternative. --- hipamd/src/hip_hcc.cpp | 24 ++++---- hipamd/tests/src/CMakeLists.txt | 3 + hipamd/tests/src/hipEnvVar.cpp | 44 ++++++++++---- hipamd/tests/src/hipEnvVarDriver.cpp | 91 ++++++++++++++++++++++++---- 4 files changed, 125 insertions(+), 37 deletions(-) diff --git a/hipamd/src/hip_hcc.cpp b/hipamd/src/hip_hcc.cpp index e9de7f53a6..a96227dc7f 100644 --- a/hipamd/src/hip_hcc.cpp +++ b/hipamd/src/hip_hcc.cpp @@ -300,7 +300,7 @@ unsigned g_deviceCnt; //Forward Declarations: //================================================================================================= INLINE bool ihipIsValidDevice(unsigned deviceIndex); -INLINE bool ihipIsVisibleDevice(unsigned deviceIndex); + //================================================================================================= // Implementation: //================================================================================================= @@ -967,25 +967,23 @@ void ihipInit() for (int i=0; i"); + } INLINE bool ihipIsValidDevice(unsigned deviceIndex) @@ -994,12 +992,12 @@ INLINE bool ihipIsValidDevice(unsigned deviceIndex) return (deviceIndex < g_deviceCnt); } -// check if the device ID is set as visible -INLINE bool ihipIsVisibleDevice(unsigned deviceIndex) -{ - return std::find(g_hip_visible_devices.begin(), g_hip_visible_devices.end(), - (int)deviceIndex) != g_hip_visible_devices.end(); -} +/*// check if the device ID is set as visible*/ +//INLINE bool ihipIsVisibleDevice(unsigned deviceIndex) +//{ + //return std::find(g_hip_visible_devices.begin(), g_hip_visible_devices.end(), + //(int)deviceIndex) != g_hip_visible_devices.end(); +/*}*/ //--- INLINE ihipDevice_t *ihipGetTlsDefaultDevice() diff --git a/hipamd/tests/src/CMakeLists.txt b/hipamd/tests/src/CMakeLists.txt index cc6af0b5d2..49ab081766 100644 --- a/hipamd/tests/src/CMakeLists.txt +++ b/hipamd/tests/src/CMakeLists.txt @@ -110,6 +110,8 @@ make_hip_executable (hip_clz hip_clz.cpp) make_hip_executable (hip_brev hip_brev.cpp) make_hip_executable (hip_ffs hip_ffs.cpp) make_hip_executable (hipGetDeviceAttribute hipGetDeviceAttribute.cpp) +make_hip_executable (hipEnvVar hipEnvVar.cpp) +make_hip_executable (hipEnvVarDriver hipEnvVarDriver.cpp) make_hip_executable (hipMemcpy hipMemcpy.cpp) make_hip_executable (hipMemcpyAsync hipMemcpyAsync.cpp) make_hip_executable (hipMemset hipMemset.cpp) @@ -137,6 +139,7 @@ make_test(hipMemset --N 10 --memsetval 0x42 ) # small copy, just 10 bytes. make_test(hipMemset --N 10013 --memsetval 0x5a ) # oddball size. make_test(hipMemset --N 256M --memsetval 0xa6 ) # big copy make_test(hipGridLaunch " " ) +make_test(hipEnvVarDriver " " ) make_test(hipPointerAttrib " " ) make_test(hipMultiThreadStreams " " ) diff --git a/hipamd/tests/src/hipEnvVar.cpp b/hipamd/tests/src/hipEnvVar.cpp index e0d834d43f..6f9047776c 100644 --- a/hipamd/tests/src/hipEnvVar.cpp +++ b/hipamd/tests/src/hipEnvVar.cpp @@ -28,24 +28,27 @@ THE SOFTWARE. #include #include -int debug = 0; +using namespace std; void usage() { printf("hipEnvVar [otpions]\n\ - -a,\t\ttotal number ofavailable GPUs and their pciBusID\n\ - -s,\t\tselect one GPU and return its pciBusID\n\ + -c,\t\ttotal number ofavailable GPUs and their pciBusID\n\ + -d,\t\tselect one GPU and return its pciBusID\n\ + -v,\t\tsend the list to HIP_VISIBLE_DEVICES env var\n\ -h,\t\tshow this help message\n\ "); } int main(int argc, char **argv) { + //string str = getenv("HIP_VISIBLE_DEVICES"); + //std::cout << "The current env HIP_VISIBLE_DEVICES is"< +#include #include - +#include +#include using namespace std; -int main() { - FILE *in; - char buff[512]; +//./hipEnvVar -c -d 0 -h + //putenv("SomeVariable=SomeValue"); + //putenv("export HIP_VISIBLE_DEVICES=0,1,2,3"); - if(!(in = popen("ls -sail", "r"))){ +int getDeviceNumber(){ + FILE *in; + char buff[512]; + string str; + if(!(in = popen("./hipEnvVar -c", "r"))){ return 1; } - - while(fgets(buff, sizeof(buff), in)!=NULL){ - cout << buff; - } + fgets(buff, sizeof(buff), in); pclose(in); - - return 0; - + return atoi(buff); +} + +int getDevicePCIBusNum(int deviceID){ + FILE *in; + char buff[512]; + string str = "./hipEnvVar -d "; + str += std::to_string(deviceID); + if(!(in = popen(str.c_str(), "r"))){ + return 1; + } + fgets(buff, sizeof(buff), in); + pclose(in); + return atoi(buff); +} + +int main() { + unsetenv("HIP_VISIBLE_DEVICES"); + //collect the device pci bus ID for all devices + int totalDeviceNum = getDeviceNumber(); + std::cout << "The total number of available devices is " << totalDeviceNum<< std::endl + <<"Valid index range is 0 - "< devPCINum; + for (int i = 0; i < totalDeviceNum ; i++) { + devPCINum.push_back(getDevicePCIBusNum(i)); + std::cout <<"The collected device PCI Bus ID of Device "< 2){ + setenv("HIP_VISIBLE_DEVICES","0,1,1000,2",1); + assert(getDeviceNumber() == 2); + + setenv("HIP_VISIBLE_DEVICES","0,1,2",1); + assert(getDeviceNumber() == 3); + // test if CUDA_VISIBLE_DEVICES will be accepted by the runtime + unsetenv("HIP_VISIBLE_DEVICES"); + setenv("CUDA_VISIBLE_DEVICES","0,1,2",1); + assert(getDeviceNumber() == 3); + } + + setenv("HIP_VISIBLE_DEVICES","-100,0,1",1); + assert(getDeviceNumber() == 0); + + std::cout << "Passed!" << std::endl; + return 0; }