-
Notifications
You must be signed in to change notification settings - Fork 579
/
Copy pathAmesos2_MUMPS_def.hpp
513 lines (423 loc) · 16.5 KB
/
Amesos2_MUMPS_def.hpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// @HEADER
//
// ***********************************************************************
//
// Amesos2: Templated Direct Sparse Solver Package
// Copyright 2011 Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux ([email protected])
//
// ***********************************************************************
//
// @HEADER
/**
\file Amesos2_MUMPS_def.hpp
\author Joshua Dennis Booth <[email protected]>
\brief Definitions for the Amesos2 MUMPS solver interface
*/
#ifndef AMESOS2_MUMPS_DEF_HPP
#define AMESOS2_MUMPS_DEF_HPP
#include <Teuchos_Tuple.hpp>
#include <Teuchos_ParameterList.hpp>
#include <Teuchos_StandardParameterEntryValidators.hpp>
#ifdef HAVE_MPI
#include <Teuchos_DefaultMpiComm.hpp>
#endif
#include <limits>
#include "Amesos2_SolverCore_def.hpp"
#include "Amesos2_MUMPS_decl.hpp"
namespace Amesos2
{
template <class Matrix, class Vector>
MUMPS<Matrix,Vector>::MUMPS(
Teuchos::RCP<const Matrix> A,
Teuchos::RCP<Vector> X,
Teuchos::RCP<const Vector> B )
: SolverCore<Amesos2::MUMPS,Matrix,Vector>(A, X, B)
, nzvals_() // initialize to empty arrays
, rowind_()
, colptr_()
, is_contiguous_(true)
{
typedef FunctionMap<MUMPS,scalar_type> function_map;
MUMPS_MATRIX_LOAD = false;
MUMPS_STRUCT = false;
#ifdef HAVE_MPI
using Teuchos::Comm;
using Teuchos::MpiComm;
using Teuchos::RCP;
using Teuchos::rcp;
using Teuchos::rcp_dynamic_cast;
//Comm
mumps_par.comm_fortran = -987654;
RCP<const Comm<int> > matComm = this->matrixA_->getComm();
//Add Exception Checking
TEUCHOS_TEST_FOR_EXCEPTION(
matComm.is_null(), std::logic_error, "Amesos2::Comm");
RCP<const MpiComm<int> > matMpiComm =
rcp_dynamic_cast<const MpiComm<int> >(matComm);
//Add Exception Checking
TEUCHOS_TEST_FOR_EXCEPTION(
matMpiComm->getRawMpiComm().is_null(),
std::logic_error, "Amesos2::MPI");
MPI_Comm rawMpiComm = (* (matMpiComm->getRawMpiComm()) )();
mumps_par.comm_fortran = (int) MPI_Comm_c2f(rawMpiComm);
#endif
mumps_par.job = -1;
mumps_par.par = 1;
mumps_par.sym = 0;
function_map::mumps_c(&(mumps_par)); //init
MUMPS_ERROR();
mumps_par.n = this->globalNumCols_;
/*Default parameters*/
mumps_par.icntl[0] = -1; // Turn off error messages
mumps_par.icntl[1] = -1; // Turn off diagnositic printing
mumps_par.icntl[2] = -1; // Turn off global information messages
mumps_par.icntl[3] = 1; // No messages
mumps_par.icntl[4] = 0; // Matrix given in assembled (Triplet) form
mumps_par.icntl[5] = 7; // Choose column permuation automatically
mumps_par.icntl[6] = 7; // Choose ordering methods automatically
mumps_par.icntl[7] = 7; // Choose scaling automatically
mumps_par.icntl[8] = 1; // Compuate Ax = b;
mumps_par.icntl[9] = 0; // iterative refinement
mumps_par.icntl[10] = 0; //Do not collect statistics
mumps_par.icntl[11] = 0; // Automatic choice of ordering strategy
mumps_par.icntl[12] = 0; //Use ScaLAPACK for root node
mumps_par.icntl[13] = 20; // Increase memory allocation 20% at a time
mumps_par.icntl[17] = 0;
mumps_par.icntl[18] = 0; // do not provide back the Schur complement
mumps_par.icntl[19] = 0; // RHS is given in dense form
mumps_par.icntl[20] = 0; //RHS is in dense form
mumps_par.icntl[21] = 0; // Do all compuations in-core
mumps_par.icntl[22] = 0; // No max MB for work
mumps_par.icntl[23] = 0; // Do not perform null pivot detection
mumps_par.icntl[24] = 0; // No null space basis compuation
mumps_par.icntl[25] = 0; // Do not condense/reduce Schur RHS
mumps_par.icntl[27] = 1; // sequential analysis
mumps_par.icntl[28] = 0; //
mumps_par.icntl[29] = 0; //
mumps_par.icntl[30] = 0; //
mumps_par.icntl[31] = 0;
mumps_par.icntl[32] = 0;
}
template <class Matrix, class Vector>
MUMPS<Matrix,Vector>::~MUMPS( )
{
/* Clean up the struc*/
if(MUMPS_STRUCT == true)
{
free(mumps_par.a);
free(mumps_par.jcn);
free(mumps_par.irn);
}
}
template<class Matrix, class Vector>
int
MUMPS<Matrix,Vector>::preOrdering_impl()
{
/* TODO: Define what it means for MUMPS
*/
#ifdef HAVE_AMESOS2_TIMERS
Teuchos::TimeMonitor preOrderTimer(this->timers_.preOrderTime_);
#endif
return(0);
}//end preOrdering_impl()
template <class Matrix, class Vector>
int
MUMPS<Matrix,Vector>::symbolicFactorization_impl()
{
typedef FunctionMap<MUMPS,scalar_type> function_map;
mumps_par.par = 1;
mumps_par.job = 1; // sym factor
function_map::mumps_c(&(mumps_par));
MUMPS_ERROR();
return(0);
}//end symblicFactortion_impl()
template <class Matrix, class Vector>
int
MUMPS<Matrix,Vector>::numericFactorization_impl()
{
using Teuchos::as;
typedef FunctionMap<MUMPS,scalar_type> function_map;
if ( this->root_ )
{
{ // Do factorization
#ifdef HAVE_AMESOS2_TIMERS
Teuchos::TimeMonitor numFactTimer(this->timers_.numFactTime_);
#endif
#ifdef HAVE_AMESOS2_VERBOSE_DEBUG
std::cout << "MUMPS:: Before numeric factorization" << std::endl;
std::cout << "nzvals_ : " << nzvals_.toString() << std::endl;
std::cout << "rowind_ : " << rowind_.toString() << std::endl;
std::cout << "colptr_ : " << colptr_.toString() << std::endl;
#endif
}
}
mumps_par.job = 2;
function_map::mumps_c(&(mumps_par));
MUMPS_ERROR();
return(0);
}//end numericFactorization_impl()
template <class Matrix, class Vector>
int
MUMPS<Matrix,Vector>::solve_impl(
const Teuchos::Ptr<MultiVecAdapter<Vector> > X,
const Teuchos::Ptr<const MultiVecAdapter<Vector> > B) const
{
typedef FunctionMap<MUMPS,scalar_type> function_map;
using Teuchos::as;
const global_size_type ld_rhs = this->root_ ? X->getGlobalLength() : 0;
const size_t nrhs = X->getGlobalNumVectors();
const size_t val_store_size = as<size_t>(ld_rhs * nrhs);
xvals_.resize(val_store_size);
bvals_.resize(val_store_size);
#ifdef HAVE_AMESOS2_TIMERS
Teuchos::TimeMonitor mvConvTimer(this->timers_.vecConvTime_);
Teuchos::TimeMonitor redistTimer( this->timers_.vecRedistTime_ );
#endif
if ( is_contiguous_ == true ) {
Util::get_1d_copy_helper<MultiVecAdapter<Vector>,
slu_type>::do_get(B, bvals_(),as<size_t>(ld_rhs), ROOTED, this->rowIndexBase_);
}
else {
Util::get_1d_copy_helper<MultiVecAdapter<Vector>,
slu_type>::do_get(B, bvals_(),as<size_t>(ld_rhs), CONTIGUOUS_AND_ROOTED, this->rowIndexBase_);
}
int ierr = 0; // returned error code
mumps_par.nrhs = nrhs;
mumps_par.lrhs = mumps_par.n;
mumps_par.job = 3;
if ( this->root_ )
{
mumps_par.rhs = bvals_.getRawPtr();
}
#ifdef HAVE_AMESOS2_TIMERS
Teuchos::TimeMonitor solveTimer(this->timers_.solveTime_);
#endif
function_map::mumps_c(&(mumps_par));
MUMPS_ERROR();
#ifdef HAVE_AMESOS2_TIMERS
Teuchos::TimeMonitor redistTimer2(this->timers_.vecRedistTime_);
#endif
if ( is_contiguous_ == true ) {
Util::put_1d_data_helper<
MultiVecAdapter<Vector>,slu_type>::do_put(X, bvals_(),
as<size_t>(ld_rhs),
ROOTED);
}
else {
Util::put_1d_data_helper<
MultiVecAdapter<Vector>,slu_type>::do_put(X, bvals_(),
as<size_t>(ld_rhs),
CONTIGUOUS_AND_ROOTED);
}
return(ierr);
}//end solve()
template <class Matrix, class Vector>
bool
MUMPS<Matrix,Vector>::matrixShapeOK_impl() const
{
// The MUMPS can only handle square for right now
return( this->globalNumRows_ == this->globalNumCols_ );
}
template <class Matrix, class Vector>
void
MUMPS<Matrix,Vector>::setParameters_impl(const Teuchos::RCP<Teuchos::ParameterList> & parameterList )
{
using Teuchos::RCP;
using Teuchos::getIntegralValue;
using Teuchos::ParameterEntryValidator;
RCP<const Teuchos::ParameterList> valid_params = getValidParameters_impl();
/*To Do --- add support for parameters */
if(parameterList->isParameter("ICNTL(1)"))
{
mumps_par.icntl[0] = getIntegralValue<local_ordinal_type>(*parameterList,
"ICNTL(1)");
}
if(parameterList->isParameter("ICNTL(2)"))
{
mumps_par.icntl[0] = getIntegralValue<local_ordinal_type>(*parameterList,
"ICNTL(2)");
}
if(parameterList->isParameter("ICNTL(3)"))
{
mumps_par.icntl[0] = getIntegralValue<local_ordinal_type>(*parameterList,
"ICNTL(3)");
}
if(parameterList->isParameter("ICNTL(4)"))
{
mumps_par.icntl[0] = getIntegralValue<local_ordinal_type>(*parameterList,
"ICNTL(4)");
}
if(parameterList->isParameter("ICNTL(6)"))
{
mumps_par.icntl[0] = getIntegralValue<local_ordinal_type>(*parameterList,
"ICNTL(6)");
}
if(parameterList->isParameter("ICNTL(9)"))
{
mumps_par.icntl[0] = getIntegralValue<local_ordinal_type>(*parameterList,
"ICNTL(9)");
}
if(parameterList->isParameter("ICNTL(11)"))
{
mumps_par.icntl[0] = getIntegralValue<local_ordinal_type>(*parameterList,
"ICNTL(11)");
}
if( parameterList->isParameter("IsContiguous") ){
is_contiguous_ = parameterList->get<bool>("IsContiguous");
}
}//end set parameters()
template <class Matrix, class Vector>
Teuchos::RCP<const Teuchos::ParameterList>
MUMPS<Matrix,Vector>::getValidParameters_impl() const
{
using Teuchos::ParameterList;
static Teuchos::RCP<const Teuchos::ParameterList> valid_params;
if( is_null(valid_params) ){
Teuchos::RCP<Teuchos::ParameterList> pl = Teuchos::parameterList();
pl->set("ICNTL(1)", "no", "See Manual" );
pl->set("ICNTL(2)", "no", "See Manual" );
pl->set("ICNTL(3)", "no", "See Manual" );
pl->set("ICNTL(4)", "no", "See Manual" );
pl->set("ICNTL(6)", "no", "See Manual" );
pl->set("ICNTL(9)", "no", "See Manual" );
pl->set("ICNTL(11)", "no", "See Manual" );
pl->set("IsContiguous", true, "Whether GIDs contiguous");
valid_params = pl;
}
return valid_params;
}//end getValidParmaeters_impl()
template <class Matrix, class Vector>
bool
MUMPS<Matrix,Vector>::loadA_impl(EPhase current_phase)
{
using Teuchos::as;
#ifdef HAVE_AMESOS2_TIMERS
Teuchos::TimeMonitor convTimer(this->timers_.mtxConvTime_);
#endif
if(MUMPS_MATRIX_LOAD == false)
{
// Only the root image needs storage allocated
if( this->root_ ){
nzvals_.resize(this->globalNumNonZeros_);
rowind_.resize(this->globalNumNonZeros_);
colptr_.resize(this->globalNumCols_ + 1);
}
local_ordinal_type nnz_ret = 0;
#ifdef HAVE_AMESOS2_TIMERS
Teuchos::TimeMonitor mtxRedistTimer( this->timers_.mtxRedistTime_ );
#endif
if ( is_contiguous_ == true ) {
Util::get_ccs_helper<
MatrixAdapter<Matrix>,slu_type,local_ordinal_type,local_ordinal_type>
::do_get(this->matrixA_.ptr(), nzvals_(), rowind_(), colptr_(),
nnz_ret, ROOTED, ARBITRARY, this->rowIndexBase_);
}
else {
Util::get_ccs_helper<
MatrixAdapter<Matrix>,slu_type,local_ordinal_type,local_ordinal_type>
::do_get(this->matrixA_.ptr(), nzvals_(), rowind_(), colptr_(),
nnz_ret, CONTIGUOUS_AND_ROOTED, ARBITRARY, this->rowIndexBase_);
}
if( this->root_ ){
TEUCHOS_TEST_FOR_EXCEPTION( nnz_ret != as<local_ordinal_type>(this->globalNumNonZeros_),
std::runtime_error,
"Did not get the expected number of non-zero vals");
}
if( this->root_ ){
ConvertToTriplet();
}
}
MUMPS_MATRIX_LOAD = true;
return (true);
}//end loadA_impl()
template <class Matrix, class Vector>
int
MUMPS<Matrix,Vector>::ConvertToTriplet()
{
MUMPS_STRUCT = true;
mumps_par.n = this->globalNumCols_;
mumps_par.nz = this->globalNumNonZeros_;
mumps_par.a = (magnitude_type*)malloc(mumps_par.nz * sizeof(magnitude_type));
mumps_par.irn = (MUMPS_INT*)malloc(mumps_par.nz *sizeof(MUMPS_INT));
mumps_par.jcn = (MUMPS_INT*)malloc(mumps_par.nz * sizeof(MUMPS_INT));
if((mumps_par.a == NULL) || (mumps_par.irn == NULL)
|| (mumps_par.jcn == NULL))
{
return -1;
}
/* Going from full CSC to full Triplet */
/* Will have to add support for symmetric case*/
local_ordinal_type tri_count = 0;
local_ordinal_type i,j;
local_ordinal_type max_local_ordinal = 0;
for(i = 0; i < (local_ordinal_type)this->globalNumCols_; i++)
{
for( j = colptr_[i]; j < colptr_[i+1]-1; j++)
{
mumps_par.jcn[tri_count] = (MUMPS_INT)i+1; //Fortran index
mumps_par.irn[tri_count] = (MUMPS_INT)rowind_[j]+1; //Fortran index
mumps_par.a[tri_count] = nzvals_[j];
tri_count++;
}
j = colptr_[i+1]-1;
mumps_par.jcn[tri_count] = (MUMPS_INT)i+1; //Fortran index
mumps_par.irn[tri_count] = (MUMPS_INT)rowind_[j]+1; //Fortran index
mumps_par.a[tri_count] = nzvals_[j];
tri_count++;
if(rowind_[j] > max_local_ordinal)
{
max_local_ordinal = rowind_[j];
}
}
TEUCHOS_TEST_FOR_EXCEPTION(std::numeric_limits<MUMPS_INT>::max() <= max_local_ordinal,
std::runtime_error,
"Matrix index larger than MUMPS_INT");
return 0;
}//end Convert to Trip()
template<class Matrix, class Vector>
void
MUMPS<Matrix,Vector>::MUMPS_ERROR()const
{
if(mumps_par.info[0] < 0)
{
TEUCHOS_TEST_FOR_EXCEPTION(false,
std::runtime_error,
"MUMPS error");
}
}//end MUMPS_ERROR()
template<class Matrix, class Vector>
const char* MUMPS<Matrix,Vector>::name = "MUMPS";
} // end namespace Amesos2
#endif // AMESOS2_MUMPS_DEF_HPP