From 10c82cf73a2c2e4a944d10294b7d51f4575e0f6e Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Wed, 25 May 2011 00:21:37 +0200 Subject: CLAccessor SPI - initial refactorings. --- src/com/jogamp/opencl/CLDevice.java | 13 +- src/com/jogamp/opencl/CLEvent.java | 7 +- src/com/jogamp/opencl/CLImage.java | 4 +- src/com/jogamp/opencl/CLInfoAccessor.java | 128 -------------------- src/com/jogamp/opencl/CLPlatform.java | 37 ++++-- src/com/jogamp/opencl/CLSampler.java | 3 +- src/com/jogamp/opencl/CLTLInfoAccessor.java | 132 +++++++++++++++++++++ src/com/jogamp/opencl/spi/CLInfoAccessor.java | 25 ++++ .../jogamp/opencl/spi/CLPlatformInfoAccessor.java | 14 +++ 9 files changed, 215 insertions(+), 148 deletions(-) delete mode 100644 src/com/jogamp/opencl/CLInfoAccessor.java create mode 100644 src/com/jogamp/opencl/CLTLInfoAccessor.java create mode 100644 src/com/jogamp/opencl/spi/CLInfoAccessor.java create mode 100644 src/com/jogamp/opencl/spi/CLPlatformInfoAccessor.java (limited to 'src') diff --git a/src/com/jogamp/opencl/CLDevice.java b/src/com/jogamp/opencl/CLDevice.java index 0381038e..84a27f33 100644 --- a/src/com/jogamp/opencl/CLDevice.java +++ b/src/com/jogamp/opencl/CLDevice.java @@ -30,6 +30,7 @@ package com.jogamp.opencl; import com.jogamp.opencl.util.CLUtil; import com.jogamp.common.nio.NativeSizeBuffer; +import com.jogamp.opencl.spi.CLInfoAccessor; import java.nio.Buffer; import java.nio.ByteOrder; import java.util.ArrayList; @@ -51,11 +52,11 @@ import static com.jogamp.opencl.CL.*; * @see CLContext#getMaxFlopsDevice(com.jogamp.opencl.CLDevice.Type) * @author Michael Bien */ -public final class CLDevice extends CLObject { +public class CLDevice extends CLObject { private Set extensions; - private final CLDeviceInfoAccessor deviceInfo; + private final CLInfoAccessor deviceInfo; private final CLPlatform platform; CLDevice(CL cl, CLPlatform platform, long id) { @@ -64,6 +65,12 @@ public final class CLDevice extends CLObject { this.deviceInfo = new CLDeviceInfoAccessor(cl, id); } + protected CLDevice(CL cl, CLPlatform platform, CLInfoAccessor deviceAccessor, long id) { + super(cl, id); + this.platform = platform; + this.deviceInfo = deviceAccessor; + } + CLDevice(CLContext context, long id) { super(context, id); this.platform = context.getPlatform(); @@ -691,7 +698,7 @@ public final class CLDevice extends CLObject { return CLUtil.obtainDeviceProperties(this); } - private final static class CLDeviceInfoAccessor extends CLInfoAccessor { + private final static class CLDeviceInfoAccessor extends CLTLInfoAccessor { private final CL cl; private final long ID; diff --git a/src/com/jogamp/opencl/CLEvent.java b/src/com/jogamp/opencl/CLEvent.java index 876be3da..a48e4fef 100644 --- a/src/com/jogamp/opencl/CLEvent.java +++ b/src/com/jogamp/opencl/CLEvent.java @@ -64,12 +64,13 @@ public class CLEvent extends CLObject implements CLResource { // apparently only ExecutionStatus.COMPLETE is allowed -> private private void registerCallback(final CLEventListener callback, ExecutionStatus trigger) { cl.clSetEventCallback(ID, trigger.STATUS, new CLEventCallback() { - public void eventStateChanged(long event, int status) { + @Override public void eventStateChanged(long event, int status) { callback.eventStateChanged(CLEvent.this, status); } }); } + @Override public void release() { int ret = cl.clReleaseEvent(ID); checkForError(ret, "can not release event"); @@ -138,7 +139,7 @@ public class CLEvent extends CLObject implements CLResource { - private class CLEventInfoAccessor extends CLInfoAccessor { + private class CLEventInfoAccessor extends CLTLInfoAccessor { @Override protected int getInfo(int name, long valueSize, Buffer value, NativeSizeBuffer valueSizeRet) { @@ -147,7 +148,7 @@ public class CLEvent extends CLObject implements CLResource { } - private class CLEventProfilingInfoAccessor extends CLInfoAccessor { + private class CLEventProfilingInfoAccessor extends CLTLInfoAccessor { @Override protected int getInfo(int name, long valueSize, Buffer value, NativeSizeBuffer valueSizeRet) { diff --git a/src/com/jogamp/opencl/CLImage.java b/src/com/jogamp/opencl/CLImage.java index 01dd6ea2..48f3934f 100644 --- a/src/com/jogamp/opencl/CLImage.java +++ b/src/com/jogamp/opencl/CLImage.java @@ -41,7 +41,7 @@ public abstract class CLImage extends CLMemory { protected CLImageFormat format; - final CLInfoAccessor imageInfo; + final CLTLInfoAccessor imageInfo; public final int width; public final int height; @@ -100,7 +100,7 @@ public abstract class CLImage extends CLMemory { } - protected final static class CLImageInfoAccessor extends CLInfoAccessor { + protected final static class CLImageInfoAccessor extends CLTLInfoAccessor { private final long id; private final CL cl; diff --git a/src/com/jogamp/opencl/CLInfoAccessor.java b/src/com/jogamp/opencl/CLInfoAccessor.java deleted file mode 100644 index 08d7305e..00000000 --- a/src/com/jogamp/opencl/CLInfoAccessor.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright 2009 - 2010 JogAmp Community. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, are - * permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * The views and conclusions contained in the software and documentation are those of the - * authors and should not be interpreted as representing official policies, either expressed - * or implied, of JogAmp Community. - */ - -package com.jogamp.opencl; - -import com.jogamp.common.nio.NativeSizeBuffer; -import com.jogamp.common.os.Platform; -import com.jogamp.opencl.util.CLUtil; -import java.nio.Buffer; -import java.nio.ByteBuffer; - -import static com.jogamp.common.nio.Buffers.*; -import static com.jogamp.opencl.CLException.*; - -/** - * Internal utility for common OpenCL clGetFooInfo calls. - * Threadsafe. - * @author Michael Bien - */ -abstract class CLInfoAccessor { - - private static final int BB_SIZE = 512; - - protected final static ThreadLocal localBB = new ThreadLocal() { - - @Override - protected ByteBuffer initialValue() { - return newDirectByteBuffer(BB_SIZE); - } - - }; - protected final static ThreadLocal localNSB = new ThreadLocal() { - - @Override - protected NativeSizeBuffer initialValue() { - return NativeSizeBuffer.allocateDirect(1); - } - - }; - - public final long getLong(int key) { - - ByteBuffer buffer = getBB(8).putLong(0, 0); - int ret = getInfo(key, 8, buffer, null); - checkForError(ret, "error while asking for info value"); - - return buffer.getLong(0); - } - - public final String getString(int key) { - - NativeSizeBuffer sizeBuffer = getNSB(); - int ret = getInfo(key, 0, null, sizeBuffer); - checkForError(ret, "error while asking for info string"); - - int clSize = (int)sizeBuffer.get(0); - ByteBuffer buffer = getBB(clSize); - - ret = getInfo(key, buffer.capacity(), buffer, null); - checkForError(ret, "error while asking for info string"); - - byte[] array = new byte[clSize]; - buffer.get(array).rewind(); - - return CLUtil.clString2JavaString(array, clSize); - - } - - public final int[] getInts(int key, int n) { - - ByteBuffer buffer = getBB(n * (Platform.is32Bit()?4:8)); - int ret = getInfo(key, buffer.capacity(), buffer, null); - checkForError(ret, "error while asking for info value"); - - int[] array = new int[n]; - for(int i = 0; i < array.length; i++) { - if(Platform.is32Bit()) { - array[i] = buffer.getInt(); - }else{ - array[i] = (int)buffer.getLong(); - } - } - buffer.rewind(); - - return array; - } - - protected ByteBuffer getBB(int minCapacity) { - if(minCapacity > BB_SIZE) { - return newDirectByteBuffer(minCapacity); - }else{ - return localBB.get(); - } - } - - protected NativeSizeBuffer getNSB() { - return localNSB.get(); - } - - protected abstract int getInfo(int name, long valueSize, Buffer value, NativeSizeBuffer valueSizeRet); - - -} diff --git a/src/com/jogamp/opencl/CLPlatform.java b/src/com/jogamp/opencl/CLPlatform.java index ee6a6bdf..684f006b 100644 --- a/src/com/jogamp/opencl/CLPlatform.java +++ b/src/com/jogamp/opencl/CLPlatform.java @@ -30,18 +30,18 @@ package com.jogamp.opencl; import com.jogamp.common.nio.Buffers; import com.jogamp.common.os.DynamicLookupHelper; -import java.nio.Buffer; -import java.security.PrivilegedAction; import com.jogamp.common.JogampRuntimeException; import com.jogamp.common.os.NativeLibrary; import com.jogamp.common.nio.NativeSizeBuffer; import com.jogamp.gluegen.runtime.FunctionAddressResolver; +import com.jogamp.opencl.spi.CLPlatformInfoAccessor; import com.jogamp.opencl.util.CLUtil; import com.jogamp.opencl.impl.CLImpl; import com.jogamp.opencl.impl.CLProcAddressTable; import com.jogamp.opencl.util.Filter; import com.jogamp.opencl.util.JOCLVersion; +import java.nio.Buffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Collections; @@ -50,6 +50,7 @@ import java.util.List; import java.util.Map; import java.util.Scanner; import java.util.Set; +import java.security.PrivilegedAction; import static java.security.AccessController.*; import static com.jogamp.opencl.CLException.*; @@ -90,7 +91,7 @@ import static com.jogamp.opencl.CL.*; * @see #getDefault() * @see #listCLPlatforms() */ -public final class CLPlatform { +public class CLPlatform { /** * OpenCL platform id for this platform. @@ -102,16 +103,23 @@ public final class CLPlatform { */ public final CLVersion version; - private static CL cl; + protected static CL cl; private Set extensions; - private final CLPlatformInfoAccessor info; + protected final CLPlatformInfoAccessor info; private CLPlatform(long id) { initialize(); this.ID = id; - this.info = new CLPlatformInfoAccessor(id, cl); + this.info = new CLTLPlatformInfoAccessor(id, cl); + this.version = new CLVersion(getInfoString(CL_PLATFORM_VERSION)); + } + + protected CLPlatform(long id, CLPlatformInfoAccessor accessor) { + initialize(); + this.ID = id; + this.info = accessor; this.version = new CLVersion(getInfoString(CL_PLATFORM_VERSION)); } @@ -128,6 +136,7 @@ public final class CLPlatform { try { final CLProcAddressTable table = new CLProcAddressTable(new FunctionAddressResolver() { + @Override public long resolve(String name, DynamicLookupHelper lookup) { //FIXME workaround to fix a gluegen issue @@ -150,6 +159,7 @@ public final class CLPlatform { //load JOCL and init table doPrivileged(new PrivilegedAction() { + @Override public Object run() { NativeLibrary libOpenCL = JOCLJNILibLoader.loadOpenCL(); @@ -284,7 +294,7 @@ public final class CLPlatform { //add device to list for (int n = 0; n < deviceIDs.length; n++) { - list.add(new CLDevice(cl, this, deviceIDs[n])); + list.add(createDevice(deviceIDs[n])); } } @@ -304,7 +314,7 @@ public final class CLPlatform { //add device to list for (int n = 0; n < deviceIDs.length; n++) { - CLDevice device = new CLDevice(cl, this, deviceIDs[n]); + CLDevice device = createDevice(deviceIDs[n]); addIfAccepted(device, list, filters); } @@ -312,6 +322,10 @@ public final class CLPlatform { } + protected CLDevice createDevice(long id) { + return new CLDevice(cl, this, id); + } + private static void addIfAccepted(I item, List list, Filter[] filters) { if(filters == null) { list.add(item); @@ -489,16 +503,16 @@ public final class CLPlatform { /** * Returns a info string in exchange for a key (CL_PLATFORM_*). */ - public String getInfoString(int key) { + public final String getInfoString(int key) { return info.getString(key); } - private final static class CLPlatformInfoAccessor extends CLInfoAccessor { + private final static class CLTLPlatformInfoAccessor extends CLTLInfoAccessor implements CLPlatformInfoAccessor { private final long ID; private final CL cl; - private CLPlatformInfoAccessor(long id, CL cl) { + private CLTLPlatformInfoAccessor(long id, CL cl) { this.ID = id; this.cl = cl; } @@ -508,6 +522,7 @@ public final class CLPlatform { return cl.clGetPlatformInfo(ID, name, valueSize, value, valueSizeRet); } + @Override public long[] getDeviceIDs(long type) { IntBuffer buffer = getBB(4).asIntBuffer(); diff --git a/src/com/jogamp/opencl/CLSampler.java b/src/com/jogamp/opencl/CLSampler.java index f215cab1..63e25cb4 100644 --- a/src/com/jogamp/opencl/CLSampler.java +++ b/src/com/jogamp/opencl/CLSampler.java @@ -73,6 +73,7 @@ public class CLSampler extends CLObject implements CLResource { return samplerInfo.getLong(CL_SAMPLER_NORMALIZED_COORDS) == CL_TRUE; } + @Override public void release() { int ret = cl.clReleaseSampler(ID); context.onSamplerReleased(this); @@ -81,7 +82,7 @@ public class CLSampler extends CLObject implements CLResource { } } - private class CLSamplerInfoAccessor extends CLInfoAccessor { + private class CLSamplerInfoAccessor extends CLTLInfoAccessor { @Override protected int getInfo(int name, long valueSize, Buffer value, NativeSizeBuffer valueSizeRet) { diff --git a/src/com/jogamp/opencl/CLTLInfoAccessor.java b/src/com/jogamp/opencl/CLTLInfoAccessor.java new file mode 100644 index 00000000..286dbe6b --- /dev/null +++ b/src/com/jogamp/opencl/CLTLInfoAccessor.java @@ -0,0 +1,132 @@ +/* + * Copyright 2009 - 2010 JogAmp Community. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without modification, are + * permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this list of + * conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, this list + * of conditions and the following disclaimer in the documentation and/or other materials + * provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * The views and conclusions contained in the software and documentation are those of the + * authors and should not be interpreted as representing official policies, either expressed + * or implied, of JogAmp Community. + */ + +package com.jogamp.opencl; + +import com.jogamp.opencl.spi.CLInfoAccessor; +import com.jogamp.common.nio.NativeSizeBuffer; +import com.jogamp.common.os.Platform; +import com.jogamp.opencl.util.CLUtil; +import java.nio.Buffer; +import java.nio.ByteBuffer; + +import static com.jogamp.common.nio.Buffers.*; +import static com.jogamp.opencl.CLException.*; + +/** + * Internal utility for common OpenCL clGetFooInfo calls. + * Threadsafe, threadlocal implementation. + * @author Michael Bien + */ +public abstract class CLTLInfoAccessor implements CLInfoAccessor { + + private static final int BB_SIZE = 512; + + protected final static ThreadLocal localBB = new ThreadLocal() { + + @Override + protected ByteBuffer initialValue() { + return newDirectByteBuffer(BB_SIZE); + } + + }; + protected final static ThreadLocal localNSB = new ThreadLocal() { + + @Override + protected NativeSizeBuffer initialValue() { + return NativeSizeBuffer.allocateDirect(1); + } + + }; + + @Override + public final long getLong(int key) { + + ByteBuffer buffer = getBB(8).putLong(0, 0); + int ret = getInfo(key, 8, buffer, null); + checkForError(ret, "error while asking for info value"); + + return buffer.getLong(0); + } + + @Override + public final String getString(int key) { + + NativeSizeBuffer sizeBuffer = getNSB(); + int ret = getInfo(key, 0, null, sizeBuffer); + checkForError(ret, "error while asking for info string"); + + int clSize = (int)sizeBuffer.get(0); + ByteBuffer buffer = getBB(clSize); + + ret = getInfo(key, buffer.capacity(), buffer, null); + checkForError(ret, "error while asking for info string"); + + byte[] array = new byte[clSize]; + buffer.get(array).rewind(); + + return CLUtil.clString2JavaString(array, clSize); + + } + + @Override + public final int[] getInts(int key, int n) { + + ByteBuffer buffer = getBB(n * (Platform.is32Bit()?4:8)); + int ret = getInfo(key, buffer.capacity(), buffer, null); + checkForError(ret, "error while asking for info value"); + + int[] array = new int[n]; + for(int i = 0; i < array.length; i++) { + if(Platform.is32Bit()) { + array[i] = buffer.getInt(); + }else{ + array[i] = (int)buffer.getLong(); + } + } + buffer.rewind(); + + return array; + } + + protected ByteBuffer getBB(int minCapacity) { + if(minCapacity > BB_SIZE) { + return newDirectByteBuffer(minCapacity); + }else{ + return localBB.get(); + } + } + + protected NativeSizeBuffer getNSB() { + return localNSB.get(); + } + + protected abstract int getInfo(int name, long valueSize, Buffer value, NativeSizeBuffer valueSizeRet); + + +} diff --git a/src/com/jogamp/opencl/spi/CLInfoAccessor.java b/src/com/jogamp/opencl/spi/CLInfoAccessor.java new file mode 100644 index 00000000..0ff0aeac --- /dev/null +++ b/src/com/jogamp/opencl/spi/CLInfoAccessor.java @@ -0,0 +1,25 @@ +/* + * Created on Thursday, May 19 2011 16:43 + */ +package com.jogamp.opencl.spi; + +/** + * Internal utility for common OpenCL clGetFooInfo calls. + * Provides common accessors to CL objects. + * @author Michael Bien + */ +public interface CLInfoAccessor { + + int[] getInts(int key, int n); + + /** + * Returns the long value for the given key. + */ + long getLong(int key); + + /** + * Returns the String value for the given key. + */ + String getString(int key); + +} diff --git a/src/com/jogamp/opencl/spi/CLPlatformInfoAccessor.java b/src/com/jogamp/opencl/spi/CLPlatformInfoAccessor.java new file mode 100644 index 00000000..eb97fb56 --- /dev/null +++ b/src/com/jogamp/opencl/spi/CLPlatformInfoAccessor.java @@ -0,0 +1,14 @@ +/* + * Created on Thursday, May 19 2011 16:47 + */ +package com.jogamp.opencl.spi; + +/** + * + * @author Michael Bien + */ +public interface CLPlatformInfoAccessor extends CLInfoAccessor { + + long[] getDeviceIDs(long type); + +} -- cgit v1.2.3