diff options
Diffstat (limited to 'src/com')
-rw-r--r-- | src/com/jogamp/opencl/CLDevice.java | 40 | ||||
-rw-r--r-- | src/com/jogamp/opencl/CLInfoAccessor.java | 52 | ||||
-rw-r--r-- | src/com/jogamp/opencl/CLPlatform.java | 89 |
3 files changed, 109 insertions, 72 deletions
diff --git a/src/com/jogamp/opencl/CLDevice.java b/src/com/jogamp/opencl/CLDevice.java index dc2b8247..0381038e 100644 --- a/src/com/jogamp/opencl/CLDevice.java +++ b/src/com/jogamp/opencl/CLDevice.java @@ -30,9 +30,7 @@ package com.jogamp.opencl; import com.jogamp.opencl.util.CLUtil; import com.jogamp.common.nio.NativeSizeBuffer; -import com.jogamp.common.os.Platform; import java.nio.Buffer; -import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.ArrayList; import java.util.Collections; @@ -63,13 +61,13 @@ public final class CLDevice extends CLObject { CLDevice(CL cl, CLPlatform platform, long id) { super(cl, id); this.platform = platform; - this.deviceInfo = new CLDeviceInfoAccessor(); + this.deviceInfo = new CLDeviceInfoAccessor(cl, id); } CLDevice(CLContext context, long id) { super(context, id); this.platform = context.getPlatform(); - this.deviceInfo = new CLDeviceInfoAccessor(); + this.deviceInfo = new CLDeviceInfoAccessor(context.getCL(), id); } public CLCommandQueue createCommandQueue() { @@ -340,8 +338,8 @@ public final class CLDevice extends CLObject { */ @CLProperty("CL_DEVICE_MAX_WORK_ITEM_SIZES") public int[] getMaxWorkItemSizes() { - int n = (int) deviceInfo.getLong(CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS); - return deviceInfo.getInts(n, CL_DEVICE_MAX_WORK_ITEM_SIZES); + int n = getMaxWorkItemDimensions(); + return deviceInfo.getInts(CL_DEVICE_MAX_WORK_ITEM_SIZES, n); } /** @@ -693,35 +691,23 @@ public final class CLDevice extends CLObject { return CLUtil.obtainDeviceProperties(this); } - private final class CLDeviceInfoAccessor extends CLInfoAccessor { + private final static class CLDeviceInfoAccessor extends CLInfoAccessor { + + 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); } - private int[] getInts(int n, int key) { - - ByteBuffer buffer = localBB.get(); - int ret = getInfo(key, buffer.capacity(), buffer, null); - CLException.checkForError(ret, "error while asking device for infos"); - - 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; - } - } - @Override public String toString() { return "CLDevice [id: " + ID diff --git a/src/com/jogamp/opencl/CLInfoAccessor.java b/src/com/jogamp/opencl/CLInfoAccessor.java index 5f6694b2..08d7305e 100644 --- a/src/com/jogamp/opencl/CLInfoAccessor.java +++ b/src/com/jogamp/opencl/CLInfoAccessor.java @@ -28,12 +28,13 @@ package com.jogamp.opencl; -import com.jogamp.common.nio.Buffers; 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.*; /** @@ -43,15 +44,17 @@ import static com.jogamp.opencl.CLException.*; */ abstract class CLInfoAccessor { + private static final int BB_SIZE = 512; + protected final static ThreadLocal<ByteBuffer> localBB = new ThreadLocal<ByteBuffer>() { @Override protected ByteBuffer initialValue() { - return Buffers.newDirectByteBuffer(512); + return newDirectByteBuffer(BB_SIZE); } }; - protected final static ThreadLocal<NativeSizeBuffer> localPB = new ThreadLocal<NativeSizeBuffer>() { + protected final static ThreadLocal<NativeSizeBuffer> localNSB = new ThreadLocal<NativeSizeBuffer>() { @Override protected NativeSizeBuffer initialValue() { @@ -62,7 +65,7 @@ abstract class CLInfoAccessor { public final long getLong(int key) { - ByteBuffer buffer = localBB.get(); + ByteBuffer buffer = getBB(8).putLong(0, 0); int ret = getInfo(key, 8, buffer, null); checkForError(ret, "error while asking for info value"); @@ -71,12 +74,16 @@ abstract class CLInfoAccessor { public final String getString(int key) { - ByteBuffer buffer = localBB.get(); - NativeSizeBuffer sizeBuffer = localPB.get(); - int ret = getInfo(key, buffer.capacity(), buffer, sizeBuffer); + 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(); @@ -84,6 +91,37 @@ abstract class CLInfoAccessor { } + 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 4637a114..ee6a6bdf 100644 --- a/src/com/jogamp/opencl/CLPlatform.java +++ b/src/com/jogamp/opencl/CLPlatform.java @@ -30,6 +30,7 @@ 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; @@ -41,7 +42,6 @@ import com.jogamp.opencl.impl.CLProcAddressTable; import com.jogamp.opencl.util.Filter; import com.jogamp.opencl.util.JOCLVersion; -import java.nio.ByteBuffer; import java.nio.IntBuffer; import java.util.ArrayList; import java.util.Collections; @@ -106,9 +106,12 @@ public final class CLPlatform { private Set<String> extensions; + private final CLPlatformInfoAccessor info; private CLPlatform(long id) { + initialize(); this.ID = id; + this.info = new CLPlatformInfoAccessor(id, cl); this.version = new CLVersion(getInfoString(CL_PLATFORM_VERSION)); } @@ -273,14 +276,15 @@ public final class CLPlatform { initialize(); List<CLDevice> list = new ArrayList<CLDevice>(); + for(int t = 0; t < types.length; t++) { CLDevice.Type type = types[t]; - NativeSizeBuffer deviceIDs = getDeviceIDs(type.TYPE); + long[] deviceIDs = info.getDeviceIDs(type.TYPE); //add device to list - for (int n = 0; n < deviceIDs.capacity(); n++) { - list.add(new CLDevice(cl, this, deviceIDs.get(n))); + for (int n = 0; n < deviceIDs.length; n++) { + list.add(new CLDevice(cl, this, deviceIDs[n])); } } @@ -296,11 +300,11 @@ public final class CLPlatform { List<CLDevice> list = new ArrayList<CLDevice>(); - NativeSizeBuffer deviceIDs = getDeviceIDs(CL_DEVICE_TYPE_ALL); + long[] deviceIDs = info.getDeviceIDs(CL_DEVICE_TYPE_ALL); //add device to list - for (int n = 0; n < deviceIDs.capacity(); n++) { - CLDevice device = new CLDevice(cl, this, deviceIDs.get(n)); + for (int n = 0; n < deviceIDs.length; n++) { + CLDevice device = new CLDevice(cl, this, deviceIDs[n]); addIfAccepted(device, list, filters); } @@ -308,30 +312,6 @@ public final class CLPlatform { } - private NativeSizeBuffer getDeviceIDs(long type) { - - IntBuffer ib = Buffers.newDirectIntBuffer(1); - - //find all devices - int ret = cl.clGetDeviceIDs(ID, type, 0, null, ib); - - NativeSizeBuffer deviceIDs = null; - - // return null rather than throwing an exception - if(ret == CL.CL_DEVICE_NOT_FOUND || ib.get(0) == 0) { - deviceIDs = NativeSizeBuffer.allocate(0); - }else{ - deviceIDs = NativeSizeBuffer.allocateDirect(ib.get(0)); - - checkForError(ret, "error while enumerating devices"); - - ret = cl.clGetDeviceIDs(ID, type, deviceIDs.capacity(), deviceIDs, null); - checkForError(ret, "error while enumerating devices"); - } - - return deviceIDs; - } - private static <I> void addIfAccepted(I item, List<I> list, Filter<I>[] filters) { if(filters == null) { list.add(item); @@ -510,16 +490,49 @@ public final class CLPlatform { * Returns a info string in exchange for a key (CL_PLATFORM_*). */ public String getInfoString(int key) { + return info.getString(key); + } - NativeSizeBuffer size = NativeSizeBuffer.allocateDirect(1); - int ret = cl.clGetPlatformInfo(ID, key, 0, null, size); - checkForError(ret, "can not receive info string"); + private final static class CLPlatformInfoAccessor extends CLInfoAccessor { - ByteBuffer bb = ByteBuffer.allocateDirect((int)size.get(0)); - ret = cl.clGetPlatformInfo(ID, key, bb.capacity(), bb, null); - checkForError(ret, "can not receive info string"); + private final long ID; + private final CL cl; + + private CLPlatformInfoAccessor(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); + } + + 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; + } + + } - return CLUtil.clString2JavaString(bb, (int)size.get(0)); } @Override |