-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuda_runtime.h
320 lines (251 loc) · 9.41 KB
/
muda_runtime.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
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
//
// Copyright 2009 - 2017 Light Transport Entertainment Inc.
//
#ifndef MUDA_RUNTIME_H
#define MUDA_RUNTIME_H
// C headers
#include <cstdlib>
// C++ headers
#include <vector>
#include <map>
#ifdef HAVE_OPENCL
#include "clew.h"
#endif // HAVE_OPENCL
namespace muda {
typedef enum {
cpu = 1, // muda
ocl_cpu,
ocl_gpu,
ocl_accel,
cuda,
brook,
} MUDADeviceTarget;
typedef enum {
host = 1,
device_global,
device_cached_global,
device_constant,
device_texture,
} MUDAMemoryType;
typedef enum {
ro, // read only.
wo, // write only.
rw, // read and write.
} MUDAMemoryAttrib;
// Forward decl.
struct _MUDAMemory;
typedef struct _MUDAMemory *MUDAMemory; // MUDA memory object.
struct _MUDAProgram;
typedef struct _MUDAProgram *MUDAProgram; // MUDA kernel object.
struct _MUDAKernel;
typedef struct _MUDAKernel *MUDAKernel; // MUDA kernel object.
class MUDADeviceImpl;
// Base class of MUDA device.
class MUDADevice {
public:
MUDADevice(MUDADeviceTarget target);
~MUDADevice();
// Function: initialize
// Initializes device.
bool initialize(int platformID = 0, int preferredDeviceID = 0,
bool verbosity = false);
// Function: getNumDevides
// Returns # of available devices.
int getNumDevices();
// Function: estimateMFlops
// Returns the Mflops of ith device.
int estimateMFlops(int deviceId);
// Function: loadKernelSource
// Loads and compiles MUDA kernel code from source file.
MUDAProgram loadKernelSource(const char *filename, int nheaders,
const char **headers, const char *options);
// Function: loadKernelBinary
// Loads precompiled MUDA binary from the file.
MUDAProgram loadKernelBinary(const char *filename);
MUDAKernel createKernel(const MUDAProgram program, const char *functionName);
// Function getModule
// Get compiled binary kernel module.
bool getModule(MUDAProgram program, std::vector<char>& binary);
MUDAMemory alloc(MUDAMemoryType memType, MUDAMemoryAttrib memAttrib,
size_t memSize);
// MUDAMemory allocImage(MUDAMemoryType memType, MUDAMemoryAttrib memAttrib,
// size_t width, size_t height, int components);
bool free(MUDAMemory mem);
bool write(int ID, MUDAMemory mem, size_t size, const void *ptr);
// bool writeImage(int ID, MUDAMemory mem, size_t width, size_t height, const
// void *ptr);
bool read(int deviceID, MUDAMemory mem, size_t size, void *ptr);
bool bindMemoryObject(MUDAKernel kernel, int argNum, MUDAMemory mem);
bool setArg(MUDAKernel kernel, int argNum, size_t size, size_t align,
void *arg);
// Function: execute
// Executes MUDA kernel.
bool execute(int deviceID, MUDAKernel kernel, int dimension, size_t sizeX,
size_t sizeY, size_t sizeZ, size_t localSizeX, size_t localSizeY,
size_t localSizeZ);
// compileSource();
private:
MUDADeviceImpl *impl;
};
//
// MDUA Device Interface class.
//
class MUDADeviceImpl {
public:
MUDADeviceImpl() {};
~MUDADeviceImpl() {};
// Function: initialize
// Initializes MUDA device(s).
virtual bool initialize(int platformID, int preferredDeviceID,
bool verbosity) = 0;
virtual int getNumDevices() = 0;
// Function: estimateMFlops
// Returns the Mflops of ith device.
virtual int estimateMFlops(int deviceId) = 0;
// Function: shutdown
// Free all MU related resources, except for memory objects.
virtual bool shutdown() = 0;
// Function: loadKernelSource
// Loads and compiles MUDA kernel from source file.
virtual MUDAProgram loadKernelSource(const char *filename, int nheaders,
const char **headers,
const char *options) = 0;
// Function: loadKernelBinary
// Loads precompiled MUDA binary from the file.
virtual MUDAProgram loadKernelBinary(const char *filename) = 0;
// Function: createKernel
// Creates kernel object from kernel module.
// You should call loadKernelSource() before calling createKernel().
virtual MUDAKernel createKernel(const MUDAProgram program,
const char *functionName) = 0;
// Function getModule
// Get compiled binary kernel module.
virtual bool getModule(MUDAProgram program, std::vector<char>& binary) = 0;
// Function: alloc
// Allocates MUDA device memory.
virtual MUDAMemory alloc(MUDAMemoryType memType, MUDAMemoryAttrib memAttrib,
size_t memSize) = 0;
// virtual MUDAMemory allocImage(MUDAMemoryType memType, MUDAMemoryAttrib
// memAttrib,
// size_t width, size_t height, int components) = 0;
// Function: free
// Frees MUDA device memory.
virtual bool free(MUDAMemory mem) = 0;
// Function: bindMemoryObject
// Binds memory object to the MUDA kernel.
virtual bool bindMemoryObject(MUDAKernel kernel, int argNum,
MUDAMemory mem) = 0;
// Function: setKernelArg
// Sets any object to the kernel.
virtual bool setArg(MUDAKernel kernel, int argNum, size_t size, size_t align,
void *arg) = 0;
// Function: execute
// Executes MUDA kernel.
virtual bool execute(int deviceID, MUDAKernel kernel, int dimension,
size_t sizeX, size_t sizeY, size_t sizeZ,
size_t localSizeX, size_t localSizeY,
size_t localSizeZ) = 0;
// Function: read
// Reads data from MUDA buffer.
virtual bool read(int deviceID, MUDAMemory mem, size_t size, void *ptr) = 0;
// Function: write
// Writes data to MUDA buffer.
// This function does not return until actual memory copy is finished
// (bloking operation).
virtual bool write(int deviceID, MUDAMemory mem, size_t size,
const void *ptr) = 0;
// Function: writeImage
// Writes image to MUDA buffer.
// This function does not return until actual memory copy is finished
// (bloking operation).
// virtual bool writeImage(int deviceID, MUDAMemory mem, size_t width, size_t
// height,
// const void *ptr) = 0;
private:
};
// OpenCL
class MUDADeviceOCL : public MUDADeviceImpl {
public:
MUDADeviceOCL(MUDADeviceTarget target);
~MUDADeviceOCL();
// Function: initialize
// Initializes OpenCL device(s).
bool initialize(int platformID = 0, int preferredDeviceID = 0,
bool verbosity = false);
int getNumDevices();
// Function: estimateMFlops
// Returns the Mflops of ith device.
int estimateMFlops(int deviceId);
// Function: shutdown
// Free all CL related resources, except for memory objects.
bool shutdown();
// Function: loadKernelSource
// Loads and compiles OpenCL kernel from source file.
MUDAProgram loadKernelSource(const char *filename, int nheaders,
const char **headers, const char *options);
// Function: loadKernelBinary
// Loads precompiled MUDA binary from the file.
MUDAProgram loadKernelBinary(const char *filename);
// Function: createKernel
// Creates CL kernel object from CL program.
// You should call loadKernelSource() before calling createKernel().
MUDAKernel createKernel(const MUDAProgram program, const char *functionName);
// Function getModule
// Get compiled binary kernel module.
bool getModule(MUDAProgram program, std::vector<char>& binary);
// Function: alloc
// Allocates OpenCL device memory.
MUDAMemory alloc(MUDAMemoryType memType, MUDAMemoryAttrib memAttrib,
size_t memSize);
// Function: allocImage
// Allocates OpenCL image memory.
// MUDAMemory allocImage(MUDAMemoryType memType, MUDAMemoryAttrib memAttrib,
// size_t width, size_t height, int components);
// Function: free
// Frees OpenCL device memory.
bool free(MUDAMemory mem);
// Function: bindMemoryObject
// Binds memory object to the OpenCL kernel.
bool bindMemoryObject(MUDAKernel kernel, int argNum, MUDAMemory mem);
// Function: setKernelArg
// Sets any object to the kernel.
bool setArg(MUDAKernel kernel, int argNum, size_t size, size_t align,
void *arg);
// Function: execute
// Executes OpenCL kernel.
bool execute(int deviceID, MUDAKernel kernel, int dimension, size_t sizeX,
size_t sizeY, size_t sizeZ, size_t localSizeX, size_t localSizeY,
size_t localSizeZ);
// Function: read
// Reads data from OpenCL buffer.
bool read(int deviceID, MUDAMemory mem, size_t size, void *ptr);
// Function: write
// Writes data to OpenCL buffer.
// This function does not return until actual memory copy is finished
// (bloking operation).
bool write(int deviceID, MUDAMemory mem, size_t size, const void *ptr);
// Function: writeImage
// Writes image to OpenCL buffer.
// This function does not return until actual memory copy is finished
// (bloking operation).
// bool writeImage(int deviceID, MUDAMemory mem, size_t width, size_t height,
// const void *ptr);
// Returns size of OpenCL memory object.
const size_t getMemoryObjectSize() const;
private:
bool useCPU;
bool debug;
bool measureProfile;
bool verb;
std::vector<MUDAProgram> programs;
#ifdef HAVE_OPENCL
cl_context context;
std::vector<cl_device_id> devices; // array
int currentDeviceID;
std::vector<cl_command_queue> commandQueues;
std::vector<cl_kernel> kernels;
// std::vector<MUDAMemory> memObjs;
#endif
};
} // namespace muda
#endif // MUDA_RUNTIME_H