-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathanydsl_runtime.cpp
More file actions
349 lines (293 loc) · 10.6 KB
/
anydsl_runtime.cpp
File metadata and controls
349 lines (293 loc) · 10.6 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <random>
#include <chrono>
#include <locale>
#include <mutex>
#include <sstream>
#include "anydsl_runtime.h"
// Make sure the definition for runtime() matches
// the declaration in anydsl_jit.h
#include "anydsl_jit.h"
#include "runtime.h"
#include "platform.h"
#include "dummy_platform.h"
#include "cpu_platform.h"
#ifdef AnyDSL_runtime_HAS_TBB_SUPPORT
#define NOMINMAX
#include <tbb/parallel_for.h>
#include <tbb/task_arena.h>
#include <tbb/task_group.h>
#include <tbb/concurrent_unordered_map.h>
#include <tbb/concurrent_queue.h>
#else
#include <thread>
#endif
struct RuntimeSingleton {
Runtime runtime;
RuntimeSingleton()
: runtime(detect_profile_level())
{
runtime.register_platform<CpuPlatform>();
register_cuda_platform(&runtime);
register_opencl_platform(&runtime);
register_hsa_platform(&runtime);
}
static std::pair<ProfileLevel, ProfileLevel> detect_profile_level() {
auto profile = std::make_pair(ProfileLevel::None, ProfileLevel::None);
const char* env_var = std::getenv("ANYDSL_PROFILE");
if (env_var) {
std::string env_str = env_var;
for (auto& c: env_str)
c = std::toupper(c, std::locale());
std::stringstream profile_levels(env_str);
std::string level;
while (profile_levels >> level) {
if (level == "FULL")
profile.first = ProfileLevel::Full;
else if (level == "FPGA_DYNAMIC")
profile.second = ProfileLevel::Fpga_dynamic;
}
}
return profile;
}
};
Runtime& runtime() {
static RuntimeSingleton singleton;
return singleton.runtime;
}
inline PlatformId to_platform(int32_t m) {
return PlatformId(m & 0x0F);
}
inline DeviceId to_device(int32_t m) {
return DeviceId(m >> 4);
}
void anydsl_info(void) {
runtime().display_info();
}
const char* anydsl_device_name(int32_t mask) {
return runtime().device_name(to_platform(mask), to_device(mask));
}
bool anydsl_device_check_feature_support(int32_t mask, const char* feature) {
return runtime().device_check_feature_support(to_platform(mask), to_device(mask), feature);
}
void* anydsl_alloc(int32_t mask, int64_t size) {
return runtime().alloc(to_platform(mask), to_device(mask), size);
}
void* anydsl_alloc_host(int32_t mask, int64_t size) {
return runtime().alloc_host(to_platform(mask), to_device(mask), size);
}
void* anydsl_alloc_unified(int32_t mask, int64_t size) {
return runtime().alloc_unified(to_platform(mask), to_device(mask), size);
}
void* anydsl_get_device_ptr(int32_t mask, void* ptr) {
return runtime().get_device_ptr(to_platform(mask), to_device(mask), ptr);
}
void anydsl_release(int32_t mask, void* ptr) {
runtime().release(to_platform(mask), to_device(mask), ptr);
}
void anydsl_release_host(int32_t mask, void* ptr) {
runtime().release_host(to_platform(mask), to_device(mask), ptr);
}
void anydsl_copy(
int32_t mask_src, const void* src, int64_t offset_src,
int32_t mask_dst, void* dst, int64_t offset_dst, int64_t size) {
runtime().copy(
to_platform(mask_src), to_device(mask_src), src, offset_src,
to_platform(mask_dst), to_device(mask_dst), dst, offset_dst, size, false);
}
void anydsl_copy_async(
int32_t mask_src, const void* src, int64_t offset_src,
int32_t mask_dst, void* dst, int64_t offset_dst, int64_t size) {
runtime().copy(
to_platform(mask_src), to_device(mask_src), src, offset_src,
to_platform(mask_dst), to_device(mask_dst), dst, offset_dst, size, true);
}
void anydsl_launch_kernel(
int32_t mask, const char* file_name, const char* kernel_name,
const uint32_t* grid, const uint32_t* block,
void** arg_data,
const uint32_t* arg_sizes,
const uint32_t* arg_aligns,
const uint32_t* arg_alloc_sizes,
const uint8_t* arg_types,
uint32_t num_args) {
LaunchParams launch_params = {
file_name,
kernel_name,
grid,
block,
{
arg_data,
arg_sizes,
arg_aligns,
arg_alloc_sizes,
reinterpret_cast<const KernelArgType*>(arg_types),
},
num_args
};
runtime().launch_kernel(to_platform(mask), to_device(mask), launch_params);
}
void anydsl_synchronize(int32_t mask) {
runtime().synchronize(to_platform(mask), to_device(mask));
}
uint64_t anydsl_get_micro_time() {
using namespace std::chrono;
return duration_cast<microseconds>(steady_clock::now().time_since_epoch()).count();
}
uint64_t anydsl_get_nano_time() {
using namespace std::chrono;
return duration_cast<nanoseconds>(steady_clock::now().time_since_epoch()).count();
}
uint64_t anydsl_get_kernel_time() {
return runtime().kernel_time().load();
}
int32_t anydsl_isinff(float x) { return std::isinf(x); }
int32_t anydsl_isnanf(float x) { return std::isnan(x); }
int32_t anydsl_isfinitef(float x) { return std::isfinite(x); }
int32_t anydsl_isinf(double x) { return std::isinf(x); }
int32_t anydsl_isnan(double x) { return std::isnan(x); }
int32_t anydsl_isfinite(double x) { return std::isfinite(x); }
void anydsl_print_i16(int16_t s) { std::cout << s; }
void anydsl_print_i32(int32_t i) { std::cout << i; }
void anydsl_print_i64(int64_t l) { std::cout << l; }
void anydsl_print_u16(uint16_t s) { std::cout << s; }
void anydsl_print_u32(uint32_t i) { std::cout << i; }
void anydsl_print_u64(uint64_t l) { std::cout << l; }
void anydsl_print_f32(float f) { std::cout << f; }
void anydsl_print_f64(double d) { std::cout << d; }
void anydsl_print_char(char c) { std::cout << c; }
void anydsl_print_string(char* s) { std::cout << s; }
void anydsl_print_flush() { std::cout << std::flush; }
void* anydsl_aligned_malloc(size_t size, size_t align) {
return Runtime::aligned_malloc(size, align);
}
void anydsl_aligned_free(void* ptr) {
return Runtime::aligned_free(ptr);
}
#ifndef __has_feature
#define __has_feature(x) 0
#endif
#if (defined (__clang__) && !__has_feature(cxx_thread_local))
#pragma message("Runtime random function is not thread-safe")
static std::mt19937 std_gen;
#else
static thread_local std::mt19937 std_gen;
#endif
static std::uniform_real_distribution<float> std_dist_f32;
static std::uniform_int_distribution<uint64_t> std_dist_u64;
void anydsl_random_seed(uint32_t seed) {
std_gen.seed(seed);
}
float anydsl_random_val_f32() {
return std_dist_f32(std_gen);
}
uint64_t anydsl_random_val_u64() {
return std_dist_u64(std_gen);
}
#ifndef AnyDSL_runtime_HAS_TBB_SUPPORT // C++11 threads version
static std::unordered_map<int32_t, std::thread> thread_pool;
static std::vector<int32_t> free_ids;
static std::mutex thread_lock;
void anydsl_parallel_for(int32_t num_threads, int32_t lower, int32_t upper, void* args, void* fun) {
// Get number of available hardware threads
if (num_threads == 0) {
num_threads = std::thread::hardware_concurrency();
// hardware_concurrency is implementation defined, may return 0
num_threads = (num_threads == 0) ? 1 : num_threads;
}
void (*fun_ptr) (void*, int32_t, int32_t) = reinterpret_cast<void (*) (void*, int32_t, int32_t)>(fun);
const int32_t linear = (upper - lower) / num_threads;
// Create a pool of threads to execute the task
std::vector<std::thread> pool(num_threads);
for (int i = 0, a = lower, b = lower + linear; i < num_threads - 1; a = b, b += linear, i++) {
pool[i] = std::thread([=]() {
fun_ptr(args, a, b);
});
}
pool[num_threads - 1] = std::thread([=]() {
fun_ptr(args, lower + (num_threads - 1) * linear, upper);
});
// Wait for all the threads to finish
for (int i = 0; i < num_threads; i++)
pool[i].join();
}
int32_t anydsl_spawn_thread(void* args, void* fun) {
std::lock_guard<std::mutex> lock(thread_lock);
int32_t (*fun_ptr) (void*) = reinterpret_cast<int32_t (*) (void*)>(fun);
int32_t id;
if (free_ids.size()) {
id = free_ids.back();
free_ids.pop_back();
} else {
id = static_cast<int32_t>(thread_pool.size());
}
auto spawned = std::make_pair(id, std::thread([=](){ fun_ptr(args); }));
thread_pool.emplace(std::move(spawned));
return id;
}
void anydsl_sync_thread(int32_t id) {
auto thread = thread_pool.end();
{
std::lock_guard<std::mutex> lock(thread_lock);
thread = thread_pool.find(id);
}
if (thread != thread_pool.end()) {
thread->second.join();
{
std::lock_guard<std::mutex> lock(thread_lock);
free_ids.push_back(thread->first);
thread_pool.erase(thread);
}
} else {
assert(0 && "Trying to synchronize on invalid thread id");
}
}
#else // TBB version
void anydsl_parallel_for(int32_t num_threads, int32_t lower, int32_t upper, void* args, void* fun) {
tbb::task_arena limited((num_threads == 0) ? tbb::task_arena::automatic : num_threads);
tbb::task_group tg;
void (*fun_ptr) (void*, int32_t, int32_t) = reinterpret_cast<void (*) (void*, int32_t, int32_t)>(fun);
limited.execute([&] {
tg.run([&] {
tbb::parallel_for(tbb::blocked_range<int32_t>(lower, upper),
[=] (const tbb::blocked_range<int32_t>& range) {
fun_ptr(args, range.begin(), range.end());
});
});
});
limited.execute([&] { tg.wait(); });
}
typedef tbb::concurrent_unordered_map<int32_t, tbb::task_group, std::hash<int32_t>> task_group_map;
typedef std::pair<task_group_map::iterator, bool> task_group_node_ref;
static task_group_map task_pool;
static tbb::concurrent_queue<int32_t> free_ids;
static std::mutex thread_lock;
int32_t anydsl_spawn_thread(void* args, void* fun) {
std::lock_guard<std::mutex> lock(thread_lock);
int32_t id = -1;
if (!free_ids.try_pop(id)) {
id = int32_t(task_pool.size());
}
int32_t(*fun_ptr) (void*) = reinterpret_cast<int32_t(*)(void*)>(fun);
assert(id >= 0);
task_group_node_ref p = task_pool.emplace(std::piecewise_construct, std::forward_as_tuple(id), std::forward_as_tuple());
tbb::task_group& tg = p.first->second;
tg.run([=] { fun_ptr(args); });
return id;
}
void anydsl_sync_thread(int32_t id) {
auto task = task_pool.end();
{
std::lock_guard<std::mutex> lock(thread_lock);
task = task_pool.find(id);
}
if (task != task_pool.end()) {
task->second.wait();
{
std::lock_guard<std::mutex> lock(thread_lock);
free_ids.push(task->first);
}
} else {
assert(0 && "Trying to synchronize on invalid task id");
}
}
#endif