29package com.jogamp.opencl;
31import java.io.BufferedReader;
32import java.io.IOException;
33import java.io.InputStream;
34import java.io.InputStreamReader;
35import java.nio.Buffer;
36import java.nio.ByteBuffer;
37import java.nio.DoubleBuffer;
38import java.nio.FloatBuffer;
39import java.nio.IntBuffer;
40import java.nio.LongBuffer;
41import java.nio.ShortBuffer;
42import java.util.ArrayList;
43import java.util.Collection;
44import java.util.Collections;
45import java.util.HashMap;
46import java.util.HashSet;
51import com.jogamp.common.nio.Buffers;
52import com.jogamp.common.nio.PointerBuffer;
53import com.jogamp.common.os.Platform;
54import com.jogamp.opencl.CLDevice.Type;
55import com.jogamp.opencl.CLMemory.Mem;
56import com.jogamp.opencl.CLSampler.AddressingMode;
57import com.jogamp.opencl.CLSampler.FilteringMode;
58import com.jogamp.opencl.llb.CL;
59import com.jogamp.opencl.llb.impl.CLImageFormatImpl;
97 this.programs = Collections.synchronizedSet(
new HashSet<CLProgram>());
98 this.samplers = Collections.synchronizedSet(
new HashSet<CLSampler>());
101 this.queuesMap =
new HashMap<CLDevice, List<CLCommandQueue>>();
103 this.errorHandler = dispatcher;
115 private synchronized void initDevices(
final CL cl) {
119 final PointerBuffer deviceCount = PointerBuffer.allocateDirect(1);
124 final ByteBuffer deviceIDs = Buffers.newDirectByteBuffer((
int)deviceCount.get());
128 devices =
new CLDevice[deviceIDs.capacity() / (Platform.is32Bit() ? 4 : 8)];
129 for (
int i = 0; i <
devices.length; i++) {
130 devices[i] =
new CLDevice(
this, Platform.is32Bit() ? deviceIDs.getInt() : deviceIDs.getLong());
148 return create(
null, deviceTypes);
170 final PointerBuffer properties = setupContextProperties(
platform);
181 throw new IllegalArgumentException(
"no devices specified");
183 throw new IllegalArgumentException(
"first device was null");
188 final PointerBuffer properties = setupContextProperties(
platform);
192 for (
int i = 0; i <
devices.length; i++) {
200 final IntBuffer status = Buffers.newDirectIntBuffer(1);
210 final IntBuffer status = Buffers.newDirectIntBuffer(1);
211 PointerBuffer pb =
null;
213 pb = PointerBuffer.allocateDirect(
devices.length);
214 for (
int i = 0; i <
devices.length; i++) {
217 throw new IllegalArgumentException(
"device at index "+i+
" was null.");
219 pb.put(i, device.
ID);
232 throw new RuntimeException(
"no OpenCL installation found");
235 return PointerBuffer.allocateDirect(3).put(CL.CL_CONTEXT_PLATFORM)
257 throw new IllegalArgumentException(
"input stream for program source must not be null");
259 final BufferedReader reader =
new BufferedReader(
new InputStreamReader(source));
260 final StringBuilder sb =
new StringBuilder(2048);
264 while ((line = reader.readLine()) !=
null)
265 sb.append(line).append(
"\n");
293 return createBuffer(Buffers.newDirectShortBuffer(size), flags);
300 return createBuffer(Buffers.newDirectIntBuffer(size), flags);
307 return createBuffer(Buffers.newDirectLongBuffer(size), flags);
314 return createBuffer(Buffers.newDirectFloatBuffer(size), flags);
321 return createBuffer(Buffers.newDirectDoubleBuffer(size), flags);
335 return createBuffer(Buffers.newDirectByteBuffer(size), flags);
381 return createImage2d(
null, width, height, rowPitch, format, flags);
388 return createImage2d(directBuffer, width, height, 0, format, flags);
404 return createImage3d(
null, width, height, depth, format, flags);
411 return createImage3d(
null, width, height, depth, rowPitch, slicePitch, format, flags);
418 return createImage3d(directBuffer, width, height, depth, 0, 0, format, flags);
424 public final <B extends Buffer>
CLImage3d<B> createImage3d(
final B directBuffer,
final int width,
final int height,
final int depth,
final int rowPitch,
final int slicePitch,
final CLImageFormat format,
final Mem... flags) {
435 List<CLCommandQueue> list =
queuesMap.get(device);
437 list =
new ArrayList<CLCommandQueue>();
447 final CLSampler sampler =
CLSampler.create(
this, addrMode, filtMode, normalizedCoords);
452 void onProgramReleased(
final CLProgram program) {
456 void onMemoryReleased(
final CLMemory<?> buffer) {
460 void onCommandQueueReleased(
final CLDevice device,
final CLCommandQueue queue) {
462 final List<CLCommandQueue> list =
queuesMap.get(device);
470 void onSamplerReleased(
final CLSampler sampler) {
475 errorHandler.addHandler(handler);
479 errorHandler.removeHandler(handler);
482 private void release(
final Collection<? extends CLResource> resources) {
484 while(!resources.isEmpty()) {
485 resources.iterator().next().release();
503 final Collection<List<CLCommandQueue>> queuesList =
queuesMap.values();
504 while(!queuesList.isEmpty())
505 release(queuesList.iterator().next());
516 device.setContext(
this);
519 private CLImageFormat[] getSupportedImageFormats(
final int flags,
final int type) {
523 final int[] entries =
new int[1];
529 final int count = entries[0];
531 return new CLImageFormat[0];
534 final CLImageFormat[] formats =
new CLImageFormat[count];
535 final CLImageFormatImpl impl = CLImageFormatImpl.create(Buffers.newDirectByteBuffer(count * CLImageFormatImpl.size()));
537 if(ret != CL.CL_SUCCESS) {
538 throw CLException.newException(ret,
"error calling clGetSupportedImageFormats");
541 final ByteBuffer buffer = impl.getBuffer();
542 for (
int i = 0; i < formats.length; i++) {
543 formats[i] =
new CLImageFormat(CLImageFormatImpl.create(buffer.slice()));
544 buffer.position(i*CLImageFormatImpl.size());
583 return Collections.unmodifiableList(
new ArrayList<CLProgram>(
programs));
601 return Collections.unmodifiableList(
new ArrayList<CLSampler>(
samplers));
628 long maxAlignment = 0;
630 maxAlignment = Math.max(maxAlignment, device.getMemBaseAddrAlign());
650 CLDevice getDevice(
final long dID) {
652 for (
int i = 0; i < deviceArray.length; i++) {
653 if(dID == deviceArray[i].
ID)
654 return deviceArray[i];
661 if (deviceTypes !=
null) {
662 for (
int i = 0; i < deviceTypes.length; i++) {
663 final Type type = deviceTypes[i];
665 throw new IllegalArgumentException(
"Device type at index "+i+
" was null.");
675 return getClass().getSimpleName()+
" [id: " +
ID
683 public boolean equals(
final Object obj) {
687 if (getClass() != obj.getClass()) {
691 if (this.
ID != other.
ID) {
700 hash = 23 * hash + (int) (this.
ID ^ (this.
ID >>> 32));
713 public synchronized void onError(
final String errinfo,
final ByteBuffer private_info,
final long cb) {
715 for (
int i = 0; i < handlers.length; i++) {
716 handlers[i].
onError(errinfo, private_info, cb);
720 private synchronized void addHandler(
final CLErrorHandler handler) {
722 if(handler ==
null) {
723 throw new IllegalArgumentException(
"handler was null.");
727 System.arraycopy(clientHandlers, 0, handlers, 0, clientHandlers.length);
728 handlers[handlers.length-1] = handler;
729 clientHandlers = handlers;
732 private synchronized void removeHandler(
final CLErrorHandler handler) {
734 if(handler ==
null) {
735 throw new IllegalArgumentException(
"handler was null.");
738 for (
int i = 0; i < clientHandlers.length; i++) {
739 if(handler.equals(clientHandlers[i])) {
740 final CLErrorHandler[] handlers =
new CLErrorHandler[clientHandlers.length-1];
741 System.arraycopy(clientHandlers, 0, handlers, 0, i);
742 System.arraycopy(clientHandlers, i, handlers, 0, handlers.length-i);
743 clientHandlers = handlers;
OpenCL buffer object wrapping an optional NIO buffer.
The command queue is used to queue a set of operations for a specific CLDevice.
synchronized void onError(final String errinfo, final ByteBuffer private_info, final long cb)
CLContext is responsible for managing objects such as command-queues, memory, program and kernel obje...
void overrideContext(final CLDevice device)
List< CLSampler > getSamplers()
Returns a read only shapshot of all samplers associated with this context.
static long createContextFromType(final CLPlatform platform, final CLErrorHandler handler, final PointerBuffer properties, final long deviceType)
CLContext(final CLPlatform platform, final long contextID, final ErrorDispatcher dispatcher)
CLImageFormat[] getSupportedImage2dFormats(final Mem... flags)
Returns all supported 2d image formats with the (optional) memory allocation flags.
final< B extends Buffer > CLImage2d< B > createImage2d(final B directBuffer, final int width, final int height, final int rowPitch, final CLImageFormat format, final Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.
final Set< CLProgram > programs
List< CLMemory<? extends Buffer > > getMemoryObjects()
Returns a read only shapshot of all allocated memory objects associated with this context.
CLDevice[] getDevices()
Returns all devices associated with this CLContext.
void addCLErrorHandler(final CLErrorHandler handler)
final CLBuffer< ShortBuffer > createShortBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
CLProgram createProgram(final String src)
Creates a program from the given sources, the returned program is not build yet.
static CLContext create()
Creates a context on all available devices (CL_DEVICE_TYPE_ALL).
final< B extends Buffer > CLImage3d< B > createImage3d(final B directBuffer, final int width, final int height, final int depth, final int rowPitch, final int slicePitch, final CLImageFormat format, final Mem... flags)
Creates a CLImage3d with the specified format, dimension and flags.
final CLBuffer< ByteBuffer > createByteBuffer(final int size, final int flags)
Creates a CLBuffer with the specified flags and buffer size in bytes.
final CLImage3d<?> createImage3d(final int width, final int height, final int depth, final int rowPitch, final int slicePitch, final CLImageFormat format, final Mem... flags)
Creates a CLImage3d with the specified format, dimension and flags.
void removeCLErrorHandler(final CLErrorHandler handler)
static CLContext create(CLPlatform platform, final Type... deviceTypes)
Creates a context on the specified platform and with the specified device types.
final CLBuffer< DoubleBuffer > createDoubleBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
final CLBuffer< IntBuffer > createIntBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
final CLBuffer<?> createBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags.
CLProgram createProgram(final InputStream source)
Creates a program and reads the source from stream, the returned program is not build yet.
CLContext getContext()
Returns the context for this OpenCL object.
final< B extends Buffer > CLImage2d< B > createImage2d(final B directBuffer, final int width, final int height, final CLImageFormat format, final Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.
final< B extends Buffer > CLImage3d< B > createImage3d(final B directBuffer, final int width, final int height, final int depth, final CLImageFormat format, final Mem... flags)
Creates a CLImage3d with the specified format, dimension and flags.
final Set< CLMemory<? extends Buffer > > memoryObjects
CLProgram createProgram(final Map< CLDevice, byte[]> binaries)
Creates a program from the given binaries, the program is not build yet.
final< B extends Buffer > CLBuffer< B > createBuffer(final B directBuffer, final int flags)
Creates a CLBuffer with the specified flags.
static long createContext(final CLPlatform platform, final CLErrorHandler handler, final PointerBuffer properties, final CLDevice... devices)
CLDevice getMaxFlopsDevice()
Returns the device with maximal FLOPS from this context.
final CLBuffer< ByteBuffer > createByteBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and buffer size in bytes.
static CLContext create(final CLDevice... devices)
Creates a context on the specified devices.
final CLBuffer< LongBuffer > createLongBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
static long toDeviceBitmap(final Type[] deviceTypes)
CLImageFormat[] getSupportedImage3dFormats(final Mem... flags)
Returns all supported 3d image formats with the (optional) memory allocation flags.
static ErrorDispatcher createErrorHandler()
final Map< CLDevice, List< CLCommandQueue > > queuesMap
boolean equals(final Object obj)
List< CLProgram > getPrograms()
Returns a read only shapshot of all programs associated with this context.
CLSampler createSampler(final AddressingMode addrMode, final FilteringMode filtMode, final boolean normalizedCoords)
static CLContext create(final CLPlatform platform)
Creates a context on the specified platform on all available devices (CL_DEVICE_TYPE_ALL).
CL getCL()
Return the low level OpenCL interface.
final CLBuffer< FloatBuffer > createFloatBuffer(final int size, final Mem... flags)
Creates a CLBuffer with the specified flags and element count.
final< B extends Buffer > CLBuffer< B > createBuffer(final B directBuffer, final Mem... flags)
Creates a CLBuffer with the specified flags.
final CLImage2d<?> createImage2d(final int width, final int height, final int rowPitch, final CLImageFormat format, final Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.
long getMaxMemBaseAddrAlign()
Returns the maximum CLDevice#getMemBaseAddrAlign() of all devices.
final CLImage2d<?> createImage2d(final int width, final int height, final CLImageFormat format, final Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.
synchronized void release()
Releases this context and all resources.
final Set< CLSampler > samplers
final CLImage3d<?> createImage3d(final int width, final int height, final int depth, final CLImageFormat format, final Mem... flags)
Creates a CLImage3d with the specified format, dimension and flags.
static CLContext create(final Type... deviceTypes)
Creates a context on the specified device types.
final CLPlatform platform
final CLBuffer<?> createBuffer(final int size, final int flags)
Creates a CLBuffer with the specified flags.
CLDevice getMaxFlopsDevice(final CLDevice.Type type)
Returns the device with maximal FLOPS of the specified device type from this context.
CLPlatform getPlatform()
Returns the CLPlatform this context is running on.
This object represents an OpenCL device.
CLPlatform getPlatform()
Returns the platform for this OpenCL object.
Main Exception type for runtime OpenCL errors and failed function calls (e.g.
static CLException newException(final int status, final String message)
Returns a CLException specific to the error code.
static void checkForError(final int status, final String message)
Throws a CLException when status != CL_SUCCESS.
Common superclass for all OpenCL memory types.
final long ID
The OpenCL object handle.
Represents a OpenCL program executed on one or more CLDevices.
void setNoSource()
Must set this if the program is created from binary so we know not to call getSource(),...
Object representing an OpenCL sampler.
Enumeration for the type of a device.
final long TYPE
Value of wrapped OpenCL device type.
Configures the mapping process.
Memory settings for configuring CLMemory.
static int flagsToInt(final Mem[] flags)
Experimental: the api may change in future, feedback appreciated.
void onError(String errinfo, ByteBuffer private_info, long cb)
Java bindings to OpenCL, the Open Computing Language.
int clReleaseContext(long context)
Interface to C language function: cl_int {@native clReleaseContext}(cl_context context)
int clGetContextInfo(long context, int param_name, long param_value_size, Buffer param_value, PointerBuffer param_value_size_ret)
Interface to C language function: cl_int {@native clGetContextInfo}(cl_context context,...
static final int CL_CONTEXT_DEVICES
Define "CL_CONTEXT_DEVICES" with expression '0x1081', CType: int.
int clGetSupportedImageFormats(long context, long flags, int image_type, int num_entries, CLImageFormatImpl image_formats, IntBuffer num_image_formats)
Interface to C language function: cl_int {@native clGetSupportedImageFormats}(cl_context context,...
static final int CL_MEM_OBJECT_IMAGE3D
Define "CL_MEM_OBJECT_IMAGE3D" with expression '0x10F2', CType: int.
static final int CL_SUCCESS
Define "CL_SUCCESS" with expression '0', CType: int.
static final int CL_MEM_OBJECT_IMAGE2D
Define "CL_MEM_OBJECT_IMAGE2D" with expression '0x10F1', CType: int.
long clCreateContextFromType(PointerBuffer properties, long device_type, CLErrorHandler pfn_notify, IntBuffer errcode_ret)
Interface to C language function: cl_context {@native clCreateContextFromType}(cl_context_properti...
long clCreateContext(PointerBuffer properties, PointerBuffer devices, CLErrorHandler pfn_notify, IntBuffer errcode_ret)
Interface to C language function: cl_context {@native clCreateContext}(intptr_t * ,...