Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Xewar313 committed Dec 2, 2024
1 parent 2bf503e commit dc89bc8
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 79 deletions.
1 change: 0 additions & 1 deletion examples/mp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ add_mp_example(hello_world)
add_mp_example_no_test(sparse_matrix)
add_mp_example_no_test(sparse_benchmark)
add_mp_example_no_test(sparse_matrix_matrix_mul)
add_mp_example_no_test(local_issue)

if(OpenMP_FOUND)
add_executable(vector-add-ref vector-add-ref.cpp)
Expand Down
32 changes: 15 additions & 17 deletions include/dr/detail/multiply_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

namespace dr::__detail {

template <std::random_access_iterator Iter>
class multiply_iterator {
template <std::random_access_iterator Iter> class multiply_iterator {
public:
using value_type = std::iter_value_t<Iter>;
using difference_type = long long;
Expand All @@ -25,7 +24,8 @@ class multiply_iterator {

using iterator_category = std::random_access_iterator_tag;

multiply_iterator(Iter iter, std::size_t len, long long pos) noexcept : iter_(iter), len_(len), pos_(pos) {}
multiply_iterator(Iter iter, std::size_t len, long long pos) noexcept
: iter_(iter), len_(len), pos_(pos) {}
multiply_iterator() noexcept = default;
~multiply_iterator() noexcept = default;
multiply_iterator(const multiply_iterator &) noexcept = default;
Expand Down Expand Up @@ -55,13 +55,9 @@ class multiply_iterator {

bool operator>(iterator other) const noexcept { return pos_ > other.pos_; }

bool operator<=(iterator other) const noexcept {
return pos_ <= other.pos_;
}
bool operator<=(iterator other) const noexcept { return pos_ <= other.pos_; }

bool operator>=(iterator other) const noexcept {
return pos_ >= other.pos_;
}
bool operator>=(iterator other) const noexcept { return pos_ >= other.pos_; }

iterator &operator++() noexcept {
++pos_;
Expand Down Expand Up @@ -119,22 +115,24 @@ class multiply_iterator {
};

template <rng::random_access_range V>
requires(rng::sized_range<V>)
requires(rng::sized_range<V>)
class multiply_view : public rng::view_interface<multiply_view<V>> {
public:
template <rng::viewable_range R>
multiply_view(R &&r, std::size_t n)
: base_(rng::views::all(std::forward<R>(r))), n_(n) {}

auto begin() const { return multiply_iterator(rng::begin(base_), base_.size(), 0); }

auto end() const { return multiply_iterator(rng::begin(base_), base_.size(), n_ * base_.size()); }
auto begin() const {
return multiply_iterator(rng::begin(base_), base_.size(), 0);
}

auto size() const
{
return rng::size(base_);
auto end() const {
return multiply_iterator(rng::begin(base_), base_.size(),
n_ * base_.size());
}

auto size() const { return rng::size(base_); }

private:
V base_;
std::size_t n_;
Expand All @@ -143,4 +141,4 @@ class multiply_view : public rng::view_interface<multiply_view<V>> {
template <rng::viewable_range R>
multiply_view(R &&r, std::size_t n) -> multiply_view<rng::views::all_t<R>>;

} // namespace dr
} // namespace dr::__detail
55 changes: 30 additions & 25 deletions include/dr/mp/containers/matrix_formats/csr_eq_distribution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <dr/detail/matrix_entry.hpp>
#include <dr/detail/multiply_view.hpp>
#include <dr/mp/containers/matrix_formats/csr_eq_segment.hpp>
#include <dr/views/csr_matrix_view.hpp>
#include <dr/detail/multiply_view.hpp>

namespace dr::mp {

template <typename T, typename I, class BackendT = MpiBackend>
class csr_eq_distribution {
using view_tuple = std::tuple<std::size_t, std::size_t, std::size_t, I*>;
using view_tuple = std::tuple<std::size_t, std::size_t, std::size_t, I *>;

public:
using value_type = dr::matrix_entry<T, I>;
using segment_type = csr_eq_segment<csr_eq_distribution>;
Expand Down Expand Up @@ -302,59 +303,63 @@ class csr_eq_distribution {
}
fence();
auto local_rows = rows_data_;
auto real_val_size = std::min(vals_data_->segment_size(), nnz_ - vals_data_->segment_size() * rank);
auto my_tuple = std::make_tuple(row_size_, row_offsets_[rank], segment_size_ * rank, local_rows);
auto real_val_size = std::min(vals_data_->segment_size(),
nnz_ - vals_data_->segment_size() * rank);
auto my_tuple = std::make_tuple(row_size_, row_offsets_[rank],
segment_size_ * rank, local_rows);
view_helper_const = tuple_alloc.allocate(1);


if (use_sycl()) {
sycl_queue().memcpy(view_helper_const, &my_tuple, sizeof(view_tuple)).wait();
sycl_queue()
.memcpy(view_helper_const, &my_tuple, sizeof(view_tuple))
.wait();
} else {
view_helper_const[0] = my_tuple;
}

auto local_cols = cols_data_->segments()[rank].begin().local();
auto local_vals = vals_data_->segments()[rank].begin().local();
local_view = std::make_shared<view_type>(get_elem_view(real_val_size, view_helper_const, local_cols, local_vals, rank));
local_view = std::make_shared<view_type>(get_elem_view(
real_val_size, view_helper_const, local_cols, local_vals, rank));
}


static auto get_elem_view(
std::size_t vals_size,
view_tuple* helper_tuple,
index_type *local_cols,
elem_type *local_vals,
std::size_t rank) {
static auto get_elem_view(std::size_t vals_size, view_tuple *helper_tuple,
index_type *local_cols, elem_type *local_vals,
std::size_t rank) {
auto local_vals_range = rng::subrange(local_vals, local_vals + vals_size);
auto local_cols_range = rng::subrange(local_cols, local_cols + vals_size);
auto zipped_results = rng::views::zip(local_vals_range, local_cols_range);
auto enumerated_zipped = rng::views::enumerate(zipped_results);
// we need to use multiply_view here,
// we need to use multiply_view here,
// because lambda is not properly copied to sycl environment
// when we use variable capture
auto multiply_range = dr::__detail::multiply_view(rng::subrange(helper_tuple, helper_tuple + 1), vals_size);
auto enumerted_with_data = rng::views::zip(enumerated_zipped, multiply_range);

// when we use variable capture
auto multiply_range = dr::__detail::multiply_view(
rng::subrange(helper_tuple, helper_tuple + 1), vals_size);
auto enumerted_with_data =
rng::views::zip(enumerated_zipped, multiply_range);

auto transformer = [=](auto x) {
auto [entry, tuple] = x;
auto [row_size, row_offset, offset, local_rows] = tuple;
auto [index, pair] = entry;
auto [val, column] = pair;
auto row = rng::distance(
local_rows,
std::upper_bound(local_rows, local_rows + row_size, offset + index) -
1) + row_offset;
auto row =
rng::distance(local_rows,
std::upper_bound(local_rows, local_rows + row_size,
offset + index) -
1) +
row_offset;
dr::index<index_type> index_obj(row, column);
value_type entry_obj(index_obj, val);
return entry_obj;
};
return rng::transform_view(enumerted_with_data, std::move(transformer));
}

using view_type = decltype(get_elem_view(0, nullptr, nullptr, nullptr,0));
using view_type = decltype(get_elem_view(0, nullptr, nullptr, nullptr, 0));

dr::mp::__detail::allocator<view_tuple> tuple_alloc;
view_tuple* view_helper_const;
view_tuple *view_helper_const;
std::shared_ptr<view_type> local_view;

std::size_t segment_size_ = 0;
Expand Down
2 changes: 1 addition & 1 deletion include/dr/mp/containers/matrix_formats/csr_eq_segment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ template <typename DSM> class csr_eq_segment_iterator {
return dr::__detail::drop_segments(dsm_->segments(), segment_index_,
index_);
}

auto local() const {
const auto my_process_segment_index = dsm_->rows_backend_.getrank();
assert(my_process_segment_index == segment_index_);
Expand Down
55 changes: 31 additions & 24 deletions include/dr/mp/containers/matrix_formats/csr_row_distribution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <dr/detail/matrix_entry.hpp>
#include <dr/detail/multiply_view.hpp>
#include <dr/mp/containers/matrix_formats/csr_row_segment.hpp>
#include <dr/views/csr_matrix_view.hpp>
#include <dr/detail/multiply_view.hpp>
#include <fmt/core.h>

namespace dr::mp {
template <typename T, typename I, class BackendT = MpiBackend>
class csr_row_distribution {
using view_tuple = std::tuple<std::size_t, std::size_t, std::size_t, I*>;
using view_tuple = std::tuple<std::size_t, std::size_t, std::size_t, I *>;

public:
using value_type = dr::matrix_entry<T, I>;
using segment_type = csr_row_segment<csr_row_distribution>;
Expand Down Expand Up @@ -291,56 +292,62 @@ class csr_row_distribution {
fence();
auto local_rows = rows_data_->segments()[rank].begin().local();
auto offset = val_offsets_[rank];
auto real_row_size = std::min(rows_data_->segment_size(), shape_.first - rows_data_->segment_size() * rank);
auto my_tuple = std::make_tuple(real_row_size, segment_size_ * rank, offset, local_rows);
auto real_row_size =
std::min(rows_data_->segment_size(),
shape_.first - rows_data_->segment_size() * rank);
auto my_tuple = std::make_tuple(real_row_size, segment_size_ * rank, offset,
local_rows);
view_helper_const = alloc.allocate(1);


if (use_sycl()) {
sycl_queue().memcpy(view_helper_const, &my_tuple, sizeof(view_tuple)).wait();
sycl_queue()
.memcpy(view_helper_const, &my_tuple, sizeof(view_tuple))
.wait();
} else {
view_helper_const[0] = my_tuple;
}

local_view = std::make_shared<view_type>(get_elem_view(vals_size_, view_helper_const, cols_data_, vals_data_, rank));
local_view = std::make_shared<view_type>(get_elem_view(
vals_size_, view_helper_const, cols_data_, vals_data_, rank));
}

static auto get_elem_view(
std::size_t vals_size,
view_tuple* helper_tuple,
index_type *local_cols,
elem_type *local_vals,
std::size_t rank) {
static auto get_elem_view(std::size_t vals_size, view_tuple *helper_tuple,
index_type *local_cols, elem_type *local_vals,
std::size_t rank) {
auto local_vals_range = rng::subrange(local_vals, local_vals + vals_size);
auto local_cols_range = rng::subrange(local_cols, local_cols + vals_size);
auto zipped_results = rng::views::zip(local_vals_range, local_cols_range);
auto enumerated_zipped = rng::views::enumerate(zipped_results);
// we need to use multiply_view here,
// we need to use multiply_view here,
// because lambda is not properly copied to sycl environment
// when we use variable capture
auto multiply_range = dr::__detail::multiply_view(rng::subrange(helper_tuple, helper_tuple + 1), vals_size);
auto enumerted_with_data = rng::views::zip(enumerated_zipped, multiply_range);

// when we use variable capture
auto multiply_range = dr::__detail::multiply_view(
rng::subrange(helper_tuple, helper_tuple + 1), vals_size);
auto enumerted_with_data =
rng::views::zip(enumerated_zipped, multiply_range);

auto transformer = [=](auto x) {
auto [entry, tuple] = x;
auto [row_size, row_offset, offset, local_rows] = tuple;
auto [index, pair] = entry;
auto [val, column] = pair;
auto row = rng::distance(
local_rows,
std::upper_bound(local_rows, local_rows + row_size, offset + index) -
1) + row_offset;
auto row =
rng::distance(local_rows,
std::upper_bound(local_rows, local_rows + row_size,
offset + index) -
1) +
row_offset;
dr::index<index_type> index_obj(row, column);
value_type entry_obj(index_obj, val);
return entry_obj;
};
return rng::transform_view(enumerted_with_data, std::move(transformer));
}

using view_type = decltype(get_elem_view(0, nullptr, nullptr, nullptr,0));
using view_type = decltype(get_elem_view(0, nullptr, nullptr, nullptr, 0));

dr::mp::__detail::allocator<view_tuple> alloc;
view_tuple* view_helper_const;
view_tuple *view_helper_const;
std::shared_ptr<view_type> local_view;

std::size_t segment_size_ = 0;
Expand Down
4 changes: 1 addition & 3 deletions include/dr/mp/containers/matrix_formats/csr_row_segment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#pragma once


namespace dr::mp {
template <typename DSM> class csr_row_segment_iterator;

Expand Down Expand Up @@ -223,7 +222,7 @@ template <typename DSM> class csr_row_segment_iterator {
assert(dsm_ != nullptr);
return dr::__detail::drop_segments(dsm_->segments(), segment_index_,
index_);
}
}

auto local() const {
const auto my_process_segment_index = dsm_->vals_backend_.getrank();
Expand All @@ -232,7 +231,6 @@ template <typename DSM> class csr_row_segment_iterator {
}

private:

// all fields need to be initialized by default ctor so every default
// constructed iter is equal to any other default constructed iter
DSM *dsm_ = nullptr;
Expand Down
12 changes: 7 additions & 5 deletions include/dr/mp/sycl_support.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ sycl::queue &sycl_queue();

namespace dr::mp::__detail {

//sometimes we only want to dereference iterator inside SYCL
// sometimes we only want to dereference iterator inside SYCL
template <typename T> auto sycl_get_deref(T v) {
using deref_type = std::remove_reference<decltype(*v)>::type;
deref_type temp;
{
sycl::buffer<deref_type> buff(&temp, 1);
sycl_queue().submit([&](auto &&h) {
sycl::accessor access(buff, h, sycl::write_only, sycl::no_init);
h.single_task([=](auto i) { access[0] = *v;});
}).wait();
sycl_queue()
.submit([&](auto &&h) {
sycl::accessor access(buff, h, sycl::write_only, sycl::no_init);
h.single_task([=](auto i) { access[0] = *v; });
})
.wait();
}
return temp;
}
Expand Down
4 changes: 1 addition & 3 deletions test/gtest/mp/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ TYPED_TEST(ReduceMP, RootIterators) {
TYPED_TEST(ReduceMP, TransformReduce) {
Ops1<TypeParam> ops(10);

auto add = [](auto &&elem) {
return elem + 1;
};
auto add = [](auto &&elem) { return elem + 1; };

auto added = dr::mp::views::transform(ops.dist_vec, add);
auto min = [](double x, double y) { return std::min(x, y); };
Expand Down

0 comments on commit dc89bc8

Please sign in to comment.