-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from uliegecsm/atomic-add
atomics: insert op with atomic min
- Loading branch information
Showing
6 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
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,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 |
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,4 @@ | ||
### TEST : InsertOp ### | ||
add_one_test( | ||
TEST_NAME InsertOp | ||
) |
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,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 |
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,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; | ||
} |