|
| 1 | +/* |
| 2 | +Copyright (c) 2021 Advanced Micro Devices, Inc. All rights reserved. |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in |
| 12 | +all copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +THE SOFTWARE. |
| 21 | +*/ |
| 22 | + |
| 23 | +#include "hipBin_util.h" |
| 24 | +#include "hipBin_amd.h" |
| 25 | +#include "hipBin_nvidia.h" |
| 26 | +#include <vector> |
| 27 | +#include <string> |
| 28 | + |
| 29 | +class HipBinUtil; |
| 30 | +class HipBinBase; |
| 31 | +class HipBinAmd; |
| 32 | +class HipBinNvidia; |
| 33 | +class HipBin; |
| 34 | + |
| 35 | + |
| 36 | +class HipBin { |
| 37 | + private: |
| 38 | + HipBinUtil* hipBinUtilPtr_; |
| 39 | + vector<HipBinBase*> hipBinBasePtrs_; |
| 40 | + vector<PlatformInfo> platformVec_; |
| 41 | + HipBinBase* hipBinNVPtr_; |
| 42 | + HipBinBase* hipBinAMDPtr_; |
| 43 | + |
| 44 | + public: |
| 45 | + HipBin(); |
| 46 | + ~HipBin(); |
| 47 | + vector<HipBinBase*>& getHipBinPtrs(); |
| 48 | + vector<PlatformInfo>& getPlaformInfo(); |
| 49 | + void executeHipBin(string filename, int argc, char* argv[]); |
| 50 | + void executeHipConfig(int argc, char* argv[]); |
| 51 | + void executeHipCC(int argc, char* argv[]); |
| 52 | +}; |
| 53 | + |
| 54 | + |
| 55 | +// Implementation ================================================ |
| 56 | +//=========================================================================== |
| 57 | + |
| 58 | +HipBin::HipBin() { |
| 59 | + hipBinUtilPtr_ = hipBinUtilPtr_->getInstance(); |
| 60 | + hipBinNVPtr_ = new HipBinNvidia(); |
| 61 | + hipBinAMDPtr_ = new HipBinAmd(); |
| 62 | + bool platformDetected = false; |
| 63 | + if (hipBinAMDPtr_->detectPlatform()) { |
| 64 | + // populates the struct with AMD info |
| 65 | + const PlatformInfo& platformInfo = hipBinAMDPtr_->getPlatformInfo(); |
| 66 | + platformVec_.push_back(platformInfo); |
| 67 | + hipBinBasePtrs_.push_back(hipBinAMDPtr_); |
| 68 | + platformDetected = true; |
| 69 | + } else if (hipBinNVPtr_->detectPlatform()) { |
| 70 | + // populates the struct with Nvidia info |
| 71 | + const PlatformInfo& platformInfo = hipBinNVPtr_->getPlatformInfo(); |
| 72 | + platformVec_.push_back(platformInfo); |
| 73 | + hipBinBasePtrs_.push_back(hipBinNVPtr_); |
| 74 | + platformDetected = true; |
| 75 | + } |
| 76 | + // if no device is detected, then it is defaulted to AMD |
| 77 | + if (!platformDetected) { |
| 78 | + cout << "Device not supported - Defaulting to AMD" << endl; |
| 79 | + // populates the struct with AMD info |
| 80 | + const PlatformInfo& platformInfo = hipBinAMDPtr_->getPlatformInfo(); |
| 81 | + platformVec_.push_back(platformInfo); |
| 82 | + hipBinBasePtrs_.push_back(hipBinAMDPtr_); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +HipBin::~HipBin() { |
| 87 | + delete hipBinNVPtr_; |
| 88 | + delete hipBinAMDPtr_; |
| 89 | + // clearing the vector so no one accesses the pointers |
| 90 | + hipBinBasePtrs_.clear(); |
| 91 | + // clearing the platform vector as the pointers are deleted |
| 92 | + platformVec_.clear(); |
| 93 | + delete hipBinUtilPtr_; |
| 94 | +} |
| 95 | + |
| 96 | +vector<PlatformInfo>& HipBin::getPlaformInfo() { |
| 97 | + return platformVec_; // Return the populated platform info. |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +vector<HipBinBase*>& HipBin::getHipBinPtrs() { |
| 102 | + return hipBinBasePtrs_; // Return the populated device pointers. |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +void HipBin::executeHipBin(string filename, int argc, char* argv[]) { |
| 107 | + if (hipBinUtilPtr_->substringPresent(filename, "hipconfig")) { |
| 108 | + executeHipConfig(argc, argv); |
| 109 | + } else if (hipBinUtilPtr_->substringPresent(filename, "hipcc")) { |
| 110 | + executeHipCC(argc, argv); |
| 111 | + } else { |
| 112 | + cout << "Command " << filename |
| 113 | + << " not supported. Name the exe as hipconfig" |
| 114 | + << " or hipcc and then try again ..." << endl; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | + |
| 119 | +void HipBin::executeHipCC(int argc, char* argv[]) { |
| 120 | + vector<HipBinBase*>& platformPtrs = getHipBinPtrs(); |
| 121 | + vector<string> argvcc; |
| 122 | + for (int i = 0; i < argc; i++) { |
| 123 | + argvcc.push_back(argv[i]); |
| 124 | + } |
| 125 | + // 0th index points to the first platform detected. |
| 126 | + // In the near future this vector will contain mulitple devices |
| 127 | + platformPtrs.at(0)->executeHipCCCmd(argvcc); |
| 128 | +} |
| 129 | + |
| 130 | + |
| 131 | +void HipBin::executeHipConfig(int argc, char* argv[]) { |
| 132 | + vector<HipBinBase*>& platformPtrs = getHipBinPtrs(); |
| 133 | + for (unsigned int j = 0; j < platformPtrs.size(); j++) { |
| 134 | + if (argc == 1) { |
| 135 | + platformPtrs.at(j)->printFull(); |
| 136 | + } |
| 137 | + for (int i = 1; i < argc; ++i) { |
| 138 | + HipBinCommand cmd; |
| 139 | + cmd = platformPtrs.at(j)->gethipconfigCmd(argv[i]); |
| 140 | + switch (cmd) { |
| 141 | + case help: platformPtrs.at(j)->printUsage(); |
| 142 | + break; |
| 143 | + case path: cout << platformPtrs.at(j)->getHipPath(); |
| 144 | + break; |
| 145 | + case roccmpath: cout << platformPtrs.at(j)->getRoccmPath(); |
| 146 | + break; |
| 147 | + case cpp_config: cout << platformPtrs.at(j)->getCppConfig(); |
| 148 | + break; |
| 149 | + case compiler: cout << CompilerTypeStr(( |
| 150 | + platformPtrs.at(j)->getPlatformInfo()).compiler); |
| 151 | + break; |
| 152 | + case platform: cout << PlatformTypeStr(( |
| 153 | + platformPtrs.at(j)->getPlatformInfo()).platform); |
| 154 | + break; |
| 155 | + case runtime: cout << RuntimeTypeStr(( |
| 156 | + platformPtrs.at(j)->getPlatformInfo()).runtime); |
| 157 | + break; |
| 158 | + case hipclangpath: cout << platformPtrs.at(j)->getCompilerPath(); |
| 159 | + break; |
| 160 | + case full: platformPtrs.at(j)->printFull(); |
| 161 | + break; |
| 162 | + case version: cout << platformPtrs.at(j)->getHipVersion(); |
| 163 | + break; |
| 164 | + case check: platformPtrs.at(j)->checkHipconfig(); |
| 165 | + break; |
| 166 | + case newline: platformPtrs.at(j)->printFull(); |
| 167 | + cout << endl; |
| 168 | + break; |
| 169 | + default: |
| 170 | + platformPtrs.at(j)->printUsage(); |
| 171 | + break; |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +//=========================================================================== |
| 178 | +//=========================================================================== |
| 179 | + |
| 180 | +int main(int argc, char* argv[]) { |
| 181 | + fs::path filename(argv[0]); |
| 182 | + filename = filename.filename(); |
| 183 | + |
| 184 | + HipBin hipBin; |
| 185 | + hipBin.executeHipBin(filename.string(), argc, argv); |
| 186 | +} |
0 commit comments