Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 105 additions & 2 deletions contrib/Orochi/contrib/hipew/src/hipew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
# define WIN32_LEAN_AND_MEAN
# define VC_EXTRALEAN
# include <windows.h>
# include <algorithm>
# include <cstdlib>
# include <string>
# include <vector>

/* Utility macros. */

Expand All @@ -54,6 +58,93 @@ typedef HMODULE DynamicLibrary;
# define dynamic_library_open(path) LoadLibraryA(path)
# define dynamic_library_close(lib) FreeLibrary(lib)
# define dynamic_library_find(lib, symbol) GetProcAddress(lib, symbol)

static std::string hipew_get_env(const char* name)
{
char* value = nullptr;
size_t size = 0;
if (_dupenv_s(&value, &size, name) != 0 || value == nullptr)
return std::string();
std::string result(value);
std::free(value);
return result;
}

static bool hipew_directory_exists(const std::string& path)
{
if (path.empty())
return false;
DWORD attributes = GetFileAttributesA(path.c_str());
return attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY);
}

static std::string hipew_join_path(const std::string& base, const char* leaf)
{
if (base.empty())
return std::string(leaf);
const char last = base.back();
if (last == '\\' || last == '/')
return base + leaf;
return base + "\\" + leaf;
}

static void hipew_append_unique(std::vector<std::string>& paths, const std::string& path)
{
if (path.empty() || std::find(paths.begin(), paths.end(), path) != paths.end())
return;
paths.push_back(path);
}

static void hipew_append_rocm_bin(std::vector<std::string>& paths, const std::string& root)
{
if (root.empty())
return;
if (hipew_directory_exists(root))
hipew_append_unique(paths, root);
const std::string bin = hipew_join_path(root, "bin");
if (hipew_directory_exists(bin))
hipew_append_unique(paths, bin);
}

static std::vector<std::string> hipew_rocm_bin_paths()
{
std::vector<std::string> result;

hipew_append_rocm_bin(result, hipew_get_env("ROCM_HOME"));
hipew_append_rocm_bin(result, hipew_get_env("ROCM_PATH"));
hipew_append_rocm_bin(result, hipew_get_env("HIP_PATH"));

const std::string virtual_env = hipew_get_env("VIRTUAL_ENV");
if (!virtual_env.empty()) {
hipew_append_rocm_bin(result, hipew_join_path(virtual_env, "Lib\\site-packages\\_rocm_sdk_devel"));
hipew_append_rocm_bin(result, hipew_join_path(virtual_env, "Lib\\site-packages\\rocm_sdk_devel"));
}

return result;
}

static const char** hipew_make_library_paths(
const char* const* dll_names,
std::vector<std::string>& storage,
std::vector<const char*>& pointers)
{
storage.clear();
pointers.clear();

const std::vector<std::string> rocm_bins = hipew_rocm_bin_paths();

for (size_t i = 0; dll_names[i] != nullptr; ++i) {
hipew_append_unique(storage, dll_names[i]);
for (const std::string& bin : rocm_bins)
hipew_append_unique(storage, hipew_join_path(bin, dll_names[i]));
}

for (const std::string& path : storage)
pointers.push_back(path.c_str());
pointers.push_back(nullptr);

return pointers.data();
}
#else
# include <dlfcn.h>

