[dtests[ Fix build issues with hipEnvVar*.cpp on windows (#1299)
* replace getopt with clara based command line options * Removed header getopt.h
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -27,57 +27,56 @@ THE SOFTWARE.
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <getopt.h>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include "clara/clara.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "hip/hip_runtime.h"
|
#include "hip/hip_runtime.h"
|
||||||
|
|
||||||
|
#ifdef _WIN64
|
||||||
|
#include <tchar.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef _WIN64
|
||||||
|
#define setenv(x,y,z) _putenv_s(x,y)
|
||||||
|
#endif
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
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") |
|
||||||
|
|
||||||
void usage() {
|
clara::Help{help} |
|
||||||
printf(
|
|
||||||
"hipEnvVar [otpions]\n\
|
clara::Opt{device,"device"}
|
||||||
-c,\t\ttotal number of available GPUs and their pciBusID\n\
|
["-d"]["--device"]
|
||||||
-d,\t\tselect one GPU and return its pciBusID\n\
|
("select one GPU and return its pciBusID") |
|
||||||
-v,\t\tsend the list to HIP_VISIBLE_DEVICES env var\n\
|
|
||||||
-h,\t\tshow this help message\n\
|
clara::Opt{env,"Set Env Value"}
|
||||||
");
|
["-v"]["--EnvValue"]
|
||||||
|
("send the list to HIP_VISIBLE_DEVICES env var, syntax -v=<value>");
|
||||||
}
|
}
|
||||||
int main(int argc, char** argv) {
|
|
||||||
// string str = getenv("HIP_VISIBLE_DEVICES");
|
|
||||||
// std::cout << "The current env HIP_VISIBLE_DEVICES is"<<str << std::endl;
|
|
||||||
extern char* optarg;
|
|
||||||
extern int optind;
|
|
||||||
int c = 0;
|
|
||||||
int retDevCnt = 0, retDevInfo = 0, setEnvVar = 0;
|
|
||||||
int device = 0;
|
|
||||||
string env;
|
|
||||||
while ((c = getopt(argc, argv, "cd:v:h")) != -1) switch (c) {
|
|
||||||
case 'c':
|
|
||||||
retDevCnt = true;
|
|
||||||
break;
|
|
||||||
case 'd':
|
|
||||||
retDevInfo = true;
|
|
||||||
device = atoi(optarg);
|
|
||||||
break;
|
|
||||||
case 'v':
|
|
||||||
setEnvVar = true;
|
|
||||||
env = optarg;
|
|
||||||
break;
|
|
||||||
case 'h':
|
|
||||||
usage();
|
|
||||||
return 0;
|
|
||||||
default:
|
|
||||||
// usage();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (setEnvVar) {
|
int main(int argc, char** argv) {
|
||||||
// env = "export HIP_VISIBLE_DEVICES=" + env;
|
bool help = false;
|
||||||
// cout<<"The received env var is: "<<env<<endl;
|
bool retDevCnt = false;
|
||||||
|
int c = 0;
|
||||||
|
int device = INT_MAX;
|
||||||
|
string env;
|
||||||
|
|
||||||
|
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()) {
|
||||||
setenv("HIP_VISIBLE_DEVICES", env.c_str(), 1);
|
setenv("HIP_VISIBLE_DEVICES", env.c_str(), 1);
|
||||||
setenv("CUDA_VISIBLE_DEVICES", env.c_str(), 1);
|
setenv("CUDA_VISIBLE_DEVICES", env.c_str(), 1);
|
||||||
cout << "set env HIP_VISIBLE_DEVICES = " << env.c_str() << endl;
|
cout << "set env HIP_VISIBLE_DEVICES = " << env.c_str() << endl;
|
||||||
|
|
||||||
// verify if the environment variable is set
|
// verify if the environment variable is set
|
||||||
char* pPath;
|
char* pPath;
|
||||||
pPath = getenv("HIP_VISIBLE_DEVICES");
|
pPath = getenv("HIP_VISIBLE_DEVICES");
|
||||||
@@ -91,23 +90,20 @@ int main(int argc, char** argv) {
|
|||||||
int devCount = 0;
|
int devCount = 0;
|
||||||
hipGetDeviceCount(&devCount);
|
hipGetDeviceCount(&devCount);
|
||||||
|
|
||||||
// printf("\nTotal number of GPU devices in the system is %d\n",devCount);
|
|
||||||
|
|
||||||
if (devCount == 0) {
|
if (devCount == 0) {
|
||||||
printf("No HIP enabled device\n");
|
printf("No HIP enabled device\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (device < 0 || device > devCount - 1) {
|
if (device != INT_MAX && (device < 0 || device > devCount - 1)) {
|
||||||
printf("Selected device %d is out of bound. Devices on your system are in range %d - %d\n",
|
printf("Selected device %d is out of bound. Devices on your system are in range %d - %d\n",
|
||||||
device, 0, devCount - 1);
|
device, 0, devCount - 1);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (retDevCnt) {
|
if (retDevCnt) {
|
||||||
// std::cout << "Total number of devices visible in system is "<< devCount << std::endl;
|
|
||||||
std::cout << devCount << std::endl;
|
std::cout << devCount << std::endl;
|
||||||
}
|
}
|
||||||
if (retDevInfo) {
|
if (device != INT_MAX) {
|
||||||
hipDevice_t deviceT;
|
hipDevice_t deviceT;
|
||||||
hipDeviceGet(&deviceT, device);
|
hipDeviceGet(&deviceT, device);
|
||||||
|
|
||||||
|
|||||||
@@ -30,6 +30,14 @@ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA
|
|||||||
#include "hip/hip_runtime.h"
|
#include "hip/hip_runtime.h"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
#ifdef _WIN64
|
||||||
|
#define popen(x,y) _popen(x,y)
|
||||||
|
#define pclose(x) _pclose(x)
|
||||||
|
#define setenv(x,y,z) _putenv_s(x,y)
|
||||||
|
#define unsetenv(x) _putenv_s(x,"")
|
||||||
|
#endif
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
int getDeviceNumber() {
|
int getDeviceNumber() {
|
||||||
|
|||||||
Reference in New Issue
Block a user