com.jogamp.opencl
Class CLContext

java.lang.Object
  extended by com.jogamp.opencl.CLObject
      extended by com.jogamp.opencl.CLContext
All Implemented Interfaces:
AutoCloseable, CLResource
Direct Known Subclasses:
CLGLContext

public class CLContext
extends CLObject

CLContext is responsible for managing objects such as command-queues, memory, program and kernel objects and for executing kernels on one or more devices specified in the context.

Must be released if no longer used to free native resources. release() will also free all associated CLResource like programs, samplers, command queues and memory objects.

For a code example see CLPlatform.

concurrency:
CLContext is threadsafe.

Author:
Michael Bien, et al.

Nested Class Summary
protected static class CLContext.ErrorDispatcher
           
 
Field Summary
protected  CLDevice[] devices
           
protected  Set<CLMemory<? extends Buffer>> memoryObjects
           
protected  CLPlatform platform
           
protected  Set<CLProgram> programs
           
protected  Map<CLDevice,List<CLCommandQueue>> queuesMap
           
protected  Set<CLSampler> samplers
           
 
Fields inherited from class com.jogamp.opencl.CLObject
context, ID
 
Constructor Summary
protected CLContext(CLPlatform platform, long contextID, CLContext.ErrorDispatcher dispatcher)
           
 
Method Summary
 void addCLErrorHandler(CLErrorHandler handler)
           
 void close()
          Deprecated. This method is not intended to be called from client code.
static CLContext create()
          Creates a context on all available devices (CL_DEVICE_TYPE_ALL).
static CLContext create(CLDevice... devices)
          Creates a context on the specified devices.
static CLContext create(CLDevice.Type... deviceTypes)
          Creates a context on the specified device types.
static CLContext create(CLPlatform platform)
          Creates a context on the specified platform on all available devices (CL_DEVICE_TYPE_ALL).
static CLContext create(CLPlatform platform, CLDevice.Type... deviceTypes)
          Creates a context on the specified platform and with the specified device types.
<B extends Buffer>
CLBuffer<B>
createBuffer(B directBuffer, CLMemory.Mem... flags)
          Creates a CLBuffer with the specified flags.
<B extends Buffer>
CLBuffer<B>
createBuffer(B directBuffer, int flags)
          Creates a CLBuffer with the specified flags.
 CLBuffer<?> createBuffer(int size, CLMemory.Mem... flags)
          Creates a CLBuffer with the specified flags.
 CLBuffer<?> createBuffer(int size, int flags)
          Creates a CLBuffer with the specified flags.
 CLBuffer<ByteBuffer> createByteBuffer(int size, CLMemory.Mem... flags)
          Creates a CLBuffer with the specified flags and buffer size in bytes.
 CLBuffer<ByteBuffer> createByteBuffer(int size, int flags)
          Creates a CLBuffer with the specified flags and buffer size in bytes.
protected static long createContext(CLPlatform platform, CLErrorHandler handler, PointerBuffer properties, CLDevice... devices)
           
protected static long createContextFromType(CLPlatform platform, CLErrorHandler handler, PointerBuffer properties, long deviceType)
           
 CLBuffer<DoubleBuffer> createDoubleBuffer(int size, CLMemory.Mem... flags)
          Creates a CLBuffer with the specified flags and element count.
protected static CLContext.ErrorDispatcher createErrorHandler()
           
 CLBuffer<FloatBuffer> createFloatBuffer(int size, CLMemory.Mem... flags)
          Creates a CLBuffer with the specified flags and element count.
<B extends Buffer>
CLImage2d<B>
createImage2d(B directBuffer, int width, int height, CLImageFormat format, CLMemory.Mem... flags)
          Creates a CLImage2d with the specified format, dimension and flags.
<B extends Buffer>
CLImage2d<B>
createImage2d(B directBuffer, int width, int height, int rowPitch, CLImageFormat format, CLMemory.Mem... flags)
          Creates a CLImage2d with the specified format, dimension and flags.
 CLImage2d<?> createImage2d(int width, int height, CLImageFormat format, CLMemory.Mem... flags)
          Creates a CLImage2d with the specified format, dimension and flags.
 CLImage2d<?> createImage2d(int width, int height, int rowPitch, CLImageFormat format, CLMemory.Mem... flags)
          Creates a CLImage2d with the specified format, dimension and flags.
