forked from BachiLi/redner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
thrust_utils.h
99 lines (90 loc) · 3.07 KB
/
thrust_utils.h
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
#pragma once
#include <thrust/memory.h>
#include <thrust/device_malloc.h>
#include <thrust/device_free.h>
#include <thrust/execution_policy.h>
#include <unordered_map>
#ifdef WIN32
#define DISPATCH(use_gpu, f, ...) \
((use_gpu) ? f(thrust::device, ##__VA_ARGS__) : f(thrust::host, ##__VA_ARGS__))
#define DISPATCH_CACHED(use_gpu, alloc, f, ...) \
((use_gpu) ? f(thrust::device(alloc), ##__VA_ARGS__) : f(thrust::host(alloc), ##__VA_ARGS__))
#else
#define DISPATCH(use_gpu, f, args...) \
((use_gpu) ? f(thrust::device, args) : f(thrust::host, args))
#define DISPATCH_CACHED(use_gpu, alloc, f, args...) \
((use_gpu) ? f(thrust::device(alloc), args) : f(thrust::host(alloc), args))
#endif
// Assume thrust only allocate one temporary buffer
struct ThrustCachedAllocator {
using value_type = char;
static constexpr int max_num_buffers = 2;
int num_buffers;
bool use_gpu;
int block_size[max_num_buffers];
char* block_ptr[max_num_buffers];
bool used[max_num_buffers];
ThrustCachedAllocator(bool use_gpu, int init_block_size = 0) : use_gpu(use_gpu) {
num_buffers = use_gpu ? 1 : 2;
for (int i = 0; i < num_buffers; i++) {
block_size[i] = init_block_size;
block_ptr[i] = nullptr;
if (block_size[i] > 0) {
if (use_gpu) {
block_ptr[i] = thrust::device_malloc<char>(block_size[i]).get();
} else {
block_ptr[i] = new char[block_size[i]];
}
}
used[i] = false;
}
}
~ThrustCachedAllocator() {
for (int i = 0; i < num_buffers; i++) {
if (block_ptr[i] != nullptr) {
if (use_gpu) {
thrust::device_free(thrust::device_ptr<char>(block_ptr[i]));
} else {
delete[] block_ptr[i];
}
}
}
}
char* allocate(std::ptrdiff_t num_bytes) {
for (int i = 0; i < num_buffers; i++) {
// Find first unused buffer
if (used[i]) {
continue;
}
if (num_bytes > block_size[i]) {
if (block_ptr[i] != nullptr) {
if (use_gpu) {
thrust::device_free(thrust::device_ptr<char>(block_ptr[i]));
} else {
delete[] block_ptr[i];
}
}
block_size[i] = num_bytes;
if (use_gpu) {
block_ptr[i] = thrust::device_malloc<char>(block_size[i]).get();
} else {
block_ptr[i] = new char[block_size[i]];
}
}
used[i] = true;
return block_ptr[i];
}
assert(false);
return nullptr;
}
void deallocate(char* ptr, size_t n) {
for (int i = 0; i < num_buffers; i++) {
if (block_ptr[i] == ptr) {
assert(used[i]);
used[i] = false;
return;
}
}
assert(false);
}
};