-
Notifications
You must be signed in to change notification settings - Fork 1
/
amp.h
210 lines (169 loc) · 4.79 KB
/
amp.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
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
//#include <array>
#include <iostream>
#include <initializer_list>
#if __CXXAMP__
#define __AMP_global __attribute__((address_space(0xFFFF00)))
#else
#define __AMP_global
#endif
#define __CL_ENABLE_EXCEPTIONS
#define CL_USE_DEPRECATED_OPENCL_1_1_APIS
#include <CL/cl.hpp>
namespace concurrency
{
template <int Rank> class extent
{
int value[Rank];
public:
int &operator[](int const i) { return value[i]; }
int const &operator[](int const i) const { return value[i]; }
extent() {}
extent(std::initializer_list<int> list)
{
auto e = list.begin();
for(int i = 0; i < Rank; ++i) value[i] = *e++;
}
};
template <int Rank> class index : public extent<Rank>
{
public:
index() {}
index(std::initializer_list<int> list) : extent<Rank>(list) {}
};
inline cl::Context &getContext()
{
static std::vector<cl::Platform> platforms;
static bool init = true;
if(init)
{
cl::Platform::get(&platforms);
if(platforms.size() == 0)
{
std::cout << "Platform size 0\n";
}
init = false;
}
static cl_context_properties properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[0])(), 0};
static cl::Context context(CL_DEVICE_TYPE_GPU, properties);
return context;
}
inline cl::CommandQueue &getQueue()
{
static std::vector<cl::Device> devices = getContext().getInfo<CL_CONTEXT_DEVICES>();
static cl::CommandQueue queue(getContext(), devices[0], 0);
return queue;
}
template <typename ValueType, int Rank = 1> class array_view
{
public: //HACK: accessor compatibility until properties work
extent<Rank> extent;
private:
ValueType __AMP_global * data;
cl::Buffer buffer;
int getIndex(index<Rank> const &index) const
{
int stride = 1;
int p = 0;
for(int i = Rank - 1; i >= 0; --i)
{
p = index[i] * stride + p;
stride = stride * extent[i];
}
return p;
}
public:
array_view(int size0, int size1, ValueType *data) :
data((ValueType __AMP_global *)data),
buffer(getContext(), CL_MEM_USE_HOST_PTR | CL_MEM_READ_ONLY, sizeof(ValueType) * size0 * size1, data)
{
extent[0] = size0;
extent[1] = size1;
}
ValueType __AMP_global &operator[](index<Rank> const &index) const
{
return data[getIndex(index)];
}
ValueType __AMP_global &operator()(int const index0, int const index1) const
{
return data[getIndex(index<Rank> {{ index0, index1 }})];
}
void synchronize()
{
getQueue().enqueueMapBuffer(buffer, true, CL_MAP_READ, 0, sizeof(ValueType) * extent[0] * extent[1]);
}
};
template<int Rank, typename Kernel> inline void parallel_for_each(extent<Rank> const &extent, Kernel const &kernel, int rank, index<Rank> &index)
{
for(int i = 0; i < extent[rank]; ++i)
{
index[rank] = i;
if(rank + 1 < Rank)
{
parallel_for_each(extent, kernel, rank + 1, index);
}
else
{
kernel(index);
}
}
}
#if __CXXAMP__
template <int Rank, typename Kernel> [[amp::kernel]] void kernel_proc(Kernel *data, index<Rank> *index) restrict(amp)
{
(*data)(*index);
}
extern "C" void amp_execute_kernel(void(*)());
struct pointer_buffer
{
void const* Data;
cl::Buffer Buffer;
};
template<typename Kernel> struct kernel_info
{
char const *Code;
char const *Entry;
std::uint32_t BufferCount, ConstSize;
void (*GetBuffers)(Kernel const* data, pointer_buffer const** buffers);
void (*GetConstData)(Kernel const* data, void* buffer);
};
template<int Rank, typename Kernel> inline void parallel_for_each(extent<Rank> const &extent, Kernel const &closure)
{
auto const &info = *((kernel_info<Kernel> const *(*)())&kernel_proc<Rank, Kernel>)();
cl_int err = CL_SUCCESS;
auto const &context = getContext();
std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
cl::Program::Binaries binaries(1, std::make_pair(info.Code, strlen(info.Code)));
cl::Program program = cl::Program(context, devices, binaries);
program.build(devices);
cl::Kernel kernel(program, info.Entry, &err);
void * const constData = std::malloc(info.ConstSize);
info.GetConstData(&closure, constData);
cl::Buffer constants(context, CL_MEM_COPY_HOST_PTR | CL_MEM_READ_ONLY, info.ConstSize, constData);
std::free(constData);
kernel.setArg(0, constants);
pointer_buffer const * buffers[10];
info.GetBuffers(&closure, buffers);
for(unsigned i = 0; i < info.BufferCount; ++i)
{
kernel.setArg(i + 1, buffers[i]->Buffer);
}
cl::Event event;
auto const &queue = getQueue();
queue.enqueueNDRangeKernel(
kernel,
cl::NullRange,
cl::NDRange(extent[0], extent[1]),
cl::NullRange,
NULL,
&event);
event.wait();
}
extern "C" void * foo = 0;
#else
template<int Rank, typename Kernel> inline void parallel_for_each(extent<Rank> const &extent, Kernel const &kernel)
{
index<Rank> index;
parallel_for_each(extent, kernel, 0, index);
}
#endif
}