QMCPACK
test_ParallelExecutorOPENMP.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2021 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
8 //
9 // File created by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #include "catch.hpp"
13 
15 
16 namespace qmcplusplus
17 {
18 /** Openmp generally works but is not guaranteed with std::atomic
19  */
20 void TestTaskOMP(const int ip, int& counter)
21 {
22 #pragma omp atomic update
23  counter++;
24 }
25 
26 TEST_CASE("ParallelExecutor<OPENMP> function case", "[concurrency]")
27 {
28  const int num_threads = omp_get_max_threads();
30  int count(0);
31  test_block(num_threads, TestTaskOMP, std::ref(count));
32  REQUIRE(count == num_threads);
33 }
34 
35 TEST_CASE("ParallelExecutor<OPENMP> lambda case", "[concurrency]")
36 {
37  const int num_threads = omp_get_max_threads();
38  std::cout << "omp_get_max_threads() == " << num_threads << '\n';
40  int count(0);
41  test_block(
42  num_threads,
43  [](int id, int& c) {
44 #pragma omp atomic update
45  c++;
46  },
47  std::ref(count));
48  REQUIRE(count == num_threads);
49 }
50 
51 TEST_CASE("ParallelExecutor<OPENMP> nested case", "[concurrency]")
52 {
53  int num_threads = 1;
55  int count(0);
56  auto nested_tasks = [num_threads](int task_id, int& my_count) {
58  test_block2(num_threads, TestTaskOMP, std::ref(my_count));
59  };
60 #ifdef _OPENMP
61  REQUIRE_THROWS_WITH(test_block(num_threads, nested_tasks, std::ref(count)),
62  Catch::Contains("ParallelExecutor should not be used for nested openmp threading"));
63 #endif
64 }
65 
66 } // namespace qmcplusplus
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
Abstraction for running concurrent tasks in parallel by an executor executor workers can be OpenMP th...
TEST_CASE("complex_helper", "[type_traits]")
omp_int_t omp_get_max_threads()
Definition: OpenMP.h:26
REQUIRE(std::filesystem::exists(filename))
void TestTaskOMP(const int ip, int &counter)
Openmp generally works but is not guaranteed with std::atomic.