Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address PR review feedback
  • Loading branch information
oleksandr-pavlyk committed Sep 26, 2024
commit 610d88d6d5fc7887627dc7b49e86e8eaf4e07cd4
43 changes: 37 additions & 6 deletions dpctl/tensor/libtensor/source/copy_as_contig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "kernels/copy_as_contiguous.hpp"
#include "utils/memory_overlap.hpp"
#include "utils/offset_utils.hpp"
#include "utils/output_validation.hpp"
#include "utils/sycl_alloc_utils.hpp"
#include "utils/type_dispatch.hpp"

Expand Down Expand Up @@ -107,10 +108,16 @@ void init_copy_as_contig_dispatch_vectors(void)
namespace
{

template <typename dimT> dimT get_nelems(const std::vector<dimT> &shape)
template <typename dimT> std::size_t get_nelems(const std::vector<dimT> &shape)
{
const dimT nelems = std::accumulate(std::begin(shape), std::end(shape),
dimT(1), std::multiplies<dimT>{});
auto mult_fn = [](std::size_t prod, const dimT &term) -> std::size_t {
return prod * static_cast<std::size_t>(term);
};

constexpr std::size_t unit{1};

const std::size_t nelems =
std::accumulate(std::begin(shape), std::end(shape), unit, mult_fn);
return nelems;
}

Expand Down Expand Up @@ -163,6 +170,14 @@ py_as_c_contig(const dpctl::tensor::usm_ndarray &src,
throw py::value_error("Destination array must be C-contiguous");
}

dpctl::tensor::validation::CheckWritable::throw_if_not_writable(dst);

// check compatibility of execution queue and allocation queue
if (!dpctl::utils::queues_are_compatible(exec_q, {src, dst})) {
throw py::value_error(
"Execution queue is not compatible with allocation queues");
}

const auto &src_strides_vec = src.get_strides_vector();

if (src_nd >= 2) {
Expand All @@ -175,7 +190,7 @@ py_as_c_contig(const dpctl::tensor::usm_ndarray &src,
}
}

const py::ssize_t nelems = get_nelems(src_shape_vec);
const std::size_t nelems = get_nelems(src_shape_vec);

if (nelems == 0) {
// nothing to do
Expand Down Expand Up @@ -254,7 +269,7 @@ py_as_f_contig(const dpctl::tensor::usm_ndarray &src,
const std::vector<sycl::event> &depends)
{
/* Same dimensions, same shape, same data-type
* dst is C-contiguous.
* dst is F-contiguous.
*/
int src_nd = src.get_ndim();
int dst_nd = dst.get_ndim();
Expand Down Expand Up @@ -288,6 +303,14 @@ py_as_f_contig(const dpctl::tensor::usm_ndarray &src,
throw py::value_error("Destination array must be F-contiguous");
}

dpctl::tensor::validation::CheckWritable::throw_if_not_writable(dst);

// check compatibility of execution queue and allocation queue
if (!dpctl::utils::queues_are_compatible(exec_q, {src, dst})) {
throw py::value_error(
"Execution queue is not compatible with allocation queues");
}

const auto &src_strides_vec = src.get_strides_vector();

if (src_nd >= 2) {
Expand All @@ -300,7 +323,7 @@ py_as_f_contig(const dpctl::tensor::usm_ndarray &src,
}
}

const py::ssize_t nelems = get_nelems(src_shape_vec);
const std::size_t nelems = get_nelems(src_shape_vec);

if (nelems == 0) {
// nothing to do
Expand Down Expand Up @@ -433,6 +456,14 @@ py_as_c_contig_f2c(const dpctl::tensor::usm_ndarray &src,
throw py::value_error("Destination array must be C-contiguous");
}

dpctl::tensor::validation::CheckWritable::throw_if_not_writable(dst);

// check compatibility of execution queue and allocation queue
if (!dpctl::utils::queues_are_compatible(exec_q, {src, dst})) {
throw py::value_error(
"Execution queue is not compatible with allocation queues");
}

if (nelems == 0) {
// nothing to do
return std::make_pair(sycl::event(), sycl::event());
Expand Down