Skip to content

Commit

Permalink
Add KokkosExt::{construct_at,destroy_at} (arborx#1084)
Browse files Browse the repository at this point in the history
Add KokkosExt::{construct_at,destroy_at}
  • Loading branch information
aprokop committed May 8, 2024
1 parent fbe8660 commit 8073b82
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
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
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ add_executable(ArborX_Test_DetailsUtils.exe
tstDetailsUtils.cpp
tstDetailsKokkosExtStdAlgorithms.cpp
tstDetailsKokkosExtKernelStdAlgorithms.cpp
tstDetailsKokkosExtUninitializedMemoryAlgorithms.cpp
tstDetailsKokkosExtMinMaxReduce.cpp
tstDetailsKokkosExtViewHelpers.cpp
utf_main.cpp
Expand Down
61 changes: 61 additions & 0 deletions test/tstDetailsKokkosExtUninitializedMemoryAlgorithms.cpp
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);
}

0 comments on commit 8073b82

Please sign in to comment.