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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
// Copyright (c) 2010, Lawrence Livermore National Security, LLC. Produced at
// the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
// reserved. See file COPYRIGHT for details.
//
// This file is part of the MFEM library. For more information and source code
// availability see http://mfem.org.
//
// MFEM is free software; you can redistribute it and/or modify it under the
// terms of the GNU Lesser General Public License (as published by the Free
// Software Foundation) version 2.1 dated February 1999.

#include "HypreParVector.hpp"
#include "HypreParMatrix.hpp"

#ifdef MOAB_HAVE_MPI

#include "hypre_parcsr.hpp"

#include <fstream>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cassert>

using namespace std;

#define MOAB_DEBUG

namespace moab
{
#define moab_hypre_assert( a, b ) \
    {                             \
    }
#define moab_hypre_assert_t( a, b )                         \
    {                                                       \
        if( !a )                                            \
        {                                                   \
            std::cout << "HYPRE Error: " << b << std::endl; \
            exit( -1 );                                     \
        }                                                   \
    }

template < typename TargetT, typename SourceT >
static TargetT* DuplicateAs( const SourceT* array, int size, bool cplusplus = true )
{
    TargetT* target_array = cplusplus ? new TargetT[size]
                                      /*     */
                                      : hypre_TAlloc( TargetT, size );

    for( int i = 0; i < size; i++ )
    {
        target_array[i] = array[i];
    }

    return target_array;
}

void HypreParMatrix::Init()
{
    A           = NULL;
    A_parcsr    = NULL;
    ParCSROwner = 1;
}

HypreParMatrix::HypreParMatrix( moab::ParallelComm* p_comm ) : pcomm( p_comm )
{
    Init();
    height = width = 0;
}

// Square block-diagonal constructor
HypreParMatrix::HypreParMatrix( moab::ParallelComm* p_comm,
                                HYPRE_Int glob_size,
                                HYPRE_Int* row_starts,
                                HYPRE_Int nnz_pr )
    : pcomm( p_comm )
{
    Init();
    resize( glob_size, row_starts, nnz_pr );
}

void HypreParMatrix::resize( HYPRE_Int glob_size,
                             HYPRE_Int* row_starts,
                             HYPRE_Int nnz_pr_diag,
                             HYPRE_Int nnz_pr_offdiag )
{
    /* Create the matrix.
        Note that this is a square matrix, so we indicate the row partition
        size twice (since number of rows = number of cols) */
    HYPRE_IJMatrixCreate( pcomm->comm(), row_starts[0], row_starts[1], row_starts[0], row_starts[1], &A );
    /* Choose a parallel csr format storage (see the User's Manual) */
    HYPRE_IJMatrixSetObjectType( A, HYPRE_PARCSR );

    int locsize = row_starts[1] - row_starts[0] + 1;
    int num_procs, rank;
    MPI_Comm_size( pcomm->comm(), &num_procs );
    MPI_Comm_rank( pcomm->comm(), &rank );

    if( nnz_pr_diag != 0 || nnz_pr_offdiag != 0 )
    {
        if( num_procs > 1 )
        {
            std::vector< HYPRE_Int > m_nnz_pr_diag( locsize, nnz_pr_diag );
            std::vector< HYPRE_Int > m_onz_pr_diag( locsize, nnz_pr_offdiag );
            HYPRE_IJMatrixSetDiagOffdSizes( A, &m_nnz_pr_diag[0], &m_onz_pr_diag[0] );
        }
        else
        {
            std::vector< HYPRE_Int > m_nnz_pr_diag( locsize, nnz_pr_diag );
            HYPRE_IJMatrixSetRowSizes( A, &m_nnz_pr_diag[0] );
        }
    }

    /* Initialize before setting coefficients */
    HYPRE_IJMatrixInitialize( A );
    /* Get the parcsr matrix object to use */
    HYPRE_IJMatrixGetObject( A, (void**)&A_parcsr );

    /* define an interpreter for the ParCSR interface */
    HYPRE_MatvecFunctions matvec_fn;
    interpreter = hypre_CTAlloc( mv_InterfaceInterpreter, 1 );
    HYPRE_ParCSRSetupInterpreter( interpreter );
    HYPRE_ParCSRSetupMatvec( &matvec_fn );
    gnrows = glob_size;
    gncols = glob_size;
    height = GetNumRows();
    width  = GetNumCols();
}

void HypreParMatrix::resize( HYPRE_Int global_num_rows,
                             HYPRE_Int global_num_cols,
                             HYPRE_Int* row_starts,
                             HYPRE_Int* col_starts,
                             HYPRE_Int* nnz_pr_diag,
                             HYPRE_Int* onz_pr_diag,
                             HYPRE_Int nnz_pr_offdiag )
{
    /* Create the matrix.
        Note that this is a square matrix, so we indicate the row partition
        size twice (since number of rows = number of cols) */
    HYPRE_IJMatrixCreate( pcomm->comm(), row_starts[0], row_starts[1], col_starts[0], col_starts[1], &A );
    /* Choose a parallel csr format storage (see the User's Manual) */
    HYPRE_IJMatrixSetObjectType( A, HYPRE_PARCSR );

    int num_procs;
    MPI_Comm_size( pcomm->comm(), &num_procs );

    /* Set the matrix pre-allocation data */
    if( num_procs > 1 )
    {
        if( nnz_pr_diag == NULL && onz_pr_diag == NULL )
        {
            std::cout << "Parameter nnz_pr_diag and onz_pr_diag cannot be NULL.\n";
            moab_hypre_assert_t( nnz_pr_diag, true );
        }

        HYPRE_IJMatrixSetDiagOffdSizes( A, nnz_pr_diag, onz_pr_diag );
    }
    else
    {
        HYPRE_IJMatrixSetRowSizes( A, nnz_pr_diag );
    }

    if( nnz_pr_offdiag )
    {
        HYPRE_IJMatrixSetMaxOffProcElmts( A, nnz_pr_offdiag );
    }

    /* Initialize before setting coefficients */
    HYPRE_IJMatrixInitialize( A );
    /* Get the parcsr matrix object to use */
    HYPRE_IJMatrixGetObject( A, (void**)&A_parcsr );

    /* define an interpreter for the ParCSR interface */
    HYPRE_MatvecFunctions matvec_fn;
    interpreter = hypre_CTAlloc( mv_InterfaceInterpreter, 1 );
    HYPRE_ParCSRSetupInterpreter( interpreter );
    HYPRE_ParCSRSetupMatvec( &matvec_fn );
    gnrows = global_num_rows;
    gncols = global_num_cols;
    height = GetNumRows();
    width  = GetNumCols();
}

// Rectangular block-diagonal constructor
HypreParMatrix::HypreParMatrix( moab::ParallelComm* p_comm,
                                HYPRE_Int global_num_rows,
                                HYPRE_Int global_num_cols,
                                HYPRE_Int* row_starts,
                                HYPRE_Int* col_starts,
                                HYPRE_Int nnz_pr_diag,
                                HYPRE_Int onz_pr_diag,
                                HYPRE_Int nnz_pr_offdiag )
    : pcomm( p_comm )
{
    Init();
    std::vector< HYPRE_Int > m_nnz_pr_diag( row_starts[1] - row_starts[0], nnz_pr_diag );
    std::vector< HYPRE_Int > m_onz_pr_diag( row_starts[1] - row_starts[0], onz_pr_diag );
    resize( global_num_rows, global_num_cols, row_starts, col_starts, &m_nnz_pr_diag[0], &m_onz_pr_diag[0],
            nnz_pr_offdiag );
}

void HypreParMatrix::MakeRef( const HypreParMatrix& master )
{
    Destroy();
    Init();
    A           = master.A;
    A_parcsr    = master.A_parcsr;
    ParCSROwner = 0;
    height      = master.GetNumRows();
    width       = master.GetNumCols();
}

HYPRE_IJMatrix HypreParMatrix::StealData()
{
    // Only safe when (diagOwner == -1 && offdOwner == -1 && colMapOwner == -1)
    // Otherwise, there may be memory leaks or hypre may destroy arrays allocated
    // with operator new.
    moab_hypre_assert( diagOwner == -1 && offdOwner == -1 && colMapOwner == -1, "" );
    moab_hypre_assert( ParCSROwner, "" );
    HYPRE_IJMatrix R = A;
    A                = NULL;
    ParCSROwner      = 0;
    Destroy();
    Init();
    return R;
}

void HypreParMatrix::StealData( HypreParMatrix& X )
{
    // Only safe when (diagOwner == -1 && offdOwner == -1 && colMapOwner == -1)
    // Otherwise, there may be memory leaks or hypre may destroy arrays allocated
    // with operator new.
    moab_hypre_assert( diagOwner == -1 && offdOwner == -1 && colMapOwner == -1, "" );
    moab_hypre_assert( ParCSROwner, "" );
    moab_hypre_assert( X.diagOwner == -1 && X.offdOwner == -1 && X.colMapOwner == -1, "" );
    moab_hypre_assert( X.ParCSROwner, "" );
    A             = X.A;
    A_parcsr      = X.A_parcsr;
    ParCSROwner   = 1;
    X.A           = NULL;
    X.A_parcsr    = NULL;
    X.ParCSROwner = 0;
    X.Destroy();
    X.Init();
}

void HypreParMatrix::GetDiag( Eigen::VectorXd& diag ) const
{
    int size = this->height;
    diag.resize( size );

    for( int j = 0; j < size; j++ )
    {
        diag( j ) = A_parcsr->diag->data[A_parcsr->diag->i[j]];
        moab_hypre_assert( A_parcsr->diag->j[A_parcsr->diag->i[j]] == j,
                           "the first entry in each row must be the diagonal one" );
    }
}

static void MakeWrapper( const hypre_CSRMatrix* mat, Eigen::SparseMatrix< double, Eigen::RowMajor >& wrapper )
{
    HYPRE_Int nr = hypre_CSRMatrixNumRows( mat );
    HYPRE_Int nc = hypre_CSRMatrixNumCols( mat );
    Eigen::SparseMatrix< double, Eigen::RowMajor > tmp( nr, nc );
    typedef Eigen::Triplet< double > T;
    HYPRE_Int nnz = hypre_CSRMatrixNumNonzeros( mat );
    int *rindx, *cindx;
    double* dindx;
#ifndef HYPRE_BIGINT
    rindx = hypre_CSRMatrixI( mat );
    cindx = hypre_CSRMatrixJ( mat );
    dindx = hypre_CSRMatrixData( mat );
#else
    rindx = DuplicateAs< int >( hypre_CSRMatrixI( mat ), nr + 1 );
    cindx = DuplicateAs< int >( hypre_CSRMatrixJ( mat ), nnz );
    dindx = hypre_CSRMatrixData( mat );
#endif
    std::vector< T > tripletList( nnz );

    for( int i = 0; i < nnz; ++i )
    {
        tripletList.push_back( T( rindx[i], cindx[i], dindx[i] ) );
    }

    tmp.setFromTriplets( tripletList.begin(), tripletList.end() );
    wrapper.swap( tmp );
}

void HypreParMatrix::GetDiag( Eigen::SparseMatrix< double, Eigen::RowMajor >& diag ) const
{
    MakeWrapper( A_parcsr->diag, diag );
}

void HypreParMatrix::GetOffd( Eigen::SparseMatrix< double, Eigen::RowMajor >& offd, HYPRE_Int*& cmap ) const
{
    MakeWrapper( A_parcsr->offd, offd );
    cmap = A_parcsr->col_map_offd;
}

// void HypreParMatrix::GetBlocks(Eigen::Array<HypreParMatrix*,Eigen::Dynamic,Eigen::Dynamic>
// &blocks,
//                                bool interleaved_rows,
//                                bool interleaved_cols) const
// {
//    int nr = blocks.rows();
//    int nc = blocks.cols();

//    hypre_ParCSRMatrix **hypre_blocks = new hypre_ParCSRMatrix*[nr * nc];
//    internal::hypre_ParCSRMatrixSplit(A, nr, nc, hypre_blocks,
//                                      interleaved_rows, interleaved_cols);

//    for (int i = 0; i < nr; i++)
//    {
//       for (int j = 0; j < nc; j++)
//       {
//          blocks[i][j] = new HypreParMatrix(hypre_blocks[i*nc + j]);
//       }
//    }

//    delete [] hypre_blocks;
// }

// HypreParMatrix * HypreParMatrix::Transpose()
// {
//    hypre_ParCSRMatrix * At;
//    hypre_ParCSRMatrixTranspose(A_parcsr, &At, 1);
//    hypre_ParCSRMatrixSetNumNonzeros(At);

//    hypre_MatvecCommPkgCreate(At);

//    return new HypreParMatrix(At);
// }

HYPRE_Int HypreParMatrix::Mult( HypreParVector& x, HypreParVector& y, double a, double b )
{
    return hypre_ParCSRMatrixMatvec( a, A_parcsr, x.x_par, b, y.x_par );
}

void HypreParMatrix::Mult( double a, const HypreParVector& x, double b, HypreParVector& y ) const
{
    hypre_ParCSRMatrixMatvec( a, A_parcsr, x.x_par, b, y.x_par );
}

void HypreParMatrix::MultTranspose( double a, const HypreParVector& x, double b, HypreParVector& y ) const
{
    hypre_ParCSRMatrixMatvecT( a, A_parcsr, y.x_par, b, x.x_par );
}

HYPRE_Int HypreParMatrix::Mult( HYPRE_ParVector x, HYPRE_ParVector y, double a, double b )
{
    return hypre_ParCSRMatrixMatvec( a, A_parcsr, (hypre_ParVector*)x, b, (hypre_ParVector*)y );
}

HYPRE_Int HypreParMatrix::MultTranspose( HypreParVector& x, HypreParVector& y, double a, double b )
{
    return hypre_ParCSRMatrixMatvecT( a, A_parcsr, x.x_par, b, y.x_par );
}

// HypreParMatrix* HypreParMatrix::LeftDiagMult(const Eigen::SparseMatrix<double, Eigen::RowMajor>
// &D,
//                                              HYPRE_Int* row_starts) const
// {
//    const bool assumed_partition = HYPRE_AssumedPartitionCheck();
//    const bool same_rows = (D.rows() == hypre_CSRMatrixNumRows(A_parcsr->diag));

//    int part_size;
//    if (assumed_partition)
//    {
//       part_size = 2;
//    }
//    else
//    {
//       MPI_Comm_size(GetComm(), &part_size);
//       part_size++;
//    }

//    HYPRE_Int global_num_rows;
//    if (same_rows)
//    {
//       row_starts = hypre_ParCSRMatrixRowStarts(A_parcsr);
//       global_num_rows = hypre_ParCSRMatrixGlobalNumRows(A_parcsr);
//    }
//    else
//    {
//       // MFEM_VERIFY(row_starts != NULL, "the number of rows in D and A is not "
//       //             "the same; row_starts must be given (not NULL)");
//       // Here, when assumed_partition is true we use row_starts[2], so
//       // row_starts must come from the GetDofOffsets/GetTrueDofOffsets methods
//       // of ParFiniteElementSpace (HYPRE's partitions have only 2 entries).
//       global_num_rows =
//          assumed_partition ? row_starts[2] : row_starts[part_size-1];
//    }

//    HYPRE_Int *col_starts = hypre_ParCSRMatrixColStarts(A_parcsr);
//    HYPRE_Int *col_map_offd;

//    // get the diag and offd blocks as SparseMatrix wrappers
//    Eigen::SparseMatrix<double, Eigen::RowMajor> A_diag, A_offd;
//    GetDiag(A_diag);
//    GetOffd(A_offd, col_map_offd);

//    // multiply the blocks with D and create a new HypreParMatrix
//    Eigen::SparseMatrix<double, Eigen::RowMajor> DA_diag = (D * A_diag);
//    Eigen::SparseMatrix<double, Eigen::RowMajor> DA_offd = (D * A_offd);

//    HypreParMatrix* DA =
//       new HypreParMatrix(GetComm(),
//                          global_num_rows, hypre_ParCSRMatrixGlobalNumCols(A),
//                          DuplicateAs<HYPRE_Int>(row_starts, part_size, false),
//                          DuplicateAs<HYPRE_Int>(col_starts, part_size, false),
//                          &DA_diag, &DA_offd,
//                          DuplicateAs<HYPRE_Int>(col_map_offd, A_offd.cols()));

//    // When HYPRE_BIGINT is defined, we want DA_{diag,offd} to delete their I and
//    // J arrays but not their data arrays; when HYPRE_BIGINT is not defined, we
//    // don't want DA_{diag,offd} to delete anything.
// // #ifndef HYPRE_BIGINT
// //    DA_diag.LoseData();
// //    DA_offd.LoseData();
// // #else
// //    DA_diag.SetDataOwner(false);
// //    DA_offd.SetDataOwner(false);
// // #endif

//    // delete DA_diag;
//    // delete DA_offd;

//    hypre_ParCSRMatrixSetRowStartsOwner(DA->A, 1);
//    hypre_ParCSRMatrixSetColStartsOwner(DA->A, 1);

//    DA->diagOwner = DA->offdOwner = 3;
//    DA->colMapOwner = 1;

//    return DA;
// }

void HypreParMatrix::ScaleRows( const Eigen::VectorXd& diag )
{
    if( hypre_CSRMatrixNumRows( A_parcsr->diag ) != hypre_CSRMatrixNumRows( A_parcsr->offd ) )
    {
        MB_SET_ERR_RET( "Row does not match" );
    }

    if( hypre_CSRMatrixNumRows( A_parcsr->diag ) != diag.size() )
    {
        MB_SET_ERR_RET( "Note the Eigen::VectorXd diag is not of compatible dimensions with A\n" );
    }

    int size           = this->height;
    double* Adiag_data = hypre_CSRMatrixData( A_parcsr->diag );
    HYPRE_Int* Adiag_i = hypre_CSRMatrixI( A_parcsr->diag );
    double* Aoffd_data = hypre_CSRMatrixData( A_parcsr->offd );
    HYPRE_Int* Aoffd_i = hypre_CSRMatrixI( A_parcsr->offd );
    double val;
    HYPRE_Int jj;

    for( int i( 0 ); i < size; ++i )
    {
        val = diag[i];

        for( jj = Adiag_i[i]; jj < Adiag_i[i + 1]; ++jj )
        {
            Adiag_data[jj] *= val;
        }

        for( jj = Aoffd_i[i]; jj < Aoffd_i[i + 1]; ++jj )
        {
            Aoffd_data[jj] *= val;
        }
    }
}

void HypreParMatrix::InvScaleRows( const Eigen::VectorXd& diag )
{
    if( hypre_CSRMatrixNumRows( A_parcsr->diag ) != hypre_CSRMatrixNumRows( A_parcsr->offd ) )
    {
        MB_SET_ERR_RET( "Row does not match" );
    }

    if( hypre_CSRMatrixNumRows( A_parcsr->diag ) != diag.size() )
    {
        MB_SET_ERR_RET( "Note the Eigen::VectorXd diag is not of compatible dimensions with A_parcsr\n" );
    }

    int size           = this->height;
    double* Adiag_data = hypre_CSRMatrixData( A_parcsr->diag );
    HYPRE_Int* Adiag_i = hypre_CSRMatrixI( A_parcsr->diag );
    double* Aoffd_data = hypre_CSRMatrixData( A_parcsr->offd );
    HYPRE_Int* Aoffd_i = hypre_CSRMatrixI( A_parcsr->offd );
    double val;
    HYPRE_Int jj;

    for( int i( 0 ); i < size; ++i )
    {
#ifdef MOAB_DEBUG

        if( 0.0 == diag( i ) )
        {
            MB_SET_ERR_RET( "HypreParMatrix::InvDiagScale : Division by 0" );
        }

#endif
        val = 1. / diag( i );

        for( jj = Adiag_i[i]; jj < Adiag_i[i + 1]; ++jj )
        {
            Adiag_data[jj] *= val;
        }

        for( jj = Aoffd_i[i]; jj < Aoffd_i[i + 1]; ++jj )
        {
            Aoffd_data[jj] *= val;
        }
    }
}

void HypreParMatrix::operator*=( double s )
{
    if( hypre_CSRMatrixNumRows( A_parcsr->diag ) != hypre_CSRMatrixNumRows( A_parcsr->offd ) )
    {
        MB_SET_ERR_RET( "Row does not match" );
    }

    HYPRE_Int size = hypre_CSRMatrixNumRows( A_parcsr->diag );
    HYPRE_Int jj;
    double* Adiag_data = hypre_CSRMatrixData( A_parcsr->diag );
    HYPRE_Int* Adiag_i = hypre_CSRMatrixI( A_parcsr->diag );

    for( jj = 0; jj < Adiag_i[size]; ++jj )
    {
        Adiag_data[jj] *= s;
    }

    double* Aoffd_data = hypre_CSRMatrixData( A_parcsr->offd );
    HYPRE_Int* Aoffd_i = hypre_CSRMatrixI( A_parcsr->offd );

    for( jj = 0; jj < Aoffd_i[size]; ++jj )
    {
        Aoffd_data[jj] *= s;
    }
}

HYPRE_Int HypreParMatrix::GetValues( const HYPRE_Int nrows,
                                     HYPRE_Int* ncols,
                                     HYPRE_Int* rows,
                                     HYPRE_Int* cols,
                                     HYPRE_Complex* values )
{
    return HYPRE_IJMatrixGetValues( A, nrows, ncols, rows, cols, values );
}

HYPRE_Int HypreParMatrix::SetValues( const HYPRE_Int nrows,
                                     HYPRE_Int* ncols,
                                     const HYPRE_Int* rows,
                                     const HYPRE_Int* cols,
                                     const HYPRE_Complex* values )
{
    return HYPRE_IJMatrixSetValues( A, nrows, ncols, rows, cols, values );
}

HYPRE_Int HypreParMatrix::AddToValues( const HYPRE_Int nrows,
                                       HYPRE_Int* ncols,
                                       const HYPRE_Int* rows,
                                       const HYPRE_Int* cols,
                                       const HYPRE_Complex* values )
{
    return HYPRE_IJMatrixAddToValues( A, nrows, ncols, rows, cols, values );
}

HYPRE_Int HypreParMatrix::verbosity( const HYPRE_Int level )
{
    return HYPRE_IJMatrixSetPrintLevel( A, level );
}

HYPRE_Int HypreParMatrix::FinalizeAssembly()
{
    return HYPRE_IJMatrixAssemble( A );
}

static void get_sorted_rows_cols( const std::vector< HYPRE_Int >& rows_cols, std::vector< HYPRE_Int >& hypre_sorted )
{
    hypre_sorted.resize( rows_cols.size() );
    std::copy( rows_cols.begin(), rows_cols.end(), hypre_sorted.begin() );
    std::sort( hypre_sorted.begin(), hypre_sorted.end() );
}

void HypreParMatrix::Threshold( double threshold )
{
    int ierr = 0;<--- Variable 'ierr' is assigned a value that is never used.
    int num_procs;
    hypre_CSRMatrix* csr_A;
    hypre_CSRMatrix* csr_A_wo_z;
    hypre_ParCSRMatrix* parcsr_A_ptr;
    int* row_starts = NULL;
    int* col_starts = NULL;
    int row_start   = -1;
    int row_end     = -1;
    int col_start   = -1;
    int col_end     = -1;
    MPI_Comm_size( pcomm->comm(), &num_procs );
    ierr += hypre_ParCSRMatrixGetLocalRange( A_parcsr, &row_start, &row_end, &col_start, &col_end );<--- Variable 'ierr' is assigned a value that is never used.
    row_starts   = hypre_ParCSRMatrixRowStarts( A_parcsr );
    col_starts   = hypre_ParCSRMatrixColStarts( A_parcsr );
    parcsr_A_ptr = hypre_ParCSRMatrixCreate( pcomm->comm(), row_starts[num_procs], col_starts[num_procs], row_starts,
                                             col_starts, 0, 0, 0 );
    csr_A        = hypre_MergeDiagAndOffd( A_parcsr );
    csr_A_wo_z   = hypre_CSRMatrixDeleteZeros( csr_A, threshold );

    /* hypre_CSRMatrixDeleteZeros will return a NULL pointer rather than a usable
       CSR matrix if it finds no non-zeros */
    if( csr_A_wo_z == NULL )
    {
        csr_A_wo_z = csr_A;
    }
    else
    {
        ierr += hypre_CSRMatrixDestroy( csr_A );<--- Variable 'ierr' is assigned a value that is never used.
    }

    ierr += GenerateDiagAndOffd( csr_A_wo_z, parcsr_A_ptr, col_start, col_end );<--- Variable 'ierr' is assigned a value that is never used.
    ierr += hypre_CSRMatrixDestroy( csr_A_wo_z );<--- Variable 'ierr' is assigned a value that is never used.
    ierr += hypre_ParCSRMatrixDestroy( A_parcsr );<--- Variable 'ierr' is assigned a value that is never used.
    hypre_IJMatrixObject( A ) = parcsr_A_ptr;
    /* Get the parcsr matrix object to use */
    HYPRE_IJMatrixGetObject( A, (void**)A_parcsr );
}

void HypreParMatrix::EliminateRowsCols( const std::vector< HYPRE_Int >& rows_cols,
                                        const HypreParVector& X,
                                        HypreParVector& B )
{
    std::vector< HYPRE_Int > rc_sorted;
    get_sorted_rows_cols( rows_cols, rc_sorted );
    internal::hypre_ParCSRMatrixEliminateAXB( A_parcsr, rc_sorted.size(), rc_sorted.data(), X.x_par, B.x_par );
}

HypreParMatrix* HypreParMatrix::EliminateRowsCols( const std::vector< HYPRE_Int >& rows_cols )
{
    std::vector< HYPRE_Int > rc_sorted;
    get_sorted_rows_cols( rows_cols, rc_sorted );
    hypre_ParCSRMatrix* Ae;
    internal::hypre_ParCSRMatrixEliminateAAe( A_parcsr, &Ae, rc_sorted.size(), rc_sorted.data() );
    HypreParMatrix* tmpMat            = new HypreParMatrix( pcomm, M(), N(), RowPart(), ColPart(), 0, 0 );
    hypre_IJMatrixObject( tmpMat->A ) = Ae;
    /* Get the parcsr matrix object to use */
    HYPRE_IJMatrixGetObject( tmpMat->A, (void**)tmpMat->A_parcsr );
    return tmpMat;
}

void HypreParMatrix::Print( const char* fname, HYPRE_Int offi, HYPRE_Int offj )
{
    hypre_ParCSRMatrixPrintIJ( A_parcsr, offi, offj, fname );
}

void HypreParMatrix::Read( const char* fname )
{
    Destroy();
    Init();
    HYPRE_Int base_i, base_j;
    hypre_ParCSRMatrixReadIJ( pcomm->comm(), fname, &base_i, &base_j, &A_parcsr );
    hypre_ParCSRMatrixSetNumNonzeros( A_parcsr );
    hypre_MatvecCommPkgCreate( A_parcsr );
    height = GetNumRows();
    width  = GetNumCols();
}

void HypreParMatrix::Destroy()
{
    if( interpreter )
    {
        hypre_TFree( interpreter );
    }

    if( A == NULL )
    {
        return;
    }

    else
    {
        HYPRE_IJMatrixDestroy( A );
    }
}

HypreParMatrix* ParMult( HypreParMatrix* A, HypreParMatrix* B )<--- The function 'ParMult' is never used.
{
    hypre_ParCSRMatrix* ab;
    ab = hypre_ParMatmul( A->A_parcsr, B->A_parcsr );
    hypre_MatvecCommPkgCreate( ab );
    HypreParMatrix* tmpMat = new HypreParMatrix( A->pcomm, A->M(), A->N(), A->RowPart(), A->ColPart(), 0, 0 );
    hypre_IJMatrixObject( tmpMat->A ) = ab;
    /* Get the parcsr matrix object to use */
    HYPRE_IJMatrixGetObject( tmpMat->A, (void**)tmpMat->A_parcsr );
    return tmpMat;
}

HypreParMatrix* RAP( HypreParMatrix* A, HypreParMatrix* P )<--- The function 'RAP' is never used.
{
    HYPRE_Int P_owns_its_col_starts = hypre_ParCSRMatrixOwnsColStarts( (hypre_ParCSRMatrix*)( P->A_parcsr ) );
    hypre_ParCSRMatrix* rap;
    hypre_BoomerAMGBuildCoarseOperator( P->A_parcsr, A->A_parcsr, P->A_parcsr, &rap );
    hypre_ParCSRMatrixSetNumNonzeros( rap );
    // hypre_MatvecCommPkgCreate(rap);
    /* Warning: hypre_BoomerAMGBuildCoarseOperator steals the col_starts
       from P (even if it does not own them)! */
    hypre_ParCSRMatrixSetRowStartsOwner( rap, 0 );
    hypre_ParCSRMatrixSetColStartsOwner( rap, 0 );

    if( P_owns_its_col_starts )
    {
        hypre_ParCSRMatrixSetColStartsOwner( P->A_parcsr, 1 );
    }

    HypreParMatrix* tmpMat = new HypreParMatrix( A->pcomm, A->M(), A->N(), A->RowPart(), A->ColPart(), 0, 0 );
    hypre_IJMatrixObject( tmpMat->A ) = rap;
    /* Get the parcsr matrix object to use */
    HYPRE_IJMatrixGetObject( tmpMat->A, (void**)tmpMat->A_parcsr );
    return tmpMat;
}

HypreParMatrix* RAP( HypreParMatrix* Rt, HypreParMatrix* A, HypreParMatrix* P )
{
    HYPRE_Int P_owns_its_col_starts  = hypre_ParCSRMatrixOwnsColStarts( (hypre_ParCSRMatrix*)( P->A_parcsr ) );
    HYPRE_Int Rt_owns_its_col_starts = hypre_ParCSRMatrixOwnsColStarts( (hypre_ParCSRMatrix*)( Rt->A_parcsr ) );
    hypre_ParCSRMatrix* rap;
    hypre_BoomerAMGBuildCoarseOperator( Rt->A_parcsr, A->A_parcsr, P->A_parcsr, &rap );
    hypre_ParCSRMatrixSetNumNonzeros( rap );

    // hypre_MatvecCommPkgCreate(rap);
    if( !P_owns_its_col_starts )
    {
        /* Warning: hypre_BoomerAMGBuildCoarseOperator steals the col_starts
           from P (even if it does not own them)! */
        hypre_ParCSRMatrixSetColStartsOwner( rap, 0 );
    }

    if( !Rt_owns_its_col_starts )
    {
        /* Warning: hypre_BoomerAMGBuildCoarseOperator steals the col_starts
           from P (even if it does not own them)! */
        hypre_ParCSRMatrixSetRowStartsOwner( rap, 0 );
    }

    HypreParMatrix* tmpMat = new HypreParMatrix( A->pcomm, A->M(), A->N(), A->RowPart(), A->ColPart(), 0, 0 );
    hypre_IJMatrixObject( tmpMat->A ) = rap;
    /* Get the parcsr matrix object to use */
    HYPRE_IJMatrixGetObject( tmpMat->A, (void**)tmpMat->A_parcsr );
    return tmpMat;
}

void EliminateBC( HypreParMatrix& A,<--- The function 'EliminateBC' is never used.
                  HypreParMatrix& Ae,
                  const std::vector< int >& ess_dof_list,
                  const HypreParVector& X,
                  HypreParVector& B )
{
    // B -= Ae*X
    Ae.Mult( -1.0, X, 1.0, B );
    hypre_CSRMatrix* A_diag = hypre_ParCSRMatrixDiag( (hypre_ParCSRMatrix*)A.A_parcsr );
    double* data            = hypre_CSRMatrixData( A_diag );
    HYPRE_Int* I            = hypre_CSRMatrixI( A_diag );
#ifdef MOAB_DEBUG
    HYPRE_Int* J            = hypre_CSRMatrixJ( A_diag );
    hypre_CSRMatrix* A_offd = hypre_ParCSRMatrixOffd( (hypre_ParCSRMatrix*)A.A_parcsr );
    HYPRE_Int* I_offd       = hypre_CSRMatrixI( A_offd );
    double* data_offd       = hypre_CSRMatrixData( A_offd );
#endif
    std::vector< HYPRE_Complex > bdata( ess_dof_list.size() ), xdata( ess_dof_list.size() );
    B.GetValues( ess_dof_list.size(), ess_dof_list.data(), bdata.data() );
    X.GetValues( ess_dof_list.size(), ess_dof_list.data(), xdata.data() );

    for( size_t i = 0; i < ess_dof_list.size(); i++ )
    {
        int r    = ess_dof_list[i];
        bdata[r] = data[I[r]] * xdata[r];
#ifdef MOAB_DEBUG

        // Check that in the rows specified by the ess_dof_list, the matrix A has
        // only one entry -- the diagonal.
        // if (I[r+1] != I[r]+1 || J[I[r]] != r || I_offd[r] != I_offd[r+1])
        if( J[I[r]] != r )
        {
            MB_SET_ERR_RET( "the diagonal entry must be the first entry in the row!" );
        }

        for( int j = I[r] + 1; j < I[r + 1]; j++ )
        {
            if( data[j] != 0.0 )
            {
                MB_SET_ERR_RET( "all off-diagonal entries must be zero!" );
            }
        }

        for( int j = I_offd[r]; j < I_offd[r + 1]; j++ )
        {
            if( data_offd[j] != 0.0 )
            {
                MB_SET_ERR_RET( "all off-diagonal entries must be zero!" );
            }
        }

#endif
    }
}

// Taubin or "lambda-mu" scheme, which alternates between positive and
// negative step sizes to approximate low-pass filter effect.

int ParCSRRelax_Taubin( hypre_ParCSRMatrix* A,  // matrix to relax with<--- The function 'ParCSRRelax_Taubin' is never used.
                        hypre_ParVector* f,     // right-hand side
                        double lambda,
                        double mu,
                        int N,
                        double max_eig,
                        hypre_ParVector* u,  // initial/updated approximation
                        hypre_ParVector* r   // another temp vector
)
{
    hypre_CSRMatrix* A_diag = hypre_ParCSRMatrixDiag( A );
    HYPRE_Int num_rows      = hypre_CSRMatrixNumRows( A_diag );
    double* u_data          = hypre_VectorData( hypre_ParVectorLocalVector( u ) );
    double* r_data          = hypre_VectorData( hypre_ParVectorLocalVector( r ) );

    for( int i = 0; i < N; i++ )
    {
        // get residual: r = f - A*u
        hypre_ParVectorCopy( f, r );
        hypre_ParCSRMatrixMatvec( -1.0, A, u, 1.0, r );
        double coef;
        ( 0 == ( i % 2 ) ) ? coef = lambda : coef = mu;

        for( HYPRE_Int j = 0; j < num_rows; j++ )
        {
            u_data[j] += coef * r_data[j] / max_eig;
        }
    }

    return 0;
}

// FIR scheme, which uses Chebyshev polynomials and a window function
// to approximate a low-pass step filter.

int ParCSRRelax_FIR( hypre_ParCSRMatrix* A,  // matrix to relax with<--- The function 'ParCSRRelax_FIR' is never used.
                     hypre_ParVector* f,     // right-hand side
                     double max_eig,
                     int poly_order,
                     double* fir_coeffs,
                     hypre_ParVector* u,   // initial/updated approximation
                     hypre_ParVector* x0,  // temporaries
                     hypre_ParVector* x1,
                     hypre_ParVector* x2,
                     hypre_ParVector* x3 )

{
    hypre_CSRMatrix* A_diag = hypre_ParCSRMatrixDiag( A );
    HYPRE_Int num_rows      = hypre_CSRMatrixNumRows( A_diag );
    double* u_data          = hypre_VectorData( hypre_ParVectorLocalVector( u ) );
    double* x0_data         = hypre_VectorData( hypre_ParVectorLocalVector( x0 ) );
    double* x1_data         = hypre_VectorData( hypre_ParVectorLocalVector( x1 ) );
    double* x2_data         = hypre_VectorData( hypre_ParVectorLocalVector( x2 ) );
    double* x3_data         = hypre_VectorData( hypre_ParVectorLocalVector( x3 ) );
    hypre_ParVectorCopy( u, x0 );
    // x1 = f -A*x0/max_eig
    hypre_ParVectorCopy( f, x1 );
    hypre_ParCSRMatrixMatvec( -1.0, A, x0, 1.0, x1 );

    for( HYPRE_Int i = 0; i < num_rows; i++ )
    {
        x1_data[i] /= -max_eig;
    }

    // x1 = x0 -x1
    for( HYPRE_Int i = 0; i < num_rows; i++ )
    {
        x1_data[i] = x0_data[i] - x1_data[i];
    }

    // x3 = f0*x0 +f1*x1
    for( HYPRE_Int i = 0; i < num_rows; i++ )
    {
        x3_data[i] = fir_coeffs[0] * x0_data[i] + fir_coeffs[1] * x1_data[i];
    }

    for( int n = 2; n <= poly_order; n++ )
    {
        // x2 = f - A*x1/max_eig
        hypre_ParVectorCopy( f, x2 );
        hypre_ParCSRMatrixMatvec( -1.0, A, x1, 1.0, x2 );

        for( HYPRE_Int i = 0; i < num_rows; i++ )
        {
            x2_data[i] /= -max_eig;
        }

        // x2 = (x1-x0) +(x1-2*x2)
        // x3 = x3 +f[n]*x2
        // x0 = x1
        // x1 = x2

        for( HYPRE_Int i = 0; i < num_rows; i++ )
        {
            x2_data[i] = ( x1_data[i] - x0_data[i] ) + ( x1_data[i] - 2 * x2_data[i] );
            x3_data[i] += fir_coeffs[n] * x2_data[i];
            x0_data[i] = x1_data[i];
            x1_data[i] = x2_data[i];
        }
    }

    for( HYPRE_Int i = 0; i < num_rows; i++ )
    {
        u_data[i] = x3_data[i];
    }

    return 0;
}

}  // namespace moab

#endif