HIP: Heterogenous-computing Interface for Portability
Loading...
Searching...
No Matches
hipBin_base.h
1/*
2Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved.
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20THE SOFTWARE.
21*/
22#ifndef SRC_HIPBIN_BASE_H_
23#define SRC_HIPBIN_BASE_H_
24
25
26#include "hipBin_util.h"
27#include <iostream>
28#include <vector>
29#include <string>
30
31// All envirnoment variables used in the code
32# define PATH "PATH"
33# define HIP_ROCCLR_HOME "HIP_ROCCLR_HOME"
34# define HIP_PATH "HIP_PATH"
35# define ROCM_PATH "ROCM_PATH"
36# define CUDA_PATH "CUDA_PATH"
37# define HSA_PATH "HSA_PATH"
38# define HIP_CLANG_PATH "HIP_CLANG_PATH"
39# define HIP_PLATFORM "HIP_PLATFORM"
40# define HIP_COMPILER "HIP_COMPILER"
41# define HIP_RUNTIME "HIP_RUNTIME"
42# define LD_LIBRARY_PATH "LD_LIBRARY_PATH"
43
44// hipcc
45# define HIPCC_COMPILE_FLAGS_APPEND "HIPCC_COMPILE_FLAGS_APPEND"
46# define HIPCC_LINK_FLAGS_APPEND "HIPCC_LINK_FLAGS_APPEND"
47# define HIP_LIB_PATH "HIP_LIB_PATH"
48# define DEVICE_LIB_PATH "DEVICE_LIB_PATH"
49# define HIP_CLANG_HCC_COMPAT_MODE "HIP_CLANG_HCC_COMPAT_MODE"
50# define HIP_COMPILE_CXX_AS_HIP "HIP_COMPILE_CXX_AS_HIP"
51# define HIPCC_VERBOSE "HIPCC_VERBOSE"
52# define HCC_AMDGPU_TARGET "HCC_AMDGPU_TARGET"
53
54# define HIP_BASE_VERSION_MAJOR "4"
55# define HIP_BASE_VERSION_MINOR "4"
56# define HIP_BASE_VERSION_PATCH "0"
57# define HIP_BASE_VERSION_GITHASH "0"
58
59
60enum PlatformType {
61 amd = 0,
62 nvidia,
63 // add new platform types to be added here
64};
65
66string PlatformTypeStr(PlatformType platform) {
67 switch (platform) {
68 case amd:
69 return "amd";
70 case nvidia:
71 return "nvidia";
72 // add new platform types to be added here
73 default:
74 return "invalid platform";
75 }
76}
77
78enum CompilerType {
79 clang = 0,
80 nvcc
81 // add new compiler types to be added here
82};
83
84
85string CompilerTypeStr(CompilerType compiler) {
86 switch (compiler) {
87 case clang:
88 return "clang";
89 case nvcc:
90 return "nvcc";
91 // add new compiler types to be added here
92 default:
93 return "invalid CompilerType";
94 }
95}
96
97
98enum RuntimeType {
99 rocclr = 0,
100 cuda
101 // add new runtime types to be added here
102};
103
104string RuntimeTypeStr(RuntimeType runtime) {
105 switch (runtime) {
106 case rocclr:
107 return "rocclr";
108 case cuda:
109 return "cuda";
110 // add new runtime types to be added here
111 default:
112 return "invalid RuntimeType";
113 }
114}
115
116enum OsType {
117 lnx = 0,
118 windows
119 // add new OS types to be added here
120};
121
122string OsTypeStr(OsType os) {
123 switch (os) {
124 case lnx:
125 return "linux";
126 case windows:
127 return "windows";
128 // add new OS types to be added here
129 default:
130 return "invalid OsType";
131 }
132}
133
135 PlatformType platform;
136 CompilerType compiler;
137 RuntimeType runtime;
138 OsType os;
139};
140
142 string path_ = "";
143 string hipPathEnv_ = "";
144 string hipRocclrPathEnv_ = "";
145 string roccmPathEnv_ = "";
146 string cudaPathEnv_ = "";
147 string hsaPathEnv_ = "";
148 string hipClangPathEnv_ = "";
149 string hipPlatformEnv_ = "";
150 string hipCompilerEnv_ = "";
151 string hipRuntimeEnv_ = "";
152 string ldLibraryPathEnv_ = "";
153 string verboseEnv_ = "";
154 string hipccCompileFlagsAppendEnv_ = "";
155 string hipccLinkFlagsAppendEnv_ = "";
156 string hipLibPathEnv_ = "";
157 string deviceLibPathEnv_ = "";
158 string hipClangHccCompactModeEnv_ = "";
159 string hipCompileCxxAsHipEnv_ = "";
160 string hccAmdGpuTargetEnv_ = "";
161 friend std::ostream& operator <<(std::ostream& os, const EnvVariables& var) {
162 os << "Path: " << var.path_ << endl;
163 os << "Hip Path: " << var.hipPathEnv_ << endl;
164 os << "Hip Rocclr Path: " << var.hipRocclrPathEnv_ << endl;
165 os << "Roccm Path: " << var.roccmPathEnv_ << endl;
166 os << "Cuda Path: " << var.cudaPathEnv_ << endl;
167 os << "Hsa Path: " << var.hsaPathEnv_ << endl;
168 os << "Hip Clang Path: " << var.hipClangPathEnv_ << endl;
169 os << "Hip Platform: " << var.hipPlatformEnv_ << endl;
170 os << "Hip Compiler: " << var.hipCompilerEnv_ << endl;
171 os << "Hip Runtime: " << var.hipRuntimeEnv_ << endl;
172 os << "LD Library Path: " << var.ldLibraryPathEnv_ << endl;
173 os << "Verbose: " << var.verboseEnv_ << endl;
174 os << "Hipcc Compile Flags Append: " <<
175 var.hipccCompileFlagsAppendEnv_ << endl;
176 os << "Hipcc Link Flags Append: " <<
177 var.hipccLinkFlagsAppendEnv_ << endl;
178 os << "Hip lib Path: " << var.hipLibPathEnv_ << endl;
179 os << "Device lib Path: " << var.deviceLibPathEnv_ << endl;
180 os << "Hip Clang HCC Compact mode: " <<
181 var.hipClangHccCompactModeEnv_ << endl;
182 os << "Hip Compile Cxx as Hip: " <<
183 var.hipCompileCxxAsHipEnv_ << endl;
184 os << "Hcc Amd Gpu Target: " << var.hccAmdGpuTargetEnv_ << endl;
185 return os;
186 }
187};
188
189enum HipBinCommand {
190 unknown = -1,
191 path,
192 roccmpath,
193 cpp_config,
194 compiler,
195 platform,
196 runtime,
197 hipclangpath,
198 full,
199 version,
200 check,
201 newline,
202 help,
203};
204
205
206
208 public:
209 HipBinBase();
210 // Interface functions
211 virtual void constructCompilerPath() = 0;
212 virtual void printFull() = 0;
213 virtual bool detectPlatform() = 0;
214 virtual const string& getCompilerPath() const = 0;
215 virtual void printCompilerInfo() const = 0;
216 virtual string getCompilerVersion() = 0;
217 virtual const PlatformInfo& getPlatformInfo() const = 0;
218 virtual string getCppConfig() = 0;
219 virtual void checkHipconfig() = 0;
220 virtual string getDeviceLibPath() const = 0;
221 virtual string getHipLibPath() const = 0;
222 virtual string getHipCC() const = 0;
223 virtual string getHipInclude() const = 0;
224 virtual void initializeHipCXXFlags() = 0;
225 virtual void initializeHipCFlags() = 0;
226 virtual void initializeHipLdFlags() = 0;
227 virtual const string& getHipCXXFlags() const = 0;
228 virtual const string& getHipCFlags() const = 0;
229 virtual const string& getHipLdFlags() const = 0;
230 virtual void executeHipCCCmd(vector<string> argv) = 0;
231 // Common functions used by all platforms
232 void getSystemInfo() const;
233 void printEnvironmentVariables() const;
234 const EnvVariables& getEnvVariables() const;
235 const OsType& getOSInfo() const;
236 const string& getHipPath() const;
237 const string& getRoccmPath() const;
238 const string& getHipVersion() const;
239 void printUsage() const;
240 bool canRunCompiler(string exeName, string& cmdOut);
241 HipBinCommand gethipconfigCmd(string argument);
242 const string& getrocm_pathOption() const;
243
244 protected:
245 // hipBinUtilPtr used by derived platforms
246 // so therefore its protected
247 HipBinUtil* hipBinUtilPtr_;
248 string rocm_pathOption_ = "";
249 void readOSInfo();
250 void readEnvVariables();
251 void constructHipPath();
252 void constructRoccmPath();
253 void readHipVersion();
254
255 private:
256 EnvVariables envVariables_, variables_;
257 OsType osInfo_;
258 string hipVersion_;
259
260};
261
262HipBinBase::HipBinBase() {
263 hipBinUtilPtr_ = hipBinUtilPtr_->getInstance();
264 readOSInfo(); // detects if windows or linux
265 readEnvVariables(); // reads the environment variables
266}
267
268// detects the OS information
269void HipBinBase::readOSInfo() {
270#if defined _WIN32 || defined _WIN64
271 osInfo_ = windows;
272#elif defined __unix || defined __linux__
273 osInfo_ = lnx;
274#endif
275}
276
277
278// reads envirnoment variables
279void HipBinBase::readEnvVariables() {
280 if (const char* path = std::getenv(PATH))
281 envVariables_.path_ = path;
282 if (const char* hip = std::getenv(HIP_PATH))
283 envVariables_.hipPathEnv_ = hip;
284 if (const char* hip_rocclr = std::getenv(HIP_ROCCLR_HOME))
285 envVariables_.hipRocclrPathEnv_ = hip_rocclr;
286 if (const char* roccm = std::getenv(ROCM_PATH))
287 envVariables_.roccmPathEnv_ = roccm;
288 if (const char* cuda = std::getenv(CUDA_PATH))
289 envVariables_.cudaPathEnv_ = cuda;
290 if (const char* hsa = std::getenv(HSA_PATH))
291 envVariables_.hsaPathEnv_ = hsa;
292 if (const char* hipClang = std::getenv(HIP_CLANG_PATH))
293 envVariables_.hipClangPathEnv_ = hipClang;
294 if (const char* hipPlatform = std::getenv(HIP_PLATFORM))
295 envVariables_.hipPlatformEnv_ = hipPlatform;
296 if (const char* hipCompiler = std::getenv(HIP_COMPILER))
297 envVariables_.hipCompilerEnv_ = hipCompiler;
298 if (const char* hipRuntime = std::getenv(HIP_RUNTIME))
299 envVariables_.hipRuntimeEnv_ = hipRuntime;
300 if (const char* ldLibaryPath = std::getenv(LD_LIBRARY_PATH))
301 envVariables_.ldLibraryPathEnv_ = ldLibaryPath;
302 if (const char* hccAmdGpuTarget = std::getenv(HCC_AMDGPU_TARGET))
303 envVariables_.hccAmdGpuTargetEnv_ = hccAmdGpuTarget;
304 if (const char* verbose = std::getenv(HIPCC_VERBOSE))
305 envVariables_.verboseEnv_ = verbose;
306 if (const char* hipccCompileFlagsAppend =
307 std::getenv(HIPCC_COMPILE_FLAGS_APPEND))
308 envVariables_.hipccCompileFlagsAppendEnv_ = hipccCompileFlagsAppend;
309 if (const char* hipccLinkFlagsAppend = std::getenv(HIPCC_LINK_FLAGS_APPEND))
310 envVariables_.hipccLinkFlagsAppendEnv_ = hipccLinkFlagsAppend;
311 if (const char* hipLibPath = std::getenv(HIP_LIB_PATH))
312 envVariables_.hipLibPathEnv_ = hipLibPath;
313 if (const char* deviceLibPath = std::getenv(DEVICE_LIB_PATH))
314 envVariables_.deviceLibPathEnv_ = deviceLibPath;
315 if (const char* hipClangHccCompactMode =
316 std::getenv(HIP_CLANG_HCC_COMPAT_MODE))
317 envVariables_.hipClangHccCompactModeEnv_ = hipClangHccCompactMode;
318 if (const char* hipCompileCxxAsHip = std::getenv(HIP_COMPILE_CXX_AS_HIP))
319 envVariables_.hipCompileCxxAsHipEnv_ = hipCompileCxxAsHip;
320}
321
322// constructs the HIP path
323void HipBinBase::constructHipPath() {
324 fs::path full_path(hipBinUtilPtr_->getSelfPath());
325 if (envVariables_.hipPathEnv_.empty())
326 variables_.hipPathEnv_ = (full_path.parent_path()).string();
327 else
328 variables_.hipPathEnv_ = envVariables_.hipPathEnv_;
329}
330
331
332// constructs the ROCM path
333void HipBinBase::constructRoccmPath() {
334 // we need to use --rocm-path option
335 string rocm_path_name = getrocm_pathOption();
336
337 // chose the --rocm-path option first, if specified.
338 if (!rocm_path_name.empty())
339 variables_.roccmPathEnv_ = rocm_path_name;
340 else if (envVariables_.roccmPathEnv_.empty()) {
341 const string& hipPath = getHipPath();
342 fs::path roccm_path(hipPath);
343 roccm_path = roccm_path.parent_path();
344 fs::path rocm_agent_enumerator_file(roccm_path);
345 rocm_agent_enumerator_file /= "bin/rocm_agent_enumerator";
346 if (!fs::exists(rocm_agent_enumerator_file)) {
347 roccm_path = "/opt/rocm";
348 }
349 } else {
350 variables_.roccmPathEnv_ = envVariables_.roccmPathEnv_;}
351}
352
353// reads the Hip Version
354void HipBinBase::readHipVersion() {
355 string hipVersion;
356 const string& hipPath = getHipPath();
357 fs::path hipVersionPath = hipPath;
358 hipVersionPath /= "bin/.hipVersion";
359 map<string, string> hipVersionMap;
360 hipVersionMap = hipBinUtilPtr_->parseConfigFile(hipVersionPath);
361 string hip_version_major, hip_version_minor,
362 hip_version_patch, hip_version_githash;
363 hip_version_major = hipBinUtilPtr_->readConfigMap(
364 hipVersionMap, "HIP_VERSION_MAJOR",
365 HIP_BASE_VERSION_MAJOR);
366 hip_version_minor = hipBinUtilPtr_->readConfigMap(
367 hipVersionMap, "HIP_VERSION_MINOR",
368 HIP_BASE_VERSION_MINOR);
369 hip_version_patch = hipBinUtilPtr_->readConfigMap(
370 hipVersionMap, "HIP_VERSION_PATCH",
371 HIP_BASE_VERSION_PATCH);
372 hip_version_githash = hipBinUtilPtr_->readConfigMap(
373 hipVersionMap, "HIP_VERSION_GITHASH",
374 HIP_BASE_VERSION_GITHASH);
375 hipVersion = hip_version_major + "." + hip_version_minor +
376 "." + hip_version_patch + "-" + hip_version_githash;
377 hipVersion_ = hipVersion;
378}
379
380// prints system information
381void HipBinBase::getSystemInfo() const {
382 const OsType& os = getOSInfo();
383 if (os == windows) {
384 cout << endl << "== Windows Display Drivers" << endl;
385 cout << "Hostname :";
386 system("hostname");
387 system("wmic path win32_VideoController get AdapterCompatibility,"
388 "InstalledDisplayDrivers,Name | findstr /B /C:\"Advanced Micro Devices\"");
389 } else {
390 assert(os == lnx);
391 cout << endl << "== Linux Kernel" << endl;
392 cout << "Hostname :" << endl;
393 system("hostname");
394 system("uname -a");
395 }
396}
397
398// prints the envirnoment variables
399void HipBinBase::printEnvironmentVariables() const {
400 const OsType& os = getOSInfo();
401 if (os == windows) {
402 cout << "PATH=" << envVariables_.path_ << "\n" << endl;
403 system("set | findstr"
404 " /B /C:\"HIP\" /C:\"HSA\" /C:\"CUDA\" /C:\"LD_LIBRARY_PATH\"");
405 } else {
406 string cmd = "echo PATH =";
407 cmd += envVariables_.path_;
408 system(cmd.c_str());
409 system("env | egrep '^HIP|^HSA|^CUDA|^LD_LIBRARY_PATH'");
410 }
411}
412
413// returns envirnoment variables
414const EnvVariables& HipBinBase::getEnvVariables() const {
415 return envVariables_;
416}
417
418
419// returns the os information
420const OsType& HipBinBase::getOSInfo() const {
421 return osInfo_;
422}
423
424// returns the HIP path
425const string& HipBinBase::getHipPath() const {
426 return variables_.hipPathEnv_;
427}
428
429// returns the Roccm path
430const string& HipBinBase::getRoccmPath() const {
431 return variables_.roccmPathEnv_;
432}
433
434// returns the Hip Version
435const string& HipBinBase::getHipVersion() const {
436 return hipVersion_;
437}
438
439// prints the help text
440void HipBinBase::printUsage() const {
441 cout << "usage: hipconfig [OPTIONS]\n";
442 cout << " --path, -p :"
443 " print HIP_PATH (use env var if set, else determine from hipconfig path)\n";
444 cout << " --rocmpath, -R :"
445 " print ROCM_PATH (use env var if set,"
446 " else determine from hip path or /opt/rocm)\n";
447 cout << " --cpp_config, -C : print C++ compiler options\n";
448 cout << " --compiler, -c : print compiler (clang or nvcc)\n";
449 cout << " --platform, -P : print platform (amd or nvidia)\n";
450 cout << " --runtime, -r : print runtime (rocclr or cuda)\n";
451 cout << " --hipclangpath, -l : print HIP_CLANG_PATH\n";
452 cout << " --full, -f : print full config\n";
453 cout << " --version, -v : print hip version\n";
454 cout << " --check : check configuration\n";
455 cout << " --newline, -n : print newline\n";
456 cout << " --help, -h : print help message\n";
457}
458
459
460
461// compiler canRun or not
462bool HipBinBase::canRunCompiler(string exeName, string& cmdOut) {
463 string complierName = exeName;
464 string temp_dir = hipBinUtilPtr_->getTempDir();
465 fs::path templateFs = temp_dir;
466 templateFs /= "canRunXXXXXX";
467 string tmpFileName = hipBinUtilPtr_->mktempFile(templateFs.string());
468 complierName += " --version > " + tmpFileName + " 2>&1";
469 bool executable = false;
470 if (system(const_cast<char*>(complierName.c_str()))) {
471 executable = false;
472 } else {
473 string myline;
474 ifstream fp;
475 fp.open(tmpFileName);
476 if (fp.is_open()) {
477 while (std::getline(fp, myline)) {
478 cmdOut += myline;
479 }
480 }
481 fp.close();
482 executable = true;
483 }
484 return executable;
485}
486
487HipBinCommand HipBinBase::gethipconfigCmd(string argument) {
488 vector<string> pathStrs = { "-p", "--path", "-path", "--p" };
489 if (hipBinUtilPtr_->checkCmd(pathStrs, argument))
490 return path;
491 vector<string> rocmPathStrs = { "-R", "--rocmpath", "-rocmpath", "--R" };
492 if (hipBinUtilPtr_->checkCmd(rocmPathStrs, argument))
493 return roccmpath;
494 vector<string> cppConfigStrs = { "-C", "--cpp_config",
495 "-cpp_config", "--C", };
496 if (hipBinUtilPtr_->checkCmd(cppConfigStrs, argument))
497 return cpp_config;
498 vector<string> CompilerStrs = { "-c", "--compiler", "-compiler", "--c" };
499 if (hipBinUtilPtr_->checkCmd(CompilerStrs, argument))
500 return compiler;
501 vector<string> platformStrs = { "-P", "--platform", "-platform", "--P" };
502 if (hipBinUtilPtr_->checkCmd(platformStrs, argument))
503 return platform;
504 vector<string> runtimeStrs = { "-r", "--runtime", "-runtime", "--r" };
505 if (hipBinUtilPtr_->checkCmd(runtimeStrs, argument))
506 return runtime;
507 vector<string> hipClangPathStrs = { "-l", "--hipclangpath",
508 "-hipclangpath", "--l" };
509 if (hipBinUtilPtr_->checkCmd(hipClangPathStrs, argument))
510 return hipclangpath;
511 vector<string> fullStrs = { "-f", "--full", "-full", "--f" };
512 if (hipBinUtilPtr_->checkCmd(fullStrs, argument))
513 return full;
514 vector<string> versionStrs = { "-v", "--version", "-version", "--v" };
515 if (hipBinUtilPtr_->checkCmd(versionStrs, argument))
516 return version;
517 vector<string> checkStrs = { "--check", "-check" };
518 if (hipBinUtilPtr_->checkCmd(checkStrs, argument))
519 return check;
520 vector<string> newlineStrs = { "--n", "-n", "--newline", "-newline" };
521 if (hipBinUtilPtr_->checkCmd(newlineStrs, argument))
522 return newline;
523 vector<string> helpStrs = { "-h", "--help", "-help", "--h" };
524 if (hipBinUtilPtr_->checkCmd(helpStrs, argument))
525 return help;
526 return full; // default is full. return full if no commands are matched
527}
528
529const string& HipBinBase::getrocm_pathOption() const {
530 return rocm_pathOption_;
531}
532
533#endif // SRC_HIPBIN_BASE_H_
Definition: hipBin_base.h:207
Definition: hipBin_util.h:144
Definition: hipBin_base.h:141
Definition: hipBin_base.h:134