2016-02-27 09:43:38 -06:00
|
|
|
/*
|
|
|
|
|
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.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-09-23 16:15:31 +05:30
|
|
|
/* HIT_START
|
2019-11-15 07:58:40 +05:30
|
|
|
* BUILD: %t %s test_common.cpp NVCC_OPTIONS -std=c++11
|
2016-09-23 16:15:31 +05:30
|
|
|
* HIT_END
|
|
|
|
|
*/
|
2016-02-27 09:43:38 -06:00
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <iostream>
|
2019-08-09 17:22:10 +05:30
|
|
|
#include "clara/clara.hpp"
|
2016-02-27 09:43:38 -06:00
|
|
|
#include <string>
|
2016-10-04 22:20:50 +05:30
|
|
|
#include "hip/hip_runtime.h"
|
2019-08-15 19:13:00 -07:00
|
|
|
#include "test_common.h"
|
2019-08-09 17:22:10 +05:30
|
|
|
|
2016-02-27 14:48:00 -06:00
|
|
|
using namespace std;
|
2019-08-09 17:22:10 +05:30
|
|
|
using namespace clara;
|
|
|
|
|
inline clara::Parser cmdline_parser(bool& help, std::string& env, int &device, bool &retDevCnt) {
|
|
|
|
|
return clara::Opt{retDevCnt}
|
|
|
|
|
["-c"]
|
|
|
|
|
("total number of GPUs available") |
|
|
|
|
|
|
|
|
|
|
clara::Help{help} |
|
2016-02-27 09:43:38 -06:00
|
|
|
|
2019-08-09 17:22:10 +05:30
|
|
|
clara::Opt{device,"device"}
|
|
|
|
|
["-d"]["--device"]
|
|
|
|
|
("select one GPU and return its pciBusID") |
|
|
|
|
|
|
|
|
|
|
clara::Opt{env,"Set Env Value"}
|
|
|
|
|
["-v"]["--EnvValue"]
|
|
|
|
|
("send the list to HIP_VISIBLE_DEVICES env var, syntax -v=<value>");
|
2016-02-27 09:43:38 -06:00
|
|
|
}
|
2019-08-09 17:22:10 +05:30
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
int main(int argc, char** argv) {
|
2019-08-09 17:22:10 +05:30
|
|
|
bool help = false;
|
|
|
|
|
bool retDevCnt = false;
|
2016-03-03 06:23:17 -06:00
|
|
|
int c = 0;
|
2019-08-09 17:22:10 +05:30
|
|
|
int device = INT_MAX;
|
2016-02-27 14:48:00 -06:00
|
|
|
string env;
|
2019-08-09 17:22:10 +05:30
|
|
|
|
|
|
|
|
auto cmd = cmdline_parser(help, env, device, retDevCnt);
|
|
|
|
|
const auto r = cmd.parse(Args{argc, argv});
|
|
|
|
|
if (!r) { std::cout<<"Valid device must be >= 0"<<std::endl; return -1;}
|
|
|
|
|
|
|
|
|
|
if (help)
|
|
|
|
|
cout << cmd << endl;
|
|
|
|
|
|
|
|
|
|
if (!env.empty()) {
|
2018-03-12 11:29:03 +05:30
|
|
|
setenv("HIP_VISIBLE_DEVICES", env.c_str(), 1);
|
|
|
|
|
setenv("CUDA_VISIBLE_DEVICES", env.c_str(), 1);
|
|
|
|
|
cout << "set env HIP_VISIBLE_DEVICES = " << env.c_str() << endl;
|
2019-08-09 17:22:10 +05:30
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
// verify if the environment variable is set
|
2016-02-27 14:48:00 -06:00
|
|
|
char* pPath;
|
2018-03-12 11:29:03 +05:30
|
|
|
pPath = getenv("HIP_VISIBLE_DEVICES");
|
|
|
|
|
if (pPath != NULL)
|
2016-02-27 14:48:00 -06:00
|
|
|
printf("HIP_VISIBLE_DEVICES is %s\n", pPath);
|
|
|
|
|
else
|
|
|
|
|
printf("HIP_VISIBLE_DEVICES is not set\n");
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-27 09:43:38 -06:00
|
|
|
// device init
|
2018-03-12 11:29:03 +05:30
|
|
|
int devCount = 0;
|
2016-02-27 09:43:38 -06:00
|
|
|
hipGetDeviceCount(&devCount);
|
|
|
|
|
|
|
|
|
|
if (devCount == 0) {
|
|
|
|
|
printf("No HIP enabled device\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2019-08-09 17:22:10 +05:30
|
|
|
if (device != INT_MAX && (device < 0 || device > devCount - 1)) {
|
2016-02-27 09:43:38 -06:00
|
|
|
printf("Selected device %d is out of bound. Devices on your system are in range %d - %d\n",
|
2018-03-12 11:29:03 +05:30
|
|
|
device, 0, devCount - 1);
|
2016-02-27 09:43:38 -06:00
|
|
|
return -1;
|
|
|
|
|
}
|
2016-02-27 14:48:00 -06:00
|
|
|
|
2016-02-27 09:43:38 -06:00
|
|
|
if (retDevCnt) {
|
2018-03-12 11:29:03 +05:30
|
|
|
std::cout << devCount << std::endl;
|
2016-02-27 09:43:38 -06:00
|
|
|
}
|
2019-08-09 17:22:10 +05:30
|
|
|
if (device != INT_MAX) {
|
2018-03-12 11:29:03 +05:30
|
|
|
hipDevice_t deviceT;
|
|
|
|
|
hipDeviceGet(&deviceT, device);
|
2016-02-27 09:43:38 -06:00
|
|
|
|
2017-07-27 17:41:49 +00:00
|
|
|
char pciBusId[100];
|
2018-03-12 11:29:03 +05:30
|
|
|
memset(pciBusId, 0, 100);
|
|
|
|
|
hipDeviceGetPCIBusId(pciBusId, 100, deviceT);
|
2017-07-27 17:41:49 +00:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
cout << pciBusId << endl;
|
2016-02-27 09:43:38 -06:00
|
|
|
}
|
|
|
|
|
exit(0);
|
|
|
|
|
}
|