Nektar++
Loading...
Searching...
No Matches
LoopExecutionSerialAVX.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: LoopExecutionSerialAVX.hpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description:
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#pragma once
36
37namespace Nektar
38{
39
40// Parallel for launchers.
41template <typename ExecSpace, typename Functor,
42 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Serial> ||
43 std::is_same_v<ExecSpace, NektarSpaces::AVX>,
44 bool>
45 Enable = true>
46inline void parallel_for(const size_t begin, const size_t end,
47 const Functor &functor)
48{
49 for (size_t i = begin; i < end; ++i)
50 {
51 functor(i);
52 }
53}
54
55// Parallel reduction launchers.
56template <typename ExecSpace, bool init, typename Reduction, typename Functor,
57 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Serial> ||
58 std::is_same_v<ExecSpace, NektarSpaces::AVX>,
59 bool>
60 Enable = true>
61inline void parallel_reduce(const size_t begin, const size_t end,
62 const Functor &functor,
63 typename Reduction::value_type *red)
64{
65 if (init)
66 {
67 *red = reduce_identity<Reduction>();
68 }
69
70 for (size_t i = begin; i < end; ++i)
71 {
72 *red = reduce_join<Reduction>(*red, functor(i));
73 }
74}
75
76// Parallel reduction launchers.
77template <typename ExecSpace, typename Reduction, typename Functor,
78 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Serial> ||
79 std::is_same_v<ExecSpace, NektarSpaces::AVX>,
80 bool>
81 Enable = true>
82inline void parallel_reduce(const size_t begin, const size_t end,
83 const Functor &functor,
84 typename Reduction::value_type &red)
85{
86 parallel_reduce<ExecSpace, true, Reduction>(begin, end, functor, &red);
87}
88
89// Parallel reduction of nred values: functor(i, vals) writes those of index i
90// to vals[0, nred), which Reduction combines into red[0, nred), together with
91// the values already there unless init. At most internalReductionMaxValues
92// values, the partial results the reduction scratch holds.
93template <typename ExecSpace, bool init, typename Reduction, unsigned int nred,
94 typename Functor,
95 std::enable_if_t<std::is_same_v<ExecSpace, NektarSpaces::Serial> ||
96 std::is_same_v<ExecSpace, NektarSpaces::AVX>,
97 bool>
98 Enable = true>
99inline void parallel_reduce(const size_t begin, const size_t end,
100 const Functor &functor,
101 typename Reduction::value_type *red)
102{
103 using TData = typename Reduction::value_type;
104
105 static_assert(nred <= internalReductionMaxValues,
106 "parallel_reduce: too many values.");
107
108 TData acc[nred];
109 for (unsigned int s = 0; s < nred; ++s)
110 {
111 acc[s] = reduce_identity<Reduction>();
112 }
113
114 for (size_t i = begin; i < end; ++i)
115 {
116 TData vals[nred];
117 functor(i, vals);
118 for (unsigned int s = 0; s < nred; ++s)
119 {
120 acc[s] = reduce_join<Reduction>(acc[s], vals[s]);
121 }
122 }
123
124 for (unsigned int s = 0; s < nred; ++s)
125 {
126 red[s] = init ? acc[s] : reduce_join<Reduction>(red[s], acc[s]);
127 }
128}
129
130} // namespace Nektar
void parallel_for(const size_t begin, const size_t end, const Functor &functor)
void parallel_reduce(const size_t begin, const size_t end, const Functor &functor, typename Reduction::value_type *red)
constexpr unsigned int internalReductionMaxValues
Definition Backends.hpp:67