| 1 |
/* |
| 2 |
* Advanced Simulation Library <http://asl.org.il> |
| 3 |
* |
| 4 |
* Copyright 2015 Avtech Scientific <http://avtechscientific.com> |
| 5 |
* |
| 6 |
* |
| 7 |
* This file is part of Advanced Simulation Library (ASL). |
| 8 |
* |
| 9 |
* ASL is free software: you can redistribute it and/or modify it |
| 10 |
* under the terms of the GNU Affero General Public License as |
| 11 |
* published by the Free Software Foundation, version 3 of the License. |
| 12 |
* |
| 13 |
* ASL is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Affero General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Affero General Public License |
| 19 |
* along with ASL. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
* |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
#include "aclMemBlock.h" |
| 25 |
#include "../../aslUtilities.h" |
| 26 |
#include "../aclUtilities.h" |
| 27 |
|
| 28 |
|
| 29 |
using namespace asl; |
| 30 |
|
| 31 |
namespace acl |
| 32 |
{ |
| 33 |
|
| 34 |
/// class for compatibility with std::shared_ptr enssure correct unmapping \ingroup LDI |
| 35 |
/// \todo: maybe pass a MemBlock to the constructor instead of a buffer and a queue? |
| 36 |
template <typename T> class VectorUnmapper |
| 37 |
{ |
| 38 |
shared_ptr<cl::Buffer> buffer; |
| 39 |
CommandQueue queue; |
| 40 |
public: |
| 41 |
VectorUnmapper(shared_ptr<cl::Buffer> buffer_, CommandQueue queue_) : |
| 42 |
buffer(buffer_), |
| 43 |
queue(queue_) |
| 44 |
{}; |
| 45 |
|
| 46 |
inline void operator() (T* d) const |
| 47 |
{ |
| 48 |
cl_int status = 0; |
| 49 |
cl::Event event; |
| 50 |
|
| 51 |
status = queue->enqueueUnmapMemObject(*buffer, |
| 52 |
d, |
| 53 |
NULL, |
| 54 |
&event); |
| 55 |
errorMessage(status, "enqueueUnmapMemObject()"); |
| 56 |
status = event.wait(); |
| 57 |
errorMessage(status, "Event::wait() - event"); |
| 58 |
d = NULL; |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
MemBlock::MemBlock(): |
| 64 |
// typeID fictitious |
| 65 |
ElementBase(true, 0, TYPE_INT) |
| 66 |
{ |
| 67 |
buffer.reset(new cl::Buffer()); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
MemBlock::MemBlock(unsigned int size_, TypeID typeID, CommandQueue queue_): |
| 72 |
ElementBase(true, size_, typeID) |
| 73 |
{ |
| 74 |
queue = queue_; |
| 75 |
|
| 76 |
cl_int status = 0; |
| 77 |
|
| 78 |
// allocate value in GPU memory with padding |
| 79 |
buffer.reset(new cl::Buffer(getContext(queue), |
| 80 |
CL_MEM_READ_WRITE, |
| 81 |
size * TYPE_SIZE[typeID] + paddingBytes(size, TYPE_SIZE[typeID], queue), |
| 82 |
NULL, |
| 83 |
&status)); |
| 84 |
errorMessage(status, "cl::Buffer()"); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
MemBlock::MemBlock(unsigned int size_, TypeID typeID, char *initArray, CommandQueue queue_): |
| 89 |
ElementBase(true, size_, typeID) |
| 90 |
{ |
| 91 |
queue = queue_; |
| 92 |
|
| 93 |
cl_int status = 0; |
| 94 |
|
| 95 |
// allocate value in GPU memory with padding |
| 96 |
buffer = shared_ptr<cl::Buffer>(new cl::Buffer(getContext(queue), |
| 97 |
CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, |
| 98 |
size * TYPE_SIZE[typeID] + paddingBytes(size, TYPE_SIZE[typeID], queue), |
| 99 |
initArray, |
| 100 |
&status)); |
| 101 |
errorMessage(status, "cl::Buffer()"); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
cl::Buffer & MemBlock::getBuffer() |
| 106 |
{ |
| 107 |
return *buffer; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
std::shared_ptr<void> MemBlock::map() |
| 112 |
{ |
| 113 |
shared_ptr<void> p; |
| 114 |
if (region.expired()) |
| 115 |
{ |
| 116 |
cl_int status = 0; |
| 117 |
cl::Event event; |
| 118 |
|
| 119 |
// blocking_map = CL_TRUE (second argument) |
| 120 |
// read and write access (third argument) |
| 121 |
p.reset(queue->enqueueMapBuffer(*buffer, |
| 122 |
CL_TRUE, |
| 123 |
CL_MAP_READ | CL_MAP_WRITE, |
| 124 |
0, |
| 125 |
size * TYPE_SIZE[typeID], |
| 126 |
NULL, |
| 127 |
&event, |
| 128 |
&status), |
| 129 |
VectorUnmapper<void> (buffer, queue)); |
| 130 |
errorMessage(status, "enqueueMapBuffer()"); |
| 131 |
status = event.wait(); |
| 132 |
errorMessage(status, "Event::wait() - event"); |
| 133 |
region = p; |
| 134 |
} |
| 135 |
return region.lock(); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
void MemBlock::swapBuffers(MemBlock & a) |
| 140 |
{ |
| 141 |
if (compatible(size, queue, a.getSize(), a.getQueue())) |
| 142 |
{ |
| 143 |
std::swap(buffer, a.buffer); |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
errorMessage("acl::Vector::swap() - operands are incompatible. \ |
| 148 |
Either they reside on different devices or their sizes do not match: " |
| 149 |
+ numToStr(size) + " and " + numToStr(a.size)); |
| 150 |
} |
| 151 |
} |
| 152 |
} |