forked from arborx/ArborX
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add KokkosExt::{construct_at,destroy_at} (arborx#1084)
Add KokkosExt::{construct_at,destroy_at}
- Loading branch information
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
src/kokkos_ext/ArborX_DetailsKokkosExtUninitializedMemoryAlgorithms.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/**************************************************************************** | ||
* Copyright (c) 2024 by the ArborX authors * | ||
* All rights reserved. * | ||
* * | ||
* This file is part of the ArborX library. ArborX is * | ||
* distributed under a BSD 3-clause license. For the licensing terms see * | ||
* the LICENSE file in the top-level directory. * | ||
* * | ||
* SPDX-License-Identifier: BSD-3-Clause * | ||
****************************************************************************/ | ||
#ifndef ARBORX_DETAILS_KOKKOS_EXT_UNINITIALIZED_MEMORY_ALGORITHMS_HPP | ||
#define ARBORX_DETAILS_KOKKOS_EXT_UNINITIALIZED_MEMORY_ALGORITHMS_HPP | ||
|
||
#include <Kokkos_Assert.hpp> | ||
#include <Kokkos_Macros.hpp> | ||
|
||
namespace ArborX::Details::KokkosExt | ||
{ | ||
|
||
template <class T, class... Args> | ||
KOKKOS_FUNCTION constexpr T *construct_at(T *p, Args &&...args) | ||
{ | ||
return ::new (const_cast<void *>(static_cast<void const volatile *>(p))) | ||
T((Args &&) args...); | ||
} | ||
|
||
template <class T> | ||
KOKKOS_FUNCTION constexpr void destroy_at(T *p) | ||
{ | ||
KOKKOS_ASSERT(p != nullptr); | ||
p->~T(); | ||
} | ||
|
||
} // namespace ArborX::Details::KokkosExt | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/**************************************************************************** | ||
* Copyright (c) 2017-2022 by the ArborX authors * | ||
* All rights reserved. * | ||
* * | ||
* This file is part of the ArborX library. ArborX is * | ||
* distributed under a BSD 3-clause license. For the licensing terms see * | ||
* the LICENSE file in the top-level directory. * | ||
* * | ||
* SPDX-License-Identifier: BSD-3-Clause * | ||
****************************************************************************/ | ||
|
||
#include "ArborX_EnableDeviceTypes.hpp" // ARBORX_DEVICE_TYPES | ||
#include <ArborX_DetailsKokkosExtUninitializedMemoryAlgorithms.hpp> | ||
#include <ArborX_Exception.hpp> | ||
|
||
#include <Kokkos_Core.hpp> | ||
|
||
#include "BoostTest_CUDA_clang_workarounds.hpp" | ||
#include <boost/test/unit_test.hpp> | ||
|
||
#define BOOST_TEST_MODULE KokkosExtUninitializedMemoryAlgorithms | ||
|
||
namespace tt = boost::test_tools; | ||
|
||
struct NoDefaultConstructor | ||
{ | ||
int value; | ||
KOKKOS_FUNCTION NoDefaultConstructor(int x) | ||
: value(x) | ||
{} | ||
KOKKOS_FUNCTION ~NoDefaultConstructor() { value = -1; } | ||
}; | ||
|
||
BOOST_AUTO_TEST_CASE_TEMPLATE(construct_destroy_at, DeviceType, | ||
ARBORX_DEVICE_TYPES) | ||
{ | ||
using ArborX::Details::KokkosExt::construct_at; | ||
using ArborX::Details::KokkosExt::destroy_at; | ||
|
||
using ExecutionSpace = typename DeviceType::execution_space; | ||
ExecutionSpace exec; | ||
|
||
int const n = 2; | ||
Kokkos::View<NoDefaultConstructor *, DeviceType> view( | ||
Kokkos::view_alloc(exec, Kokkos::WithoutInitializing, "Test::view"), n); | ||
Kokkos::parallel_for( | ||
"Test::construct", Kokkos::RangePolicy<ExecutionSpace>(exec, 0, n), | ||
KOKKOS_LAMBDA(int i) { construct_at(&view(i), i); }); | ||
|
||
auto view_host = | ||
Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace{}, view); | ||
BOOST_TEST(view_host(0).value == 0); | ||
BOOST_TEST(view_host(1).value == 1); | ||
|
||
Kokkos::parallel_for( | ||
"Test::destroy", Kokkos::RangePolicy<ExecutionSpace>(exec, 0, n), | ||
KOKKOS_LAMBDA(int i) { destroy_at(&view(i)); }); | ||
Kokkos::deep_copy(exec, view_host, view); | ||
BOOST_TEST(view_host(0).value == -1); | ||
BOOST_TEST(view_host(1).value == -1); | ||
} |