From 43e156c0029ecdcef2b4d8055bcb2b024ca60d86 Mon Sep 17 00:00:00 2001 From: pensun Date: Sat, 27 Feb 2016 09:43:38 -0600 Subject: [PATCH] add test case and its driver for HIP_VISIBLE_DEVICES/CUDA_VISIBLE_DEVICES --- hipamd/tests/src/hipEnvVar.cpp | 102 +++++++++++++++++++++++++++ hipamd/tests/src/hipEnvVarDriver.cpp | 38 ++++++++++ 2 files changed, 140 insertions(+) create mode 100644 hipamd/tests/src/hipEnvVar.cpp create mode 100644 hipamd/tests/src/hipEnvVarDriver.cpp diff --git a/hipamd/tests/src/hipEnvVar.cpp b/hipamd/tests/src/hipEnvVar.cpp new file mode 100644 index 0000000000..e0d834d43f --- /dev/null +++ b/hipamd/tests/src/hipEnvVar.cpp @@ -0,0 +1,102 @@ +/* +Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + + +#include +#include +#include +#include +#include +#include + +int debug = 0; + +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\ + -h,\t\tshow this help message\n\ + "); +} +int main(int argc, char **argv) +{ + extern char *optarg; + extern int optind; + int c, err = 0; + int retDevCnt=0, retDevInfo=0; + int device=0; + //std::cout << "reach here!!" << std::endl; + while ((c = getopt(argc, argv, "cd:h")) != -1) + switch (c) { + case 'c': + retDevCnt = true; + break; + case 'd': + retDevInfo = true; + device = atoi(optarg); + break; + case 'h': + usage(); + return 0; + break; + default : + //usage(); + return -1; + break; + case '?': + err = 1; + break; + } + // device init + int devCount=0; + hipGetDeviceCount(&devCount); + + //printf("\nTotal number of GPU devices in the system is %d\n",devCount); + + if (devCount == 0) { + printf("No HIP enabled device\n"); + return -1; + } + if (device < 0 || device > devCount -1) { + printf("Selected device %d is out of bound. Devices on your system are in range %d - %d\n", + device, 0, devCount -1); + return -1; + } + if (retDevCnt) { + std::cout << "Total number of devices visible in system is "<< devCount << std::endl; + } + if (retDevInfo) { + hipSetDevice(device); + hipDeviceProp_t devProp; + + hipDeviceGetProperties(&devProp, device); + if (devProp.major < 1) { + printf("Device %d does not support HIP\n", device); + return -1; + } + + std::cout << "The selected device pciBusID is " << devProp.pciBusID << std::endl; + } + + exit(0); +} + diff --git a/hipamd/tests/src/hipEnvVarDriver.cpp b/hipamd/tests/src/hipEnvVarDriver.cpp new file mode 100644 index 0000000000..df8d2c0f2c --- /dev/null +++ b/hipamd/tests/src/hipEnvVarDriver.cpp @@ -0,0 +1,38 @@ +/* Copyright (c) 2015-2016 Advanced Micro Devices, Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT +LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO +EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR +THE USE OR OTHER DEALINGS IN TcHE SOFTWARE. */ + +#include +#include + +using namespace std; + +int main() { + FILE *in; + char buff[512]; + + if(!(in = popen("ls -sail", "r"))){ + return 1; + } + + while(fgets(buff, sizeof(buff), in)!=NULL){ + cout << buff; + } + pclose(in); + + return 0; + +}