Skip to content

Commit

Permalink
Allow passing a temporary std::vector to partition_space (kokkos#6167)
Browse files Browse the repository at this point in the history
* Allow passing a temporary std::vector to partition_space

* Comment on ignoring content of weights

* Add comment that we test that we can use temporaries for weights
  • Loading branch information
masterleinad committed Jun 2, 2023
1 parent 65ffe4c commit 0b7bed5
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 8 deletions.
4 changes: 3 additions & 1 deletion core/src/Cuda/Kokkos_Cuda_Instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,13 @@ std::vector<Cuda> partition_space(const Cuda&, Args...) {
}

template <class T>
std::vector<Cuda> partition_space(const Cuda&, std::vector<T>& weights) {
std::vector<Cuda> partition_space(const Cuda&, std::vector<T> const& weights) {
static_assert(
std::is_arithmetic<T>::value,
"Kokkos Error: partitioning arguments must be integers or floats");

// We only care about the number of instances to create and ignore weights
// otherwise.
std::vector<Cuda> instances(weights.size());
Impl::create_Cuda_instances(instances);
return instances;
Expand Down
4 changes: 3 additions & 1 deletion core/src/HIP/Kokkos_HIP_Instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,13 @@ std::vector<HIP> partition_space(const HIP &, Args...) {
}

template <class T>
std::vector<HIP> partition_space(const HIP &, std::vector<T> &weights) {
std::vector<HIP> partition_space(const HIP &, std::vector<T> const &weights) {
static_assert(
std::is_arithmetic<T>::value,
"Kokkos Error: partitioning arguments must be integers or floats");

// We only care about the number of instances to create and ignore weights
// otherwise.
std::vector<HIP> instances(weights.size());
Impl::create_HIP_instances(instances);
return instances;
Expand Down
2 changes: 1 addition & 1 deletion core/src/Kokkos_Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ std::vector<ExecSpace> partition_space(ExecSpace const& space, Args...) {

template <class ExecSpace, class T>
std::vector<ExecSpace> partition_space(ExecSpace const& space,
std::vector<T>& weights) {
std::vector<T> const& weights) {
static_assert(is_execution_space<ExecSpace>::value,
"Kokkos Error: partition_space expects an Execution Space as "
"first argument");
Expand Down
2 changes: 1 addition & 1 deletion core/src/OpenMP/Kokkos_OpenMP_Instance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ std::vector<OpenMP> partition_space(OpenMP const& main_instance, Args... args) {

template <typename T>
std::vector<OpenMP> partition_space(OpenMP const& main_instance,
std::vector<T>& weights) {
std::vector<T> const& weights) {
return Impl::create_OpenMP_instances(main_instance, weights);
}
} // namespace Experimental
Expand Down
5 changes: 4 additions & 1 deletion core/src/SYCL/Kokkos_SYCL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ std::vector<SYCL> partition_space(const SYCL& sycl_space, Args...) {

template <class T>
std::vector<SYCL> partition_space(const SYCL& sycl_space,
std::vector<T>& weights) {
std::vector<T> const& weights) {
static_assert(
std::is_arithmetic<T>::value,
"Kokkos Error: partitioning arguments must be integers or floats");
Expand All @@ -178,6 +178,9 @@ std::vector<SYCL> partition_space(const SYCL& sycl_space,
sycl::device device =
sycl_space.impl_internal_space_instance()->m_queue->get_device();
std::vector<SYCL> instances;

// We only care about the number of instances to create and ignore weights
// otherwise.
instances.reserve(weights.size());
for (unsigned int i = 0; i < weights.size(); ++i)
instances.emplace_back(sycl::queue(context, device));
Expand Down
6 changes: 3 additions & 3 deletions core/unit_test/TestExecSpacePartitioning.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ TEST(TEST_CATEGORY, partitioning_by_args) {
}

TEST(TEST_CATEGORY, partitioning_by_vector) {
std::vector<int> weights{1, 1};
auto instances =
Kokkos::Experimental::partition_space(TEST_EXECSPACE(), weights);
// Make sure we can use a temporary as argument for weights
auto instances = Kokkos::Experimental::partition_space(
TEST_EXECSPACE(), std::vector<int> /*weights*/ {1, 1});
ASSERT_EQ(int(instances.size()), 2);
test_partitioning(instances);
}
Expand Down

0 comments on commit 0b7bed5

Please sign in to comment.