diff options
author | Sven Gothel <[email protected]> | 2011-06-28 14:13:22 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-06-28 14:13:22 +0200 |
commit | da0378be055d5c304b0a0b5e1b5f3ca66122ebf8 (patch) | |
tree | 26ca2e70da90e0c43c30a51d8d0cbe6c9ca2809f /src | |
parent | 368f062ec9d6bbb89129739adb59cf042dd0bf78 (diff) | |
parent | 08a479b22d4ba9da9ee79fc938ac4de7fb83dc5a (diff) |
Fix last merge of mbien/master
Diffstat (limited to 'src')
-rw-r--r-- | src/com/jogamp/opencl/CLContext.java | 9 | ||||
-rw-r--r-- | src/com/jogamp/opencl/CLDevice.java | 34 | ||||
-rw-r--r-- | src/com/jogamp/opencl/CLEvent.java | 8 | ||||
-rw-r--r-- | src/com/jogamp/opencl/CLImage.java | 7 | ||||
-rw-r--r-- | src/com/jogamp/opencl/CLPlatform.java | 103 | ||||
-rw-r--r-- | src/com/jogamp/opencl/CLSampler.java | 6 | ||||
-rw-r--r-- | src/com/jogamp/opencl/impl/CLTLAccessorFactory.java | 92 | ||||
-rw-r--r-- | src/com/jogamp/opencl/impl/CLTLInfoAccessor.java (renamed from src/com/jogamp/opencl/CLInfoAccessor.java) | 12 | ||||
-rw-r--r-- | src/com/jogamp/opencl/spi/CLAccessorFactory.java | 18 | ||||
-rw-r--r-- | src/com/jogamp/opencl/spi/CLInfoAccessor.java | 25 | ||||
-rw-r--r-- | src/com/jogamp/opencl/spi/CLPlatformInfoAccessor.java | 14 |
11 files changed, 238 insertions, 90 deletions
diff --git a/src/com/jogamp/opencl/CLContext.java b/src/com/jogamp/opencl/CLContext.java index c8a847a2..43755c37 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()); diff --git a/src/com/jogamp/opencl/CLDevice.java b/src/com/jogamp/opencl/CLDevice.java index 95f9d1a6..07f7c5f2 100644 --- a/src/com/jogamp/opencl/CLDevice.java +++ b/src/com/jogamp/opencl/CLDevice.java @@ -1,5 +1,5 @@ /* - * Copyright 2009 - 2010 JogAmp Community. All rights reserved. + * Copyright (c) 2009 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: @@ -29,8 +29,7 @@ package com.jogamp.opencl; import com.jogamp.opencl.util.CLUtil; -import com.jogamp.common.nio.PointerBuffer; -import java.nio.Buffer; +import com.jogamp.opencl.spi.CLInfoAccessor; import java.nio.ByteOrder; import java.util.ArrayList; import java.util.Collections; @@ -51,23 +50,23 @@ 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<String> extensions; - private final CLDeviceInfoAccessor deviceInfo; + 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); } - 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() { @@ -691,21 +690,8 @@ public final class CLDevice extends CLObject { return CLUtil.obtainDeviceProperties(this); } - 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, PointerBuffer 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 4365fd6a..928d0d92 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.PointerBuffer; import java.nio.Buffer; @@ -64,12 +65,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 +140,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, PointerBuffer valueSizeRet) { @@ -147,7 +149,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, PointerBuffer valueSizeRet) { diff --git a/src/com/jogamp/opencl/CLImage.java b/src/com/jogamp/opencl/CLImage.java index acb36aed..fa3ab800 100644 --- a/src/com/jogamp/opencl/CLImage.java +++ b/src/com/jogamp/opencl/CLImage.java @@ -1,5 +1,5 @@ /* - * Copyright 2009 - 2010 JogAmp Community. All rights reserved. + * Copyright (c) 2009 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: @@ -29,6 +29,7 @@ package com.jogamp.opencl; import com.jogamp.common.nio.PointerBuffer; +import com.jogamp.opencl.impl.CLTLInfoAccessor; import java.nio.Buffer; import static com.jogamp.opencl.CL.*; @@ -41,7 +42,7 @@ public abstract class CLImage<B extends Buffer> extends CLMemory<B> { protected CLImageFormat format; - final CLInfoAccessor imageInfo; + final CLTLInfoAccessor imageInfo; public final int width; public final int height; @@ -100,7 +101,7 @@ public abstract class CLImage<B extends Buffer> extends CLMemory<B> { } - 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/CLPlatform.java b/src/com/jogamp/opencl/CLPlatform.java index d9a258c0..ab2e32b0 100644 --- a/src/com/jogamp/opencl/CLPlatform.java +++ b/src/com/jogamp/opencl/CLPlatform.java @@ -1,5 +1,5 @@ /* - * Copyright 2009 - 2010 JogAmp Community. All rights reserved. + * Copyright (c) 2009 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: @@ -28,17 +28,18 @@ package com.jogamp.opencl; +import com.jogamp.opencl.impl.CLTLAccessorFactory; 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.PointerBuffer; 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.spi.CLAccessorFactory; import com.jogamp.opencl.util.Filter; import com.jogamp.opencl.util.JOCLVersion; @@ -50,6 +51,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 +92,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 +104,27 @@ public final class CLPlatform { */ public final CLVersion version; - private static CL cl; + protected static CL cl; + private static CLAccessorFactory defaultFactory; + private final CLAccessorFactory factory; private Set<String> extensions; - private final CLPlatformInfoAccessor info; + protected final CLPlatformInfoAccessor info; private CLPlatform(long id) { + this(id, null); + } + + protected CLPlatform(long id, CLAccessorFactory factory) { initialize(); this.ID = id; - this.info = new CLPlatformInfoAccessor(id, cl); + 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)); } @@ -119,15 +132,34 @@ public final 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 { final CLProcAddressTable table = new CLProcAddressTable(new FunctionAddressResolver() { + @Override public long resolve(String name, DynamicLookupHelper lookup) { //FIXME workaround to fix a gluegen issue @@ -150,6 +182,7 @@ public final class CLPlatform { //load JOCL and init table doPrivileged(new PrivilegedAction<Object>() { + @Override public Object run() { NativeLibrary libOpenCL = JOCLJNILibLoader.loadOpenCL(); @@ -284,7 +317,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 +337,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 +345,10 @@ public final class CLPlatform { } + protected CLDevice createDevice(long id) { + return new CLDevice(cl, this, id); + } + private static <I> void addIfAccepted(I item, List<I> list, Filter<I>[] filters) { if(filters == null) { list.add(item); @@ -489,50 +526,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 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, PointerBuffer 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"); - - PointerBuffer deviceIDs = PointerBuffer.wrap(getBB(count*PointerBuffer.ELEMENT_SIZE)); - 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 73e153d9..8632143b 100644 --- a/src/com/jogamp/opencl/CLSampler.java +++ b/src/com/jogamp/opencl/CLSampler.java @@ -1,5 +1,5 @@ /* - * Copyright 2009 - 2010 JogAmp Community. All rights reserved. + * Copyright (c) 2009 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: @@ -29,6 +29,7 @@ package com.jogamp.opencl; import com.jogamp.common.nio.PointerBuffer; +import com.jogamp.opencl.impl.CLTLInfoAccessor; import java.nio.Buffer; @@ -73,6 +74,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 +83,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, PointerBuffer 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..2f34fec5 --- /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.PointerBuffer; +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, PointerBuffer 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, PointerBuffer 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"); + + PointerBuffer deviceIDs = PointerBuffer.wrap(getBB(count*PointerBuffer.ELEMENT_SIZE)); + 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/CLInfoAccessor.java b/src/com/jogamp/opencl/impl/CLTLInfoAccessor.java index b005ec04..c31b22a6 100644 --- a/src/com/jogamp/opencl/CLInfoAccessor.java +++ b/src/com/jogamp/opencl/impl/CLTLInfoAccessor.java @@ -1,5 +1,5 @@ /* - * Copyright 2009 - 2010 JogAmp Community. All rights reserved. + * Copyright (c) 2009 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: @@ -26,8 +26,9 @@ * or implied, of JogAmp Community. */ -package com.jogamp.opencl; +package com.jogamp.opencl.impl; +import com.jogamp.opencl.spi.CLInfoAccessor; import com.jogamp.common.nio.PointerBuffer; import com.jogamp.common.os.Platform; import com.jogamp.opencl.util.CLUtil; @@ -39,10 +40,10 @@ import static com.jogamp.opencl.CLException.*; /** * Internal utility for common OpenCL clGetFooInfo calls. - * Threadsafe. + * Threadsafe, threadlocal implementation. * @author Michael Bien */ -abstract class CLInfoAccessor { +public abstract class CLTLInfoAccessor implements CLInfoAccessor { private static final int BB_SIZE = 512; @@ -63,6 +64,7 @@ abstract class CLInfoAccessor { }; + @Override public final long getLong(int key) { ByteBuffer buffer = getBB(8).putLong(0, 0); @@ -72,6 +74,7 @@ abstract class CLInfoAccessor { return buffer.getLong(0); } + @Override public final String getString(int key) { PointerBuffer sizeBuffer = getNSB(); @@ -91,6 +94,7 @@ abstract class CLInfoAccessor { } + @Override public final int[] getInts(int key, int n) { ByteBuffer buffer = getBB(n * (Platform.is32Bit()?4:8)); 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); + +} 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); + +} |