File: main.cpp

package info (click to toggle)
libomp-jonathonl 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 76 kB
  • sloc: cpp: 432; makefile: 4
file content (49 lines) | stat: -rw-r--r-- 1,145 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

#include "omp.hpp"
#include <iostream>
#include <cassert>

int main()
{
  std::vector<double> arr(257, 0.0);
  std::mutex named_section;
  std::size_t total = 0;

  omp::internal::thread_pool2 pool(8);
  omp::parallel_for_exp(omp::static_schedule(), arr.begin(), arr.end(), [&total, &named_section](double& element, const omp::iteration_context& ctx)
  {
    element = (ctx.index + 1);

    omp::critical(named_section, [&total, element, &ctx]()
    {
      ++total;
    });

    {
      std::lock_guard<std::mutex> critical(named_section);
      // lock_guard is usually a better alternative to omp::critical.
    }

    omp::critical([]()
    {

    });
  }, pool); // 8);

  omp::parallel_for(omp::dynamic_schedule(), omp::sequence_iterator(-2), omp::sequence_iterator(5), [&total, &named_section](int& element, const omp::iteration_context& ctx)
  {
    std::lock_guard<std::mutex> critical(named_section);
    ++total;
  }, 3);

  std::cout << total << std::endl;
  assert(total == 264);

  unsigned num_threads = 8;
  omp::parallel([](std::size_t thread_idx)
  {

  }, num_threads);

  return total == 264 ? EXIT_SUCCESS : EXIT_FAILURE;
}