X Tutup
Skip to content

Commit 22b16ee

Browse files
committed
JavaCL: removed all typed pointers for entities (!), and only using raw calls.
1 parent d9e98eb commit 22b16ee

File tree

14 files changed

+144
-197
lines changed

14 files changed

+144
-197
lines changed

Core/src/main/velocity/com/nativelibs4java/opencl/CLAbstractEntity.java

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,16 @@
33
import org.bridj.*;
44
import static org.bridj.Pointer.*;
55

6-
abstract class CLAbstractEntity<T extends TypedPointer> {
6+
abstract class CLAbstractEntity {
77
private long entityPeer;
8-
private T entity;
98
private final boolean nullable;
109

11-
CLAbstractEntity(T entity) {
12-
this(entity, false);
13-
}
14-
CLAbstractEntity(long entityPeer) {
10+
protected CLAbstractEntity(long entityPeer) {
1511
this(entityPeer, false);
1612
}
17-
CLAbstractEntity(T entity, boolean nullable) {
18-
this(entity, getPeer(entity), nullable);
19-
}
20-
21-
CLAbstractEntity(long entityPeer, boolean nullable) {
22-
this(null, entityPeer, nullable);
23-
}
24-
private CLAbstractEntity(T entity, long entityPeer, boolean nullable) {
13+
14+
protected CLAbstractEntity(long entityPeer, boolean nullable) {
2515
this.nullable = nullable;
26-
this.entity = entity;
2716
this.entityPeer = entityPeer;
2817
checkNullity(entityPeer);
2918
}
@@ -34,16 +23,8 @@ private void checkNullity(long entityPeer) {
3423
}
3524
}
3625

37-
protected void setEntity(T entity) {
38-
long entityPeer = getPeer(entity);
39-
checkNullity(entityPeer);
40-
this.entity = entity;
41-
this.entityPeer = entityPeer;
42-
}
43-
4426
protected void setEntity(long entityPeer) {
4527
checkNullity(entityPeer);
46-
this.entity = null;
4728
this.entityPeer = entityPeer;
4829
}
4930

@@ -91,7 +72,7 @@ public synchronized void release() {
9172
doRelease();
9273
}
9374

94-
public static <E extends TypedPointer, A extends CLAbstractEntity<E>> Pointer<E> getEntities(A[] objects, Pointer<E> out) {
75+
public static <E extends TypedPointer, A extends CLAbstractEntity> Pointer<E> getEntities(A[] objects, Pointer<E> out) {
9576
for (int i = 0, len = objects.length; i < len; i++)
9677
out.setSizeTAtOffset(i * Pointer.SIZE, objects[i].getEntityPeer());
9778
return out;
@@ -101,15 +82,6 @@ private void checkNullity() {
10182
throw new RuntimeException("This " + getClass().getSimpleName() + " has been manually released and can't be used anymore !");
10283
}
10384

104-
protected abstract T createEntityPointer(long peer);
105-
106-
synchronized T getEntity() {
107-
checkNullity();
108-
if (entity == null && entityPeer != 0) {
109-
entity = createEntityPointer(entityPeer);
110-
}
111-
return entity;
112-
}
11385
synchronized long getEntityPeer() {
11486
checkNullity();
11587
return entityPeer;
@@ -118,7 +90,6 @@ synchronized long getEntityPeer() {
11890
synchronized void doRelease() {
11991
if (entityPeer != 0) {
12092
clear();
121-
entity = null;
12293
entityPeer = 0;
12394
}
12495
}
@@ -136,9 +107,7 @@ protected void finalize() throws Throwable {
136107
public int hashCode() {
137108
return entityPeer == 0
138109
? 0
139-
: entity == null
140-
? Long.valueOf(entityPeer).hashCode()
141-
: entity.hashCode();
110+
: Long.valueOf(entityPeer).hashCode();
142111
}
143112

144113
/**
@@ -148,7 +117,7 @@ public int hashCode() {
148117
public boolean equals(Object obj) {
149118
if (!getClass().isInstance(obj))
150119
return false;
151-
CLAbstractEntity<?> e = (CLAbstractEntity<?>)obj;
120+
CLAbstractEntity e = (CLAbstractEntity)obj;
152121
return getEntityPeer() == e.getEntityPeer();
153122
}
154123

Core/src/main/velocity/com/nativelibs4java/opencl/CLContext.java

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
* Contexts are used by the OpenCL runtime for managing objects such as command-queues, memory, program and kernel objects and for executing kernels on one or more devices specified in the context.
5252
* @author Olivier Chafik
5353
*/
54-
public class CLContext extends CLAbstractEntity<cl_context> {
54+
public class CLContext extends CLAbstractEntity {
5555

5656
#macro (docCreateBufferCopy $bufferType $details)
5757
/**
@@ -131,9 +131,9 @@ else if ("false".equals(prop) || "0".equals(env))
131131
#declareInfosGetter("infos", "CL.clGetContextInfo")
132132

133133
CLPlatform platform;
134-
protected Pointer<cl_device_id> deviceIds;
134+
protected Pointer<SizeT> deviceIds;
135135

136-
CLContext(CLPlatform platform, Pointer<cl_device_id> deviceIds, cl_context context) {
136+
CLContext(CLPlatform platform, Pointer<SizeT> deviceIds, long context) {
137137
super(context);
138138
this.platform = platform;
139139
this.deviceIds = deviceIds;
@@ -143,11 +143,6 @@ else if ("false".equals(prop) || "0".equals(env))
143143
}
144144
}
145145

146-
@Override
147-
protected cl_context createEntityPointer(long peer) {
148-
return new cl_context(peer);
149-
}
150-
151146
/**
152147
* Creates a user event object. <br/>
153148
* User events allow applications to enqueue commands that wait on a user event to finish before the command is executed by the device.
@@ -171,7 +166,7 @@ public CLEvent createUserEvent() {
171166
* @return new OpenCL queue
172167
*/
173168
public CLQueue createDefaultQueue(QueueProperties... queueProperties) {
174-
return new CLDevice(platform, deviceIds.get(0)).createQueue(this, queueProperties);
169+
return new CLDevice(platform, deviceIds.getSizeT()).createQueue(this, queueProperties);
175170
}
176171

177172
/**
@@ -180,7 +175,7 @@ public CLQueue createDefaultQueue(QueueProperties... queueProperties) {
180175
* @return new out-of-order OpenCL queue
181176
*/
182177
public CLQueue createDefaultOutOfOrderQueue() {
183-
return new CLDevice(platform, deviceIds.get(0)).createOutOfOrderQueue(this);
178+
return new CLDevice(platform, deviceIds.getSizeT()).createOutOfOrderQueue(this);
184179
}
185180

186181

@@ -211,23 +206,23 @@ public CLQueue createDefaultOutOfOrderQueueIfPossible() {
211206
* @return new profiling-enabled OpenCL queue
212207
*/
213208
public CLQueue createDefaultProfilingQueue() {
214-
return new CLDevice(platform, deviceIds.get(0)).createProfilingQueue(this);
209+
return new CLDevice(platform, deviceIds.getSizeT()).createProfilingQueue(this);
215210
}
216211

217212
@SuppressWarnings("deprecation")
218213
public CLImageFormat[] getSupportedImageFormats(CLBuffer.Flags flags, CLBuffer.ObjectType imageType) {
219214
Pointer<Integer> pCount = allocateInt();
220215
int memFlags = (int) flags.value();
221216
int imTyp = (int) imageType.value();
222-
CL.clGetSupportedImageFormats(getEntity(), memFlags, imTyp, 0, null, pCount);
217+
CL.clGetSupportedImageFormats(getEntityPeer(), memFlags, imTyp, 0, 0, getPeer(pCount));
223218
//cl_image_format ft = new cl_image_format();
224219
//int sz = ft.size();
225220
int n = pCount.getInt();
226221
if (n == 0) {
227222
n = 30; // There HAS to be at least one format. the spec even says even more, but in fact on Mac OS X / CPU there's only one...
228223
}
229224
Pointer<cl_image_format> formats = allocateArray(cl_image_format.class, n);
230-
CL.clGetSupportedImageFormats(getEntity(), memFlags, imTyp, n, formats, (Pointer<Integer>) null);
225+
CL.clGetSupportedImageFormats(getEntityPeer(), memFlags, imTyp, n, getPeer(formats), 0);
231226
List<CLImageFormat> ret = new ArrayList<CLImageFormat>(n);
232227
for (cl_image_format ft : formats) {
233228
if (ft.image_channel_data_type() == 0 && ft.image_channel_order() == 0)
@@ -241,12 +236,12 @@ public CLImageFormat[] getSupportedImageFormats(CLBuffer.Flags flags, CLBuffer.O
241236
@SuppressWarnings("deprecation")
242237
public CLSampler createSampler(boolean normalized_coords, AddressingMode addressing_mode, FilterMode filter_mode) {
243238
#declareReusablePtrsAndPErr()
244-
cl_sampler sampler = CL.clCreateSampler(
245-
getEntity(),
239+
long sampler = CL.clCreateSampler(
240+
getEntityPeer(),
246241
normalized_coords ? CL_TRUE : CL_FALSE,
247242
(int) addressing_mode.value(),
248243
(int) filter_mode.value(),
249-
pErr
244+
getPeer(pErr)
250245
);
251246
#checkPErr()
252247
return new CLSampler(sampler);
@@ -262,13 +257,13 @@ public int getDeviceCount() {
262257
*/
263258
public synchronized CLDevice[] getDevices() {
264259
if (deviceIds == null) {
265-
deviceIds = infos.getMemory(getEntityPeer(), CL_CONTEXT_DEVICES).as(cl_device_id.class);
260+
deviceIds = infos.getMemory(getEntityPeer(), CL_CONTEXT_DEVICES).as(SizeT.class);
266261
}
267262
int n = (int)deviceIds.getValidElements();
268263

269264
CLDevice[] devices = new CLDevice[n];
270265
for (int i = n; i-- != 0;) {
271-
devices[i] = new CLDevice(platform, deviceIds.get(i));
266+
devices[i] = new CLDevice(platform, deviceIds.getSizeTAtOffset(i * Pointer.SIZE));
272267
}
273268
return devices;
274269
}
@@ -320,19 +315,29 @@ public CLDevice guessCurrentGLDevice() {
320315
Pointer<SizeT> pCount = allocateSizeT();
321316
Pointer<Pointer<?>> mem = allocatePointer();
322317
if (Platform.isMacOSX())
323-
error(CL.clGetGLContextInfoAPPLE(getEntity(), OpenGLContextUtils.CGLGetCurrentContext(),
324-
CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,
325-
Pointer.SIZE, mem, pCount));
318+
error(CL.clGetGLContextInfoAPPLE(
319+
getEntityPeer(),
320+
getPeer(OpenGLContextUtils.CGLGetCurrentContext()),
321+
CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,
322+
Pointer.SIZE,
323+
getPeer(mem),
324+
getPeer(pCount)));
326325
else
327-
error(CL.clGetGLContextInfoKHR((Pointer)propsRef, CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR, Pointer.SIZE, mem, pCount));
326+
error(CL.clGetGLContextInfoKHR(
327+
getPeer(propsRef),
328+
CL_CURRENT_DEVICE_FOR_GL_CONTEXT_KHR,
329+
Pointer.SIZE,
330+
getPeer(mem),
331+
getPeer(pCount)
332+
));
328333

329334
if (pCount.getSizeT() != Pointer.SIZE)
330335
throw new RuntimeException("Not a device : len = " + pCount.get().intValue());
331336

332337
Pointer p = mem.getPointer();
333338
if (p.equals(Pointer.NULL))
334339
return null;
335-
return new CLDevice(null, new cl_device_id(p));
340+
return new CLDevice(null, getPeer(p));
336341
}
337342

338343
private static <T extends CLMem> T markAsGL(T mem) {

Core/src/main/velocity/com/nativelibs4java/opencl/CLDevice.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,26 +30,21 @@
3030
* {@link CLPlatform#listGPUDevices(boolean) }
3131
*/
3232
@SuppressWarnings("unused")
33-
public class CLDevice extends CLAbstractEntity<cl_device_id> {
33+
public class CLDevice extends CLAbstractEntity {
3434

3535
#declareInfosGetter("infos", "CL.clGetDeviceInfo")
3636

3737
volatile CLPlatform platform;
3838

39-
CLDevice(CLPlatform platform, cl_device_id device) {
39+
CLDevice(CLPlatform platform, long device) {
4040
super(device);
4141
this.platform = platform;
4242
}
4343

44-
@Override
45-
protected cl_device_id createEntityPointer(long peer) {
46-
return new cl_device_id(peer);
47-
}
48-
4944
public synchronized CLPlatform getPlatform() {
5045
if (platform == null) {
5146
Pointer pplat = infos.getPointer(getEntityPeer(), CL_DEVICE_PLATFORM);
52-
platform = new CLPlatform(pplat == null ? null : new cl_platform_id(pplat));
47+
platform = new CLPlatform(getPeer(pplat));
5348
}
5449
return platform;
5550
}
@@ -313,7 +308,7 @@ public CLQueue createQueue(CLContext context, QueueProperties... queueProperties
313308
long flags = 0;
314309
for (QueueProperties prop : queueProperties)
315310
flags |= prop.value();
316-
cl_command_queue queue = CL.clCreateCommandQueue(context.getEntity(), getEntity(), flags, pErr);
311+
long queue = CL.clCreateCommandQueue(context.getEntityPeer(), getEntityPeer(), flags, getPeer(pErr));
317312
#checkPErr()
318313

319314
return new CLQueue(context, queue, this);
@@ -322,7 +317,7 @@ public CLQueue createQueue(CLContext context, QueueProperties... queueProperties
322317
@Deprecated
323318
public CLQueue createQueue(EnumSet<QueueProperties> queueProperties, CLContext context) {
324319
#declareReusablePtrsAndPErr()
325-
cl_command_queue queue = CL.clCreateCommandQueue(context.getEntity(), getEntity(), QueueProperties.getValue(queueProperties), pErr);
320+
long queue = CL.clCreateCommandQueue(context.getEntityPeer(), getEntityPeer(), QueueProperties.getValue(queueProperties), getPeer(pErr));
326321
#checkPErr()
327322

328323
return new CLQueue(context, queue, this);

Core/src/main/velocity/com/nativelibs4java/opencl/CLEvent.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*
2929
* @author ochafik
3030
*/
31-
public class CLEvent extends CLAbstractEntity<cl_event> {
31+
public class CLEvent extends CLAbstractEntity {
3232

3333
/**
3434
* Pass this to special value to any method that expects a variable number of events to wait for and that returns an event, to completely avoid returning the completion event (will return null instead of the event).
@@ -39,19 +39,10 @@ public class CLEvent extends CLAbstractEntity<cl_event> {
3939

4040
#declareInfosGetter("profilingInfos", "CL.clGetEventProfilingInfo")
4141
42-
CLEvent(cl_event evt) {
43-
super(evt, false);
44-
}
45-
4642
CLEvent(long evt) {
4743
super(evt, false);
4844
}
4945
50-
@Override
51-
protected cl_event createEntityPointer(long peer) {
52-
return new cl_event(peer);
53-
}
54-
5546
public interface EventCallback {
5647
public void callback(CLEvent event, int executionStatus);
5748
}
@@ -82,7 +73,7 @@ public void apply(OpenCLLibrary.cl_event evt, int executionStatus, Pointer voidP
8273
};
8374
// TODO manage lifespan of cb
8475
BridJ.protectFromGC(cb);
85-
error(CL.clSetEventCallback(getEntity(), commandExecStatus, pointerTo(cb), null));
76+
error(CL.clSetEventCallback(getEntityPeer(), commandExecStatus, getPeer(pointerTo(cb)), 0));
8677
} catch (Throwable th) {
8778
// TODO check if supposed to handle OpenCL 1.1
8879
throw new UnsupportedOperationException("Cannot set event callback (OpenCL 1.1 feature).", th);

Core/src/main/velocity/com/nativelibs4java/opencl/CLImage.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,16 @@ protected CLEvent read(CLQueue queue, Pointer<SizeT> origin, Pointer<SizeT> regi
6767
6868
}*/
6969
#declareReusablePtrsAndEventsInOutBlockable()
70-
error(CL.clEnqueueReadImage(queue.getEntity(), getEntity(),
70+
error(CL.clEnqueueReadImage(
71+
queue.getEntityPeer(),
72+
getEntityPeer(),
7173
blocking ? CL_TRUE : CL_FALSE,
72-
origin,
73-
region,
74+
getPeer(origin),
75+
getPeer(region),
7476
rowPitch,
7577
slicePitch,
76-
out,
77-
#eventsInOutArgs()
78+
getPeer(out),
79+
#eventsInOutArgsRaw()
7880
));
7981
#returnEventOut("queue")
8082
}
@@ -84,14 +86,16 @@ protected CLEvent write(CLQueue queue, Pointer<SizeT> origin, Pointer<SizeT> reg
8486
}
8587
protected CLEvent write(CLQueue queue, Pointer<SizeT> origin, Pointer<SizeT> region, long rowPitch, long slicePitch, Pointer<?> in, boolean blocking, CLEvent... eventsToWaitFor) {
8688
#declareReusablePtrsAndEventsInOutBlockable()
87-
error(CL.clEnqueueWriteImage(queue.getEntity(), getEntity(),
89+
error(CL.clEnqueueWriteImage(
90+
queue.getEntityPeer(),
91+
getEntityPeer(),
8892
blocking ? CL_TRUE : CL_FALSE,
89-
origin,
90-
region,
93+
getPeer(origin),
94+
getPeer(region),
9195
rowPitch,
9296
slicePitch,
93-
in,
94-
#eventsInOutArgs()
97+
getPeer(in),
98+
#eventsInOutArgsRaw()
9599
));
96100
CLEvent evt = #eventOutWrapper("queue");
97101

0 commit comments

Comments
 (0)
X Tutup