Debian Codegraph @

packages/belos/epetra/test/Epetra64Driver/build_simple_matrices.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Belos: Block Linear Solvers Package
4 //
5 // Copyright 2004-2016 NTESS and the Belos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef __BUILD_SIMPLE_MATRICES_HPP
11 #define __BUILD_SIMPLE_MATRICES_HPP
12
13 #include <stdio.h>
14
15
16 #ifdef EPETRA_MPI
17 #include <mpi.h>
18 #include "Epetra_MpiComm.h"
19 #else
20 #include "Epetra_SerialComm.h"
21 #endif
22
23 #include "Epetra_CrsMatrix.h"
24 #include "Epetra_Map.h"
25
26 #ifdef KDDKDD_DEBUG
27 #include "Epetra_Vector.h"
28 #include "Epetra_Import.h"
29 #include "Epetra_Export.h"
30 #endif
31
32 #include "build_maps.hpp"
33
34 #include "Teuchos_Assert.hpp"
35
36 ////////////////////////////////////////////////////////////////////////////
37
38 // Build the following Laplacian matrix with nGlobalRows rows.
39 //
40 // | 1 -1 |
41 // |-1 2 -1 |
42 // | -1 2 -1 |
43 // | ... |
44 // | -1 1|
45 //
46 // Distribute the matrix according to the provided row and column maps.
47
48 template <typename itype>
49 void build_simple_matrix(
50 Epetra_Comm &comm, // Communicator to use
51 Epetra_CrsMatrix *&A, // OUTPUT: Matrix returned
52 itype nGlobalRows, // Number of global matrix rows and columns
53 bool testEpetra64, // if true, add 2*INT_MAX to each global ID
54 // to exercise Epetra64
55 bool verbose // if true, print out matrix information
56 )
57 {
58 Epetra_Map *rowMap = NULL; // Row map for the created matrix
59 Epetra_Map *colMap = NULL; // Col map for the created matrix
60 Epetra_Map *vectorMap = NULL; // Range/Domain map for the created matrix
61
62 long long offsetEpetra64;
63
64 build_maps(nGlobalRows, testEpetra64, comm,
65 &vectorMap, &rowMap, &colMap, offsetEpetra64, verbose);
66
67 // Create an integer vector nnzPerRow that is used to build the Epetra Matrix.
68 // nnzPerRow[i] is the number of entries for the ith global equation
69 int nMyRows = rowMap->NumMyElements();
70 std::vector<int> nnzPerRow(nMyRows+1, 0);
71
72 // Also create lists of the nonzeros to be assigned to processors.
73 // To save programming time and complexity, these vectors are allocated
74 // bigger than they may actually be needed.
75 std::vector<itype> iv(3*nMyRows+1);
76 std::vector<itype> jv(3*nMyRows+1);
77 std::vector<double> vv(3*nMyRows+1);
78
79 itype nMyNonzeros = 0;
80 for (itype i = 0, myrowcnt = 0; i < nGlobalRows; i++) {
81 if (rowMap->MyGID(i+offsetEpetra64)) {
82 // This processor owns part of this row; see whether it owns the nonzeros
83 if (i > 0 && (!colMap || colMap->MyGID(i-1+offsetEpetra64))) {
84 iv[nMyNonzeros] = i + offsetEpetra64;
85 jv[nMyNonzeros] = i-1 + offsetEpetra64;
86 vv[nMyNonzeros] = -1;
87 nMyNonzeros++;
88 nnzPerRow[myrowcnt]++;
89 }
90 if (!colMap || colMap->MyGID(i+offsetEpetra64)) {
91 iv[nMyNonzeros] = i + offsetEpetra64;
92 jv[nMyNonzeros] = i + offsetEpetra64;
93 vv[nMyNonzeros] = ((i == 0 || i == nGlobalRows-1) ? 1. : 2.);
94 nMyNonzeros++;
95 nnzPerRow[myrowcnt]++;
96 }
97 if (i < nGlobalRows - 1 && (!colMap || colMap->MyGID(i+1+offsetEpetra64))) {
98 iv[nMyNonzeros] = i + offsetEpetra64;
99 jv[nMyNonzeros] = i+1 + offsetEpetra64;
100 vv[nMyNonzeros] = -1;
101 nMyNonzeros++;
102 nnzPerRow[myrowcnt]++;
103 }
104 myrowcnt++;
105 }
106 }
107
108 // Create an Epetra_Matrix
109 A = new Epetra_CrsMatrix(Epetra_DataAccess::Copy, *rowMap, &nnzPerRow[0], true);
110
111 int info;
112
113 for (int sum = 0, i=0; i < nMyRows; i++) {
114 if (nnzPerRow[i]) {
115 info = A->InsertGlobalValues(iv[sum],nnzPerRow[i],&vv[sum],&jv[sum]);
116 TEUCHOS_ASSERT(info==0);
117 sum += nnzPerRow[i];
118 }
119 }
120
121 // Finish up
122 if (vectorMap)
123 info = A->FillComplete(*vectorMap, *vectorMap);
124 else
125 info = A->FillComplete();
126
127 // Clean up
128 delete rowMap;
129 delete colMap;
130 delete vectorMap;
131
132 TEUCHOS_ASSERT(info==0);
133
134 }
135
136 #endif