Nektar++
Loading...
Searching...
No Matches
TestParallelReduce.hpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: TestParallelReduce.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: Test fixture for the parallel_reduce loop-execution
32// backends.
33//
34///////////////////////////////////////////////////////////////////////////////
35
36#pragma once
37
40
42
45
46#include <algorithm>
47#include <cstdlib>
48#include <cstring>
49#include <limits>
50#include <memory>
51#include <numeric>
52#include <string>
53
54#if defined(_MSC_VER)
55#undef max
56#undef min
57#endif
58
59using namespace Nektar;
60using namespace Nektar::LibUtilities;
61using namespace Nektar::MultiRegions;
62
64
65template <typename TData> class TestParallelReduce
66{
67public:
69 {
70 m_meshName = "run/segment.xml";
71 }
72
74 {
75 BOOST_TEST_MESSAGE("teardown this->fixture");
76
77 if (this->m_in)
78 {
79 delete this->m_in;
80 }
81 }
82
83 void Configure()
84 {
85 BOOST_TEST_MESSAGE("Creating input and output fields");
86
87 // Construct a fake command-line argument array to be fed to
88 // Session::Reader::CreateInstance. The first element stands for
89 // the name of the executable which, in our case, doesn't matter.
90 int argc = 2;
91 char **argv = new char *[argc];
92 argv[0] = strdup("exe_name");
93 argv[1] = strdup(m_meshName.data());
94
95 this->m_session = SessionReader::CreateInstance(argc, argv);
97
98 for (int i = 0; i < argc; ++i)
99 {
100 free(argv[i]);
101 }
102 delete[] argv;
103
105 this->m_session, graph, true, "u", Collections::eNoCollection);
106
107 // One component per session variable, so the reducers are exercised
108 // over a field wider than a single component.
109 const auto &components = this->m_session->GetVariables();
110 auto blocksAttr = GetBlockAttributes<TData, FieldState::Phys>(
111 m_expList, MultiRegions::UnitInterleaveWidths(m_expList));
112 auto f_in =
113 Field<TData, FieldState::Phys>("f_in", blocksAttr, components, 1);
114 m_in = new Field<TData, FieldState::Phys>(std::move(f_in));
115 }
116
118 {
119 // GetCoords() and Equation::Evaluate() take double arrays, whatever
120 // TData is; the values are narrowed when copied into the field.
121 Array<OneD, double> x(this->m_expList->GetTotPoints(), 0.0);
122 Array<OneD, double> y(this->m_expList->GetTotPoints(), 0.0);
123 Array<OneD, double> z(this->m_expList->GetTotPoints(), 0.0);
124 Array<OneD, double> fce(this->m_expList->GetTotPoints());
125 this->m_expList->GetCoords(x, y, z);
126
127 for (unsigned int nc = 0; nc < this->m_in->GetNumComponents(); ++nc)
128 {
129 auto func = this->m_session->GetFunction("Forcing", nc);
130 func->Evaluate(x, y, z, fce);
131
132 auto ptr = fce.data();
133 for (unsigned int blk = 0; blk < this->m_in->GetBlocks().size();
134 ++blk)
135 {
136 auto &block = this->m_in->GetBlocks()[blk];
137 auto inptr =
138 block.template GetPtr<NektarSpaces::HostSpace, WriteOnly>();
139 auto size = block.GetNumElements() * block.GetNumData();
140 std::copy(ptr, ptr + size, inptr + nc * block.CompSize());
141 ptr += size;
142 }
143 }
144 }
145
146 TData sum()
147 {
148 TData out = 0;
149 for (unsigned int blk = 0; blk < this->m_in->GetBlocks().size(); ++blk)
150 {
151 auto x = this->m_in->GetBlocks()[blk]
152 .template GetPtr<NektarSpaces::HostSpace, ReadOnly>();
153 auto size = this->m_in->GetBlocks()[blk].GetNumElements() *
154 this->m_in->GetBlocks()[blk].GetNumData();
155 auto stride = this->m_in->GetBlocks()[blk].CompSize();
156
157 for (unsigned int nc = 0; nc < this->m_in->GetNumComponents();
158 ++nc, x += stride)
159 {
160 out = std::accumulate(x, x + size, out);
161 }
162 }
163 return out;
164 }
165
166 TData max()
167 {
168 TData out = std::numeric_limits<TData>::lowest();
169 for (unsigned int blk = 0; blk < this->m_in->GetBlocks().size(); ++blk)
170 {
171 auto x = this->m_in->GetBlocks()[blk]
172 .template GetPtr<NektarSpaces::HostSpace, ReadOnly>();
173 auto size = this->m_in->GetBlocks()[blk].GetNumElements() *
174 this->m_in->GetBlocks()[blk].GetNumData();
175 auto stride = this->m_in->GetBlocks()[blk].CompSize();
176
177 for (unsigned int nc = 0; nc < this->m_in->GetNumComponents();
178 ++nc, x += stride)
179 {
180 out = std::max(out, *(std::max_element(x, x + size)));
181 }
182 }
183 return out;
184 }
185
186 TData min()
187 {
188 TData out = std::numeric_limits<TData>::max();
189 for (unsigned int blk = 0; blk < this->m_in->GetBlocks().size(); ++blk)
190 {
191 auto x = this->m_in->GetBlocks()[blk]
192 .template GetPtr<NektarSpaces::HostSpace, ReadOnly>();
193 auto size = this->m_in->GetBlocks()[blk].GetNumElements() *
194 this->m_in->GetBlocks()[blk].GetNumData();
195 auto stride = this->m_in->GetBlocks()[blk].CompSize();
196
197 for (unsigned int nc = 0; nc < this->m_in->GetNumComponents();
198 ++nc, x += stride)
199 {
200 out = std::min(out, *(std::min_element(x, x + size)));
201 }
202 }
203 return out;
204 }
205
206protected:
207 std::string m_meshName = "";
211};
212
SpatialDomains::MeshGraphSharedPtr graph
#define NEKTAR_TEST_GLOBAL_CONFIGURATION(args)
A Field represents expansion data to be operated on.
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
unsigned int GetNumComponents() const
Gets the number of components for a vector field.
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
ExpListSharedPtr m_expList
SessionReaderSharedPtr m_session
Field< TData, FieldState::Phys > * m_in
std::shared_ptr< SessionReader > SessionReaderSharedPtr
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.