-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsystem.cpp
More file actions
172 lines (153 loc) · 3.63 KB
/
Copy pathsystem.cpp
File metadata and controls
172 lines (153 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// Licensed under the MIT License. See LICENSE.TXT file in the project root for full license information.
//
#include "system.hpp"
#ifdef _WIN32
#include <Windows.h>
#else
#include <dlfcn.h>
#endif
#include <iterator> // std::size
static
void* load_library(const char* filename)
{
void* handle = nullptr;
#ifdef _WIN32
HMODULE hLib = LoadLibraryA(filename);
handle = reinterpret_cast<void*>(hLib);
#else
handle = dlopen(filename, RTLD_LAZY | RTLD_LOCAL);
#endif
return handle;
}
static
void free_library(void* handle)
{
#ifdef _WIN32
HMODULE hLib = reinterpret_cast<HMODULE>(handle);
FreeLibrary(hLib);
#else
dlclose(handle);
#endif
}
template<int count>
void* load_any(const char* (&filenames) [count])
{
for (auto filename : filenames)
{
void* handle = load_library(filename);
if (handle != nullptr)
{
return handle;
}
}
return nullptr;
}
static
bool check_halide_features(const char* features)
{
std::string target_string = "host-";
target_string += features;
bool valid = Halide::Target::validate_target_string(target_string);
return valid;
}
static
bool check_CUDA()
{
if (!check_halide_features("cuda"))
return false;
const char* libs [] =
{
#ifdef _WIN32
"nvcuda.dll",
#else
"libcuda.so",
"libcuda.dylib",
"/Library/Frameworks/CUDA.framework/CUDA",
#endif
};
static void* lib_handle = load_any(libs);
bool has = (lib_handle != nullptr);
// NOTE(marcos): keeping the handle open to prevent external deletion
//free_library(lib_handle);
return has;
}
static
bool check_OpenCL()
{
if (!check_halide_features("opencl"))
return false;
const char* libs [] =
{
#ifdef _WIN32
"opencl.dll",
#else
"libOpenCL.so",
"/System/Library/Frameworks/OpenCL.framework/OpenCL",
#endif
};
static void* lib_handle = load_any(libs);
bool has = (lib_handle != nullptr);
// NOTE(marcos): keeping the handle open to prevent external deletion
//free_library(lib_handle);
return has;
}
static
bool check_Metal()
{
if (!check_halide_features("metal"))
return false;
const char* libs [] =
{
#ifdef _WIN32
""
#else
"/System/Library/Frameworks/Metal.framework/Versions/Current/Metal",
#endif
};
static void* lib_handle = load_any(libs);
bool has = (lib_handle != nullptr);
// NOTE(marcos): keeping the handle open to prevent external deletion
//free_library(lib_handle);
return has;
}
static
bool check_D3D12()
{
if (!check_halide_features("d3d12compute"))
return false;
const char* libs [] =
{
#ifdef _WIN32
"d3d12.dll",
#else
""
#endif
};
static void* lib_handle = load_any(libs);
bool has = (lib_handle != nullptr);
// NOTE(marcos): keeping the handle open to prevent external deletion
//free_library(lib_handle);
return has;
}
SystemInfo::SystemInfo()
{
this->host = Halide::get_host_target();
cuda = check_CUDA();
opencl = check_OpenCL();
metal = check_Metal();
d3d12 = check_D3D12();
}
bool SystemInfo::Supports(Halide::Target::Feature feature) const
{
return host.has_feature(feature);
}
bool SystemInfo::Supports(Halide::Target::Arch architecture, int bits) const
{
bool support = (host.arch == architecture);
if (bits != 0)
{
support &= (host.bits == bits);
}
return support;
}