<B extends Buffer>
CLImage3d<B>
createImage3d(B directBuffer, int width, int height, int depth, CLImageFormat format, CLMemory.Mem... flags)
          Creates a CLImage3d with the specified format, dimension and flags.
<B extends Buffer>
CLImage3d<B>
createImage3d(B directBuffer, int width, int height, int depth, int rowPitch, int slicePitch, CLImageFormat format, CLMemory.Mem... flags)
          Creates a CLImage3d with the specified format, dimension and flags.
 CLImage3d<?> createImage3d(int width, int height, int depth, CLImageFormat format, CLMemory.Mem... flags)
          Creates a CLImage3d with the specified format, dimension and flags.
 CLImage3d<?> createImage3d(int width, int height, int depth, int rowPitch, int slicePitch, CLImageFormat format, CLMemory.Mem... flags)
          Creates a CLImage3d with the specified format, dimension and flags.
 CLBuffer<IntBuffer> createIntBuffer(int size, CLMemory.Mem... flags)
          Creates a CLBuffer with the specified flags and element count.
 CLBuffer<LongBuffer> createLongBuffer(int size, CLMemory.Mem... flags)
          Creates a CLBuffer with the specified flags and element count.
 CLProgram createProgram(InputStream source)
          Creates a program and reads the source from stream, the returned program is not build yet.
 CLProgram createProgram(Map<CLDevice,byte[]> binaries)
          Creates a program from the given binaries, the program is not build yet.
 CLProgram createProgram(String src)
          Creates a program from the given sources, the returned program is not build yet.
 CLSampler createSampler(CLSampler.AddressingMode addrMode, CLSampler.FilteringMode filtMode, boolean normalizedCoords)
           
 CLBuffer<ShortBuffer> createShortBuffer(int size, CLMemory.Mem... flags)
          Creates a CLBuffer with the specified flags and element count.
 boolean equals(Object obj)
           
 CL getCL()
          Return the low level OpenCL interface.
 CLContext getContext()
          Returns the context for this OpenCL object.
 CLDevice[] getDevices()
          Returns all devices associated with this CLContext.
 CLDevice getMaxFlopsDevice()
          Returns the device with maximal FLOPS from this context.
 CLDevice getMaxFlopsDevice(CLDevice.Type type)
          Returns the device with maximal FLOPS of the specified device type from this context.
 List<CLMemory<? extends Buffer>> getMemoryObjects()
          Returns a read only shapshot of all allocated memory objects associated with this context.
 CLPlatform getPlatform()
          Returns the CLPlatform this context is running on.
 List<CLProgram> getPrograms()
          Returns a read only shapshot of all programs associated with this context.
 List<CLSampler> getSamplers()
          Returns a read only shapshot of all samplers associated with this context.
 CLImageFormat[] getSupportedImage2dFormats(CLMemory.Mem... flags)
          Returns all supported 2d image formats with the (optional) memory allocation flags.
 CLImageFormat[] getSupportedImage3dFormats(CLMemory.Mem... flags)
          Returns all supported 3d image formats with the (optional) memory allocation flags.
 int hashCode()
           
 boolean isReleased()
          Returns true if CLResource.release() has been called.
protected  void overrideContext(CLDevice device)
           
 void release()
          Releases this context and all resources.
 void removeCLErrorHandler(CLErrorHandler handler)
           
protected static long toDeviceBitmap(CLDevice.Type[] deviceTypes)
           
 String toString()
           
 
Methods inherited from class com.jogamp.opencl.CLObject
getID
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

devices

protected CLDevice[] devices

programs

protected final Set<CLProgram> programs

samplers

protected final Set<CLSampler> samplers

memoryObjects

protected final Set<CLMemory<? extends Buffer>> memoryObjects

queuesMap

protected final Map<CLDevice,List<CLCommandQueue>> queuesMap

platform

protected final CLPlatform platform
Constructor Detail

CLContext

protected CLContext(CLPlatform platform,
                    long contextID,
                    CLContext.ErrorDispatcher dispatcher)
Method Detail

create

public static CLContext create()
Creates a context on all available devices (CL_DEVICE_TYPE_ALL). The platform to be used is implementation dependent.


create

public static CLContext create(CLDevice.Type... deviceTypes)
Creates a context on the specified device types. The platform to be used is implementation dependent.