Expand Down Expand Up @@ -619,12 +710,18 @@ void hipewInit( int* resultDriver, int* resultRtc, uint32_t flags, const char**

#ifdef _WIN32
// Expected in C:/Windows/System32 or similar, no path needed.
const char* hip_paths[] = {
const char* hip_dll_names[] = {
"amdhip64_7.dll",
"amdhip64_6.dll",
"amdhip64.dll", // <- hip '5.x' DLL.
NULL };
const char* hiprtc_paths[] = {
const char* hiprtc_dll_names[] = {
"hiprtc07013.dll",
"hiprtc07012.dll",
"hiprtc07011.dll",
"hiprtc07010.dll",
"hiprtc0709.dll",
"hiprtc0708.dll",
"hiprtc0707.dll",
"hiprtc0706.dll",
"hiprtc0705.dll",
Expand All @@ -645,6 +742,12 @@ void hipewInit( int* resultDriver, int* resultRtc, uint32_t flags, const char**
"hiprtc0504.dll",
"hiprtc0503.dll",
NULL };
static std::vector<std::string> hip_path_storage;
static std::vector<const char*> hip_path_pointers;
static std::vector<std::string> hiprtc_path_storage;
static std::vector<const char*> hiprtc_path_pointers;
const char** hip_paths = hipew_make_library_paths(hip_dll_names, hip_path_storage, hip_path_pointers);
const char** hiprtc_paths = hipew_make_library_paths(hiprtc_dll_names, hiprtc_path_storage, hiprtc_path_pointers);
#elif defined(__APPLE__)
// Default installation path.
const char *hip_paths[] = {"", NULL};
Expand Down
112 changes: 102 additions & 10 deletions hiprt/hiprt_libpath.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,108 @@

#ifdef _WIN32

const char* g_hip_paths[] = {
"amdhip64_7.dll",
"amdhip64_6.dll",
"amdhip64.dll", // <- hip '5.x' DLL.
nullptr };

const char* g_hiprtc_paths[] = { "hiprtc0707.dll", "hiprtc0706.dll", "hiprtc0705.dll", "hiprtc0704.dll", "hiprtc0703.dll",
"hiprtc0702.dll", "hiprtc0701.dll", "hiprtc0700.dll", "hiprtc0605.dll", "hiprtc0604.dll",
"hiprtc0603.dll", "hiprtc0602.dll", "hiprtc0601.dll", "hiprtc0600.dll", "hiprtc0507.dll",
"hiprtc0506.dll", "hiprtc0505.dll", "hiprtc0504.dll", "hiprtc0503.dll", nullptr };
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>

namespace hiprt
{
namespace detail
{
inline std::string getEnvVariable( const char* name )
{
const char* value = std::getenv( name );
return value ? std::string( value ) : std::string();
}

inline std::string joinPath( const std::string& base, const char* leaf )
{
if ( base.empty() ) return std::string( leaf );
const char last = base.back();
if ( last == '\\' || last == '/' ) return base + leaf;
return base + "\\" + leaf;
}

inline void appendUnique( std::vector<std::string>& paths, const std::string& path )
{
if ( path.empty() || std::find( paths.begin(), paths.end(), path ) != paths.end() ) return;
paths.push_back( path );
}

inline void appendRocmRoot( std::vector<std::string>& paths, const std::string& root )
{
if ( root.empty() ) return;
appendUnique( paths, root );
appendUnique( paths, joinPath( root, "bin" ) );
}

inline std::vector<std::string> rocmBinPaths()
{
std::vector<std::string> result;
appendRocmRoot( result, getEnvVariable( "ROCM_HOME" ) );
appendRocmRoot( result, getEnvVariable( "ROCM_PATH" ) );
appendRocmRoot( result, getEnvVariable( "HIP_PATH" ) );

const std::string virtualEnv = getEnvVariable( "VIRTUAL_ENV" );
if ( !virtualEnv.empty() )
{
appendRocmRoot( result, joinPath( virtualEnv, "Lib\\site-packages\\_rocm_sdk_devel" ) );
appendRocmRoot( result, joinPath( virtualEnv, "Lib\\site-packages\\rocm_sdk_devel" ) );
}

return result;
}

inline const char** makeLibraryPaths(
const char* const* dllNames,
std::vector<std::string>& storage,
std::vector<const char*>& pointers )
{
storage.clear();
pointers.clear();

const std::vector<std::string> rocmBins = rocmBinPaths();
for ( size_t i = 0; dllNames[i] != nullptr; ++i )
{
appendUnique( storage, dllNames[i] );
for ( const std::string& bin : rocmBins )
appendUnique( storage, joinPath( bin, dllNames[i] ) );
}

for ( const std::string& path : storage )
pointers.push_back( path.c_str() );
pointers.push_back( nullptr );

return pointers.data();
}

inline const char** getHipPaths()
{
static const char* dllNames[] = { "amdhip64_7.dll", "amdhip64_6.dll", "amdhip64.dll", nullptr };
static std::vector<std::string> storage;
static std::vector<const char*> pointers;
return makeLibraryPaths( dllNames, storage, pointers );
}

inline const char** getHiprtcPaths()
{
static const char* dllNames[] = { "hiprtc07013.dll", "hiprtc07012.dll", "hiprtc07011.dll", "hiprtc07010.dll",
"hiprtc0709.dll", "hiprtc0708.dll", "hiprtc0707.dll", "hiprtc0706.dll",
"hiprtc0705.dll", "hiprtc0704.dll", "hiprtc0703.dll", "hiprtc0702.dll",
"hiprtc0701.dll", "hiprtc0700.dll", "hiprtc0605.dll", "hiprtc0604.dll",
"hiprtc0603.dll", "hiprtc0602.dll", "hiprtc0601.dll", "hiprtc0600.dll",
"hiprtc0507.dll", "hiprtc0506.dll", "hiprtc0505.dll", "hiprtc0504.dll",
"hiprtc0503.dll", nullptr };
static std::vector<std::string> storage;
static std::vector<const char*> pointers;
return makeLibraryPaths( dllNames, storage, pointers );
}
} // namespace detail
} // namespace hiprt

static const char** g_hip_paths = hiprt::detail::getHipPaths();
static const char** g_hiprtc_paths = hiprt::detail::getHiprtcPaths();
Comment on lines +134 to +135
#elif defined( __APPLE__ )

const char** g_hip_paths = nullptr;
Expand Down
Loading