Distributed Ranges
Loading...
Searching...
No Matches
equal.hpp
1// SPDX-FileCopyrightText: Intel Corporation
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#pragma once
6#include <concepts>
7#include <dr/concepts/concepts.hpp>
8#include <dr/mp/algorithms/reduce.hpp>
9#include <dr/mp/algorithms/transform.hpp>
10#include <dr/mp/views/zip.hpp>
11
12namespace dr::mp::_detail {
13template <dr::distributed_range R1, dr::distributed_range R2>
14 requires std::equality_comparable_with<rng::range_value_t<R1>,
15 rng::range_value_t<R2>>
16bool equal(std::size_t root, bool root_provided, R1 &&r1, R2 &&r2) {
17
18 if (rng::distance(r1) != rng::distance(r2)) {
19 return false;
20 }
21
22 // we must use ints instead of bools, because distributed ranges do not
23 // support bools
24 auto compare = [](auto &&elems) {
25 return elems.first == elems.second ? 1 : 0;
26 };
27
28 auto zipped_views = views::zip(r1, r2);
29 auto compared = dr::mp::views::transform(zipped_views, compare);
30
31 auto min = [](double x, double y) { return std::min(x, y); };
32 if (root_provided) {
33 auto result = mp::reduce(root, compared, 1, min);
34 return result == 1;
35 }
36 auto result = mp::reduce(compared, 1, min);
37 return result == 1;
38}
39
40} // namespace dr::mp::_detail
41
42namespace dr::mp {
43template <dr::distributed_range R1, dr::distributed_range R2>
44 requires std::equality_comparable_with<rng::range_value_t<R1>,
45 rng::range_value_t<R2>>
46bool equal(std::size_t root, R1 &&r1, R2 &&r2) {
47 return _detail::equal(root, true, r1, r2);
48}
49
50template <dr::distributed_range R1, dr::distributed_range R2>
51 requires std::equality_comparable_with<rng::range_value_t<R1>,
52 rng::range_value_t<R2>>
53bool equal(R1 &&r1, R2 &&r2) {
54 return _detail::equal(0, false, r1, r2);
55}
56
57} // namespace dr::mp