Skip to content

Commit

Permalink
add subregion read and write method
Browse files Browse the repository at this point in the history
  • Loading branch information
StRigaud committed Sep 27, 2023
1 parent 9441119 commit 8dfa75e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
12 changes: 12 additions & 0 deletions clic/include/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Array : public std::enable_shared_from_this<Array>
{
public:
using Pointer = std::shared_ptr<Array>;

static auto
New() -> Array::Pointer
{
Expand All @@ -41,14 +42,25 @@ class Array : public std::enable_shared_from_this<Array>
friend auto
operator<<(std::ostream & out, const Array::Pointer & array) -> std::ostream &;


auto
allocate() -> void;

auto
write(const void * host_data) -> void;
auto
write(const void * host_data, const std::array<size_t, 3> & region, const std::array<size_t, 3> & buffer_origin)
-> void;

auto
read(void * host_data) const -> void;
auto
read(void * host_data, const std::array<size_t, 3> & region, const std::array<size_t, 3> & buffer_origin) const
-> void;

auto
copy(const Array::Pointer & dst) const -> void;

auto
fill(const float & value) const -> void;

Expand Down
52 changes: 48 additions & 4 deletions clic/src/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,28 @@ Array::write(const void * host_data) -> void
{
allocate();
}
backend_.writeMemory(
device(), get(), { this->width(), this->height(), this->depth() }, { 0, 0, 0 }, dtype(), mtype(), host_data);
std::array<size_t, 3> _origin = { 0, 0, 0 };
std::array<size_t, 3> _shape = { this->width(), this->height(), this->depth() };
std::array<size_t, 3> _region = { this->width(), this->height(), this->depth() };
backend_.writeMemory(device(), get(), _shape, _origin, _region, dtype(), mtype(), host_data);
}

auto
Array::write(const void * host_data, const std::array<size_t, 3> & region, const std::array<size_t, 3> & buffer_origin)
-> void
{
if (host_data == nullptr)
{
throw std::runtime_error("Error: host_data is null");
}
if (!initialized())
{
allocate();
}
std::array<size_t, 3> _origin = buffer_origin;
std::array<size_t, 3> _region = region;
std::array<size_t, 3> _shape = { this->width(), this->height(), this->depth() };
backend_.writeMemory(device(), get(), _shape, _origin, _region, dtype(), mtype(), host_data);
}

auto
Expand All @@ -113,7 +133,28 @@ Array::read(void * host_data) const -> void
{
throw std::runtime_error("Error: Array is not initialized, it cannot be read");
}
backend_.readMemory(device(), c_get(), { width(), height(), depth() }, { 0, 0, 0 }, dtype(), mtype(), host_data);
std::array<size_t, 3> _origin = { 0, 0, 0 };
std::array<size_t, 3> _shape = { this->width(), this->height(), this->depth() };
std::array<size_t, 3> _region = { this->width(), this->height(), this->depth() };
backend_.readMemory(device(), c_get(), _shape, _origin, _region, dtype(), mtype(), host_data);
}

auto
Array::read(void * host_data, const std::array<size_t, 3> & region, const std::array<size_t, 3> & buffer_origin) const
-> void
{
if (host_data == nullptr)
{
throw std::runtime_error("Error: host_data is null");
}
if (!initialized())
{
throw std::runtime_error("Error: Array is not initialized, it cannot be read");
}
std::array<size_t, 3> _origin = buffer_origin;
std::array<size_t, 3> _region = region;
std::array<size_t, 3> _shape = { this->width(), this->height(), this->depth() };
backend_.readMemory(device(), c_get(), _shape, _origin, _region, dtype(), mtype(), host_data);
}

auto
Expand Down Expand Up @@ -164,7 +205,10 @@ Array::fill(const float & value) const -> void
{
std::cerr << "Error: Arrays are not initialized_" << std::endl;
}
backend_.setMemory(device(), get(), { width(), height(), depth() }, { 0, 0, 0 }, dtype(), mtype(), value);
std::array<size_t, 3> _origin = { 0, 0, 0 };
std::array<size_t, 3> _region = { this->width(), this->height(), this->depth() };
std::array<size_t, 3> _shape = { this->width(), this->height(), this->depth() };
backend_.setMemory(device(), get(), _shape, _origin, _region, dtype(), mtype(), value);
}

auto
Expand Down

0 comments on commit 8dfa75e

Please sign in to comment.