-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathanydsl_runtime.hpp
More file actions
204 lines (164 loc) · 4.34 KB
/
anydsl_runtime.hpp
File metadata and controls
204 lines (164 loc) · 4.34 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
#ifndef ANYDSL_RUNTIME_HPP
#define ANYDSL_RUNTIME_HPP
#ifndef ANYDSL_RUNTIME_H
#include "anydsl_runtime.h"
#endif
namespace anydsl {
enum class Platform : int32_t {
Host = ANYDSL_HOST,
Cuda = ANYDSL_CUDA,
OpenCL = ANYDSL_OPENCL,
HSA = ANYDSL_HSA
};
struct Device {
Device(int32_t id) : id(id) {}
int32_t id;
};
inline int32_t make_device(Platform p, Device d) {
return ANYDSL_DEVICE((int32_t)p, d.id);
}
template <typename T>
class Array {
public:
Array()
: data_(nullptr), size_(0), dev_(0)
{}
Array(int64_t size)
: Array(Platform::Host, Device(0), size)
{}
Array(int32_t dev, T* ptr, int64_t size)
: data_(ptr), size_(size), dev_(dev)
{}
Array(Platform p, Device d, int64_t size)
: dev_(make_device(p, d)) {
allocate(size);
}
Array(Array&& other)
: data_(other.data_),
size_(other.size_),
dev_(other.dev_) {
other.data_ = nullptr;
}
Array& operator = (Array&& other) {
deallocate();
dev_ = other.dev_;
size_ = other.size_;
data_ = other.data_;
other.data_ = nullptr;
return *this;
}
Array(const Array&) = delete;
Array& operator = (const Array&) = delete;
~Array() { deallocate(); }
T* begin() { return data_; }
const T* begin() const { return data_; }
T* end() { return data_ + size_; }
const T* end() const { return data_ + size_; }
T* data() { return data_; }
const T* data() const { return data_; }
int64_t size() const { return size_; }
int32_t device() const { return dev_; }
const T& operator [] (int i) const { return data_[i]; }
T& operator [] (int i) { return data_[i]; }
T* release() {
T* ptr = data_;
data_ = nullptr;
size_ = 0;
dev_ = 0;
return ptr;
}
protected:
void allocate(int64_t size) {
size_ = size;
data_ = (T*)anydsl_alloc(dev_, sizeof(T) * size);
}
void deallocate() {
if (data_) anydsl_release(dev_, (void*)data_);
}
T* data_;
int64_t size_;
int32_t dev_;
};
template <typename T>
void copy(const Array<T>& a, Array<T>& b) {
anydsl_copy(a.device(), (const void*)a.data(), 0,
b.device(), (void*)b.data(), 0,
a.size() * sizeof(T));
}
template <typename T>
void copy(const Array<T>& a, Array<T>& b, int64_t size) {
anydsl_copy(a.device(), (const void*)a.data(), 0,
b.device(), (void*)b.data(), 0,
size * sizeof(T));
}
template <typename T>
void copy(const Array<T>& a, int64_t offset_a, Array<T>& b, int64_t offset_b, int64_t size) {
anydsl_copy(a.device(), (const void*)a.data(), offset_a * sizeof(T),
b.device(), (void*)b.data(), offset_b * sizeof(T),
size * sizeof(T));
}
class Event {
public:
inline Event(int32_t dev)
: dev_(dev),
event_(0)
{
create();
}
inline ~Event()
{
destroy();
}
inline Event(Event&& other)
: dev_(other.dev_),
event_(other.event_)
{
other.event_ = 0;
}
inline Event& operator=(Event&& other)
{
destroy();
dev_ = other.dev_;
event_ = other.event_;
other.event_ = 0;
return *this;
}
inline Event(const Event&) = delete;
inline Event& operator=(const Event&) = delete;
inline bool record()
{
anydsl_record_event(dev_, event_);
return true;
}
inline bool wait()
{
anydsl_sync_event(dev_, event_);
return true;
}
inline anydsl_event_t handle() const { return event_; }
inline static float elapsedTimeMS(const Event& start, const Event& end)
{
if (!anydsl_check_event(start.dev_, start.handle()))
return -1;
if (!anydsl_check_event(end.dev_, end.handle()))
return -1;
const uint64_t us = anydsl_query_us_event(start.dev_, start.handle(), end.handle());
return us / 1000.0f;
}
private:
inline void create()
{
event_ = anydsl_create_event(dev_);
}
inline void destroy()
{
if (event_ != 0) {
anydsl_destroy_event(dev_, event_);
event_ = 0;
}
}
int32_t dev_;
anydsl_event_t event_;
};
} // namespace anydsl
#endif