-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathanydsl_runtime.hpp
More file actions
149 lines (119 loc) · 3.59 KB
/
anydsl_runtime.hpp
File metadata and controls
149 lines (119 loc) · 3.59 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
#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));
}
template <typename T>
void copy_async(const Array<T>& a, Array<T>& b) {
anydsl_copy_async(a.device(), (const void*)a.data(), 0,
b.device(), (void*)b.data(), 0,
a.size() * sizeof(T));
}
template <typename T>
void copy_async(const Array<T>& a, Array<T>& b, int64_t size) {
anydsl_copy_async(a.device(), (const void*)a.data(), 0,
b.device(), (void*)b.data(), 0,
size * sizeof(T));
}
template <typename T>
void copy_async(const Array<T>& a, int64_t offset_a, Array<T>& b, int64_t offset_b, int64_t size) {
anydsl_copy_async(a.device(), (const void*)a.data(), offset_a * sizeof(T),
b.device(), (void*)b.data(), offset_b * sizeof(T),
size * sizeof(T));
}
} // namespace anydsl
#endif