Nektar++
Loading...
Searching...
No Matches
library
UnitTests
LibUtilities
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
38
#include <
MultiRegions/ExpList.h
>
39
#include <
SpatialDomains/MeshGraphIO.h
>
40
41
#include <
LibUtilities/BasicUtils/Field/Field.hpp
>
42
43
#include <
UnitTests/TestBoostSetup.hpp
>
44
#include <
UnitTests/TestGlobalConfiguration.hpp
>
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
59
using namespace
Nektar
;
60
using namespace
Nektar::LibUtilities
;
61
using namespace
Nektar::MultiRegions
;
62
63
NEKTAR_TEST_GLOBAL_CONFIGURATION
(
Nektar::UnitTests::TestArgs::None
);
64
65
template
<
typename
TData>
class
TestParallelReduce
66
{
67
public
:
68
TestParallelReduce
()
69
{
70
m_meshName
=
"run/segment.xml"
;
71
}
72
73
~TestParallelReduce
()
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);
96
auto
graph
=
SpatialDomains::MeshGraphIO::Read
(this->
m_session
);
97
98
for
(
int
i = 0; i < argc; ++i)
99
{
100
free(argv[i]);
101
}
102
delete
[] argv;
103
104
this->
m_expList
=
MemoryManager<ExpList>::AllocateSharedPtr
(
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
117
void
SetTestCase
()
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
206
protected
:
207
std::string
m_meshName
=
""
;
208
Field<TData, FieldState::Phys>
*
m_in
=
nullptr
;
209
ExpListSharedPtr
m_expList
;
210
SessionReaderSharedPtr
m_session
;
211
};
212
213
#include <
UnitTests/TestBoostTeardown.hpp
>
ExpList.h
Field.hpp
MeshGraphIO.h
TestBoostSetup.hpp
TestBoostTeardown.hpp
graph
SpatialDomains::MeshGraphSharedPtr graph
Definition
TestContFieldLocalToGlobalMap.cpp:224
TestGlobalConfiguration.hpp
NEKTAR_TEST_GLOBAL_CONFIGURATION
#define NEKTAR_TEST_GLOBAL_CONFIGURATION(args)
Definition
TestGlobalConfiguration.hpp:227
Nektar::Array
Definition
BasicUtils/SharedArray.hpp:57
Nektar::LibUtilities::Field
A Field represents expansion data to be operated on.
Definition
LibUtilities/BasicUtils/Field/Field.hpp:54
Nektar::LibUtilities::Field::GetBlocks
std::vector< BlockAccessor< TData, TState > > & GetBlocks()
Get BlockAccessors for the field.
Definition
LibUtilities/BasicUtils/Field/Field.hpp:603
Nektar::LibUtilities::Field::GetNumComponents
unsigned int GetNumComponents() const
Gets the number of components for a vector field.
Definition
LibUtilities/BasicUtils/Field/Field.hpp:659
Nektar::LibUtilities::SessionReader::CreateInstance
static SessionReaderSharedPtr CreateInstance(int argc, char *argv[])
Creates an instance of the SessionReader class.
Definition
SessionReader.h:146
Nektar::MemoryManager::AllocateSharedPtr
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
Definition
NekMemoryManager.hpp:167
Nektar::SpatialDomains::MeshGraphIO::Read
static MeshGraphSharedPtr Read(const LibUtilities::SessionReaderSharedPtr pSession, LibUtilities::DomainRangeShPtr rng=LibUtilities::NullDomainRangeShPtr, bool fillGraph=true, SpatialDomains::MeshGraphSharedPtr partitionedGraph=nullptr)
Definition
MeshGraphIO.cpp:65
TestParallelReduce
Definition
TestParallelReduce.hpp:66
TestParallelReduce::m_meshName
std::string m_meshName
Definition
TestParallelReduce.hpp:207
TestParallelReduce::m_expList
ExpListSharedPtr m_expList
Definition
TestParallelReduce.hpp:209
TestParallelReduce::m_session
SessionReaderSharedPtr m_session
Definition
TestParallelReduce.hpp:210
TestParallelReduce::sum
TData sum()
Definition
TestParallelReduce.hpp:146
TestParallelReduce::~TestParallelReduce
~TestParallelReduce()
Definition
TestParallelReduce.hpp:73
TestParallelReduce::m_in
Field< TData, FieldState::Phys > * m_in
Definition
TestParallelReduce.hpp:208
TestParallelReduce::TestParallelReduce
TestParallelReduce()
Definition
TestParallelReduce.hpp:68
TestParallelReduce::max
TData max()
Definition
TestParallelReduce.hpp:166
TestParallelReduce::Configure
void Configure()
Definition
TestParallelReduce.hpp:83
TestParallelReduce::SetTestCase
void SetTestCase()
Definition
TestParallelReduce.hpp:117
TestParallelReduce::min
TData min()
Definition
TestParallelReduce.hpp:186
Nektar::Collections::eNoCollection
@ eNoCollection
Definition
Operator.h:89
Nektar::LibUtilities
Definition
GitRevision.h:54
Nektar::LibUtilities::SessionReaderSharedPtr
std::shared_ptr< SessionReader > SessionReaderSharedPtr
Definition
SessionReader.h:127
Nektar::MultiRegions
Definition
BlockOpShapeBlock.cpp.in:78
Nektar::MultiRegions::ExpListSharedPtr
std::shared_ptr< ExpList > ExpListSharedPtr
Shared pointer to an ExpList object.
Definition
LocTraceToTraceMap.h:56
Nektar::UnitTests::TestArgs::None
@ None
exe
Nektar
Definition
CoupledSolver.h:2
Nektar::LibUtilities::func
Definition
Interpreter/Interpreter.cpp:108
Generated on Tue Dec 1 2026 10:16:13 for Nektar++ by
1.9.8