Clover Git
OpenCL 1.1 software implementation
|
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 #include "buffer.h" 00034 #include "device.h" 00035 00036 #include "../memobject.h" 00037 00038 #include <cstdlib> 00039 #include <cstring> 00040 #include <iostream> 00041 00042 using namespace Coal; 00043 00044 CPUBuffer::CPUBuffer(CPUDevice *device, MemObject *buffer, cl_int *rs) 00045 : DeviceBuffer(), p_device(device), p_buffer(buffer), p_data(0), 00046 p_data_malloced(false) 00047 { 00048 if (buffer->type() == MemObject::SubBuffer) 00049 { 00050 // We need to create this CPUBuffer based on the CPUBuffer of the 00051 // parent buffer 00052 SubBuffer *subbuf = (SubBuffer *)buffer; 00053 MemObject *parent = subbuf->parent(); 00054 CPUBuffer *parentcpubuf = (CPUBuffer *)parent->deviceBuffer(device); 00055 00056 char *tmp_data = (char *)parentcpubuf->data(); 00057 tmp_data += subbuf->offset(); 00058 00059 p_data = (void *)tmp_data; 00060 } 00061 else if (buffer->flags() & CL_MEM_USE_HOST_PTR) 00062 { 00063 // We use the host ptr, we are already allocated 00064 p_data = buffer->host_ptr(); 00065 } 00066 00067 // NOTE: This function can also reject Image buffers by setting a value 00068 // != CL_SUCCESS in rs. 00069 } 00070 00071 CPUBuffer::~CPUBuffer() 00072 { 00073 if (p_data_malloced) 00074 { 00075 std::free((void *)p_data); 00076 } 00077 } 00078 00079 void *CPUBuffer::data() const 00080 { 00081 return p_data; 00082 } 00083 00084 void *CPUBuffer::nativeGlobalPointer() const 00085 { 00086 return data(); 00087 } 00088 00089 bool CPUBuffer::allocate() 00090 { 00091 size_t buf_size = p_buffer->size(); 00092 00093 if (buf_size == 0) 00094 // Something went wrong... 00095 return false; 00096 00097 if (!p_data) 00098 { 00099 // We don't use a host ptr, we need to allocate a buffer 00100 p_data = std::malloc(buf_size); 00101 00102 if (!p_data) 00103 return false; 00104 00105 p_data_malloced = true; 00106 } 00107 00108 if (p_buffer->type() != MemObject::SubBuffer && 00109 p_buffer->flags() & CL_MEM_COPY_HOST_PTR) 00110 { 00111 std::memcpy(p_data, p_buffer->host_ptr(), buf_size); 00112 } 00113 00114 // Say to the memobject that we are allocated 00115 p_buffer->deviceAllocated(this); 00116 00117 return true; 00118 } 00119 00120 DeviceInterface *CPUBuffer::device() const 00121 { 00122 return p_device; 00123 } 00124 00125 bool CPUBuffer::allocated() const 00126 { 00127 return p_data != 0; 00128 }