diff --git a/projects/hip/CMakeLists.txt b/projects/hip/CMakeLists.txt index cbd2050025..53663e44b3 100644 --- a/projects/hip/CMakeLists.txt +++ b/projects/hip/CMakeLists.txt @@ -175,7 +175,8 @@ if(HIP_PLATFORM STREQUAL "hcc") src/hip_memory.cpp src/hip_peer.cpp src/hip_stream.cpp - src/hip_module.cpp) + src/hip_module.cpp + src/env.cpp) set(SOURCE_FILES_DEVICE src/device_util.cpp diff --git a/projects/hip/src/env.cpp b/projects/hip/src/env.cpp new file mode 100644 index 0000000000..90d00feac0 --- /dev/null +++ b/projects/hip/src/env.cpp @@ -0,0 +1,91 @@ +#include "hip_hcc.h" +#include "trace_helper.h" +#include "env.h" + +//--- +// Read environment variables. +void ihipReadEnv_I(int *var_ptr, const char *var_name1, const char *var_name2, const char *description) +{ + char * env = getenv(var_name1); + + // Check second name if first not defined, used to allow HIP_ or CUDA_ env vars. + if ((env == NULL) && strcmp(var_name2, "0")) { + env = getenv(var_name2); + } + + // Default is set when variable is initialized (at top of this file), so only override if we find + // an environment variable. + if (env) { + long int v = strtol(env, NULL, 0); + *var_ptr = (int) (v); + } + if (HIP_PRINT_ENV) { + printf ("%-30s = %2d : %s\n", var_name1, *var_ptr, description); + } +} + + +void ihipReadEnv_S(std::string *var_ptr, const char *var_name1, const char *var_name2, const char *description) +{ + char * env = getenv(var_name1); + + // Check second name if first not defined, used to allow HIP_ or CUDA_ env vars. + if ((env == NULL) && strcmp(var_name2, "0")) { + env = getenv(var_name2); + } + + if (env) { + *static_cast(var_ptr) = env; + } + if (HIP_PRINT_ENV) { + printf ("%-30s = %s : %s\n", var_name1, var_ptr->c_str(), description); + } +} + + +void ihipReadEnv_Callback(void *var_ptr, const char *var_name1, const char *var_name2, const char *description, std::string (*setterCallback)(void * var_ptr, const char * env)) +{ + char * env = getenv(var_name1); + + // Check second name if first not defined, used to allow HIP_ or CUDA_ env vars. + if ((env == NULL) && strcmp(var_name2, "0")) { + env = getenv(var_name2); + } + + std::string var_string = "0"; + if (env) { + var_string = setterCallback(var_ptr, env); + } + if (HIP_PRINT_ENV) { + printf ("%-30s = %s : %s\n", var_name1, var_string.c_str(), description); + } +} + + + + +void tokenize(const std::string &s, char delim, std::vector *tokens) +{ + std::stringstream ss; + ss.str(s); + std::string item; + while (getline(ss, item, delim)) { + item.erase (std::remove (item.begin(), item.end(), ' '), item.end()); // remove whitespace. + tokens->push_back(item); + } +} + +void trim(std::string *s) +{ + // trim whitespace from beginning and end: + const char *t = "\t\n\r\f\v"; + s->erase(0, s->find_first_not_of(t)); + s->erase(s->find_last_not_of(t)+1); +} + +static void ltrim(std::string *s) +{ + // trim whitespace from beginning + const char *t = "\t\n\r\f\v"; + s->erase(0, s->find_first_not_of(t)); +} diff --git a/projects/hip/src/env.h b/projects/hip/src/env.h new file mode 100644 index 0000000000..d1ec36f0c8 --- /dev/null +++ b/projects/hip/src/env.h @@ -0,0 +1,24 @@ +#pragma once + +extern void HipReadEnv(); + + + +#define READ_ENV_I(_build, _ENV_VAR, _ENV_VAR2, _description) \ + ihipReadEnv_I(&_ENV_VAR, #_ENV_VAR, #_ENV_VAR2, _description); + +#define READ_ENV_S(_build, _ENV_VAR, _ENV_VAR2, _description) \ + ihipReadEnv_S(&_ENV_VAR, #_ENV_VAR, #_ENV_VAR2, _description); + +#define READ_ENV_C(_build, _ENV_VAR, _ENV_VAR2, _description, _callback) \ + ihipReadEnv_Callback(&_ENV_VAR, #_ENV_VAR, #_ENV_VAR2, _description, _callback); + + +extern void ihipReadEnv_I(int *var_ptr, const char *var_name1, const char *var_name2, const char *description); +extern void ihipReadEnv_S(std::string *var_ptr, const char *var_name1, const char *var_name2, const char *description); +extern void ihipReadEnv_Callback(void *var_ptr, const char *var_name1, const char *var_name2, const char *description, std::string (*setterCallback)(void * var_ptr, const char * env)); + + +// String functions: +extern void trim(std::string *s); +extern void tokenize(const std::string &s, char delim, std::vector *tokens); diff --git a/projects/hip/src/hip_hcc.cpp b/projects/hip/src/hip_hcc.cpp index c84b93b503..1de3a9e7d3 100644 --- a/projects/hip/src/hip_hcc.cpp +++ b/projects/hip/src/hip_hcc.cpp @@ -46,6 +46,7 @@ THE SOFTWARE. #include "hip/hip_runtime.h" #include "hip_hcc.h" #include "trace_helper.h" +#include "env.h" #ifndef USE_COPY_EXT_V2 @@ -1037,166 +1038,6 @@ void ihipCtx_t::locked_waitAllStreams() } - -//--- -// Read environment variables. -void ihipReadEnv_I(int *var_ptr, const char *var_name1, const char *var_name2, const char *description) -{ - char * env = getenv(var_name1); - - // Check second name if first not defined, used to allow HIP_ or CUDA_ env vars. - if ((env == NULL) && strcmp(var_name2, "0")) { - env = getenv(var_name2); - } - - // Default is set when variable is initialized (at top of this file), so only override if we find - // an environment variable. - if (env) { - long int v = strtol(env, NULL, 0); - *var_ptr = (int) (v); - } - if (HIP_PRINT_ENV) { - printf ("%-30s = %2d : %s\n", var_name1, *var_ptr, description); - } -} - - -void ihipReadEnv_S(std::string *var_ptr, const char *var_name1, const char *var_name2, const char *description) -{ - char * env = getenv(var_name1); - - // Check second name if first not defined, used to allow HIP_ or CUDA_ env vars. - if ((env == NULL) && strcmp(var_name2, "0")) { - env = getenv(var_name2); - } - - if (env) { - *static_cast(var_ptr) = env; - } - if (HIP_PRINT_ENV) { - printf ("%-30s = %s : %s\n", var_name1, var_ptr->c_str(), description); - } -} - - -void ihipReadEnv_Callback(void *var_ptr, const char *var_name1, const char *var_name2, const char *description, std::string (*setterCallback)(void * var_ptr, const char * env)) -{ - char * env = getenv(var_name1); - - // Check second name if first not defined, used to allow HIP_ or CUDA_ env vars. - if ((env == NULL) && strcmp(var_name2, "0")) { - env = getenv(var_name2); - } - - std::string var_string = "0"; - if (env) { - var_string = setterCallback(var_ptr, env); - } - if (HIP_PRINT_ENV) { - printf ("%-30s = %s : %s\n", var_name1, var_string.c_str(), description); - } -} - - -#if defined (DEBUG) - -#define READ_ENV_I(_build, _ENV_VAR, _ENV_VAR2, _description) \ - if ((_build == release) || (_build == debug) {\ - ihipReadEnv_I(&_ENV_VAR, #_ENV_VAR, #_ENV_VAR2, _description);\ - }; -#define READ_ENV_S(_build, _ENV_VAR, _ENV_VAR2, _description) \ - if ((_build == release) || (_build == debug) {\ - ihipReadEnv_S(&_ENV_VAR, #_ENV_VAR, #_ENV_VAR2, _description);\ - }; -#define READ_ENV_C(_build, _ENV_VAR, _ENV_VAR2, _description, _callback) \ - if ((_build == release) || (_build == debug) {\ - ihipReadEnv_Callback(&_ENV_VAR, #_ENV_VAR, #_ENV_VAR2, _description, _callback);\ - }; - -#else - -#define READ_ENV_I(_build, _ENV_VAR, _ENV_VAR2, _description) \ - if (_build == release) {\ - ihipReadEnv_I(&_ENV_VAR, #_ENV_VAR, #_ENV_VAR2, _description);\ - }; - -#define READ_ENV_S(_build, _ENV_VAR, _ENV_VAR2, _description) \ - if (_build == release) {\ - ihipReadEnv_S(&_ENV_VAR, #_ENV_VAR, #_ENV_VAR2, _description);\ - }; -#define READ_ENV_C(_build, _ENV_VAR, _ENV_VAR2, _description, _callback) \ - if (_build == release) {\ - ihipReadEnv_Callback(&_ENV_VAR, #_ENV_VAR, #_ENV_VAR2, _description, _callback);\ - }; - -#endif - - -static void tokenize(const std::string &s, char delim, std::vector *tokens) -{ - std::stringstream ss; - ss.str(s); - std::string item; - while (getline(ss, item, delim)) { - item.erase (std::remove (item.begin(), item.end(), ' '), item.end()); // remove whitespace. - tokens->push_back(item); - } -} - -static void trim(std::string *s) -{ - // trim whitespace from beginning and end: - const char *t = "\t\n\r\f\v"; - s->erase(0, s->find_first_not_of(t)); - s->erase(s->find_last_not_of(t)+1); -} - -static void ltrim(std::string *s) -{ - // trim whitespace from beginning - const char *t = "\t\n\r\f\v"; - s->erase(0, s->find_first_not_of(t)); -} - - -// TODO - change last arg to pointer. -void parseTrigger(std::string triggerString, std::vector &profTriggers ) -{ - std::vector tidApiTokens; - tokenize(std::string(triggerString), ',', &tidApiTokens); - for (auto t=tidApiTokens.begin(); t != tidApiTokens.end(); t++) { - std::vector oneToken; - //std::cout << "token=" << *t << "\n"; - tokenize(std::string(*t), '.', &oneToken); - int tid = 1; - uint64_t apiTrigger = 0; - if (oneToken.size() == 1) { - // the case with just apiNum - apiTrigger = std::strtoull(oneToken[0].c_str(), nullptr, 0); - } else if (oneToken.size() == 2) { - // the case with tid.apiNum - tid = std::strtoul(oneToken[0].c_str(), nullptr, 0); - apiTrigger = std::strtoull(oneToken[1].c_str(), nullptr, 0); - } else { - throw ihipException(hipErrorRuntimeOther); // TODO -> bad env var? - } - - if (tid > 10000) { - throw ihipException(hipErrorRuntimeOther); // TODO -> bad env var? - } else { - profTriggers.resize(tid+1); - //std::cout << "tid:" << tid << " add: " << apiTrigger << "\n"; - profTriggers[tid].add(apiTrigger); - } - } - - - for (int tid=1; tid &profTriggers ) +{ + std::vector tidApiTokens; + tokenize(std::string(triggerString), ',', &tidApiTokens); + for (auto t=tidApiTokens.begin(); t != tidApiTokens.end(); t++) { + std::vector oneToken; + //std::cout << "token=" << *t << "\n"; + tokenize(std::string(*t), '.', &oneToken); + int tid = 1; + uint64_t apiTrigger = 0; + if (oneToken.size() == 1) { + // the case with just apiNum + apiTrigger = std::strtoull(oneToken[0].c_str(), nullptr, 0); + } else if (oneToken.size() == 2) { + // the case with tid.apiNum + tid = std::strtoul(oneToken[0].c_str(), nullptr, 0); + apiTrigger = std::strtoull(oneToken[1].c_str(), nullptr, 0); + } else { + throw ihipException(hipErrorRuntimeOther); // TODO -> bad env var? + } + + if (tid > 10000) { + throw ihipException(hipErrorRuntimeOther); // TODO -> bad env var? + } else { + profTriggers.resize(tid+1); + //std::cout << "tid:" << tid << " add: " << apiTrigger << "\n"; + profTriggers[tid].add(apiTrigger); + } + } + + + for (int tid=1; tid #include #include "hsa/hsa_ext_amd.h" + +#include "hip/hip_runtime.h" #include "hip_util.h" +#include "env.h" #if defined(__HCC__) && (__hcc_workweek__ < 16354) @@ -35,7 +38,6 @@ THE SOFTWARE. #define USE_IPC 0 - //--- // Environment variables: @@ -295,9 +297,6 @@ extern void recordApiTrace(std::string *fullStr, const std::string &apiStr); - - - class ihipException : public std::exception { public: