From dcf83966f7fdd5bfc0753f29d763dfd85e1bfb1e Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Wed, 18 May 2011 23:50:43 +0200 Subject: clarified stream closing in javadoc, initial capacity for StringBuilder. --- src/com/jogamp/opencl/CLContext.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/com/jogamp') diff --git a/src/com/jogamp/opencl/CLContext.java b/src/com/jogamp/opencl/CLContext.java index 850f6c0e..147fc2ae 100644 --- a/src/com/jogamp/opencl/CLContext.java +++ b/src/com/jogamp/opencl/CLContext.java @@ -242,7 +242,7 @@ public class CLContext extends CLObject implements CLResource { } /** - * Creates a program from the given sources, the program is not build yet. + * Creates a program from the given sources, the returned program is not build yet. */ public CLProgram createProgram(String src) { CLProgram program = CLProgram.create(this, src); @@ -251,7 +251,8 @@ public class CLContext extends CLObject implements CLResource { } /** - * Creates a program and reads the source from stream, the program is not build yet. + * Creates a program and reads the source from stream, the returned program is not build yet. + * The InputStream is automatically closed after the sources have been read. * @throws IOException when a IOException occurred while reading or closing the stream. */ public CLProgram createProgram(InputStream source) throws IOException { @@ -260,14 +261,14 @@ public class CLContext extends CLObject implements CLResource { throw new IllegalArgumentException("input stream for program source must not be null"); BufferedReader reader = new BufferedReader(new InputStreamReader(source)); - StringBuilder sb = new StringBuilder(); + StringBuilder sb = new StringBuilder(2048); String line; try { while ((line = reader.readLine()) != null) sb.append(line).append("\n"); } finally { - source.close(); + reader.close(); } return createProgram(sb.toString()); -- cgit v1.2.3 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/com/jogamp') 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 From 08a479b22d4ba9da9ee79fc938ac4de7fb83dc5a Mon Sep 17 00:00:00 2001 From: Michael Bien Date: Thu, 26 May 2011 02:01:18 +0200 Subject: introduced CLAccessorFactory spi and threadlocal default impl for CLDevice and CLPlatform. --- src/com/jogamp/opencl/CLDevice.java | 33 +----- src/com/jogamp/opencl/CLEvent.java | 1 + src/com/jogamp/opencl/CLImage.java | 1 + src/com/jogamp/opencl/CLPlatform.java | 84 ++++++------- src/com/jogamp/opencl/CLSampler.java | 1 + src/com/jogamp/opencl/CLTLInfoAccessor.java | 132 --------------------- .../jogamp/opencl/impl/CLTLAccessorFactory.java | 92 ++++++++++++++ src/com/jogamp/opencl/impl/CLTLInfoAccessor.java | 132 +++++++++++++++++++++ src/com/jogamp/opencl/spi/CLAccessorFactory.java | 18 +++ 9 files changed, 287 insertions(+), 207 deletions(-) delete mode 100644 src/com/jogamp/opencl/CLTLInfoAccessor.java create mode 100644 src/com/jogamp/opencl/impl/CLTLAccessorFactory.java create mode 100644 src/com/jogamp/opencl/impl/CLTLInfoAccessor.java create mode 100644 src/com/jogamp/opencl/spi/CLAccessorFactory.java (limited to 'src/com/jogamp') diff --git a/src/com/jogamp/opencl/CLDevice.java b/src/com/jogamp/opencl/CLDevice.java index 84a27f33..6ca4cf38 100644 --- a/src/com/jogamp/opencl/CLDevice.java +++ b/src/com/jogamp/opencl/CLDevice.java @@ -29,9 +29,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; import java.util.Collections; @@ -59,22 +57,16 @@ public class CLDevice extends CLObject { private final CLInfoAccessor deviceInfo; private final CLPlatform platform; - CLDevice(CL cl, CLPlatform platform, long id) { + protected CLDevice(CL cl, CLPlatform platform, long id) { super(cl, id); this.platform = platform; - this.deviceInfo = new CLDeviceInfoAccessor(cl, id); + this.deviceInfo = platform.getAccessorFactory().createDeviceInfoAccessor(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) { + protected CLDevice(CLContext context, long id) { super(context, id); this.platform = context.getPlatform(); - this.deviceInfo = new CLDeviceInfoAccessor(context.getCL(), id); + this.deviceInfo = platform.getAccessorFactory().createDeviceInfoAccessor(cl, id); } public CLCommandQueue createCommandQueue() { @@ -698,21 +690,8 @@ public class CLDevice extends CLObject { return CLUtil.obtainDeviceProperties(this); } - private final static class CLDeviceInfoAccessor extends CLTLInfoAccessor { - - private final CL cl; - private final long ID; - - private CLDeviceInfoAccessor(CL cl, long id) { - this.cl = cl; - this.ID = id; - } - - @Override - protected int getInfo(int name, long valueSize, Buffer value, NativeSizeBuffer valueSizeRet) { - return cl.clGetDeviceInfo(ID, name, valueSize, value, valueSizeRet); - } - + public final CLInfoAccessor getCLAccessor() { + return deviceInfo; } @Override diff --git a/src/com/jogamp/opencl/CLEvent.java b/src/com/jogamp/opencl/CLEvent.java index a48e4fef..52b316da 100644 --- a/src/com/jogamp/opencl/CLEvent.java +++ b/src/com/jogamp/opencl/CLEvent.java @@ -28,6 +28,7 @@ package com.jogamp.opencl; +import com.jogamp.opencl.impl.CLTLInfoAccessor; import com.jogamp.opencl.impl.CLEventCallback; import com.jogamp.common.nio.NativeSizeBuffer; import java.nio.Buffer; diff --git a/src/com/jogamp/opencl/CLImage.java b/src/com/jogamp/opencl/CLImage.java index 48f3934f..76993d62 100644 --- a/src/com/jogamp/opencl/CLImage.java +++ b/src/com/jogamp/opencl/CLImage.java @@ -28,6 +28,7 @@ package com.jogamp.opencl; +import com.jogamp.opencl.impl.CLTLInfoAccessor; import com.jogamp.common.nio.NativeSizeBuffer; import java.nio.Buffer; diff --git a/src/com/jogamp/opencl/CLPlatform.java b/src/com/jogamp/opencl/CLPlatform.java index 684f006b..4a87b43d 100644 --- a/src/com/jogamp/opencl/CLPlatform.java +++ b/src/com/jogamp/opencl/CLPlatform.java @@ -28,6 +28,7 @@ package com.jogamp.opencl; +import com.jogamp.opencl.impl.CLTLAccessorFactory; import com.jogamp.common.nio.Buffers; import com.jogamp.common.os.DynamicLookupHelper; import com.jogamp.common.JogampRuntimeException; @@ -38,10 +39,10 @@ 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.spi.CLAccessorFactory; 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; @@ -104,22 +105,26 @@ public class CLPlatform { public final CLVersion version; protected static CL cl; + private static CLAccessorFactory defaultFactory; + private final CLAccessorFactory factory; private Set extensions; protected final CLPlatformInfoAccessor info; private CLPlatform(long id) { - initialize(); - this.ID = id; - this.info = new CLTLPlatformInfoAccessor(id, cl); - this.version = new CLVersion(getInfoString(CL_PLATFORM_VERSION)); + this(id, null); } - protected CLPlatform(long id, CLPlatformInfoAccessor accessor) { + protected CLPlatform(long id, CLAccessorFactory factory) { initialize(); this.ID = id; - this.info = accessor; + if(factory == null) { + this.factory = defaultFactory; + }else{ + this.factory = factory; + } + this.info = this.factory.createPlatformInfoAccessor(cl, id); this.version = new CLVersion(getInfoString(CL_PLATFORM_VERSION)); } @@ -127,11 +132,29 @@ public class CLPlatform { * Eagerly initializes JOCL. Subsequent calls do nothing. * @throws JogampRuntimeException if something went wrong in the initialization (e.g. OpenCL lib not found). */ - public synchronized static void initialize() throws JogampRuntimeException { + public static void initialize() throws JogampRuntimeException { + initialize(null); + } + + // keep package private until SPI is stablized + /** + * Eagerly initializes JOCL. Subsequent calls do nothing. + * @param factory CLAccessorFactory used for creating the bindings. + * @throws JogampRuntimeException if something went wrong in the initialization (e.g. OpenCL lib not found). + */ + synchronized static void initialize(CLAccessorFactory factory) throws JogampRuntimeException { if(cl != null) { return; } + + if(defaultFactory == null) { + if(factory == null) { + defaultFactory = new CLTLAccessorFactory(); + }else{ + defaultFactory = factory; + } + } try { @@ -507,47 +530,12 @@ public class CLPlatform { return info.getString(key); } - private final static class CLTLPlatformInfoAccessor extends CLTLInfoAccessor implements CLPlatformInfoAccessor { - - private final long ID; - private final CL cl; - - private CLTLPlatformInfoAccessor(long id, CL cl) { - this.ID = id; - this.cl = cl; - } - - @Override - protected int getInfo(int name, long valueSize, Buffer value, NativeSizeBuffer valueSizeRet) { - return cl.clGetPlatformInfo(ID, name, valueSize, value, valueSizeRet); - } - - @Override - public long[] getDeviceIDs(long type) { - - IntBuffer buffer = getBB(4).asIntBuffer(); - int ret = cl.clGetDeviceIDs(ID, type, 0, null, buffer); - int count = buffer.get(0); - - // return an empty buffer rather than throwing an exception - if(ret == CL.CL_DEVICE_NOT_FOUND || count == 0) { - return new long[0]; - }else{ - checkForError(ret, "error while enumerating devices"); - - NativeSizeBuffer deviceIDs = NativeSizeBuffer.wrap(getBB(count*NativeSizeBuffer.elementSize())); - ret = cl.clGetDeviceIDs(ID, type, count, deviceIDs, null); - checkForError(ret, "error while enumerating devices"); - - long[] ids = new long[count]; - for (int i = 0; i < ids.length; i++) { - ids[i] = deviceIDs.get(i); - } - return ids; - } - - } + final CLAccessorFactory getAccessorFactory(){ + return factory; + } + public final CLPlatformInfoAccessor getCLAccessor(){ + return info; } @Override diff --git a/src/com/jogamp/opencl/CLSampler.java b/src/com/jogamp/opencl/CLSampler.java index 63e25cb4..015445e3 100644 --- a/src/com/jogamp/opencl/CLSampler.java +++ b/src/com/jogamp/opencl/CLSampler.java @@ -28,6 +28,7 @@ package com.jogamp.opencl; +import com.jogamp.opencl.impl.CLTLInfoAccessor; import com.jogamp.common.nio.NativeSizeBuffer; import java.nio.Buffer; diff --git a/src/com/jogamp/opencl/CLTLInfoAccessor.java b/src/com/jogamp/opencl/CLTLInfoAccessor.java deleted file mode 100644 index 286dbe6b..00000000 --- a/src/com/jogamp/opencl/CLTLInfoAccessor.java +++ /dev/null @@ -1,132 +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.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/impl/CLTLAccessorFactory.java b/src/com/jogamp/opencl/impl/CLTLAccessorFactory.java new file mode 100644 index 00000000..1c7f9526 --- /dev/null +++ b/src/com/jogamp/opencl/impl/CLTLAccessorFactory.java @@ -0,0 +1,92 @@ +/* + * Created on Wednesday, May 25 2011 00:57 + */ +package com.jogamp.opencl.impl; + +import java.nio.IntBuffer; +import com.jogamp.common.nio.NativeSizeBuffer; +import com.jogamp.opencl.CL; +import com.jogamp.opencl.spi.CLAccessorFactory; +import com.jogamp.opencl.spi.CLInfoAccessor; +import com.jogamp.opencl.spi.CLPlatformInfoAccessor; +import java.nio.Buffer; + +import static com.jogamp.opencl.CLException.*; + +/** + * + * @author Michael Bien + */ +public class CLTLAccessorFactory implements CLAccessorFactory { + + @Override + public CLInfoAccessor createDeviceInfoAccessor(CL cl, long id) { + return new CLDeviceInfoAccessor(cl, id); + } + + @Override + public CLPlatformInfoAccessor createPlatformInfoAccessor(CL cl, long id) { + return new CLTLPlatformInfoAccessor(cl, id); + } + + private final static class CLDeviceInfoAccessor extends CLTLInfoAccessor { + + private final CL cl; + private final long ID; + + private CLDeviceInfoAccessor(CL cl, long id) { + this.cl = cl; + this.ID = id; + } + + @Override + public int getInfo(int name, long valueSize, Buffer value, NativeSizeBuffer valueSizeRet) { + return cl.clGetDeviceInfo(ID, name, valueSize, value, valueSizeRet); + } + + } + + private final static class CLTLPlatformInfoAccessor extends CLTLInfoAccessor implements CLPlatformInfoAccessor { + + private final long ID; + private final CL cl; + + private CLTLPlatformInfoAccessor(CL cl, long id) { + this.ID = id; + this.cl = cl; + } + + @Override + public int getInfo(int name, long valueSize, Buffer value, NativeSizeBuffer valueSizeRet) { + return cl.clGetPlatformInfo(ID, name, valueSize, value, valueSizeRet); + } + + @Override + public long[] getDeviceIDs(long type) { + + IntBuffer buffer = getBB(4).asIntBuffer(); + int ret = cl.clGetDeviceIDs(ID, type, 0, null, buffer); + int count = buffer.get(0); + + // return an empty buffer rather than throwing an exception + if(ret == CL.CL_DEVICE_NOT_FOUND || count == 0) { + return new long[0]; + }else{ + checkForError(ret, "error while enumerating devices"); + + NativeSizeBuffer deviceIDs = NativeSizeBuffer.wrap(getBB(count*NativeSizeBuffer.elementSize())); + ret = cl.clGetDeviceIDs(ID, type, count, deviceIDs, null); + checkForError(ret, "error while enumerating devices"); + + long[] ids = new long[count]; + for (int i = 0; i < ids.length; i++) { + ids[i] = deviceIDs.get(i); + } + return ids; + } + + } + + } + +} diff --git a/src/com/jogamp/opencl/impl/CLTLInfoAccessor.java b/src/com/jogamp/opencl/impl/CLTLInfoAccessor.java new file mode 100644 index 00000000..ee5d0e47 --- /dev/null +++ b/src/com/jogamp/opencl/impl/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.impl; + +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/CLAccessorFactory.java b/src/com/jogamp/opencl/spi/CLAccessorFactory.java new file mode 100644 index 00000000..6239bb37 --- /dev/null +++ b/src/com/jogamp/opencl/spi/CLAccessorFactory.java @@ -0,0 +1,18 @@ +/* + * Created on Wednesday, May 25 2011 00:53 + */ +package com.jogamp.opencl.spi; + +import com.jogamp.opencl.CL; + +/** + * Implementations of this interface are factories responsible for creating CLAccessors. + * @author Michael Bien + */ +public interface CLAccessorFactory { + + CLInfoAccessor createDeviceInfoAccessor(CL cl, long id); + + CLPlatformInfoAccessor createPlatformInfoAccessor(CL cl, long id); + +} -- cgit v1.2.3