Branch data Line data Source code
1 : : // *****************************************************************************
2 : : /*!
3 : : \file src/Inciter/IntegralReducer.cpp
4 : : \copyright 2012-2015 J. Bakosi,
5 : : 2016-2018 Los Alamos National Security, LLC.,
6 : : 2019-2021 Triad National Security, LLC.,
7 : : 2022-2025 J. Bakosi
8 : : All rights reserved. See the LICENSE file for details.
9 : : \brief Custom Charm++ reducer for merging integrals across PEs
10 : : \details Custom Charm++ reducer for merging integrals across PEs.
11 : : */
12 : : // *****************************************************************************
13 : :
14 : : #include <memory>
15 : :
16 : : #include "Integrals.hpp"
17 : : #include "IntegralReducer.hpp"
18 : : #include "Exception.hpp"
19 : :
20 : : namespace inciter {
21 : : namespace integrals {
22 : :
23 : : std::pair< int, std::unique_ptr<char[]> >
24 [ + - ]: 363 : serialize( std::size_t meshid, const std::vector< std::map<int,tk::real> >& d )
25 : : // *****************************************************************************
26 : : // Serialize integrals to raw memory stream
27 : : //! \param[in] meshid Mesh ID
28 : : //! \param[in] d Integral contributions
29 : : //! \return Pair of the length and the raw stream containing the serialized
30 : : //! vectors
31 : : // *****************************************************************************
32 : : {
33 : : // Prepare for serializing integrals to a raw binary stream, compute size
34 : : PUP::sizer sizer;
35 : : // cppcheck-suppress uninitvar
36 : : sizer | meshid;
37 : : sizer | const_cast< std::vector< std::map< int, tk::real > >& >( d );
38 : :
39 : : // Create raw character stream to store the serialized vectors
40 : : std::unique_ptr<char[]> flatData = std::make_unique<char[]>( sizer.size() );
41 : :
42 : : // Serialize integrals
43 : : PUP::toMem packer( flatData.get() );
44 : : // cppcheck-suppress uninitvar
45 : : packer | meshid;
46 : : packer | const_cast< std::vector< std::map< int, tk::real > >& >( d );
47 : :
48 : : // Return size of and raw stream
49 : 363 : return { sizer.size(), std::move(flatData) };
50 : : }
51 : :
52 : : CkReductionMsg*
53 [ + - ]: 93 : mergeIntegrals( int nmsg, CkReductionMsg **msgs )
54 : : // *****************************************************************************
55 : : // Charm++ custom reducer for merging integrals during reduction across PEs
56 : : //! \param[in] nmsg Number of messages in msgs
57 : : //! \param[in] msgs Charm++ reduction message containing the serialized
58 : : //! integrals
59 : : //! \return Aggregated integrals built for further aggregation if needed
60 : : // *****************************************************************************
61 : : {
62 : : // Will store deserialized integrals
63 : : std::size_t meshid;
64 : : std::vector< std::map< int, tk::real > > v;
65 : :
66 : : // Create PUP deserializer based on message passed in
67 [ + - ]: 93 : PUP::fromMem creator( msgs[0]->getData() );
68 : :
69 : : // Deserialize vector from raw stream
70 : : // cppcheck-suppress uninitvar
71 : : creator | meshid;
72 : : creator | v;
73 : :
74 [ + + ]: 265 : for (int m=1; m<nmsg; ++m) {
75 : : // Unpack vector
76 : : std::size_t mid;
77 : : std::vector< std::map< int, tk::real > > w;
78 [ + - ]: 172 : PUP::fromMem curCreator( msgs[m]->getData() );
79 : : // cppcheck-suppress uninitvar
80 : : curCreator | mid;
81 : : curCreator | w;
82 : : // Aggregate integrals
83 : : // cppcheck-suppress uninitvar
84 : : // cppcheck-suppress unreadVariable
85 [ + - ]: 172 : meshid = mid;
86 : : Assert( v.size() == w.size(), "Size mismatch during integrals aggregation");
87 : : Assert( v.size() == NUMINT, "Size mismatch during integrals aggregation" );
88 : : // Aggregate applying integrals aggregation policy
89 : : // Copy ITER, TIME, DT
90 : : // cppcheck-suppress containerOutOfBounds
91 : : v[ITER] = w[ITER];
92 : : // cppcheck-suppress containerOutOfBounds
93 : : v[TIME] = w[TIME];
94 : : // cppcheck-suppress containerOutOfBounds
95 : : v[DT] = w[DT];
96 : : // Sum integrals
97 : : // cppcheck-suppress containerOutOfBounds
98 [ + - ][ + + ]: 257 : for (const auto& [s,d] : w[MASS_FLOW_RATE]) v[MASS_FLOW_RATE][s] += d;
99 [ + - ][ + + ]: 190 : for (const auto& [s,d] : w[FORCE_X]) v[FORCE_X][s] += d;
100 [ + - ][ + + ]: 190 : for (const auto& [s,d] : w[FORCE_Y]) v[FORCE_Y][s] += d;
101 [ + - ][ + + ]: 190 : for (const auto& [s,d] : w[FORCE_Z]) v[FORCE_Z][s] += d;
102 : 172 : }
103 : :
104 : : // Serialize concatenated diagnostics vector to raw stream
105 : : // cppcheck-suppress uninitvar
106 [ + - ]: 93 : auto stream = serialize( meshid, v );
107 : :
108 : : // Forward serialized diagnostics
109 [ + - ]: 186 : return CkReductionMsg::buildNew( stream.first, stream.second.get() );
110 : 93 : }
111 : :
112 : : } // integrals::
113 : : } // inciter::
|