Skip to content

Commit

Permalink
Merge pull request #7 from uliegecsm/atomic-add
Browse files Browse the repository at this point in the history
atomics: insert op with atomic min
  • Loading branch information
romintomasetti authored Jun 20, 2024
2 parents 2a0b5c3 + 5521471 commit c7938e7
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ add_library(Kokkos::utils ALIAS KokkosUtils)
target_sources(
KokkosUtils
INTERFACE
include/kokkos-utils/atomics/InsertOp.hpp
include/kokkos-utils/concepts/View.hpp
)

Expand Down
27 changes: 27 additions & 0 deletions include/kokkos-utils/atomics/InsertOp.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef KOKKOS_UTILS_ATOMICS_INSERTOP_HPP
#define KOKKOS_UTILS_ATOMICS_INSERTOP_HPP

#include <concepts>

#include "kokkos-utils/concepts/View.hpp"

namespace Kokkos::utils::atomics
{

/**
* @brief Insert an element in a view at a specific index using @c Kokkos::atomic_min.
*
* To be used with *e.g.* @c Kokkos::UnorderedMap::insert.
*/
struct InsertMin
{
template <concepts::View ViewType, std::integral IndexType, typename ValueType>
KOKKOS_FUNCTION
void op(const ViewType& values, const IndexType index, ValueType&& value) const {
Kokkos::atomic_min(&values(index), std::forward<ValueType>(value));
}
};

} // namespace Kokkos::utils::atomics

#endif // KOKKOS_UTILS_ATOMICS_INSERTOP_HPP
29 changes: 27 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ function(add_one_test)
${SOURCE_FILE}
)

# Compile options.
target_compile_options(
${EXECUTABLE_NAME}
PRIVATE
-Wall
-Wextra
-Werror
)

# Link the executable to the library, GTest and Kokkos.
target_link_libraries(
${EXECUTABLE_NAME}
PRIVATE
Kokkos::utils
GTest::gtest_main
Kokkos::kokkoscore
test_common
)

# Add the test to CTest.
Expand All @@ -55,5 +63,22 @@ function(add_one_test)

endfunction()

# Common test library (object-like) which each test links to.
add_library(test_common OBJECT)
target_sources(
test_common
PRIVATE
main.cpp
)
target_link_libraries(
test_common
PUBLIC
GTest::gtest
Kokkos::kokkoscore
)

### TESTS : atomics ###
add_subdirectory(atomics)

### TESTS : concepts ###
add_subdirectory(concepts)
4 changes: 4 additions & 0 deletions tests/atomics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### TEST : InsertOp ###
add_one_test(
TEST_NAME InsertOp
)
52 changes: 52 additions & 0 deletions tests/atomics/test_InsertOp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "gtest/gtest.h"

#include "Kokkos_Core.hpp"

#include "kokkos-utils/atomics/InsertOp.hpp"

using execution_space = Kokkos::DefaultExecutionSpace;

/**
* @file
*
* @addtogroup unittests
*
* **Atomic insertion**
*
* This group of tests check the behavior of our atomic insert operators.
*/

namespace Kokkos::utils::tests::atomics
{

//! @test Check that @ref Kokkos::utils::atomics::InsertMin works as expected.
TEST(atomics, InsertMin)
{
using view_t = Kokkos::View<int*, execution_space>;

constexpr int trials = 150;

const execution_space space {};

const view_t data(Kokkos::view_alloc(space, "data"), 2);

const utils::atomics::InsertMin insert_min {};

Kokkos::parallel_for(
"atomics::InsertMin",
Kokkos::RangePolicy<execution_space>(space, 0, trials),
KOKKOS_LAMBDA(const int trial)
{
insert_min.op(data, 0, trials - trial - 2);
insert_min.op(data, 1, 2);
}
);
space.fence();

const auto mirror = Kokkos::create_mirror_view_and_copy(Kokkos::DefaultHostExecutionSpace{}, data);

ASSERT_EQ(mirror(0), -1);
ASSERT_EQ(mirror(1), 0);
}

} // namespace Kokkos::utils::tests::atomics
17 changes: 17 additions & 0 deletions tests/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "gtest/gtest.h"

#include "Kokkos_Core.hpp"

//! Initialize Google Test and @c Kokkos.
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);

Kokkos::initialize(argc, argv);

const auto result = RUN_ALL_TESTS();

Kokkos::finalize();

return result;
}

0 comments on commit c7938e7

Please sign in to comment.