Permalink
Browse files

Merge branch 'images' into develop

  • Loading branch information...
2 parents 6c871ab + 91950ff commit 6ba5d2e4ef6f0953459a918290e8acef9856d39f @zhensydow zhensydow committed Jan 13, 2012
Showing with 1,284 additions and 25 deletions.
  1. +827 −3 src/Control/Parallel/OpenCL/CommandQueue.chs
  2. +446 −15 src/Control/Parallel/OpenCL/Memory.chs
  3. +11 −7 src/Control/Parallel/OpenCL/Types.chs
View
830 src/Control/Parallel/OpenCL/CommandQueue.chs
@@ -39,7 +39,10 @@ module Control.Parallel.OpenCL.CommandQueue(
clGetCommandQueueReferenceCount, clGetCommandQueueProperties,
clSetCommandQueueProperty,
-- * Memory Commands
- clEnqueueReadBuffer, clEnqueueWriteBuffer,
+ clEnqueueReadBuffer, clEnqueueWriteBuffer, clEnqueueReadImage,
+ clEnqueueWriteImage, clEnqueueCopyImage, clEnqueueCopyImageToBuffer,
+ clEnqueueCopyBufferToImage, clEnqueueMapBuffer, clEnqueueMapImage,
+ clEnqueueUnmapMemObject,
-- * Executing Kernels
clEnqueueNDRangeKernel, clEnqueueTask, clEnqueueMarker,
clEnqueueWaitForEvents, clEnqueueBarrier,
@@ -52,8 +55,8 @@ import Foreign
import Foreign.C.Types
import Control.Parallel.OpenCL.Types(
CLint, CLbool, CLuint, CLCommandQueueProperty_, CLCommandQueueInfo_,
- CLCommandQueue, CLDeviceID, CLContext, CLCommandQueueProperty(..),
- CLEvent, CLMem, CLKernel,
+ CLMapFlags_, CLMapFlag(..), CLCommandQueue, CLDeviceID, CLContext,
+ CLCommandQueueProperty(..), CLEvent, CLMem, CLKernel,
whenSuccess, wrapCheckSuccess, wrapPError, wrapGetInfo, getCLValue,
bitmaskToCommandQueueProperties, bitmaskFromFlags )
@@ -78,6 +81,22 @@ foreign import CALLCONV "clEnqueueReadBuffer" raw_clEnqueueReadBuffer ::
CLCommandQueue -> CLMem -> CLbool -> CSize -> CSize -> Ptr () -> CLuint -> Ptr CLEvent -> Ptr CLEvent -> IO CLint
foreign import CALLCONV "clEnqueueWriteBuffer" raw_clEnqueueWriteBuffer ::
CLCommandQueue -> CLMem -> CLbool -> CSize -> CSize -> Ptr () -> CLuint -> Ptr CLEvent -> Ptr CLEvent -> IO CLint
+foreign import CALLCONV "clEnqueueReadImage" raw_clEnqueueReadImage ::
+ CLCommandQueue -> CLMem -> CLbool -> Ptr CSize -> Ptr CSize -> CSize -> CSize -> Ptr () -> CLuint -> Ptr CLEvent -> Ptr CLEvent -> IO CLint
+foreign import CALLCONV "clEnqueueWriteImage" raw_clEnqueueWriteImage ::
+ CLCommandQueue -> CLMem -> CLbool -> Ptr CSize -> Ptr CSize -> CSize -> CSize -> Ptr () -> CLuint -> Ptr CLEvent -> Ptr CLEvent -> IO CLint
+foreign import CALLCONV "clEnqueueCopyImage" raw_clEnqueueCopyImage ::
+ CLCommandQueue -> CLMem -> CLMem -> Ptr CSize -> Ptr CSize -> Ptr CSize -> CLuint -> Ptr CLEvent -> Ptr CLEvent -> IO CLint
+foreign import CALLCONV "clEnqueueCopyImageToBuffer" raw_clEnqueueCopyImageToBuffer ::
+ CLCommandQueue -> CLMem -> CLMem -> Ptr CSize -> Ptr CSize -> CSize -> CLuint -> Ptr CLEvent -> Ptr CLEvent -> IO CLint
+foreign import CALLCONV "clEnqueueCopyBufferToImage" raw_clEnqueueCopyBufferToImage ::
+ CLCommandQueue -> CLMem -> CLMem -> CSize -> Ptr CSize -> Ptr CSize -> CLuint -> Ptr CLEvent -> Ptr CLEvent -> IO CLint
+foreign import CALLCONV "clEnqueueMapBuffer" raw_clEnqueueMapBuffer ::
+ CLCommandQueue -> CLMem -> CLbool -> CLMapFlags_ -> CSize -> CSize -> CLuint -> Ptr CLEvent -> Ptr CLEvent -> Ptr CLint -> IO (Ptr ())
+foreign import CALLCONV "clEnqueueMapImage" raw_clEnqueueMapImage ::
+ CLCommandQueue -> CLMem -> CLbool -> CLMapFlags_ -> Ptr CSize -> Ptr CSize -> Ptr CSize -> Ptr CSize -> CLuint -> Ptr CLEvent -> Ptr CLEvent -> Ptr CLint -> IO (Ptr ())
+foreign import CALLCONV "clEnqueueUnmapMemObject" raw_clEnqueueUnmapMemObject ::
+ CLCommandQueue -> CLMem -> Ptr () -> CLuint -> Ptr CLEvent -> Ptr CLEvent -> IO CLint
foreign import CALLCONV "clEnqueueNDRangeKernel" raw_clEnqueueNDRangeKernel ::
CLCommandQueue -> CLKernel -> CLuint -> Ptr CSize -> Ptr CSize -> Ptr CSize -> CLuint -> Ptr CLEvent -> Ptr CLEvent -> IO CLint
foreign import CALLCONV "clEnqueueTask" raw_clEnqueueTask ::
@@ -363,6 +382,811 @@ clEnqueueWriteBuffer :: Integral a => CLCommandQueue -> CLMem -> Bool -> a -> a
-> Ptr () -> [CLEvent] -> IO CLEvent
clEnqueueWriteBuffer cq mem check off size dat = clEnqueue (raw_clEnqueueWriteBuffer cq mem (fromBool check) (fromIntegral off) (fromIntegral size) dat)
+{-| Enqueues a command to read from a 2D or 3D image object to host memory.
+
+Returns an event object that identifies this particular read command and can be
+used to query or queue a wait for this particular command to complete. event can
+be NULL in which case it will not be possible for the application to query the
+status of this command or queue a wait for this command to complete.
+
+Notes
+
+If blocking is 'True' i.e. the read command is blocking, 'clEnqueueReadImage'
+does not return until the buffer data has been read and copied into memory
+pointed to by ptr.
+
+If blocking_read is 'False' i.e. map operation is non-blocking,
+'clEnqueueReadImage' queues a non-blocking read command and returns. The
+contents of the buffer that ptr points to cannot be used until the read command
+has completed. The event argument returns an event object which can be used to
+query the execution status of the read command. When the read command has
+completed, the contents of the buffer that ptr points to can be used by the
+application.
+
+Calling 'clEnqueueReadImage' to read a region of the image object with the ptr
+argument value set to host_ptr + (origin.z * image slice pitch + origin.y *
+image row pitch + origin.x * bytes per pixel), where host_ptr is a pointer to
+the memory region specified when the image object being read is created with
+'CL_MEM_USE_HOST_PTR', must meet the following requirements in order to avoid
+undefined behavior:
+
+ * All commands that use this image object have finished execution before the
+read command begins execution.
+
+ * The row_pitch and slice_pitch argument values in clEnqueueReadImage must be
+set to the image row pitch and slice pitch.
+
+ * The image object is not mapped.
+
+ * The image object is not used by any command-queue until the read command has
+finished execution.
+
+'clEnqueueReadImage' returns the 'CLEvent' if the function is executed
+successfully. It can throw the following 'CLError' exceptions:
+
+ * 'CL_INVALID_COMMAND_QUEUE' if command_queue is not a valid command-queue.
+
+ * 'CL_INVALID_CONTEXT' if the context associated with command_queue and image
+are not the same or if the context associated with command_queue and events in
+event_wait_list are not the same.
+
+ * 'CL_INVALID_MEM_OBJECT' if image is not a valid image object.
+
+ * 'CL_INVALID_VALUE' if the region being read specified by origin and region is
+out of bounds or if ptr is a nullPtr value.
+
+ * 'CL_INVALID_VALUE' if image is a 2D image object and z is not equal to 0 or
+depth is not equal to 1 or slice_pitch is not equal to 0.
+
+ * 'CL_INVALID_EVENT_WAIT_LIST' if event objects in event_wait_list are not
+valid events.
+
+ * 'CL_MEM_OBJECT_ALLOCATION_FAILURE' if there is a failure to allocate memory
+for data store associated with image.
+
+ * 'CL_OUT_OF_HOST_MEMORY' if there is a failure to allocate resources required
+by the OpenCL implementation on the host.
+
+-}
+clEnqueueReadImage :: Integral a
+ => CLCommandQueue -- ^ Refers to the command-queue in
+ -- which the read command will be
+ -- queued. command_queue and image must
+ -- be created with the same OpenCL
+ -- contex
+ -> CLMem -- ^ Refers to a valid 2D or 3D image object.
+ -> Bool -- ^ Indicates if the read operations are blocking
+ -- or non-blocking.
+ -> (a,a,a) -- ^ Defines the (x, y, z) offset in pixels in
+ -- the image from where to read. If image is a
+ -- 2D image object, the z value given must be
+ -- 0.
+ -> (a,a,a) -- ^ Defines the (width, height, depth) in
+ -- pixels of the 2D or 3D rectangle being
+ -- read. If image is a 2D image object, the
+ -- depth value given must be 1.
+ -> a -- ^ The length of each row in bytes. This value must
+ -- be greater than or equal to the element size in
+ -- bytes * width. If row_pitch is set to 0, the
+ -- appropriate row pitch is calculated based on the
+ -- size of each element in bytes multiplied by width.
+ -> a -- ^ Size in bytes of the 2D slice of the 3D region
+ -- of a 3D image being read. This must be 0 if image
+ -- is a 2D image. This value must be greater than or
+ -- equal to row_pitch * height. If slice_pitch is set
+ -- to 0, the appropriate slice pitch is calculated
+ -- based on the row_pitch * height.
+ -> Ptr () -- ^ The pointer to a buffer in host memory
+ -- where image data is to be read from.
+ -> [CLEvent] -- ^ Specify events that need to complete
+ -- before this particular command can be
+ -- executed. If event_wait_list is empty,
+ -- then this particular command does not wait
+ -- on any event to complete. The events
+ -- specified in the list act as
+ -- synchronization points. The context
+ -- associated with events in event_wait_list
+ -- and command_queue must be the same.
+ -> IO CLEvent
+clEnqueueReadImage cq mem check (orix,oriy,oriz) (regx,regy,regz) rp sp dat xs =
+ withArray (fmap fromIntegral [orix,oriy,oriz]) $ \pori ->
+ withArray (fmap fromIntegral [regx,regy,regz]) $ \preg ->
+ clEnqueue (raw_clEnqueueReadImage cq mem (fromBool check) pori preg (fromIntegral rp) (fromIntegral sp) dat) xs
+
+{-| Enqueues a command to write from a 2D or 3D image object to host memory.
+
+Returns an event object that identifies this particular write command and can be
+used to query or queue a wait for this particular command to complete. event can
+be NULL in which case it will not be possible for the application to query the
+status of this command or queue a wait for this command to complete.
+
+Notes
+
+If blocking_write is 'True' the OpenCL implementation copies the data referred
+to by ptr and enqueues the write command in the command-queue. The memory
+pointed to by ptr can be reused by the application after the
+'clEnqueueWriteImage' call returns.
+
+If blocking_write is 'False' the OpenCL implementation will use ptr to perform a
+nonblocking write. As the write is non-blocking the implementation can return
+immediately. The memory pointed to by ptr cannot be reused by the application
+after the call returns. The event argument returns an event object which can be
+used to query the execution status of the write command. When the write command
+has completed, the memory pointed to by ptr can then be reused by the
+application.
+
+Calling 'clEnqueueWriteImage' to update the latest bits in a region of the image
+object with the ptr argument value set to host_ptr + (origin.z * image slice
+pitch + origin.y * image row pitch + origin.x * bytes per pixel), where host_ptr
+is a pointer to the memory region specified when the image object being written
+is created with 'CL_MEM_USE_HOST_PTR', must meet the following requirements in
+order to avoid undefined behavior:
+
+ * The host memory region being written contains the latest bits when the
+enqueued write command begins execution.
+
+ * The input_row_pitch and input_slice_pitch argument values in
+clEnqueueWriteImage must be set to the image row pitch and slice pitch.
+
+ * The image object is not mapped.
+
+ * The image object is not used by any command-queue until the write command has
+finished execution.
+
+'clEnqueueWriteImage' returns the 'CLEvent' if the function is executed
+successfully. It can throw the following 'CLError' exceptions:
+
+ * 'CL_INVALID_COMMAND_QUEUE' if command_queue is not a valid command-queue.
+
+ * 'CL_INVALID_CONTEXT' if the context associated with command_queue and image
+are not the same or if the context associated with command_queue and events in
+event_wait_list are not the same.
+
+ * 'CL_INVALID_MEM_OBJECT' if image is not a valid image object.
+
+ * 'CL_INVALID_VALUE' if the region being write or written specified by origin
+and region is out of bounds or if ptr is a NULL value.
+
+ * 'CL_INVALID_VALUE' if image is a 2D image object and z is not equal to 0 or
+depth is not equal to 1 or slice_pitch is not equal to 0.
+
+ * 'CL_INVALID_EVENT_WAIT_LIST' if event objects in event_wait_list are not
+valid events.
+
+ * 'CL_MEM_OBJECT_ALLOCATION_FAILURE' if there is a failure to allocate memory
+for data store associated with image.
+
+ * 'CL_OUT_OF_HOST_MEMORY' if there is a failure to allocate resources required
+by the OpenCL implementation on the host.
+
+-}
+clEnqueueWriteImage :: Integral a
+ => CLCommandQueue -- ^ Refers to the command-queue in
+ -- which the write command will be
+ -- queued. command_queue and image must
+ -- be created with the same OpenCL
+ -- contex
+ -> CLMem -- ^ Refers to a valid 2D or 3D image object.
+ -> Bool -- ^ Indicates if the write operation is blocking
+ -- or non-blocking.
+ -> (a,a,a) -- ^ Defines the (x, y, z) offset in pixels in
+ -- the image from where to write or write. If
+ -- image is a 2D image object, the z value
+ -- given must be 0.
+ -> (a,a,a) -- ^ Defines the (width, height, depth) in
+ -- pixels of the 2D or 3D rectangle being
+ -- write or written. If image is a 2D image
+ -- object, the depth value given must be 1.
+ -> a -- ^ The length of each row in bytes. This value
+ -- must be greater than or equal to the element size
+ -- in bytes * width. If input_row_pitch is set to 0,
+ -- the appropriate row pitch is calculated based on
+ -- the size of each element in bytes multiplied by
+ -- width.
+ -> a -- ^ Size in bytes of the 2D slice of the 3D region
+ -- of a 3D image being written. This must be 0 if
+ -- image is a 2D image. This value must be greater
+ -- than or equal to row_pitch * height. If
+ -- input_slice_pitch is set to 0, the appropriate
+ -- slice pitch is calculated based on the row_pitch
+ -- * height.
+ -> Ptr () -- ^ The pointer to a buffer in host memory
+ -- where image data is to be written to.
+ -> [CLEvent] -- ^ Specify events that need to complete
+ -- before this particular command can be
+ -- executed. If event_wait_list is empty,
+ -- then this particular command does not
+ -- wait on any event to complete. The events
+ -- specified in event_wait_list act as
+ -- synchronization points. The context
+ -- associated with events in event_wait_list
+ -- and command_queue must be the same.
+ -> IO CLEvent
+clEnqueueWriteImage cq mem check (orix,oriy,oriz) (regx,regy,regz) rp sp dat xs =
+ withArray (fmap fromIntegral [orix,oriy,oriz]) $ \pori ->
+ withArray (fmap fromIntegral [regx,regy,regz]) $ \preg ->
+ clEnqueue (raw_clEnqueueWriteImage cq mem (fromBool check) pori preg (fromIntegral rp) (fromIntegral sp) dat) xs
+
+{-| Enqueues a command to copy image objects.
+
+Notes
+
+It is currently a requirement that the src_image and dst_image image memory
+objects for 'clEnqueueCopyImage' must have the exact same image format (i.e. the
+'CLImageFormat' descriptor specified when src_image and dst_image are created
+must match).
+
+src_image and dst_image can be 2D or 3D image objects allowing us to perform the
+following actions:
+
+ * Copy a 2D image object to a 2D image object.
+
+ * Copy a 2D image object to a 2D slice of a 3D image object.
+
+ * Copy a 2D slice of a 3D image object to a 2D image object.
+
+ * Copy a 3D image object to a 3D image object.
+
+'clEnqueueCopyImage' returns the 'CLEvent' if the function is executed
+successfully. It can throw the following 'CLError' exceptions:
+
+ * 'CL_INVALID_COMMAND_QUEUE if command_queue is not a valid command-queue.
+
+ * 'CL_INVALID_CONTEXT if the context associated with command_queue, src_image
+and dst_image are not the same or if the context associated with command_queue
+and events in event_wait_list are not the same.
+
+ * 'CL_INVALID_MEM_OBJECT if src_image and dst_image are not valid image
+objects.
+
+ * 'CL_IMAGE_FORMAT_MISMATCH if src_image and dst_image do not use the same
+image format.
+
+ * 'CL_INVALID_VALUE if the 2D or 3D rectangular region specified by src_origin
+and src_origin + region refers to a region outside src_image, or if the 2D or 3D
+rectangular region specified by dst_origin and dst_origin + region refers to a
+region outside dst_image.
+
+ * 'CL_INVALID_VALUE if src_image is a 2D image object and src_origin.z is not
+equal to 0 or region.depth is not equal to 1.
+
+ * 'CL_INVALID_VALUE if dst_image is a 2D image object and dst_origen.z is not
+equal to 0 or region.depth is not equal to 1.
+
+ * 'CL_INVALID_EVENT_WAIT_LIST if event objects in event_wait_list are not valid
+events.
+
+ * 'CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory
+for data store associated with src_image or dst_image.
+
+ * 'CL_OUT_OF_HOST_MEMORY if there is a failure to allocate resources required
+by the OpenCL implementation on the host.
+
+ * 'CL_MEM_COPY_OVERLAP if src_image and dst_image are the same image object and
+the source and destination regions overlap.
+
+-}
+clEnqueueCopyImage :: Integral a
+ => CLCommandQueue -- ^ Refers to the command-queue in
+ -- which the copy command will be
+ -- queued. The OpenCL context associated
+ -- with command_queue, src_image and
+ -- dst_image must be the same.
+ -> CLMem -- ^ src
+ -> CLMem -- ^ dst
+ -> (a,a,a) -- ^ Defines the starting (x, y, z) location in
+ -- pixels in src_image from where to start the
+ -- data copy. If src_image is a 2D image
+ -- object, the z value given must be 0.
+ -> (a,a,a) -- ^ Defines the starting (x, y, z) location in
+ -- pixels in dst_image from where to start the
+ -- data copy. If dst_image is a 2D image
+ -- object, the z value given must be 0.
+ -> (a,a,a) -- ^ Defines the (width, height, depth) in
+ -- pixels of the 2D or 3D rectangle to copy. If
+ -- src_image or dst_image is a 2D image object,
+ -- the depth value given must be 1.
+ -> [CLEvent] -- ^ Specify events that need to complete
+ -- before this particular command can be
+ -- executed. If event_wait_list is empty, then
+ -- this particular command does not wait on
+ -- any event to complete.
+ -> IO CLEvent
+clEnqueueCopyImage cq src dst (src_orix,src_oriy,src_oriz) (dst_orix,dst_oriy,dst_oriz) (regx,regy,regz) xs =
+ withArray (fmap fromIntegral [src_orix,src_oriy,src_oriz]) $ \psrc_ori ->
+ withArray (fmap fromIntegral [dst_orix,dst_oriy,dst_oriz]) $ \pdst_ori ->
+ withArray (fmap fromIntegral [regx,regy,regz]) $ \preg ->
+ clEnqueue (raw_clEnqueueCopyImage cq src dst psrc_ori pdst_ori preg) xs
+
+
+{-| Enqueues a command to copy an image object to a buffer object.
+
+Returns an event object that identifies this particular copy command and can be
+used to query or queue a wait for this particular command to complete. event can
+be NULL in which case it will not be possible for the application to query the
+status of this command or queue a wait for this command to
+complete. 'clEnqueueBarrier' can be used instead.
+
+'clEnqueueCopyImageToBuffer' returns the 'CLEvent' if the function is executed
+successfully. It can throw the following 'CLError' exceptions:
+
+ * CL_INVALID_COMMAND_QUEUE if command_queue is not a valid command-queue.
+
+ * CL_INVALID_CONTEXT if the context associated with command_queue, src_image
+and dst_buffer are not the same or if the context associated with command_queue
+and events in event_wait_list are not the same.
+
+ * CL_INVALID_MEM_OBJECT if src_image is not a valid image object and dst_buffer
+is not a valid buffer object.
+
+ * CL_INVALID_VALUE if the 2D or 3D rectangular region specified by src_origin
+and src_origin + region refers to a region outside src_image, or if the region
+specified by dst_offset and dst_offset + dst_cb refers to a region outside
+dst_buffer.
+
+ * CL_INVALID_VALUE if src_image is a 2D image object and src_origin.z is not
+equal to 0 or region.depth is not equal to 1.
+
+ * CL_INVALID_EVENT_WAIT_LIST if event objects in event_wait_list are not valid
+events.
+
+ * CL_MEM_OBJECT_ALLOCATION_FAILURE if there is a failure to allocate memory for
+data store associated with src_image or dst_buffer.
+
+ * CL_OUT_OF_HOST_MEMORY if there is a failure to allocate resources required by
+the OpenCL implementation on the host.
+
+-}
+clEnqueueCopyImageToBuffer :: Integral a
+ => CLCommandQueue -- ^ The OpenCL context
+ -- associated with
+ -- command_queue, src_image, and
+ -- dst_buffer must be the same.
+ -> CLMem -- ^ src. A valid image object.
+ -> CLMem -- ^ dst. A valid buffer object.
+ -> (a,a,a) -- ^ Defines the (x, y, z) offset in
+ -- pixels in the image from where to
+ -- copy. If src_image is a 2D image
+ -- object, the z value given must be 0.
+ -> (a,a,a) -- ^ Defines the (width, height, depth)
+ -- in pixels of the 2D or 3D rectangle
+ -- to copy. If src_image is a 2D image
+ -- object, the depth value given must
+ -- be 1.
+ -> a -- ^ The offset where to begin copying data
+ -- into dst_buffer. The size in bytes of the
+ -- region to be copied referred to as dst_cb
+ -- is computed as width * height * depth *
+ -- bytes/image element if src_image is a 3D
+ -- image object and is computed as width *
+ -- height * bytes/image element if src_image
+ -- is a 2D image object.
+ -> [CLEvent] -- ^ Specify events that need to
+ -- complete before this particular
+ -- command can be executed. If
+ -- event_wait_list is empty, then
+ -- this particular command does not
+ -- wait on any event to complete. The
+ -- events specified in
+ -- event_wait_list act as
+ -- synchronization points. The
+ -- context associated with events in
+ -- event_wait_list and command_queue
+ -- must be the same.
+ -> IO CLEvent
+clEnqueueCopyImageToBuffer cq src dst (src_orix,src_oriy,src_oriz) (regx,regy,regz) offset xs =
+ withArray (fmap fromIntegral [src_orix,src_oriy,src_oriz]) $ \psrc_ori ->
+ withArray (fmap fromIntegral [regx,regy,regz]) $ \preg ->
+ clEnqueue (raw_clEnqueueCopyImageToBuffer cq src dst psrc_ori preg (fromIntegral offset)) xs
+
+{-| Enqueues a command to copy a buffer object to an image object.
+
+The size in bytes of the region to be copied from src_buffer referred to as
+src_cb is computed as width * height * depth * bytes/image element if dst_image
+is a 3D image object and is computed as width * height * bytes/image element if
+dst_image is a 2D image object.
+
+Returns an event object that identifies this particular copy command and can be
+used to query or queue a wait for this particular command to complete. event can
+be NULL in which case it will not be possible for the application to query the
+status of this command or queue a wait for this command to
+complete. 'clEnqueueBarrier' can be used instead.
+
+'clEnqueueCopyBufferToImage' returns the 'CLEvent' if the function is executed
+successfully. It can throw the following 'CLError' exceptions:
+
+ * 'CL_INVALID_COMMAND_QUEUE' if command_queue is not a valid command-queue.
+
+ * 'CL_INVALID_CONTEXT' if the context associated with command_queue, src_buffer
+and dst_image are not the same or if the context associated with command_queue
+and events in event_wait_list are not the same.
+
+ * 'CL_INVALID_MEM_OBJECT' if src_buffer is not a valid buffer object and
+dst_image is not a valid image object.
+
+ * 'CL_INVALID_VALUE' if the 2D or 3D rectangular region specified by dst_origin
+and dst_origin + region refers to a region outside dst_origin, or if the region
+specified by src_offset and src_offset + src_cb refers to a region outside
+src_buffer.
+
+ * 'CL_INVALID_VALUE' if dst_image is a 2D image object and dst_origin.z is not
+equal to 0 or region.depth is not equal to 1.
+
+ * 'CL_INVALID_EVENT_WAIT_LIST' if event objects in event_wait_list are not
+valid events.
+
+ * 'CL_MEM_OBJECT_ALLOCATION_FAILURE' if there is a failure to allocate memory
+for data store associated with src_buffer or dst_image.
+
+ * 'CL_OUT_OF_HOST_MEMORY' if there is a failure to allocate resources required
+by the OpenCL implementation on the host.
+
+-}
+clEnqueueCopyBufferToImage :: Integral a
+ => CLCommandQueue -- ^ The OpenCL context
+ -- associated with
+ -- command_queue, src_image, and
+ -- dst_buffer must be the same.
+ -> CLMem -- ^ src. A valid buffer object.
+ -> CLMem -- ^ dst. A valid image object.
+ -> a -- ^ The offset where to begin copying data
+ -- from src_buffer.
+ -> (a,a,a) -- ^ The (x, y, z) offset in pixels
+ -- where to begin copying data to
+ -- dst_image. If dst_image is a 2D
+ -- image object, the z value given by
+ -- must be 0.
+ -> (a,a,a) -- ^ Defines the (width, height, depth)
+ -- in pixels of the 2D or 3D rectangle
+ -- to copy. If dst_image is a 2D image
+ -- object, the depth value given by
+ -- must be 1.
+ -> [CLEvent] -- ^ Specify events that need to
+ -- complete before this particular
+ -- command can be executed. If
+ -- event_wait_list is empty, then
+ -- this particular command does not
+ -- wait on any event to complete. The
+ -- events specified in
+ -- event_wait_list act as
+ -- synchronization points. The
+ -- context associated with events in
+ -- event_wait_list and command_queue
+ -- must be the same.
+ -> IO CLEvent
+clEnqueueCopyBufferToImage cq src dst offset (dst_orix,dst_oriy,dst_oriz) (regx,regy,regz) xs =
+ withArray (fmap fromIntegral [dst_orix,dst_oriy,dst_oriz]) $ \pdst_ori ->
+ withArray (fmap fromIntegral [regx,regy,regz]) $ \preg ->
+ clEnqueue (raw_clEnqueueCopyBufferToImage cq src dst (fromIntegral offset) pdst_ori preg) xs
+
+{-| Enqueues a command to map a region of the buffer object given by buffer into
+the host address space and returns a pointer to this mapped region.
+
+If blocking_map is 'True', 'clEnqueueMapBuffer' does not return until the
+specified region in buffer can be mapped.
+
+If blocking_map is 'False' i.e. map operation is non-blocking, the pointer to
+the mapped region returned by 'clEnqueueMapBuffer' cannot be used until the map
+command has completed. The event argument returns an event object which can be
+used to query the execution status of the map command. When the map command is
+completed, the application can access the contents of the mapped region using
+the pointer returned by 'clEnqueueMapBuffer'.
+
+Returns an event object that identifies this particular copy command and can be
+used toquery or queue a wait for this particular command to complete. event can
+be NULL in which case it will not be possible for the application to query the
+status of this command or queue a wait for this command to complete.
+
+The contents of the regions of a memory object mapped for writing
+(i.e. 'CL_MAP_WRITE' is set in map_flags argument to 'clEnqueueMapBuffer' or
+'clEnqueueMapImage') are considered to be undefined until this region is
+unmapped. Reads and writes by a kernel executing on a device to a memory
+region(s) mapped for writing are undefined.
+
+Multiple command-queues can map a region or overlapping regions of a memory
+object for reading (i.e. map_flags = 'CL_MAP_READ'). The contents of the regions
+of a memory object mapped for reading can also be read by kernels executing on a
+device(s). The behavior of writes by a kernel executing on a device to a mapped
+region of a memory object is undefined. Mapping (and unmapping) overlapped
+regions of a buffer or image memory object for writing is undefined.
+
+The behavior of OpenCL function calls that enqueue commands that write or copy
+to regions of a memory object that are mapped is undefined.
+
+'clEnqueueMapBuffer' will return a pointer to the mapped region if the function
+is executed successfully. A nullPtr pointer is returned otherwise with one of
+the following exception:
+
+ * 'CL_INVALID_COMMAND_QUEUE' if command_queue is not a valid command-queue.
+
+ * 'CL_INVALID_CONTEXT' if the context associated with command_queue, src_image
+and dst_buffer are not the same or if the context associated with command_queue
+and events in event_wait_list are not the same.
+
+ * 'CL_INVALID_MEM_OBJECT' if buffer is not a valid buffer object.
+
+ * 'CL_INVALID_VALUE' if region being mapped given by (offset, cb) is out of
+bounds or if values specified in map_flags are not valid
+
+ * 'CL_INVALID_EVENT_WAIT_LIST' if event objects in event_wait_list are not
+valid events.
+
+ * 'CL_MAP_FAILURE' if there is a failure to map the requested region into the
+host address space. This error cannot occur for buffer objects created with
+'CL_MEM_USE_HOST_PTR' or 'CL_MEM_ALLOC_HOST_PTR'.
+
+ * 'CL_MEM_OBJECT_ALLOCATION_FAILURE' if there is a failure to allocate memory
+for data store associated with buffer.
+
+ * 'CL_OUT_OF_HOST_MEMORY' if there is a failure to allocate resources required
+by the OpenCL implementation on the host.
+
+The pointer returned maps a region starting at offset and is atleast cb bytes in
+size. The result of a memory access outside this region is undefined.
+
+-}
+clEnqueueMapBuffer :: Integral a => CLCommandQueue
+ -> CLMem -- ^ A valid buffer object. The OpenCL context
+ -- associated with command_queue and buffer must
+ -- be the same.
+ -> Bool -- ^ Indicates if the map operation is blocking or
+ -- non-blocking.
+ -> [CLMapFlag] -- ^ Is a list and can be set to
+ -- 'CL_MAP_READ' to indicate that the
+ -- region specified by (offset, cb) in the
+ -- buffer object is being mapped for
+ -- reading, and/or 'CL_MAP_WRITE' to
+ -- indicate that the region specified by
+ -- (offset, cb) in the buffer object is
+ -- being mapped for writing.
+ -> a -- ^ The offset in bytes of the region in the buffer
+ -- object that is being mapped.
+ -> a -- ^ The size in bytes of the region in the buffer
+ -- object that is being mapped.
+ -> [CLEvent] -- ^ Specify events that need to complete
+ -- before this particular command can be
+ -- executed. If event_wait_list is empty,
+ -- then this particular command does not wait
+ -- on any event to complete. The events
+ -- specified in event_wait_list act as
+ -- synchronization points. The context
+ -- associated with events in event_wait_list
+ -- and command_queue must be the same.
+
+ -> IO (CLEvent, Ptr ())
+clEnqueueMapBuffer cq mem check xs offset cb [] =
+ alloca $ \pevent -> do
+ val <- wrapPError $ \perr -> raw_clEnqueueMapBuffer cq mem (fromBool check) flags (fromIntegral offset) (fromIntegral cb) 0 nullPtr pevent perr
+ event <- peek pevent
+ return (event, val)
+
+ where
+ flags = bitmaskFromFlags xs
+clEnqueueMapBuffer cq mem check xs offset cb events =
+ allocaArray nevents $ \pevents -> do
+ pokeArray pevents events
+ alloca $ \pevent -> do
+ val <- wrapPError $ \perr -> raw_clEnqueueMapBuffer cq mem (fromBool check) flags (fromIntegral offset) (fromIntegral cb) cnevents pevents pevent perr
+ event <- peek pevent
+ return (event, val)
+ where
+ flags = bitmaskFromFlags xs
+ nevents = length events
+ cnevents = fromIntegral nevents
+
+{-| Enqueues a command to map a region of an image object into the host address
+space and returns a pointer to this mapped region.
+
+If blocking_map is 'False' i.e. map operation is non-blocking, the pointer to
+the mapped region returned by 'clEnqueueMapImage' cannot be used until the map
+command has completed. The event argument returns an event object which can be
+used to query the execution status of the map command. When the map command is
+completed, the application can access the contents of the mapped region using
+the pointer returned by 'clEnqueueMapImage'.
+
+Returns an event object that identifies this particular copy command and can be
+used to query or queue a wait for this particular command to complete. event can
+be NULL in which case it will not be possible for the application to query the
+status of this command or queue a wait for this command to complete.
+
+If the buffer or image object is created with 'CL_MEM_USE_HOST_PTR' set in
+mem_flags, the following will be true:
+
+* The host_ptr specified in 'clCreateBuffer', 'clCreateImage2D', or
+'clCreateImage3D' is guaranteed to contain the latest bits in the region being
+mapped when the 'clEnqueueMapBuffer' or 'clEnqueueMapImage' command has
+completed.
+
+ * The pointer value returned by 'clEnqueueMapBuffer' or 'clEnqueueMapImage'
+will be derived from the host_ptr specified when the buffer or image object is
+created.
+
+The contents of the regions of a memory object mapped for writing
+(i.e. 'CL_MAP_WRITE' is set in map_flags argument to 'clEnqueueMapBuffer' or
+'clEnqueueMapImage') are considered to be undefined until this region is
+unmapped. Reads and writes by a kernel executing on a device to a memory
+region(s) mapped for writing are undefined.
+
+Multiple command-queues can map a region or overlapping regions of a memory
+object for reading (i.e. map_flags = 'CL_MAP_READ'). The contents of the regions
+of a memory object mapped for reading can also be read by kernels executing on a
+device(s). The behavior of writes by a kernel executing on a device to a mapped
+region of a memory object is undefined. Mapping (and unmapping) overlapped
+regions of a buffer or image memory object for writing is undefined.
+
+The behavior of OpenCL function calls that enqueue commands that write or copy
+to regions of a memory object that are mapped is undefined.
+
+'clEnqueueMapImage' will return a pointer to the mapped region if the
+function is executed successfully also the scan-line (row) pitch in bytes for
+the mapped region and the size in bytes of each 2D slice for the mapped
+region. For a 2D image, zero is returned as slice pitch. A nullPtr pointer is
+returned otherwise with one of the following exception:
+
+ * 'CL_INVALID_COMMAND_QUEUE' if command_queue is not a valid command-queue.
+
+ * 'CL_INVALID_CONTEXT' if the context associated with command_queue and image
+are not the same or if the context associated with command_queue and events in
+event_wait_list are not the same.
+
+ * 'CL_INVALID_MEM_OBJECT' if image is not a valid image object.
+
+ * 'CL_INVALID_VALUE' if region being mapped given by (origin, origin+region) is
+out of bounds or if values specified in map_flags are not valid.
+
+ * 'CL_INVALID_VALUE' if image is a 2D image object and z is not equal to 0 or
+depth is not equal to 1.
+
+ * 'CL_INVALID_EVENT_WAIT_LIST' if event objects in event_wait_list are not
+valid events.
+
+ * 'CL_MAP_FAILURE' if there is a failure to map the requested region into the
+host address space. This error cannot occur for image objects created with
+'CL_MEM_USE_HOST_PTR' or 'CL_MEM_ALLOC_HOST_PTR'.
+
+ * 'CL_MEM_OBJECT_ALLOCATION_FAILURE' if there is a failure to allocate memory
+for data store associated with image.
+
+ * 'CL_OUT_OF_HOST_MEMORY' if there is a failure to allocate resources required
+by the OpenCL implementation on the host.
+
+The pointer returned maps a 2D or 3D region starting at origin and is atleast
+(image_row_pitch * y + x) pixels in size for a 2D image, and is atleast
+(image_slice_pitch * z] + image_row_pitch * y + x) pixels in size for a 3D
+image. The result of a memory access outside this region is undefined.
+
+-}
+clEnqueueMapImage :: Integral a => CLCommandQueue
+ -> CLMem -- ^ A valid image object. The OpenCL context
+ -- associated with command_queue and image must be
+ -- the same.
+ -> Bool -- ^ Indicates if the map operation is blocking or
+ -- non-blocking. If blocking_map is 'True',
+ -- 'clEnqueueMapImage' does not return until the
+ -- specified region in image can be mapped.
+ -> [CLMapFlag] -- ^ Is a bit-field and can be set to
+ -- 'CL_MAP_READ' to indicate that the region
+ -- specified by (origin, region) in the
+ -- image object is being mapped for reading,
+ -- and/or 'CL_MAP_WRITE' to indicate that the
+ -- region specified by (origin, region) in
+ -- the image object is being mapped for
+ -- writing.
+ -> (a,a,a) -- ^ Define the (x, y, z) offset in pixels of
+ -- the 2D or 3D rectangle region that is to be
+ -- mapped. If image is a 2D image object, the z
+ -- value given must be 0.
+ -> (a,a,a) -- ^ Define the (width, height, depth) in pixels
+ -- of the 2D or 3D rectangle region that is to
+ -- be mapped. If image is a 2D image object, the
+ -- depth value given must be 1.
+ -> [CLEvent] -- ^ Specify events that need to complete
+ -- before 'clEnqueueMapImage' can be
+ -- executed. If event_wait_list is empty, then
+ -- 'clEnqueueMapImage' does not wait on any
+ -- event to complete. The events specified in
+ -- event_wait_list act as synchronization
+ -- points. The context associated with events
+ -- in event_wait_list and command_queue must
+ -- be the same.
+ -> IO (CLEvent, (Ptr (), CSize, CSize))
+clEnqueueMapImage cq mem check xs (orix,oriy,oriz) (regx,regy,regz) [] =
+ alloca $ \ppitch ->
+ alloca $ \pslice ->
+ withArray (fmap fromIntegral [orix,oriy,oriz]) $ \pori ->
+ withArray (fmap fromIntegral [regx,regy,regz]) $ \preg ->
+ alloca $ \pevent -> do
+ val <- wrapPError $ \perr -> raw_clEnqueueMapImage cq mem (fromBool check) flags pori preg ppitch pslice 0 nullPtr pevent perr
+ event <- peek pevent
+ pitch <- peek ppitch
+ slice <- peek pslice
+ return (event, (val, pitch, slice))
+
+ where
+ flags = bitmaskFromFlags xs
+clEnqueueMapImage cq mem check xs (orix,oriy,oriz) (regx,regy,regz) events =
+ alloca $ \ppitch ->
+ alloca $ \pslice ->
+ withArray (fmap fromIntegral [orix,oriy,oriz]) $ \pori ->
+ withArray (fmap fromIntegral [regx,regy,regz]) $ \preg ->
+ allocaArray nevents $ \pevents -> do
+ pokeArray pevents events
+ alloca $ \pevent -> do
+ val <- wrapPError $ \perr -> raw_clEnqueueMapImage cq mem (fromBool check) flags pori preg ppitch pslice cnevents pevents pevent perr
+ event <- peek pevent
+ pitch <- peek ppitch
+ slice <- peek pslice
+ return (event, (val, pitch, slice))
+
+ where
+ flags = bitmaskFromFlags xs
+ nevents = length events
+ cnevents = fromIntegral nevents
+
+{-| Enqueues a command to unmap a previously mapped region of a memory object.
+
+Returns an event object that identifies this particular copy command and can be
+used to query or queue a wait for this particular command to complete. event can
+be NULL in which case it will not be possible for the application to query the
+status of this command or queue a wait for this command to
+complete. 'clEnqueueBarrier' can be used instead.
+
+Reads or writes from the host using the pointer returned by 'clEnqueueMapBuffer'
+or 'clEnqueueMapImage' are considered to be complete.
+
+'clEnqueueMapBuffer' and 'clEnqueueMapImage' increments the mapped count of the
+memory object. The initial mapped count value of a memory object is
+zero. Multiple calls to 'clEnqueueMapBuffer' or 'clEnqueueMapImage' on the same
+memory object will increment this mapped count by appropriate number of
+calls. 'clEnqueueUnmapMemObject' decrements the mapped count of the memory
+object.
+
+'clEnqueueMapBuffer' and 'clEnqueueMapImage' act as synchronization points for a
+region of the memory object being mapped.
+
+'clEnqueueUnmapMemObject' returns the 'CLEvent' if the function is executed
+successfully. It can throw the following 'CLError' exceptions:
+
+ * CL_INVALID_COMMAND_QUEUE if command_queue is not a valid command-queue.
+
+ * CL_INVALID_MEM_OBJECT if memobj is not a valid memory object.
+
+ * CL_INVALID_VALUE if mapped_ptr is not a valid pointer returned by
+'clEnqueueMapBuffer' or 'clEnqueueMapImage' for memobj.
+
+ * CL_INVALID_EVENT_WAIT_LIST if event objects in event_wait_list are not valid
+events.
+
+ * CL_OUT_OF_HOST_MEMORY if there is a failure to allocate resources required by
+the OpenCL implementation on the host.
+
+ * CL_INVALID_CONTEXT if the context associated with command_queue and memobj
+are not the same or if the context associated with command_queue and events in
+event_wait_list are not the same.
+-}
+clEnqueueUnmapMemObject :: CLCommandQueue
+ -> CLMem -- ^ A valid memory object. The OpenCL
+ -- context associated with command_queue and
+ -- memobj must be the same.
+ -> Ptr () -- ^ The host address returned by a
+ -- previous call to 'clEnqueueMapBuffer' or
+ -- 'clEnqueueMapImage' for memobj.
+ -> [CLEvent] -- ^ Specify events that need to
+ -- complete before
+ -- 'clEnqueueUnmapMemObject' can be
+ -- executed. If event_wait_list is
+ -- empty, then 'clEnqueueUnmapMemObject'
+ -- does not wait on any event to
+ -- complete. The events specified in
+ -- event_wait_list act as
+ -- synchronization points. The context
+ -- associated with events in
+ -- event_wait_list and command_queue
+ -- must be the same.
+
+ -> IO CLEvent
+clEnqueueUnmapMemObject cq mem pp = clEnqueue (raw_clEnqueueUnmapMemObject cq mem pp)
+
-- -----------------------------------------------------------------------------
{-| Enqueues a command to execute a kernel on a device. Each work-item is
uniquely identified by a global identifier. The global ID, which can be read
View
461 src/Control/Parallel/OpenCL/Memory.chs
@@ -33,11 +33,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
module Control.Parallel.OpenCL.Memory(
-- * Types
CLMem, CLSampler, CLMemFlag(..), CLMemObjectType(..), CLAddressingMode(..),
- CLFilterMode(..),
+ CLFilterMode(..), CLImageFormat(..),
-- * Memory Functions
clCreateBuffer, clRetainMemObject, clReleaseMemObject, clGetMemType,
clGetMemFlags, clGetMemSize, clGetMemHostPtr, clGetMemMapCount,
clGetMemReferenceCount, clGetMemContext,
+ -- * Image Functions
+ clCreateImage2D, clCreateImage3D, clGetSupportedImageFormats,
+ clGetImageFormat, clGetImageElementSize, clGetImageRowPitch,
+ clGetImageSlicePitch, clGetImageWidth, clGetImageHeight, clGetImageDepth,
-- * Sampler Functions
clCreateSampler, clRetainSampler, clReleaseSampler, clGetSamplerReferenceCount,
clGetSamplerContext, clGetSamplerAddressingMode, clGetSamplerFilterMode,
@@ -47,13 +51,14 @@ module Control.Parallel.OpenCL.Memory(
-- -----------------------------------------------------------------------------
import Foreign
import Foreign.C.Types
+import Control.Applicative( (<$>), (<*>) )
import Control.Parallel.OpenCL.Types(
CLMem, CLContext, CLSampler, CLint, CLuint, CLbool, CLMemFlags_,
- CLMemInfo_, CLAddressingMode_, CLFilterMode_, CLSamplerInfo_,
+ CLMemInfo_, CLAddressingMode_, CLFilterMode_, CLSamplerInfo_, CLImageInfo_,
CLAddressingMode(..), CLFilterMode(..), CLMemFlag(..), CLMemObjectType_,
CLMemObjectType(..),
- wrapPError, wrapCheckSuccess, wrapGetInfo, getEnumCL, bitmaskFromFlags,
- bitmaskToMemFlags, getCLValue )
+ wrapPError, wrapCheckSuccess, wrapGetInfo, whenSuccess, getEnumCL,
+ bitmaskFromFlags, bitmaskToMemFlags, getCLValue )
#ifdef __APPLE__
#include <cl.h>
@@ -64,23 +69,23 @@ import Control.Parallel.OpenCL.Types(
-- -----------------------------------------------------------------------------
foreign import CALLCONV "clCreateBuffer" raw_clCreateBuffer ::
CLContext -> CLMemFlags_ -> CSize -> Ptr () -> Ptr CLint -> IO CLMem
---foreign import CALLCONV "clCreateImage2D" raw_clCreateImage2D ::
--- CLContext -> CLMemFlags_ -> CLImageFormat_p -> CSize -> CSize -> CSize
--- -> Ptr () -> Ptr CLint -> IO CLMem
---foreign import CALLCONV "clCreateImage3D" raw_clCreateImage3D ::
--- CLContext -> CLMemFlags_-> CLImageFormat_p -> CSize -> CSize -> CSize -> CSize
--- -> CSize -> Ptr () -> Ptr CLint -> IO CLMem
+foreign import CALLCONV "clCreateImage2D" raw_clCreateImage2D ::
+ CLContext -> CLMemFlags_ -> CLImageFormat_p -> CSize -> CSize -> CSize
+ -> Ptr () -> Ptr CLint -> IO CLMem
+foreign import CALLCONV "clCreateImage3D" raw_clCreateImage3D ::
+ CLContext -> CLMemFlags_-> CLImageFormat_p -> CSize -> CSize -> CSize -> CSize
+ -> CSize -> Ptr () -> Ptr CLint -> IO CLMem
foreign import CALLCONV "clRetainMemObject" raw_clRetainMemObject ::
CLMem -> IO CLint
foreign import CALLCONV "clReleaseMemObject" raw_clReleaseMemObject ::
CLMem -> IO CLint
---foreign import CALLCONV "clGetSupportedImageFormats" raw_clGetSupportedImageFormats ::
--- CLContext -> CLMemFlags_ -> CLMemObjectType_ -> CLuint -> CLImageFormat_p
--- -> Ptr CLuint -> IO CLint
+foreign import CALLCONV "clGetSupportedImageFormats" raw_clGetSupportedImageFormats ::
+ CLContext -> CLMemFlags_ -> CLMemObjectType_ -> CLuint -> CLImageFormat_p
+ -> Ptr CLuint -> IO CLint
foreign import CALLCONV "clGetMemObjectInfo" raw_clGetMemObjectInfo ::
CLMem -> CLMemInfo_ -> CSize -> Ptr () -> Ptr CSize -> IO CLint
---foreign import CALLCONV "clGetImageInfo" raw_clGetImageInfo ::
--- CLMem -> CLImageInfo_ -> CSize -> Ptr () -> Ptr CSize -> IO CLint
+foreign import CALLCONV "clGetImageInfo" raw_clGetImageInfo ::
+ CLMem -> CLImageInfo_ -> CSize -> Ptr () -> Ptr CSize -> IO CLint
foreign import CALLCONV "clCreateSampler" raw_clCreateSampler ::
CLContext -> CLbool -> CLAddressingMode_ -> CLFilterMode_ -> Ptr CLint -> IO CLSampler
foreign import CALLCONV "clRetainSampler" raw_clRetainSampler ::
@@ -133,6 +138,432 @@ clRetainMemObject mem = wrapCheckSuccess $ raw_clRetainMemObject mem
clReleaseMemObject :: CLMem -> IO Bool
clReleaseMemObject mem = wrapCheckSuccess $ raw_clReleaseMemObject mem
+-- -----------------------------------------------------------------------------
+#c
+enum CLChannelOrder {
+ cL_R=CL_R,
+ cL_A=CL_A,
+ cL_INTENSITY=CL_INTENSITY,
+ cL_LUMINANCE=CL_LUMINANCE,
+ cL_RG=CL_RG,
+ cL_RA=CL_RA,
+ cL_RGB=CL_RGB,
+ cL_RGBA=CL_RGBA,
+ cL_ARGB=CL_ARGB,
+ cL_BGRA=CL_BGRA,
+ };
+#endc
+{-| Specifies the number of channels and the channel layout i.e. the memory
+layout in which channels are stored in the image. Valid values are described in
+the table below.
+
+ * 'CL_R', 'CL_A'.
+
+ * 'CL_INTENSITY', This format can only be used if channel data type =
+'CL_UNORM_INT8', 'CL_UNORM_INT16', 'CL_SNORM_INT8', 'CL_SNORM_INT16',
+'CL_HALF_FLOAT', or 'CL_FLOAT'.
+
+ * 'CL_LUMINANCE', This format can only be used if channel data type =
+'CL_UNORM_INT8', 'CL_UNORM_INT16', 'CL_SNORM_INT8', 'CL_SNORM_INT16',
+'CL_HALF_FLOAT', or 'CL_FLOAT'.
+
+ * 'CL_RG', 'CL_RA'.
+
+ * 'CL_RGB', This format can only be used if channel data type =
+'CL_UNORM_SHORT_565', 'CL_UNORM_SHORT_555' or 'CL_UNORM_INT101010'.
+
+ * 'CL_RGBA'.
+
+ * 'CL_ARGB', 'CL_BGRA'. This format can only be used if channel data type =
+'CL_UNORM_INT8', 'CL_SNORM_INT8', 'CL_SIGNED_INT8' or 'CL_UNSIGNED_INT8'.
+-}
+{#enum CLChannelOrder {upcaseFirstLetter} deriving(Show)#}
+
+#c
+enum CLChannelType {
+ cL_SNORM_INT8=CL_SNORM_INT8,
+ cL_SNORM_INT16=CL_SNORM_INT16,
+ cL_UNORM_INT8=CL_UNORM_INT8,
+ cL_UNORM_INT16=CL_UNORM_INT16,
+ cL_UNORM_SHORT_565=CL_UNORM_SHORT_565,
+ cL_UNORM_SHORT_555=CL_UNORM_SHORT_555,
+ cL_UNORM_INT_101010=CL_UNORM_INT_101010,
+ cL_SIGNED_INT8=CL_SIGNED_INT8,
+ cL_SIGNED_INT16=CL_SIGNED_INT16,
+ cL_SIGNED_INT32=CL_SIGNED_INT32,
+ cL_UNSIGNED_INT8=CL_UNSIGNED_INT8,
+ cL_UNSIGNED_INT16=CL_UNSIGNED_INT16,
+ cL_UNSIGNED_INT32=CL_UNSIGNED_INT32,
+ cL_HALF_FLOAT=CL_HALF_FLOAT,
+ cL_FLOAT=CL_FLOAT,
+ };
+#endc
+{-| Describes the size of the channel data type. The number of bits per element
+determined by the image_channel_data_type and image_channel_order must be a
+power of two. The list of supported values is described in the table below.
+
+ * 'CL_SNORM_INT8', Each channel component is a normalized signed 8-bit integer
+value.
+
+ * 'CL_SNORM_INT16', Each channel component is a normalized signed 16-bit
+integer value.
+
+ * 'CL_UNORM_INT8', Each channel component is a normalized unsigned 8-bit
+integer value.
+
+ * 'CL_UNORM_INT16', Each channel component is a normalized unsigned 16-bit
+integer value.
+
+ * 'CL_UNORM_SHORT_565', Represents a normalized 5-6-5 3-channel RGB image. The
+channel order must be 'CL_RGB'.
+
+ * 'CL_UNORM_SHORT_555', Represents a normalized x-5-5-5 4-channel xRGB
+image. The channel order must be 'CL_RGB'.
+
+ * 'CL_UNORM_INT_101010', Represents a normalized x-10-10-10 4-channel xRGB
+image. The channel order must be 'CL_RGB'.
+
+ * 'CL_SIGNED_INT8', Each channel component is an unnormalized signed 8-bit
+integer value.
+
+ * 'CL_SIGNED_INT16', Each channel component is an unnormalized signed 16-bit
+integer value.
+
+ * 'CL_SIGNED_INT32', Each channel component is an unnormalized signed 32-bit
+integer value.
+
+ * 'CL_UNSIGNED_INT8', Each channel component is an unnormalized unsigned 8-bit
+integer value.
+
+ * 'CL_UNSIGNED_INT16', Each channel component is an unnormalized unsigned
+16-bit integer value.
+
+ * 'CL_UNSIGNED_INT32', Each channel component is an unnormalized unsigned
+32-bit integer value.
+
+ * 'CL_HALF_FLOAT', Each channel component is a 16-bit half-float value.
+
+ * 'CL_FLOAT', Each channel component is a single precision floating-point
+value.
+-}
+{#enum CLChannelType {upcaseFirstLetter} deriving(Show)#}
+
+data CLImageFormat = CLImageFormat
+ { image_channel_order :: ! CLChannelOrder
+ , image_channel_data_type :: ! CLChannelType }
+ deriving( Show )
+{#pointer *cl_image_format as CLImageFormat_p -> CLImageFormat#}
+instance Storable CLImageFormat where
+ alignment _ = alignment (undefined :: CDouble)
+ sizeOf _ = {#sizeof cl_image_format #}
+ peek p =
+ CLImageFormat <$> fmap getEnumCL ({#get cl_image_format.image_channel_order #} p)
+ <*> fmap getEnumCL ({#get cl_image_format.image_channel_data_type #} p)
+ poke p (CLImageFormat a b) = do
+ {#set cl_image_format.image_channel_order #} p (getCLValue a)
+ {#set cl_image_format.image_channel_data_type #} p (getCLValue b)
+
+-- -----------------------------------------------------------------------------
+{-| Creates a 2D image object.
+
+'clCreateImage2D' returns a valid non-zero image object created if the image
+object is created successfully. Otherwise, it throws one of the following
+'CLError' exceptions:
+
+ * 'CL_INVALID_CONTEXT' if context is not a valid context.
+
+ * 'CL_INVALID_VALUE' if values specified in flags are not valid.
+
+ * 'CL_INVALID_IMAGE_FORMAT_DESCRIPTOR' if values specified in image_format are
+not valid.
+
+ * 'CL_INVALID_IMAGE_SIZE' if image_width or image_height are 0 or if they
+exceed values specified in 'CL_DEVICE_IMAGE2D_MAX_WIDTH' or
+'CL_DEVICE_IMAGE2D_MAX_HEIGHT' respectively for all devices in context or if
+values specified by image_row_pitch do not follow rules described in the
+argument description above.
+
+ * 'CL_INVALID_HOST_PTR' if host_ptr is 'nullPtr' and 'CL_MEM_USE_HOST_PTR' or
+'CL_MEM_COPY_HOST_PTR' are set in flags or if host_ptr is not 'nullPtr' but
+'CL_MEM_COPY_HOST_PTR' or 'CL_MEM_USE_HOST_PTR' are not set in flags.
+
+ * 'CL_IMAGE_FORMAT_NOT_SUPPORTED' if the image_format is not supported.
+
+ * 'CL_MEM_OBJECT_ALLOCATION_FAILURE' if there is a failure to allocate memory
+for image object.
+
+ * 'CL_INVALID_OPERATION' if there are no devices in context that support images
+(i.e. 'CL_DEVICE_IMAGE_SUPPORT' (specified in the table of OpenCL Device Queries
+for 'clGetDeviceInfo') is 'False').
+
+ * 'CL_OUT_OF_HOST_MEMORY' if there is a failure to allocate resources required
+by the OpenCL implementation on the host.
+
+-}
+
+clCreateImage2D :: Integral a => CLContext -- ^ A valid OpenCL context on which
+ -- the image object is to be created.
+ -> [CLMemFlag] -- ^ A list of flags that is used to specify
+ -- allocation and usage information about the
+ -- image memory object being created.
+ -> CLImageFormat -- ^ Structure that describes format
+ -- properties of the image to be allocated.
+ -> a -- ^ The width of the image in pixels. It must be values
+ -- greater than or equal to 1.
+ -> a -- ^ The height of the image in pixels. It must be
+ -- values greater than or equal to 1.
+ -> a -- ^ The scan-line pitch in bytes. This must be 0 if
+ -- host_ptr is 'nullPtr' and can be either 0 or greater
+ -- than or equal to image_width * size of element in
+ -- bytes if host_ptr is not 'nullPtr'. If host_ptr is
+ -- not 'nullPtr' and image_row_pitch is equal to 0,
+ -- image_row_pitch is calculated as image_width * size
+ -- of element in bytes. If image_row_pitch is not 0, it
+ -- must be a multiple of the image element size in
+ -- bytes.
+ -> Ptr () -- ^ A pointer to the image data that may already
+ -- be allocated by the application. The size of the
+ -- buffer that host_ptr points to must be greater
+ -- than or equal to image_row_pitch *
+ -- image_height. The size of each element in bytes
+ -- must be a power of 2. The image data specified
+ -- by host_ptr is stored as a linear sequence of
+ -- adjacent scanlines. Each scanline is stored as a
+ -- linear sequence of image elements.
+ -> IO CLMem
+clCreateImage2D ctx xs fmt iw ih irp ptr = wrapPError $ \perr -> with fmt $ \pfmt -> do
+ raw_clCreateImage2D ctx flags pfmt ciw cih cirp ptr perr
+ where
+ flags = bitmaskFromFlags xs
+ ciw = fromIntegral iw
+ cih = fromIntegral ih
+ cirp = fromIntegral irp
+
+{-| Creates a 3D image object.
+
+'clCreateImage3D' returns a valid non-zero image object created if the image
+object is created successfully. Otherwise, it throws one of the following
+'CLError' exceptions:
+
+ * 'CL_INVALID_CONTEXT' if context is not a valid context.
+
+ * 'CL_INVALID_VALUE' if values specified in flags are not valid.
+
+ * 'CL_INVALID_IMAGE_FORMAT_DESCRIPTOR' if values specified in image_format are
+not valid.
+
+ * 'CL_INVALID_IMAGE_SIZE' if image_width, image_height are 0 or if image_depth
+less than or equal to 1 or if they exceed values specified in
+'CL_DEVICE_IMAGE3D_MAX_WIDTH', CL_DEVICE_IMAGE3D_MAX_HEIGHT' or
+'CL_DEVICE_IMAGE3D_MAX_DEPTH' respectively for all devices in context or if
+values specified by image_row_pitch and image_slice_pitch do not follow rules
+described in the argument description above.
+
+ * 'CL_INVALID_HOST_PTR' if host_ptr is 'nullPtr' and 'CL_MEM_USE_HOST_PTR' or
+'CL_MEM_COPY_HOST_PTR' are set in flags or if host_ptr is not 'nullPtr' but
+'CL_MEM_COPY_HOST_PTR' or 'CL_MEM_USE_HOST_PTR' are not set in flags.
+
+ * 'CL_IMAGE_FORMAT_NOT_SUPPORTED' if the image_format is not supported.
+
+ * 'CL_MEM_OBJECT_ALLOCATION_FAILURE' if there is a failure to allocate memory
+for image object.
+
+ * 'CL_INVALID_OPERATION' if there are no devices in context that support images
+(i.e. 'CL_DEVICE_IMAGE_SUPPORT' (specified in the table of OpenCL Device Queries
+for clGetDeviceInfo) is 'False').
+
+ * 'CL_OUT_OF_HOST_MEMORY' if there is a failure to allocate resources required
+by the OpenCL implementation on the host.
+
+-}
+clCreateImage3D :: Integral a => CLContext -- ^ A valid OpenCL context on which
+ -- the image object is to be created.
+ -> [CLMemFlag] -- ^ A list of flags that is used to specify
+ -- allocation and usage information about the
+ -- image memory object being created.
+ -> CLImageFormat -- ^ Structure that describes format
+ -- properties of the image to be allocated.
+ -> a -- ^ The width of the image in pixels. It must be values
+ -- greater than or equal to 1.
+ -> a -- ^ The height of the image in pixels. It must be
+ -- values greater than or equal to 1.
+ -> a -- ^ The depth of the image in pixels. This must be a
+ -- value greater than 1.
+ -> a -- ^ The scan-line pitch in bytes. This must be 0 if
+ -- host_ptr is 'nullPtr' and can be either 0 or greater
+ -- than or equal to image_width * size of element in
+ -- bytes if host_ptr is not 'nullPtr'. If host_ptr is
+ -- not 'nullPtr' and image_row_pitch is equal to 0,
+ -- image_row_pitch is calculated as image_width * size
+ -- of element in bytes. If image_row_pitch is not 0, it
+ -- must be a multiple of the image element size in
+ -- bytes.
+ -> a -- ^ The size in bytes of each 2D slice in the 3D
+ -- image. This must be 0 if host_ptr is 'nullPtr' and
+ -- can be either 0 or greater than or equal to
+ -- image_row_pitch * image_height if host_ptr is not
+ -- 'nullPtr'. If host_ptr is not 'nullPtr' and
+ -- image_slice_pitch equal to 0, image_slice_pitch is
+ -- calculated as image_row_pitch * image_height. If
+ -- image_slice_pitch is not 0, it must be a multiple of
+ -- the image_row_pitch.
+ -> Ptr () -- ^ A pointer to the image data that may already
+ -- be allocated by the application. The size of the
+ -- buffer that host_ptr points to must be greater
+ -- than or equal to image_slice_pitch *
+ -- image_depth. The size of each element in bytes
+ -- must be a power of 2. The image data specified
+ -- by host_ptr is stored as a linear sequence of
+ -- adjacent 2D slices. Each 2D slice is a linear
+ -- sequence of adjacent scanlines. Each scanline is
+ -- a linear sequence of image elements.
+ -> IO CLMem
+clCreateImage3D ctx xs fmt iw ih idepth irp isp ptr = wrapPError $ \perr -> with fmt $ \pfmt -> do
+ raw_clCreateImage3D ctx flags pfmt ciw cih cid cirp cisp ptr perr
+ where
+ flags = bitmaskFromFlags xs
+ ciw = fromIntegral iw
+ cih = fromIntegral ih
+ cid = fromIntegral idepth
+ cirp = fromIntegral irp
+ cisp = fromIntegral isp
+
+getNumSupportedImageFormats :: CLContext -> [CLMemFlag] -> CLMemObjectType -> IO CLuint
+getNumSupportedImageFormats ctx xs mtype = alloca $ \(value_size :: Ptr CLuint) -> do
+ whenSuccess (raw_clGetSupportedImageFormats ctx flags (getCLValue mtype) 0 nullPtr value_size)
+ $ peek value_size
+ where
+ flags = bitmaskFromFlags xs
+
+{-| Get the list of image formats supported by an OpenCL
+implementation. 'clGetSupportedImageFormats' can be used to get the list of
+image formats supported by an OpenCL implementation when the following
+information about an image memory object is specified:
+
+ * Context
+ * Image type - 2D or 3D image
+ * Image object allocation information
+
+Throws 'CL_INVALID_CONTEXT' if context is not a valid context, throws
+'CL_INVALID_VALUE' if flags or image_type are not valid.
+
+-}
+clGetSupportedImageFormats :: CLContext -- ^ A valid OpenCL context on which the
+ -- image object(s) will be created.
+ -> [CLMemFlag] -- ^ A bit-field that is used to
+ -- specify allocation and usage
+ -- information about the image
+ -- memory object.
+ -> CLMemObjectType -- ^ Describes the image type
+ -- and must be either
+ -- 'CL_MEM_OBJECT_IMAGE2D' or
+ -- 'CL_MEM_OBJECT_IMAGE3D'.
+ -> IO [CLImageFormat]
+clGetSupportedImageFormats ctx xs mtype = do
+ num <- getNumSupportedImageFormats ctx xs mtype
+ allocaArray (fromIntegral num) $ \(buff :: Ptr CLImageFormat) -> do
+ whenSuccess (raw_clGetSupportedImageFormats ctx flags (getCLValue mtype) num (castPtr buff) nullPtr)
+ $ peekArray (fromIntegral num) buff
+ where
+ flags = bitmaskFromFlags xs
+
+-- -----------------------------------------------------------------------------
+#c
+enum CLImageInfo {
+ cL_IMAGE_FORMAT=CL_IMAGE_FORMAT,
+ cL_IMAGE_ELEMENT_SIZE=CL_IMAGE_ELEMENT_SIZE,
+ cL_IMAGE_ROW_PITCH=CL_IMAGE_ROW_PITCH,
+ cL_IMAGE_SLICE_PITCH=CL_IMAGE_SLICE_PITCH,
+ cL_IMAGE_WIDTH=CL_IMAGE_WIDTH,
+ cL_IMAGE_HEIGHT=CL_IMAGE_HEIGHT,
+ cL_IMAGE_DEPTH=CL_IMAGE_DEPTH,
+ };
+#endc
+{#enum CLImageInfo {upcaseFirstLetter} #}
+
+-- | Return image format descriptor specified when image is created with
+-- clCreateImage2D or clCreateImage3D.
+--
+-- This function execute OpenCL clGetImageInfo with 'CL_IMAGE_FORMAT'.
+clGetImageFormat :: CLMem -> IO CLImageFormat
+clGetImageFormat mem =
+ wrapGetInfo (\(dat :: Ptr CLImageFormat) ->
+ raw_clGetImageInfo mem infoid size (castPtr dat)) id
+ where
+ infoid = getCLValue CL_IMAGE_FORMAT
+ size = fromIntegral $ sizeOf (undefined :: CLImageFormat)
+
+-- | Return size of each element of the image memory object given by image. An
+-- element is made up of n channels. The value of n is given in 'CLImageFormat'
+-- descriptor.
+--
+-- This function execute OpenCL clGetImageInfo with 'CL_IMAGE_ELEMENT_SIZE'.
+clGetImageElementSize :: CLMem -> IO CSize
+clGetImageElementSize mem =
+ wrapGetInfo (\(dat :: Ptr CSize) ->
+ raw_clGetImageInfo mem infoid size (castPtr dat)) id
+ where
+ infoid = getCLValue CL_IMAGE_ELEMENT_SIZE
+ size = fromIntegral $ sizeOf (undefined :: CSize)
+
+-- | Return size in bytes of a row of elements of the image object given by
+-- image.
+--
+-- This function execute OpenCL clGetImageInfo with 'CL_IMAGE_ROW_PITCH'.
+clGetImageRowPitch :: CLMem -> IO CSize
+clGetImageRowPitch mem =
+ wrapGetInfo (\(dat :: Ptr CSize) ->
+ raw_clGetImageInfo mem infoid size (castPtr dat)) id
+ where
+ infoid = getCLValue CL_IMAGE_ROW_PITCH
+ size = fromIntegral $ sizeOf (undefined :: CSize)
+
+-- | Return size in bytes of a 2D slice for the 3D image object given by
+-- image. For a 2D image object this value will be 0.
+--
+-- This function execute OpenCL clGetImageInfo with 'CL_IMAGE_SLICE_PITCH'.
+clGetImageSlicePitch :: CLMem -> IO CSize
+clGetImageSlicePitch mem =
+ wrapGetInfo (\(dat :: Ptr CSize) ->
+ raw_clGetImageInfo mem infoid size (castPtr dat)) id
+ where
+ infoid = getCLValue CL_IMAGE_SLICE_PITCH
+ size = fromIntegral $ sizeOf (undefined :: CSize)
+
+-- | Return width of image in pixels.
+--
+-- This function execute OpenCL clGetImageInfo with 'CL_IMAGE_WIDTH'.
+clGetImageWidth :: CLMem -> IO CSize
+clGetImageWidth mem =
+ wrapGetInfo (\(dat :: Ptr CSize) ->
+ raw_clGetImageInfo mem infoid size (castPtr dat)) id
+ where
+ infoid = getCLValue CL_IMAGE_WIDTH
+ size = fromIntegral $ sizeOf (undefined :: CSize)
+
+-- | Return height of image in pixels.
+--
+-- This function execute OpenCL clGetImageInfo with 'CL_IMAGE_HEIGHT'.
+clGetImageHeight :: CLMem -> IO CSize
+clGetImageHeight mem =
+ wrapGetInfo (\(dat :: Ptr CSize) ->
+ raw_clGetImageInfo mem infoid size (castPtr dat)) id
+ where
+ infoid = getCLValue CL_IMAGE_HEIGHT
+ size = fromIntegral $ sizeOf (undefined :: CSize)
+
+-- | Return depth of the image in pixels. For a 2D image, depth equals 0.
+--
+-- This function execute OpenCL clGetImageInfo with 'CL_IMAGE_DEPTH'.
+clGetImageDepth :: CLMem -> IO CSize
+clGetImageDepth mem =
+ wrapGetInfo (\(dat :: Ptr CSize) ->
+ raw_clGetImageInfo mem infoid size (castPtr dat)) id
+ where
+ infoid = getCLValue CL_IMAGE_DEPTH
+ size = fromIntegral $ sizeOf (undefined :: CSize)
+
+-- -----------------------------------------------------------------------------
#c
enum CLMemInfo {
cL_MEM_TYPE=CL_MEM_TYPE,
View
18 src/Control/Parallel/OpenCL/Types.chs
@@ -36,7 +36,7 @@ module Control.Parallel.OpenCL.Types(
CLDeviceID, CLContext, CLCommandQueue, CLPlatformInfo_, CLDeviceType_,
CLDeviceInfo_, CLContextInfo_, CLContextProperty_, CLCommandQueueInfo_,
CLEventInfo_, CLProfilingInfo_, CLCommandType_, CLCommandQueueProperty_,
- CLMemFlags_, CLImageFormat_p, CLMemObjectType_, CLMemInfo_, CLImageInfo_,
+ CLMemFlags_, CLMemObjectType_, CLMemInfo_, CLImageInfo_, CLMapFlags_,
CLProgramInfo_, CLBuildStatus_,CLKernel, CLProgramBuildInfo_, CLKernelInfo_,
CLKernelWorkGroupInfo_, CLDeviceLocalMemType_, CLDeviceMemCacheType_,
CLSampler, CLFilterMode_, CLSamplerInfo_, CLAddressingMode_,
@@ -45,7 +45,7 @@ module Control.Parallel.OpenCL.Types(
CLDeviceExecCapability(..), CLDeviceLocalMemType(..), CLDeviceType(..),
CLCommandQueueProperty(..), CLCommandType(..), CLCommandExecutionStatus(..),
CLProfilingInfo(..), CLPlatformInfo(..), CLMemFlag(..), CLMemObjectType(..),
- CLBuildStatus(..), CLAddressingMode(..), CLFilterMode(..),
+ CLBuildStatus(..), CLAddressingMode(..), CLFilterMode(..), CLMapFlag(..),
-- * Functions
wrapPError, wrapCheckSuccess, wrapGetInfo, whenSuccess, getCLValue,
throwCLError, getEnumCL, bitmaskToFlags, getCommandExecutionStatus,
@@ -102,6 +102,7 @@ type CLMemFlags_ = {#type cl_mem_flags#}
type CLMemObjectType_ = {#type cl_mem_object_type#}
type CLMemInfo_ = {#type cl_mem_info#}
type CLImageInfo_ = {#type cl_image_info#}
+type CLMapFlags_ = {#type cl_map_flags#}
type CLProgramInfo_ = {#type cl_program_info#}
type CLProgramBuildInfo_ = {#type cl_program_build_info#}
type CLBuildStatus_ = {#type cl_build_status#}
@@ -111,11 +112,6 @@ type CLFilterMode_ = {#type cl_filter_mode#}
type CLSamplerInfo_ = {#type cl_sampler_info#}
type CLAddressingMode_ = {#type cl_addressing_mode#}
-{#pointer *cl_image_format as CLImageFormat_p#}
-
---type CLImageChannelOrder_ = {#type cl_channel_order#}
---type CLImageChannelDataType_ = {#type cl_channel_type#}
-
-- -----------------------------------------------------------------------------
#c
enum CLError {
@@ -621,6 +617,14 @@ initialize the contents of the cl_mem object allocated using host-accessible
{#enum CLMemFlag {upcaseFirstLetter} deriving( Show, Bounded, Eq, Ord ) #}
#c
+enum CLMapFlag {
+ cL_MAP_READ=CL_MAP_READ,
+ cL_MAP_WRITE=CL_MAP_WRITE
+ };
+#endc
+{#enum CLMapFlag {upcaseFirstLetter} deriving( Show, Bounded, Eq, Ord ) #}
+
+#c
enum CLMemObjectType {
cL_MEM_OBJECT_BUFFER=CL_MEM_OBJECT_BUFFER,
cL_MEM_OBJECT_IMAGE2D=CL_MEM_OBJECT_IMAGE2D,

0 comments on commit 6ba5d2e

Please sign in to comment.