|
Packit |
534379 |
// Copyright(c) 2018, Intel Corporation
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
534379 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
534379 |
// this list of conditions and the following disclaimer.
|
|
Packit |
534379 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
534379 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
534379 |
// and/or other materials provided with the distribution.
|
|
Packit |
534379 |
// * Neither the name of Intel Corporation nor the names of its contributors
|
|
Packit |
534379 |
// may be used to endorse or promote products derived from this software
|
|
Packit |
534379 |
// without specific prior written permission.
|
|
Packit |
534379 |
//
|
|
Packit |
534379 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
534379 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
534379 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
534379 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
534379 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
534379 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
534379 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
534379 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
534379 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
534379 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
534379 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
534379 |
#include "pyshared_buffer.h"
|
|
Packit |
534379 |
#include <opae/cxx/core/handle.h>
|
|
Packit |
534379 |
#include "pycontext.h"
|
|
Packit |
534379 |
|
|
Packit |
534379 |
namespace py = pybind11;
|
|
Packit |
534379 |
using opae::fpga::types::shared_buffer;
|
|
Packit |
534379 |
using opae::fpga::types::handle;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
shared_buffer represents a system memory buffer that can be shared with the accelerator.
|
|
Packit |
534379 |
It implements the Python buffer protocol and can be converted to a native bytearray object.
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_allocate() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
shared_buffer factory method - allocate a shared buffer object.
|
|
Packit |
534379 |
Args:
|
|
Packit |
534379 |
handle: An accelerator handle object that identifies an open accelerator
|
|
Packit |
534379 |
obect to share the buffer with.
|
|
Packit |
534379 |
len: The length in bytes of the requested buffer.
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
shared_buffer::ptr_t shared_buffer_allocate(handle::ptr_t hndl, size_t size) {
|
|
Packit |
534379 |
auto buf = shared_buffer::allocate(hndl, size);
|
|
Packit |
534379 |
buffer_registry::instance().add_buffer(hndl, buf);
|
|
Packit |
534379 |
return buf;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_size() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Get the length of the buffer in bytes.
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_wsid() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Get the underlying buffer's workspace ID.
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_io_address() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Get the address of the buffer suitable for programming into the
|
|
Packit |
534379 |
accelerator device.
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_fill() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Fill the buffer with a given value.
|
|
Packit |
534379 |
|
|
Packit |
534379 |
Args:
|
|
Packit |
534379 |
value: The value to use when filling the buffer.
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_compare() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Compare this shared_buffer (the first len bytes) object with another one.
|
|
Packit |
534379 |
Returns 0 if the two buffers (up to len) are equal.
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_getitem() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Get the byte at the given offset.
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
uint8_t shared_buffer_getitem(shared_buffer::ptr_t buf, uint32_t offset) {
|
|
Packit |
534379 |
return *(buf->c_type() + offset);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_setitem() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Set the bytes at the given offset using all bytes in the argument.
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
void shared_buffer_setitem(opae::fpga::types::shared_buffer::ptr_t buf,
|
|
Packit |
534379 |
uint32_t offset, pybind11::int_ item) {
|
|
Packit |
534379 |
int *ptr =
|
|
Packit |
534379 |
reinterpret_cast<int *>(const_cast<uint8_t *>(buf->c_type() + offset));
|
|
Packit |
534379 |
*ptr = item.cast<int>();
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_getslice() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Get a slice of the bytes as determined by the slice arguments ([start:stop:step])
|
|
Packit |
534379 |
Args:
|
|
Packit |
534379 |
start: start offset of buffer
|
|
Packit |
534379 |
stop: end offset of the buffer (not incusive)
|
|
Packit |
534379 |
step: step offset
|
|
Packit |
534379 |
|
|
Packit |
534379 |
NOTE: This current implementation copies the data into a new list.
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
py::list shared_buffer_getslice(shared_buffer::ptr_t buf, py::slice slice) {
|
|
Packit |
534379 |
size_t start, stop, step, length;
|
|
Packit |
534379 |
if (!slice.compute(buf->size(), &start, &stop, &step, &length))
|
|
Packit |
534379 |
throw py::error_already_set();
|
|
Packit |
534379 |
py::list list;
|
|
Packit |
534379 |
for (size_t i = start; i < stop; i += step) {
|
|
Packit |
534379 |
list.append(*(buf->c_type() + i));
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
return list;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_read32() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Cast the memory at the given offset into a 32-bit integer
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_read64() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Cast the memory at the given offset into a 64-bit integer
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_write32() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Write a 32-bit integer at the memory at the given offset
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_write64() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Write a 64-bit integer at the memory at the given offset
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_copy() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Copy the given number of bytes from the current buffer to the buffer in the argument.
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
void shared_buffer_copy(shared_buffer::ptr_t self, shared_buffer::ptr_t other,
|
|
Packit |
534379 |
size_t size) {
|
|
Packit |
534379 |
uint8_t *src = const_cast<uint8_t *>(self->c_type());
|
|
Packit |
534379 |
uint8_t *dst = const_cast<uint8_t *>(other->c_type());
|
|
Packit |
534379 |
|
|
Packit |
534379 |
std::copy(src, src + (size ? size : self->size()), dst);
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
const char *shared_buffer_doc_split() {
|
|
Packit |
534379 |
return R"opaedoc(
|
|
Packit |
534379 |
Split the buffer into other shared_buffer objects.
|
|
Packit |
534379 |
The arguments to this method make up a list of sizes to use when splitting the buffer.
|
|
Packit |
534379 |
For example, say a shared_buffer object is 1024 bytes and split is called with sizes
|
|
Packit |
534379 |
256, 256, 512 then the result is a list of shared_buffer objects with those sizes
|
|
Packit |
534379 |
respectively.
|
|
Packit |
534379 |
)opaedoc";
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
class split_buffer : public shared_buffer {
|
|
Packit |
534379 |
public:
|
|
Packit |
534379 |
typedef std::shared_ptr<split_buffer> ptr_t;
|
|
Packit |
534379 |
split_buffer(const split_buffer &) = delete;
|
|
Packit |
534379 |
split_buffer &operator=(const split_buffer &) = delete;
|
|
Packit |
534379 |
|
|
Packit |
534379 |
split_buffer(shared_buffer::ptr_t parent, size_t len, uint8_t *virt,
|
|
Packit |
534379 |
uint64_t wsid, uint64_t io_address)
|
|
Packit |
534379 |
: shared_buffer(nullptr, len, virt, wsid, io_address), parent_(parent) {}
|
|
Packit |
534379 |
|
|
Packit |
534379 |
virtual ~split_buffer() { parent_.reset(); }
|
|
Packit |
534379 |
|
|
Packit |
534379 |
private:
|
|
Packit |
534379 |
shared_buffer::ptr_t parent_;
|
|
Packit |
534379 |
};
|
|
Packit |
534379 |
|
|
Packit |
534379 |
std::vector<shared_buffer::ptr_t> shared_buffer_split(shared_buffer::ptr_t buf,
|
|
Packit |
534379 |
py::args args) {
|
|
Packit |
534379 |
std::vector<shared_buffer::ptr_t> buffers;
|
|
Packit |
534379 |
if (!args || py::len(args) == 1) {
|
|
Packit |
534379 |
buffers.push_back(buf);
|
|
Packit |
534379 |
} else {
|
|
Packit |
534379 |
size_t offset = 0;
|
|
Packit |
534379 |
uint8_t *virt = const_cast<uint8_t *>(buf->c_type());
|
|
Packit |
534379 |
auto wsid = buf->wsid();
|
|
Packit |
534379 |
auto io_address = buf->io_address();
|
|
Packit |
534379 |
for (auto a : args) {
|
|
Packit |
534379 |
auto len = a.cast<size_t>();
|
|
Packit |
534379 |
if (offset + len > buf->size()) {
|
|
Packit |
534379 |
throw std::invalid_argument("buffer not big enough to split this way");
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
buffers.push_back(
|
|
Packit |
534379 |
std::make_shared<split_buffer>(buf, len, virt, wsid, io_address));
|
|
Packit |
534379 |
offset += len;
|
|
Packit |
534379 |
io_address += len;
|
|
Packit |
534379 |
virt += len;
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
}
|
|
Packit |
534379 |
return buffers;
|
|
Packit |
534379 |
}
|