create

public static CLContext create(CLPlatform platform)
Creates a context on the specified platform on all available devices (CL_DEVICE_TYPE_ALL).


create

public static CLContext create(CLPlatform platform,
                               CLDevice.Type... deviceTypes)
Creates a context on the specified platform and with the specified device types.


create

public static CLContext create(CLDevice... devices)
Creates a context on the specified devices.


createContextFromType

protected static long createContextFromType(CLPlatform platform,
                                            CLErrorHandler handler,
                                            PointerBuffer properties,
                                            long deviceType)

createContext

protected static long createContext(CLPlatform platform,
                                    CLErrorHandler handler,
                                    PointerBuffer properties,
                                    CLDevice... devices)

createProgram

public CLProgram createProgram(String src)
Creates a program from the given sources, the returned program is not build yet.


createProgram

public CLProgram createProgram(InputStream source)
                        throws IOException
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.

createProgram

public CLProgram createProgram(Map<CLDevice,byte[]> binaries)
Creates a program from the given binaries, the program is not build yet.
Creating a program will fail if:


createShortBuffer

public final CLBuffer<ShortBuffer> createShortBuffer(int size,
                                                     CLMemory.Mem... flags)
Creates a CLBuffer with the specified flags and element count. No flags creates a MEM.READ_WRITE buffer.


createIntBuffer

public final CLBuffer<IntBuffer> createIntBuffer(int size,
                                                 CLMemory.Mem... flags)
Creates a CLBuffer with the specified flags and element count. No flags creates a MEM.READ_WRITE buffer.


createLongBuffer

public final CLBuffer<LongBuffer> createLongBuffer(int size,
                                                   CLMemory.Mem... flags)
Creates a CLBuffer with the specified flags and element count. No flags creates a MEM.READ_WRITE buffer.


createFloatBuffer

public final CLBuffer<FloatBuffer> createFloatBuffer(int size,
                                                     CLMemory.Mem... flags)
Creates a CLBuffer with the specified flags and element count. No flags creates a MEM.READ_WRITE buffer.


createDoubleBuffer

public final CLBuffer<DoubleBuffer> createDoubleBuffer(int size,
                                                       CLMemory.Mem... flags)
Creates a CLBuffer with the specified flags and element count. No flags creates a MEM.READ_WRITE buffer.


createByteBuffer

public final CLBuffer<ByteBuffer> createByteBuffer(int size,
                                                   CLMemory.Mem... flags)
Creates a CLBuffer with the specified flags and buffer size in bytes. No flags creates a MEM.READ_WRITE buffer.


createByteBuffer

public final CLBuffer<ByteBuffer> createByteBuffer(int size,
                                                   int flags)
Creates a CLBuffer with the specified flags and buffer size in bytes.


createBuffer

public final CLBuffer<?> createBuffer(int size,
                                      CLMemory.Mem... flags)
Creates a CLBuffer with the specified flags. No flags creates a MEM.READ_WRITE buffer.


createBuffer

public final CLBuffer<?> createBuffer(int size,
                                      int flags)
Creates a CLBuffer with the specified flags.


createBuffer

public final <B extends Buffer> CLBuffer<B> createBuffer(B directBuffer,
                                                         CLMemory.Mem... flags)
Creates a CLBuffer with the specified flags. No flags creates a MEM.READ_WRITE buffer.


createBuffer

public final <B extends Buffer> CLBuffer<B> createBuffer(B directBuffer,
                                                         int flags)
Creates a CLBuffer with the specified flags.


createImage2d

