8000 Enables building Partition from local ranges by MarcelKoch · Pull Request #1227 · ginkgo-project/ginkgo · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Enables building Partition from local ranges #1227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 51 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
04ef94c
add build_from_local_range to partition
greole Feb 23, 2022
f1f2450
move build_from_local_range into its own header
MarcelKoch Feb 23, 2022
4d0f645
fixups after rebase
MarcelKoch Dec 5, 2022
be7e36c
use span for local range
MarcelKoch Dec 5, 2022
9cc97cc
adds kernel to remove duplicate start/ends
MarcelKoch Dec 5, 2022
b0c5603
adds test for removing duplicate start/ends
MarcelKoch Dec 5, 2022
68ae3a1
adds tests for build_from_local_range
MarcelKoch Dec 5, 2022
8d5efce
adds note on invalid inputs
MarcelKoch Dec 5, 2022
1077d9e
fixes reference kernel
MarcelKoch Dec 5, 2022
6dcade0
allows specifying part ids for contiguous partition constructor
MarcelKoch Dec 9, 2022
1ee1a97
adds sorting kernel for ranges + part-ids
MarcelKoch Dec 9, 2022
d849429
adds consistency check kernel (reference
MarcelKoch Dec 9, 2022
9683e4e
wip
MarcelKoch Dec 9, 2022
56598f4
fixes ranges gathering
MarcelKoch Dec 12, 2022
b782563
adds MPI tests
MarcelKoch Dec 12, 2022
239af98
removes dead code
MarcelKoch Dec 12, 2022
f98acaf
adds device consecutive ranges check
MarcelKoch Dec 19, 2022
5962b02
add omp sorting kernel
MarcelKoch Dec 19, 2022
684b563
fixes tests
MarcelKoch Dec 20, 2022
b664ec4
adds cuda/hip/dpcpp sort kernels
MarcelKoch Dec 20, 2022
efd1a45
adds creator from local sizes
MarcelKoch Dec 20, 2022
5a5b040
review updates
MarcelKoch Dec 20, 2022
45e95ef
bump copyright
MarcelKoch Jan 12, 2023
c5caeba
review updates:
MarcelKoch Jan 12, 2023
28bbc6f
safeguard against negative reduction size
MarcelKoch Jan 27, 2023
92feba4
remove unused function
MarcelKoch Jan 27, 2023
1c43396
fixes partition documentation
MarcelKoch Jan 27, 2023
d2c01b3
changes layout of gather ranges
MarcelKoch Feb 9, 2023
457b7f0
adapts sorting to changed ranges layout
MarcelKoch Feb 9, 2023
0d68caf
adapts consecutive check to changed ranges layout
MarcelKoch Feb 10, 2023
3752a5b
adds kernel to compress ranges
MarcelKoch Feb 10, 2023
4be22f9
review updates:
MarcelKoch Feb 10, 2023
9ef64f2
review update:
MarcelKoch Feb 21, 2023
29a5a8a
fixing dpcpp
MarcelKoch Feb 21, 2023
f8d3a8c
don't mix host and device buffers for MPI
MarcelKoch Feb 22, 2023
31ae609
adds permutation iterator
MarcelKoch Apr 19, 2023
3b7d008
use permute iterator for STL algorithms
MarcelKoch Apr 19, 2023
e6e3958
review updates:
MarcelKoch Apr 20, 2023
8edb0e5
review updates:
MarcelKoch Apr 20, 2023
8000e88
adds copy_assignable wrapper class
MarcelKoch Apr 20, 2023
348b067
adds invalid state exception
MarcelKoch Apr 26, 2023
af51e52
review updates:
MarcelKoch Apr 26, 2023
5fb31ba
Format files
ginkgo-bot Apr 26, 2023
0383f94
uses placement-new for copy-assignable wrapper
MarcelKoch Apr 27, 2023
e90d154
Format files
ginkgo-bot Apr 27, 2023
a8c9900
remove undefined lambda default constructor
MarcelKoch Apr 28, 2023
e3308ff
use workaround for old dpcpp version
MarcelKoch Jul 5, 2023
8cdb658
fixup after rebase
MarcelKoch Jul 7, 2023
5285e41
review updates:
MarcelKoch Jul 7, 2023
09bfa7f
use custom stream for thrust policy
MarcelKoch Aug 10, 2023
8393dca
correctly define permutation map
MarcelKoch Aug 17, 2023
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
54 changes: 54 additions & 0 deletions common/cuda_hip/distributed/partition_helpers_kernels.hpp.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2023, the Ginkgo authors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

template <typename GlobalIndexType>
void sort_by_range_start(
std::shared_ptr<const DefaultExecutor> exec,
array<GlobalIndexType>& range_start_ends,
array<experimental::distributed::comm_index_type>& part_ids)
{
auto num_ranges = range_start_ends.get_num_elems() / 2;
auto strided_indices = thrust::make_transform_iterator(
thrust::make_counting_iterator(0),
[] __host__ __device__(const int i) { return 2 * i; });
auto start_it = thrust::make_permutation_iterator(
range_start_ends.get_data(), strided_indices);
auto end_it = thrust::make_permutation_iterator(
range_start_ends.get_data() + 1, strided_indices);
auto zip_it = thrust::make_zip_iterator(
thrust::make_tuple(end_it, part_ids.get_data()));
< 6DB6 /td> thrust::stable_sort_by_key(thrust_policy(exec), start_it,
start_it + num_ranges, zip_it);
}

GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(
GKO_DECLARE_PARTITION_HELPERS_SORT_BY_RANGE_START);
1 change: 1 addition & 0 deletions common/unified/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(UNIFIED_SOURCES
components/format_conversion_kernels.cpp
components/precision_conversion_kernels.cpp
components/reduce_array_kernels.cpp
distributed/partition_helpers_kernels.cpp
distributed/partition_kernels.cpp
matrix/coo_kernels.cpp
matrix/csr_kernels.cpp
Expand Down
102 changes: 102 additions & 0 deletions common/unified/distributed/partition_helpers_kernels.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2023, the Ginkgo authors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#include "core/distributed/partition_helpers_kernels.hpp"


#include "common/unified/base/kernel_launch.hpp"
#include "common/unified/base/kernel_launch_reduction.hpp"


namespace gko {
namespace kernels {
namespace GKO_DEVICE_NAMESPACE {
namespace partition_helpers {


template <typename GlobalIndexType>
void check_consecutive_ranges(std::shared_ptr<const DefaultExecutor> exec,
const array<GlobalIndexType>& range_start_ends,
bool& result)
{
array<uint32> result_uint32{exec, 1};
auto num_ranges = range_start_ends.get_num_elems() / 2;
// need additional guard because DPCPP doesn't return the initial value for
// empty inputs
if (num_ranges > 1) {
run_kernel_reduction(
exec,
[] GKO_KERNEL(const auto i, const auto* ranges) {
return ranges[2 * i] == ranges[2 * i + 1];
},
[] GKO_KERNEL(const auto a, const auto b) {
return static_cast<uint32>(a && b);
},
[] GKO_KERNEL(auto x) { return x; }, static_cast<uint32>(true),
result_uint32.get_data(), num_ranges - 1,
range_start_ends.get_const_data() + 1);
result =
static_cast<bool>(exec->copy_val_to_host(result_uint32.get_data()));
} else {
result = true;
}
}

GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(
GKO_DECLARE_PARTITION_HELPERS_CHECK_CONSECUTIVE_RANGES);


template <typename GlobalIndexType>
void compress_ranges(std::shared_ptr<const DefaultExecutor> exec,
const array<GlobalIndexType>& range_start_ends,
array<GlobalIndexType>& range_offsets)
{
run_kernel(
exec,
[] GKO_KERNEL(const auto i, const auto* start_ends, auto* offsets) {
if (i == 0) {
offsets[0] = start_ends[0];
}
offsets[i + 1] = start_ends[2 * i + 1];
},
range_offsets.get_num_elems() - 1, range_start_ends.get_const_data(),
range_offsets.get_data());
}

GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(
GKO_DECLARE_PARTITION_HELPERS_COMPRESS_RANGES);


} // namespace partition_helpers
} // namespace GKO_DEVICE_NAMESPACE
} // namespace kernels
} // namespace gko
9 changes: 6 additions & 3 deletions common/unified/distributed/partition_kernels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,22 @@ void count_ranges(std::shared_ptr<const DefaultExecutor> exec,
template <typename GlobalIndexType>
void build_from_contiguous(std::shared_ptr<const DefaultExecutor> exec,
const array<GlobalIndexType>& ranges,
const array<comm_index_type>& part_id_mapping,
GlobalIndexType* range_bounds,
comm_index_type* part_ids)
{
run_kernel(
exec,
[] GKO_KERNEL(auto i, auto ranges, auto bounds, auto ids) {
[] GKO_KERNEL(auto i, auto ranges, auto mapping, auto bounds, auto ids,
bool uses_mapping) {
if (i == 0) {
bounds[0] = 0;
}
bounds[i + 1] = ranges[i + 1];
ids[i] = i;
ids[i] = uses_mapping ? mapping[i] : i;
},
ranges.get_num_elems() - 1, ranges, range_bounds, part_ids);
ranges.get_num_elems() - 1, ranges, part_id_mapping, range_bounds,
part_ids, part_id_mapping.get_num_elems() > 0);
Comment on lines +83 to +84
Copy link
10000 Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just an observation: Maybe we should ensure that empty arrays always point to nullptr, then we can use the pointer directly to check whether to use it

}

GKO_INSTANTIATE_FOR_EACH_INDEX_TYPE(GKO_PARTITION_BUILD_FROM_CONTIGUOUS);
Expand Down
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ if(GINKGO_BUILD_MPI)
PRIVATE
mpi/exception.cpp
distributed/matrix.cpp
distributed/partition_helpers.cpp
distributed/vector.cpp
distributed/preconditioner/schwarz.cpp)
endif()
Expand Down
130 changes: 130 additions & 0 deletions core/base/copy_assignable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2023, the Ginkgo authors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#ifndef GKO_CORE_BASE_COPY_ASSIGNABLE_HPP_
#define GKO_CORE_BASE_COPY_ASSIGNABLE_HPP_


#include <vector>


namespace gko {
namespace detail {


template <typename T, typename = void>
class copy_assignable;


/**
* Helper class to make a type copy assignable.
*
* This class wraps an object of a type that has a copy constructor, but not
* a copy assignment. This is most often the case for lambdas. The wrapped
* object can then be copy assigned, by relying on the copy constructor.
*
* @tparam T type with a copy constructor
*/
template <typename T>
class copy_assignable<
T, typename std::enable_if<std::is_copy_constructible<T>::value>::type> {
public:
copy_assignable() = default;

copy_assignable(const copy_assignable& other)
{
if (this != &other) {
*this = other;
}
}

copy_assignable(copy_assignable&& other) noexcept
{
if (this != &other) {
*this = std::move(other);
}
}

copy_assignable(const T& obj) : obj_{new (buf)(T)(obj)} {}

copy_assignable(T&& obj) : obj_{new (buf)(T)(std::move(obj))} {}

copy_assignable& operator=(const copy_assignable& other)
{
if (this != &other) {
if (obj_) {
obj_->~T();
}
obj_ = new (buf)(T)(*other.obj_);
}
return *this;
}

copy_assignable& operator=(copy_assignable&& other) noexcept
{
if (this != &other) {
if (obj_) {
obj_->~T();
}
obj_ = new (buf)(T)(std::move(*other.obj_));
}
return *this;
}

~copy_assignable()
{
if (obj_) {
obj_->~T();
}
}

template <typename... Args>
decltype(auto) operator()(Args&&... args) const
{
return (*obj_)(std::forward<Args>(args)...);
}

T const& get() const { return *obj_; }

T& get() { return *obj_; }

private:
//!< Store wrapped object on the stack, should use std::optional in c++17
T* obj_{};
alignas(T) unsigned char buf[sizeof(T)];
};


} // namespace detail
} // namespace gko

#endif // GKO_CORE_BASE_COPY_ASSIGNABLE_HPP_
Loading
0