Class CLContext

  • All Implemented Interfaces:
    AutoCloseable, CLResource, AutoCloseable
    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.
    • 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.
      • 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:
        • the submitted binaries are invalid or can not be loaded from the OpenCL driver
        • the binaries do not fit to the CLDevices associated with this context
        • binaries are missing for one or more CLDevices
      • 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 BufferCLBuffer<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 BufferCLBuffer<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 BufferCLImage2d<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 BufferCLImage2d<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 BufferCLImage3d<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 BufferCLImage3d<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.
      • 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.
      • 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​(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)
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • 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
        Specified by:
        close in interface AutoCloseable
        See Also:
        AutoCloseable