1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// *****************************************************************************
/*!
  \file      src/LinearSolver/ConjugateGradients.hpp
  \copyright 2012-2015 J. Bakosi,
             2016-2018 Los Alamos National Security, LLC.,
             2019-2021 Triad National Security, LLC.
             All rights reserved. See the LICENSE file for details.
  \brief     Charm++ chare array for distributed conjugate gradients
  \details   Charm++ chare array for asynchronous distributed
    conjugate gradients linear solver.

    There are a potentially large number of ConjugateGradients Charm++ chares.
    Each ConjugateGradient chare gets a chunk of the full load, due to partiting
    the mesh, on which the solve is performed.

    The implementation uses the Charm++ runtime system and is fully
    asynchronous, overlapping computation and communication. The algorithm
    utilizes the structured dagger (SDAG) Charm++ functionality.
*/
// *****************************************************************************
#ifndef ConjugateGradients_h
#define ConjugateGradients_h

#include "Types.hpp"
#include "CSR.hpp"

#include "NoWarning/conjugategradients.decl.h"

namespace tk {

//! \brief ConjugateGradients Charm++ chare array used to perform a distributed
//!   linear solve with the conjugate gradients algorithm
class ConjugateGradients : public CBase_ConjugateGradients {

  public:
    #if defined(__clang__)
      #pragma clang diagnostic push
      #pragma clang diagnostic ignored "-Wunused-parameter"
    #elif defined(STRICT_GNUC)
      #pragma GCC diagnostic push
      #pragma GCC diagnostic ignored "-Wunused-parameter"
    #endif
    // Include Charm++ SDAG code. See http://charm.cs.illinois.edu/manuals/html/
    // charm++/manual.html, Sec. "Structured Control Flow: Structured Dagger".
    ConjugateGradients_SDAG_CODE
    #if defined(__clang__)
      #pragma clang diagnostic pop
    #elif defined(STRICT_GNUC)
      #pragma GCC diagnostic pop
    #endif

    //! Constructor
    explicit ConjugateGradients(
      const CSR& A,
      const std::vector< tk::real >& x,
      const std::vector< tk::real >& b,
      const std::vector< std::size_t >& gid,
      const std::unordered_map< std::size_t, std::size_t >& lid,
      const NodeCommMap& nodecommmap );

    #if defined(__clang__)
      #pragma clang diagnostic push
      #pragma clang diagnostic ignored "-Wundefined-func-template"
    #endif
    //! Constructor taking a tuple of {A,x,b} by rvalue reference
    explicit ConjugateGradients(<--- Member variable 'ConjugateGradients::m_tol' is not initialized in the constructor.
      std::tuple< tk::CSR,
                  std::vector< tk::real >,
                  std::vector< tk::real > >&& system,
      const std::vector< std::size_t >& gid,
      const std::unordered_map< std::size_t, std::size_t >& lid,
      const NodeCommMap& nodecommmap ) :
      ConjugateGradients( std::move(std::get<0>(system)),
                          std::move(std::get<1>(system)),
                          std::move(std::get<2>(system)),
                          gid, lid, nodecommmap ) {}

    //! Migrate constructor
    explicit ConjugateGradients( CkMigrateMessage* ) {}<--- Member variable 'ConjugateGradients::m_nr' is not initialized in the constructor.<--- Member variable 'ConjugateGradients::m_nb' is not initialized in the constructor.<--- Member variable 'ConjugateGradients::m_nq' is not initialized in the constructor.<--- Member variable 'ConjugateGradients::m_normb' is not initialized in the constructor.<--- Member variable 'ConjugateGradients::m_it' is not initialized in the constructor.<--- Member variable 'ConjugateGradients::m_maxit' is not initialized in the constructor.<--- Member variable 'ConjugateGradients::m_tol' is not initialized in the constructor.<--- Member variable 'ConjugateGradients::m_rho' is not initialized in the constructor.<--- Member variable 'ConjugateGradients::m_rho0' is not initialized in the constructor.<--- Member variable 'ConjugateGradients::m_alpha' is not initialized in the constructor.<--- Member variable 'ConjugateGradients::m_converged' is not initialized in the constructor.<--- Member variable 'ConjugateGradients::m_nx' is not initialized in the constructor.
    #if defined(__clang__)
      #pragma clang diagnostic pop
    #endif

    //! Solve linear system
    void solve( std::size_t maxit, tk::real tol, CkCallback c );

    //! Initialize linear solve: set initial guess and boundary conditions
    void init( const std::vector< tk::real >& x,
               const std::vector< tk::real >& b,
               const std::unordered_map< std::size_t,
                       std::vector< std::pair< bool, tk::real > > >& bc,
               std::size_t ignorebc,
               CkCallback cb );

    //! Setup solver
    void setup( CkCallback c );

    //! Compute the norm of the right hand side
    void normb( tk::real n );

    //! Compute rho = (r,r)
    void rho( tk::real r );

    //! Receive contributions to r = b - A * x on chare-boundaries
    void comres( const std::vector< std::size_t >& gid,
                 const std::vector< std::vector< tk::real > >& rc );

