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
|
package com.mbien.opencl;
import com.sun.gluegen.runtime.BufferFactory;
import com.sun.gluegen.runtime.PointerBuffer;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import static com.mbien.opencl.CLException.*;
import static com.mbien.opencl.CL.*;
/**
*
* @author Michael Bien
*/
public abstract class CLMemory <B extends Buffer> implements CLResource {
B buffer;
public final long ID;
protected final CLContext context;
protected final CL cl;
protected <Buffer> CLMemory(CLContext context, long id) {
this.context = context;
this.cl = context.cl;
this.ID = id;
}
protected CLMemory(CLContext context, B directBuffer, long id) {
this.buffer = directBuffer;
this.context = context;
this.cl = context.cl;
this.ID = id;
}
/**
* Returns true if a host pointer must be specified on mem object creation.
*/
protected static final boolean isHostPointerFlag(int flags) {
return (flags & CL_MEM_COPY_HOST_PTR) != 0
|| (flags & CL_MEM_USE_HOST_PTR) != 0;
}
protected static final int sizeOfBufferElem(Buffer buffer) {
if (buffer instanceof ByteBuffer) {
return BufferFactory.SIZEOF_BYTE;
} else if (buffer instanceof IntBuffer) {
return BufferFactory.SIZEOF_INT;
} else if (buffer instanceof ShortBuffer) {
return BufferFactory.SIZEOF_SHORT;
} else if (buffer instanceof FloatBuffer) {
return BufferFactory.SIZEOF_FLOAT;
} else if (buffer instanceof DoubleBuffer) {
return BufferFactory.SIZEOF_DOUBLE;
}
throw new RuntimeException("Unexpected buffer type " + buffer.getClass().getName());
}
/**
* Returns a new instance of CLMemory pointing to the same CLResource but using a different Buffer.
*/
public abstract <T extends Buffer> CLMemory<T> cloneWith(T directBuffer);
public CLMemory<B> use(B buffer) {
if(this.buffer != null && buffer != null && this.buffer.getClass() != buffer.getClass()) {
throw new IllegalArgumentException(
"expected a Buffer of class " + this.buffer.getClass()
+" but got " + buffer.getClass());
}
this.buffer = buffer;
return this;
}
public B getBuffer() {
return buffer;
}
/**
* Returns the size of the wrapped direct buffer in byte.
*/
public int getSize() {
if(buffer == null) {
return 0;
}
return sizeOfBufferElem(buffer) * buffer.capacity();
}
public long getCLSize() {
PointerBuffer pb = PointerBuffer.allocateDirect(1);
int ret = cl.clGetMemObjectInfo(ID, CL_MEM_SIZE, PointerBuffer.elementSize(), pb.getBuffer(), null);
checkForError(ret, "can not optain buffer info");
return pb.get();
}
public void release() {
int ret = cl.clReleaseMemObject(ID);
context.onMemoryReleased(this);
checkForError(ret, "can not release mem object");
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final CLMemory<?> other = (CLMemory<?>) obj;
if (this.ID != other.ID) {
return false;
}
if (this.context != other.context && (this.context == null || !this.context.equals(other.context))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 83 * hash + (int) (this.ID ^ (this.ID >>> 32));
hash = 83 * hash + (this.context != null ? this.context.hashCode() : 0);
return hash;
}
@Override
public String toString() {
return "CLMemory [id: " + ID+"]";
}
/**
* Memory settings for configuring CLMemory.
*/
public enum Mem {
/**
* Enum representing CL_MEM_READ_WRITE.
* This flag specifies that the memory object will be read and
* written by a kernel.
*/
READ_WRITE(CL_MEM_READ_WRITE),
/**
* Enum representing CL_MEM_WRITE_ONLY.
* This flags specifies that the memory object will be written
* but not read by a kernel.
* Reading from a buffer or image object created with WRITE_ONLY
* inside a kernel is undefined.
*/
WRITE_ONLY(CL_MEM_WRITE_ONLY),
/**
* Enum representing CL_MEM_READ_ONLY.
* This flag specifies that the memory object is a read-only memory
* object when used inside a kernel. Writing to a buffer or image object
* created withREAD_ONLY inside a kernel is undefined.
*/
READ_ONLY(CL_MEM_READ_ONLY),
/**
* Enum representing CL_MEM_USE_HOST_PTR.
* If specified, it indicates that the application wants the OpenCL
* implementation to use memory referenced by host_ptr as the storage
* bits for the memory object. OpenCL implementations are allowed
* to cache the buffer contents pointed to by host_ptr in device memory.
* This cached copy can be used when kernels are executed on a device.
*/
USE_BUFFER(CL_MEM_USE_HOST_PTR),
/**
* Enum representing CL_MEM_ALLOC_HOST_PTR.
* This flag specifies that the application wants the OpenCL implementation
* to allocate memory from host accessible memory.
* {@link #ALLOC_HOST_PTR} and {@link #USE_BUFFER} are mutually exclusive.
*/
ALLOCATE_BUFFER(CL_MEM_ALLOC_HOST_PTR),
/**
* Enum representing CL_MEM_COPY_HOST_PTR.
* If {@link #COPY_BUFFER} specified, it indicates that the application
* wants the OpenCL implementation to allocate memory for the memory object
* and copy the data from memory referenced by host_ptr.<br/>
* {@link #COPY_BUFFER} and {@link #USE_BUFFER} are mutually exclusive.
*/
COPY_BUFFER(CL_MEM_COPY_HOST_PTR);
/**
* Value of wrapped OpenCL flag.
*/
public final int CONFIG;
private Mem(int config) {
this.CONFIG = config;
}
public static Mem valueOf(int bufferFlag) {
switch (bufferFlag) {
case CL_MEM_READ_WRITE:
return Mem.READ_WRITE;
case CL_MEM_READ_ONLY:
return Mem.READ_ONLY;
case CL_MEM_WRITE_ONLY:
return Mem.WRITE_ONLY;
case CL_MEM_USE_HOST_PTR:
return Mem.USE_BUFFER;
case(CL_MEM_ALLOC_HOST_PTR):
return ALLOCATE_BUFFER;
case CL_MEM_COPY_HOST_PTR:
return Mem.COPY_BUFFER;
}
return null;
}
static int flagsToInt(Mem[] flags) {
int clFlags = 0;
if (flags != null) {
for (int i = 0; i < flags.length; i++) {
clFlags |= flags[i].CONFIG;
}
}
if (clFlags == 0) {
clFlags = CL_MEM_READ_WRITE;
}
return clFlags;
}
}
/**
* Configures the mapping process of
* {@link com.mbien.opencl.CLCommandQueue#putMapBuffer(CLBuffer, CLMemory.Map, boolean)}.
*/
public enum Map {
/**
* Enum representing CL_MAP_READ | CL_MAP_WRITE.
* This flag specifies that the memory object will be mapped for read and write operation.
*/
READ_WRITE(CL_MAP_READ | CL_MAP_WRITE),
/**
* Enum representing CL_MAP_WRITE.
* This flag specifies that the memory object will be mapped for write operation.
*/
WRITE(CL_MAP_WRITE),
/**
* Enum representing CL_MAP_READ.
* This flag specifies that the memory object will be mapped for read operation.
*/
READ(CL_MAP_READ);
/**
* Value of wrapped OpenCL flag.
*/
public final int FLAGS;
private Map(int flags) {
this.FLAGS = flags;
}
public Map valueOf(int flag) {
if(flag == WRITE.FLAGS)
return WRITE;
else if(flag == READ.FLAGS)
return READ;
else if(flag == READ_WRITE.FLAGS)
return READ_WRITE;
return null;
}
}
}
|