ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
sccs_thread_sum.h
Go to the documentation of this file.
1#ifndef SCCS_THREAD_SUM_H
2#define SCCS_THREAD_SUM_H
3
4#include <array>
5#include <cstddef>
6#include <vector>
7
8#ifdef _OPENMP
9#include <omp.h>
10#endif
11
12namespace ModuleSccs
13{
14// Sums over the grid points [0, size): each thread runs accumulate(i, partial)
15// over a fixed static share of the points in index order, starting from zero,
16// and the partials are added in thread order. Unlike an OpenMP reduction, whose
17// partials meet in run-dependent order, a given thread count gives the same
18// sums on every run, and one thread gives the serial loop. The sqrt-CG
19// iteration count depends on the last bits of its inner products.
20template <std::size_t Count, typename Accumulate>
21std::array<double, Count> thread_sums(std::size_t size, const Accumulate &accumulate)
22{
23#ifdef _OPENMP
24 const int thread_count = omp_get_max_threads();
25#else
26 const int thread_count = 1;
27#endif
28 std::array<double, Count> zero;
29 zero.fill(0.0);
30 std::vector<std::array<double, Count>> partials(thread_count, zero);
31#ifdef _OPENMP
32#pragma omp parallel num_threads(thread_count)
33#endif
34 {
35#ifdef _OPENMP
36 const int thread = omp_get_thread_num();
37#else
38 const int thread = 0;
39#endif
40 std::array<double, Count> partial = zero;
41#ifdef _OPENMP
42#pragma omp for schedule(static)
43#endif
44 for (std::size_t i = 0; i < size; ++i)
45 {
46 accumulate(i, partial);
47 }
48 partials[thread] = partial;
49 }
50 std::array<double, Count> sums = zero;
51 for (int thread = 0; thread < thread_count; ++thread)
52 {
53 for (std::size_t k = 0; k < Count; ++k)
54 {
55 sums[k] += partials[thread][k];
56 }
57 }
58 return sums;
59}
60} // namespace ModuleSccs
61
62#endif
Definition sccs_fixed_src.h:11
std::array< double, Count > thread_sums(std::size_t size, const Accumulate &accumulate)
Definition sccs_thread_sum.h:21