    //! Receive contributions to boundary conditions on chare-boundaries
    void combc( const std::unordered_map< std::size_t,
                       std::vector< std::pair< bool, tk::real > > >& bc );

    //! Receive contributions to q = A * p on chare-boundaries
    void comq( const std::vector< std::size_t >& gid,
               const std::vector< std::vector< tk::real > >& qc );

    void comx( const std::vector< std::size_t >& gid,
               const std::vector< std::vector< tk::real > >& xc );

    //! Compute the dot product (p,q)
    void pq( tk::real d );

    //! Compute the norm of the residual: (r,r)
    void normres( tk::real r );

    //! Access solution
    std::vector< tk::real > solution() const { return m_x; }

    //! Return convergence flag
    bool converged() const { return m_converged; }

    /** @name Pack/unpack (Charm++ serialization) routines */
    ///@{
    //! \brief Pack/Unpack serialize member function
    //! \param[in,out] p Charm++'s PUP::er serializer object reference
    void pup( PUP::er &p ) override {
      p | m_A;
      p | m_x;
      p | m_b;
      p | m_gid;
      p | m_lid;
      p | m_nodeCommMap;
      p | m_r;
      p | m_rc;
      p | m_nr;
      p | m_bc;
      p | m_bcc;
      p | m_bcmask;
      p | m_nb;
      p | m_p;
      p | m_q;
      p | m_qc;
      p | m_nq;
      p | m_initres;
      p | m_solved;
      p | m_normb;
      p | m_it;
      p | m_maxit;
      p | m_tol;
      p | m_rho;
      p | m_rho0;
      p | m_alpha;
      p | m_converged;
      p | m_xc;
      p | m_nx;
    }
    //! \brief Pack/Unpack serialize operator|
    //! \param[in,out] p Charm++'s PUP::er serializer object reference
    //! \param[in,out] c ConjugateGradients object reference
    friend void operator|( PUP::er& p, ConjugateGradients& c ) { c.pup(p); }
    ///@}

  private:
    //! Sparse matrix
    CSR m_A;
    //! Solution/unknown
    std::vector< tk::real > m_x;
    //! Right hand side
    std::vector< tk::real > m_b;
    //! Global node IDs
    std::vector< std::size_t > m_gid;
    //! Local node IDs associated to global ones
    std::unordered_map< std::size_t, std::size_t > m_lid;
    //! Global mesh node IDs shared with other chares associated to chare IDs
    NodeCommMap m_nodeCommMap;
    //! Auxiliary vector for CG solve
    std::vector< tk::real > m_r;
    //! Receive buffer for communication of r = b - A * x
    std::unordered_map< std::size_t, std::vector< tk::real > > m_rc;
    //! Counter for assembling m_r
    std::size_t m_nr;
    //! Dirichlet boundary conditions
    std::unordered_map< std::size_t,
        std::vector< std::pair< bool, tk::real > > > m_bc;
    //! Dirichlet boundary conditions communication buffer
    std::unordered_map< std::size_t,
        std::vector< std::pair< bool, tk::real > > > m_bcc;
    //! Dirichlet boundary condition mask
    std::vector< tk::real > m_bcmask;
    //! Counter for assembling boundary conditions
    std::size_t m_nb;
    //! Auxiliary vector for CG solve
    std::vector< tk::real > m_p;
    //! Auxiliary vector for CG solve
    std::vector< tk::real > m_q;
    //! Receive buffer for communication of q = A * p
    std::unordered_map< std::size_t, std::vector< tk::real > > m_qc;
    //! Counter for assembling m_q
    std::size_t m_nq;
    //! Charm++ callback to continue with when the setup is complete
    CkCallback m_initres;
    //! Charm++ callback to continue with when the solve is complete
    CkCallback m_solved;
    //! L2 norm of the right hand side
    tk::real m_normb;
    //! Iteration count
    std::size_t m_it;
    //! Max iteration count
    std::size_t m_maxit;
    //! Stop tolerance
    tk::real m_tol;
    //! Helper scalar for CG algorithm
    tk::real m_rho;
    //! Helper scalar for CG algorithm
    tk::real m_rho0;
    //! Helper scalar for CG algorithm
    tk::real m_alpha;
    //! Convergence flag: true if linear smoother converged to tolerance
    bool m_converged;
    //! Receive buffer for solution
    std::unordered_map< std::size_t, std::vector< tk::real > > m_xc;
    //! Counter for assembling the solution on chare boundaries
    std::size_t m_nx;

    //! Initiate computationa of dot product of two vectors
    void dot( const std::vector< tk::real >& a,
              const std::vector< tk::real >& b,
              CkCallback c );

    //! Initiate A * x for computing the residual, r = b - A * x
    void residual();
    //! Finish computing the initial residual, r = b - A * x
    void initres();

    //! Apply boundary conditions
    void apply( CkCallback cb );

    //! Initiate computing q = A * p
    void qAp();
    //! Finish computing q = A * p
    void q();

    //! Start next linear solver iteration
    void next();

    //! Assemble solution on chare boundaries
    void x();
};

} // tk::

#endif // ConjugateGradients_h