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
|
2018-03-12 11:29:03 +05:30
|
|
|
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:
|
2016-02-27 09:43:38 -06:00
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial
|
|
|
|
|
portions of the Software.
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
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-02-27 09:43:38 -06:00
|
|
|
|
2016-09-23 16:15:31 +05:30
|
|
|
/* HIT_START
|
|
|
|
|
* BUILD: %t %s test_common.cpp NVCC_OPTIONS -std=c++11
|
2019-04-03 11:54:50 +05:30
|
|
|
* TEST: %t
|
2016-09-23 16:15:31 +05:30
|
|
|
* HIT_END
|
|
|
|
|
*/
|
|
|
|
|
|
2016-02-27 09:43:38 -06:00
|
|
|
#include <iostream>
|
2016-02-27 14:48:00 -06:00
|
|
|
#include <vector>
|
2016-02-27 09:43:38 -06:00
|
|
|
#include <stdio.h>
|
2016-02-27 14:48:00 -06:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <assert.h>
|
2016-05-02 12:49:53 -05:00
|
|
|
#include <string>
|
2016-10-04 22:20:50 +05:30
|
|
|
#include "hip/hip_runtime.h"
|
2017-07-24 17:16:13 +00:00
|
|
|
#include <chrono>
|
|
|
|
|
#include <thread>
|
2019-08-15 19:13:00 -07:00
|
|
|
#include "test_common.h"
|
2019-08-09 17:22:10 +05:30
|
|
|
|
2016-02-27 09:43:38 -06:00
|
|
|
using namespace std;
|
|
|
|
|
|
2020-02-28 06:16:12 -05:00
|
|
|
const string directed_dir = string(".") + PATH_SEPERATOR_STR + "directed_tests" + PATH_SEPERATOR_STR + "hipEnvVar";
|
|
|
|
|
const string dir = string(".") + PATH_SEPERATOR_STR + "hipEnvVar";
|
2019-11-18 01:48:29 -05:00
|
|
|
|
2020-02-28 06:16:12 -05:00
|
|
|
int readHipEnvVar(string flags, char* buff){
|
|
|
|
|
|
|
|
|
|
std::cout << "\nFinding hipEnvVar in " << directed_dir << "...\n";
|
|
|
|
|
FILE* directed_in = popen((directed_dir + flags).c_str(), "r");
|
|
|
|
|
|
|
|
|
|
if(fgets(buff, 512, directed_in) == NULL){
|
|
|
|
|
std::cout << "Finding hipEnvVar in " << dir << "...\n";
|
|
|
|
|
FILE* in = popen((dir + flags).c_str(), "r");
|
2019-11-18 01:48:29 -05:00
|
|
|
if(fgets(buff, 512, in) == NULL){
|
2020-02-28 06:16:12 -05:00
|
|
|
pclose(directed_in);
|
2019-11-18 01:48:29 -05:00
|
|
|
pclose(in);
|
2018-08-17 12:14:42 -07:00
|
|
|
return 1;
|
|
|
|
|
}
|
2020-02-28 06:16:12 -05:00
|
|
|
pclose(in);
|
|
|
|
|
}
|
|
|
|
|
std::cout << "hipEnvVar Found!\n";
|
|
|
|
|
pclose(directed_in);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int getDeviceNumber(bool print_err=true) {
|
|
|
|
|
char buff[512];
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
|
|
|
|
|
|
if (readHipEnvVar(string(" -c"), buff)){
|
|
|
|
|
strncpy(buff, "1", 512);
|
|
|
|
|
if (print_err){
|
|
|
|
|
std::cerr << "The system cannot find hipEnvVar, using 1 as number of devices\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (print_err) {
|
|
|
|
|
std::cout << buff;
|
2018-03-12 11:29:03 +05:30
|
|
|
}
|
2016-02-27 14:48:00 -06:00
|
|
|
return atoi(buff);
|
|
|
|
|
}
|
2016-02-27 09:43:38 -06:00
|
|
|
|
2016-09-27 23:00:11 -05:00
|
|
|
// Query the current device ID remotely to hipEnvVar
|
2019-11-18 01:48:29 -05:00
|
|
|
void getDevicePCIBusNumRemote(int deviceID, char* pciBusID) {
|
2017-07-24 17:16:13 +00:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
2020-02-28 06:16:12 -05:00
|
|
|
if (readHipEnvVar((" -d " + std::to_string(deviceID)), pciBusID)){
|
|
|
|
|
std::cerr << "The system cannot find hipEnvVar\n";
|
2017-07-24 17:16:13 +00:00
|
|
|
}
|
2019-11-18 01:48:29 -05:00
|
|
|
cout << pciBusID;
|
|
|
|
|
return;
|
2016-02-27 14:48:00 -06:00
|
|
|
}
|
2016-02-27 09:43:38 -06:00
|
|
|
|
2017-07-27 17:41:49 +00:00
|
|
|
// Query the current device ID locally on AMD path
|
2018-03-12 11:29:03 +05:30
|
|
|
void getDevicePCIBusNum(int deviceID, char* pciBusID) {
|
2017-07-27 17:41:49 +00:00
|
|
|
hipDevice_t deviceT;
|
|
|
|
|
hipDeviceGet(&deviceT, deviceID);
|
2016-09-27 23:00:11 -05:00
|
|
|
|
2020-02-28 06:16:12 -05:00
|
|
|
memset(pciBusID, 0, 512);
|
|
|
|
|
hipDeviceGetPCIBusId(pciBusID, 512, deviceT);
|
2016-09-27 23:00:11 -05:00
|
|
|
}
|
|
|
|
|
|
2016-02-27 14:48:00 -06:00
|
|
|
int main() {
|
2019-11-18 01:48:29 -05:00
|
|
|
unsetenv(HIP_VISIBLE_DEVICES_STR);
|
|
|
|
|
unsetenv(CUDA_VISIBLE_DEVICES_STR);
|
2017-07-27 17:41:49 +00:00
|
|
|
std::vector<std::string> devPCINum;
|
2020-02-28 06:16:12 -05:00
|
|
|
char pciBusID[512];
|
2018-03-12 11:29:03 +05:30
|
|
|
// collect the device pci bus ID for all devices
|
2016-02-27 14:48:00 -06:00
|
|
|
int totalDeviceNum = getDeviceNumber();
|
2018-03-12 11:29:03 +05:30
|
|
|
std::cout << "The total number of available devices is " << totalDeviceNum << std::endl
|
|
|
|
|
<< "Valid index range is 0 - " << totalDeviceNum - 1 << std::endl;
|
|
|
|
|
for (int i = 0; i < totalDeviceNum; i++) {
|
2017-07-27 17:41:49 +00:00
|
|
|
getDevicePCIBusNum(i, pciBusID);
|
|
|
|
|
devPCINum.push_back(pciBusID);
|
2018-03-12 11:29:03 +05:30
|
|
|
std::cout << "The collected device PCI Bus ID of Device " << i << " is " << devPCINum.back()
|
|
|
|
|
<< std::endl;
|
2016-02-27 14:48:00 -06:00
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
// select each of the available devices to be the target device,
|
|
|
|
|
// query the returned device pci bus number, check if match the database
|
|
|
|
|
for (int i = 0; i < totalDeviceNum; i++) {
|
|
|
|
|
setenv("HIP_VISIBLE_DEVICES", (char*)std::to_string(i).c_str(), 1);
|
|
|
|
|
setenv("CUDA_VISIBLE_DEVICES", (char*)std::to_string(i).c_str(), 1);
|
|
|
|
|
getDevicePCIBusNumRemote(0, pciBusID);
|
2017-07-27 17:41:49 +00:00
|
|
|
if (devPCINum[i] == pciBusID) {
|
2018-03-12 11:29:03 +05:30
|
|
|
std::cout << "The returned PciBusID is not correct" << std::endl;
|
|
|
|
|
std::cout << "Expected " << devPCINum[i] << ", but get " << pciBusID << endl;
|
2016-02-27 14:48:00 -06:00
|
|
|
exit(-1);
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
// check when set an invalid device number
|
|
|
|
|
setenv("HIP_VISIBLE_DEVICES", "1000,0,1", 1);
|
|
|
|
|
setenv("CUDA_VISIBLE_DEVICES", "1000,0,1", 1);
|
2020-02-28 06:16:12 -05:00
|
|
|
assert(getDeviceNumber(false) == 0);
|
2016-02-27 09:43:38 -06:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
if (totalDeviceNum > 2) {
|
|
|
|
|
setenv("HIP_VISIBLE_DEVICES", "0,1,1000,2", 1);
|
|
|
|
|
setenv("CUDA_VISIBLE_DEVICES", "0,1,1000,2", 1);
|
2020-02-28 06:16:12 -05:00
|
|
|
assert(getDeviceNumber(false) == 2);
|
2016-02-27 14:48:00 -06:00
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
setenv("HIP_VISIBLE_DEVICES", "0,1,2", 1);
|
|
|
|
|
setenv("CUDA_VISIBLE_DEVICES", "0,1,2", 1);
|
2020-02-28 06:16:12 -05:00
|
|
|
assert(getDeviceNumber(false) == 3);
|
2016-02-27 14:48:00 -06:00
|
|
|
// test if CUDA_VISIBLE_DEVICES will be accepted by the runtime
|
2019-11-18 01:48:29 -05:00
|
|
|
unsetenv(HIP_VISIBLE_DEVICES_STR);
|
|
|
|
|
unsetenv(CUDA_VISIBLE_DEVICES_STR);
|
2018-03-12 11:29:03 +05:30
|
|
|
setenv("CUDA_VISIBLE_DEVICES", "0,1,2", 1);
|
2020-02-28 06:16:12 -05:00
|
|
|
assert(getDeviceNumber(false) == 3);
|
2016-02-27 14:48:00 -06:00
|
|
|
}
|
|
|
|
|
|
2018-03-12 11:29:03 +05:30
|
|
|
setenv("HIP_VISIBLE_DEVICES", "-100,0,1", 1);
|
|
|
|
|
setenv("CUDA_VISIBLE_DEVICES", "-100,0,1", 1);
|
2020-02-28 06:16:12 -05:00
|
|
|
assert(getDeviceNumber(false) == 0);
|
2016-02-27 14:48:00 -06:00
|
|
|
|
2016-03-02 04:35:37 -06:00
|
|
|
std::cout << "PASSED" << std::endl;
|
2016-02-27 14:48:00 -06:00
|
|
|
return 0;
|
2020-02-28 06:16:12 -05:00
|
|
|
}
|