public final CLImage2d<?> createImage2d(int width,
                                        int height,
                                        CLImageFormat format,
                                        CLMemory.Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.


createImage2d

public final CLImage2d<?> createImage2d(int width,
                                        int height,
                                        int rowPitch,
                                        CLImageFormat format,
                                        CLMemory.Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.


createImage2d

public final <B extends Buffer> CLImage2d<B> createImage2d(B directBuffer,
                                                           int width,
                                                           int height,
                                                           CLImageFormat format,
                                                           CLMemory.Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.


createImage2d

public final <B extends Buffer> CLImage2d<B> createImage2d(B directBuffer,
                                                           int width,
                                                           int height,
                                                           int rowPitch,
                                                           CLImageFormat format,
                                                           CLMemory.Mem... flags)
Creates a CLImage2d with the specified format, dimension and flags.


createImage3d

public final CLImage3d<?> createImage3d(int width,
                                        int height,
                                        int depth,
                                        CLImageFormat format,
                                        CLMemory.Mem... flags)
Creates a CLImage3d with the specified format, dimension and flags.


createImage3d

public final CLImage3d<?> createImage3d(int width,
                                        int height,
                                        int depth,
                                        int rowPitch,
                                        int slicePitch,
                                        CLImageFormat format,
                                        CLMemory.Mem... flags)
Creates a CLImage3d with the specified format, dimension and flags.


createImage3d

public final <B extends Buffer> CLImage3d<B> createImage3d(B directBuffer,
                                                           int width,
                                                           int height,
                                                           int depth,
                                                           CLImageFormat format,
                                                           CLMemory.Mem... flags)
Creates a CLImage3d with the specified format, dimension and flags.


createImage3d

public final <B extends Buffer> CLImage3d<B> createImage3d(B directBuffer,
                                                           int width,
                                                           int height,
                                                           int depth,
                                                           int rowPitch,
                                                           int slicePitch,
                                                           CLImageFormat format,
                                                           CLMemory.Mem... flags)
Creates a CLImage3d with the specified format, dimension and flags.


createSampler

public CLSampler createSampler(CLSampler.AddressingMode addrMode,
                               CLSampler.FilteringMode filtMode,
                               boolean normalizedCoords)

addCLErrorHandler

public void addCLErrorHandler(CLErrorHandler handler)

removeCLErrorHandler

public void removeCLErrorHandler(CLErrorHandler handler)

release

public void release()
Releases this context and all resources.

Specified by:
release in interface CLResource

overrideContext

protected void overrideContext(CLDevice device)

getSupportedImage2dFormats

public CLImageFormat[] getSupportedImage2dFormats(CLMemory.Mem... flags)
Returns all supported 2d image formats with the (optional) memory allocation flags.


getSupportedImage3dFormats

public CLImageFormat[] getSupportedImage3dFormats(CLMemory.Mem... flags)
Returns all supported 3d image formats with the (optional) memory allocation flags.


getPlatform

public CLPlatform getPlatform()
Returns the CLPlatform this context is running on.

Overrides:
getPlatform in class CLObject

getContext

public CLContext getContext()
Description copied from class: CLObject
Returns the context for this OpenCL object.

Overrides:
getContext in class CLObject

getPrograms

public List<CLProgram> getPrograms()
Returns a read only shapshot of all programs associated with this context.


getMemoryObjects

public List<CLMemory<? extends Buffer>> getMemoryObjects()
Returns a read only shapshot of all allocated memory objects associated with this context.


getSamplers

public List<CLSampler> getSamplers()
Returns a read only shapshot of all samplers associated with this context.


getMaxFlopsDevice

public CLDevice getMaxFlopsDevice()
Returns the device with maximal FLOPS from this context. The device speed is estimated by calculating the product of MAX_COMPUTE_UNITS and MAX_CLOCK_FREQUENCY.

See Also:
getMaxFlopsDevice(com.jogamp.opencl.CLDevice.Type)

getMaxFlopsDevice

public CLDevice getMaxFlopsDevice(CLDevice.Type type)
Returns the device with maximal FLOPS of the specified device type from this context. The device speed is estimated by calculating the product of MAX_COMPUTE_UNITS and MAX_CLOCK_FREQUENCY.


getDevices

public CLDevice[] getDevices()
Returns all devices associated with this CLContext.


getCL

public CL getCL()
Return the low level OpenCL interface.


toDeviceBitmap

protected static long toDeviceBitmap(CLDevice.Type[] deviceTypes)

toString

public String toString()
Overrides:
toString in class CLObject

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

createErrorHandler

protected static CLContext.ErrorDispatcher createErrorHandler()

close

@Deprecated
public final void close()
Deprecated. This method is not intended to be called from client code.

Implementation detail, satisfying AutoCloseable. Might be remove as soon we have extension methods, but shall validate with Java 1.5 specs.

Specified by:
close in interface AutoCloseable
See Also:
AutoCloseable

isReleased

public boolean isReleased()
Description copied from interface: CLResource
Returns true if CLResource.release() has been called.

Specified by:
isReleased in interface CLResource