Clover Git
OpenCL 1.1 software implementation

memobject.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Denis Steckelmacher <steckdenis@yahoo.fr>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *     * Redistributions of source code must retain the above copyright
00008  *       notice, this list of conditions and the following disclaimer.
00009  *     * Redistributions in binary form must reproduce the above copyright
00010  *       notice, this list of conditions and the following disclaimer in the
00011  *       documentation and/or other materials provided with the distribution.
00012  *     * Neither the name of the copyright holder nor the
00013  *       names of its contributors may be used to endorse or promote products
00014  *       derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00020  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00033 #ifndef __MEMOBJECT_H__
00034 #define __MEMOBJECT_H__
00035 
00036 #include "object.h"
00037 
00038 #include <CL/cl.h>
00039 
00040 namespace Coal
00041 {
00042 
00043 class DeviceBuffer;
00044 class Context;
00045 class DeviceInterface;
00046 
00050 class MemObject : public Object
00051 {
00052     public:
00056         enum Type
00057         {
00058             Buffer,
00059             SubBuffer,
00060             Image2D,
00061             Image3D
00062         };
00063 
00074         MemObject(Context *ctx, cl_mem_flags flags, void *host_ptr,
00075                   cl_int *errcode_ret);
00076         ~MemObject();
00077 
00091         virtual cl_int init();
00092         virtual bool allocate(DeviceInterface *device); 
00093         virtual size_t size() const = 0;                
00094         virtual Type type() const = 0;                  
00096         cl_mem_flags flags() const;                     
00097         void *host_ptr() const;                         
00098         DeviceBuffer *deviceBuffer(DeviceInterface *device) const; 
00100         void deviceAllocated(DeviceBuffer *buffer);     
00114         void setDestructorCallback(void (CL_CALLBACK *pfn_notify)(cl_mem memobj,
00115                                                              void *user_data),
00116                                    void *user_data);
00117 
00122         cl_int info(cl_mem_info param_name,
00123                     size_t param_value_size,
00124                     void *param_value,
00125                     size_t *param_value_size_ret) const;
00126 
00127     private:
00128         unsigned int p_num_devices, p_devices_to_allocate;
00129         cl_mem_flags p_flags;
00130         void *p_host_ptr;
00131         DeviceBuffer **p_devicebuffers;
00132 
00133         void (CL_CALLBACK *p_dtor_callback)(cl_mem memobj, void *user_data);
00134         void *p_dtor_userdata;
00135 };
00136 
00140 class Buffer : public MemObject
00141 {
00142     public:
00151         Buffer(Context *ctx, size_t size, void *host_ptr, cl_mem_flags flags,
00152                cl_int *errcode_ret);
00153 
00154         size_t size() const; 
00155         Type type() const;   
00156     private:
00157         size_t p_size;
00158 
00159 };
00160 
00164 class SubBuffer : public MemObject
00165 {
00166     public:
00175         SubBuffer(class Buffer *parent, size_t offset, size_t size,
00176                   cl_mem_flags flags, cl_int *errcode_ret);
00177 
00178         size_t size() const;                    
00179         Type type() const;                      
00180         bool allocate(DeviceInterface *device); 
00182         size_t offset() const;                  
00183         class Buffer *parent() const;           
00185     private:
00186         size_t p_offset, p_size;
00187         class Buffer *p_parent;
00188 };
00189 
00193 class Image2D : public MemObject
00194 {
00195     public:
00207         Image2D(Context *ctx, size_t width, size_t height, size_t row_pitch,
00208                 const cl_image_format *format, void *host_ptr,
00209                 cl_mem_flags flags, cl_int *errcode_ret);
00210 
00211         virtual size_t size() const;           
00212         virtual Type type() const;             
00214         size_t width() const;                  
00215         size_t height() const;                 
00216         size_t row_pitch() const;              
00217         virtual size_t slice_pitch() const;    
00218         const cl_image_format &format() const; 
00228         cl_int imageInfo(cl_image_info param_name,
00229                          size_t param_value_size,
00230                          void *param_value,
00231                          size_t *param_value_size_ret) const;
00232 
00233         static size_t element_size(const cl_image_format &format);  
00234         static unsigned int channels(const cl_image_format &format);
00235         static size_t pixel_size(const cl_image_format &format);    
00236         size_t pixel_size() const;                                  
00237         size_t element_size() const;                                
00238         unsigned int channels() const;                              
00240     private:
00241         size_t p_width, p_height, p_row_pitch;
00242         cl_image_format p_format;
00243 };
00244 
00248 class Image3D : public Image2D
00249 {
00250     public:
00264         Image3D(Context *ctx, size_t width, size_t height, size_t depth,
00265                 size_t row_pitch, size_t slice_pitch,
00266                 const cl_image_format *format, void *host_ptr,
00267                 cl_mem_flags flags, cl_int *errcode_ret);
00268 
00269         size_t size() const;        
00270         Type type() const;          
00272         size_t depth() const;       
00273         size_t slice_pitch() const; 
00275     private:
00276         size_t p_depth, p_slice_pitch;
00277 };
00278 
00279 }
00280 
00281 struct _cl_mem : public Coal::MemObject
00282 {};
00283 
00284 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines