EpetraExt Package Browser (Single Doxygen Collection)
Development
Toggle main menu visibility
Loading...
Searching...
No Matches
src
matlab
EpetraExt_PutRowMatrix.cpp
Go to the documentation of this file.
1
//@HEADER
2
// ***********************************************************************
3
//
4
// EpetraExt: Epetra Extended - Linear Algebra Services Package
5
// Copyright (2011) Sandia Corporation
6
//
7
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8
// the U.S. Government retains certain rights in this software.
9
//
10
// Redistribution and use in source and binary forms, with or without
11
// modification, are permitted provided that the following conditions are
12
// met:
13
//
14
// 1. Redistributions of source code must retain the above copyright
15
// notice, this list of conditions and the following disclaimer.
16
//
17
// 2. Redistributions in binary form must reproduce the above copyright
18
// notice, this list of conditions and the following disclaimer in the
19
// documentation and/or other materials provided with the distribution.
20
//
21
// 3. Neither the name of the Corporation nor the names of the
22
// contributors may be used to endorse or promote products derived from
23
// this software without specific prior written permission.
24
//
25
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
//
37
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38
//
39
// ***********************************************************************
40
//@HEADER
41
42
#include "
EpetraExt_PutRowMatrix.h
"
43
#include "
Epetra_Comm.h
"
44
#include "
Epetra_Map.h
"
45
#include "
Epetra_Vector.h
"
46
#include "
Epetra_IntVector.h
"
47
#include "
Epetra_SerialDenseVector.h
"
48
#include "
Epetra_IntSerialDenseVector.h
"
49
#include "
Epetra_Import.h
"
50
#include "
Epetra_RowMatrix.h
"
51
#include "
Epetra_CrsMatrix.h
"
52
53
using namespace
Matlab
;
54
namespace
Matlab
{
55
56
int
CopyRowMatrix
(mxArray* matlabA,
const
Epetra_RowMatrix
& A) {
57
int
valueCount = 0;
58
//int* valueCount = &temp;
59
60
Epetra_Map
map = A.
RowMatrixRowMap
();
61
const
Epetra_Comm
& comm = map.
Comm
();
62
int
numProc = comm.
NumProc
();
63
64
if
(numProc==1)
65
DoCopyRowMatrix
(matlabA, valueCount, A);
66
else
{
67
int
numRows = map.
NumMyElements
();
68
69
//cout << "creating allGidsMap\n";
70
Epetra_Map
allGidsMap(-1, numRows, 0,comm);
71
//cout << "done creating allGidsMap\n";
72
73
Epetra_IntVector
allGids(allGidsMap);
74
for
(
int
i=0; i<numRows; i++) allGids[i] = map.
GID
(i);
75
76
// Now construct a RowMatrix on PE 0 by strip-mining the rows of the input matrix A.
77
int
numChunks = numProc;
78
int
stripSize = allGids.
GlobalLength
()/numChunks;
79
int
remainder = allGids.
GlobalLength
()%numChunks;
80
int
curStart = 0;
81
int
curStripSize = 0;
82
Epetra_IntSerialDenseVector
importGidList;
83
int
numImportGids = 0;
84
if
(comm.
MyPID
()==0)
85
importGidList.
Size
(stripSize+1);
// Set size of vector to max needed
86
for
(
int
i=0; i<numChunks; i++) {
87
if
(comm.
MyPID
()==0) {
// Only PE 0 does this part
88
curStripSize = stripSize;
89
if
(i<remainder) curStripSize++;
// handle leftovers
90
for
(
int
j=0; j<curStripSize; j++) importGidList[j] = j + curStart;
91
curStart += curStripSize;
92
}
93
// The following import map will be non-trivial only on PE 0.
94
//cout << "creating importGidMap\n";
95
Epetra_Map
importGidMap(-1, curStripSize, importGidList.
Values
(), 0, comm);
96
//cout << "done creating importGidMap\n";
97
Epetra_Import
gidImporter(importGidMap, allGidsMap);
98
Epetra_IntVector
importGids(importGidMap);
99
if
(importGids.
Import
(allGids, gidImporter,
Insert
))
return
(-1);
100
101
// importGids now has a list of GIDs for the current strip of matrix rows.
102
// Use these values to build another importer that will get rows of the matrix.
103
104
// The following import map will be non-trivial only on PE 0.
105
//cout << "creating importMap\n";
106
//cout << "A.RowMatrixRowMap().MinAllGID: " << A.RowMatrixRowMap().MinAllGID() << "\n";
107
Epetra_Map
importMap(-1, importGids.
MyLength
(), importGids.
Values
(), A.
RowMatrixRowMap
().
MinAllGID
(), comm);
108
//cout << "done creating importMap\n";
109
Epetra_Import
importer(importMap, map);
110
Epetra_CrsMatrix
importA(
Copy
, importMap, 0);
111
if
(importA.
Import
(A, importer,
Insert
))
return
(-1);
112
if
(importA.
FillComplete
())
return
(-1);
113
114
// Finally we are ready to write this strip of the matrix to ostream
115
if
(
DoCopyRowMatrix
(matlabA, valueCount, importA))
return
(-1);
116
}
117
}
118
119
if
(A.
RowMatrixRowMap
().
Comm
().
MyPID
() == 0) {
120
// set max cap
121
int
* matlabAcolumnIndicesPtr = mxGetJc(matlabA);
122
matlabAcolumnIndicesPtr[A.
NumGlobalRows
()] = valueCount;
123
}
124
125
return
(0);
126
}
127
128
int
DoCopyRowMatrix
(mxArray* matlabA,
int
& valueCount,
const
Epetra_RowMatrix
& A) {
129
//cout << "doing DoCopyRowMatrix\n";
130
int
ierr = 0;
131
int
numRows = A.
NumGlobalRows
();
132
//cout << "numRows: " << numRows << "\n";
133
Epetra_Map
rowMap = A.
RowMatrixRowMap
();
134
Epetra_Map
colMap = A.
RowMatrixColMap
();
135
int
minAllGID = rowMap.
MinAllGID
();
136
137
const
Epetra_Comm
& comm = rowMap.
Comm
();
138
//cout << "did global setup\n";
139
if
(comm.
MyPID
()!=0) {
140
if
(A.
NumMyRows
()!=0) ierr = -1;
141
if
(A.
NumMyCols
()!=0) ierr = -1;
142
}
143
else
{
144
// declare and get initial values of all matlabA pointers
145
double
* matlabAvaluesPtr = mxGetPr(matlabA);
146
int
* matlabAcolumnIndicesPtr = mxGetJc(matlabA);
147
int
* matlabArowIndicesPtr = mxGetIr(matlabA);
148
149
// set all matlabA pointers to the proper offset
150
matlabAvaluesPtr += valueCount;
151
matlabArowIndicesPtr += valueCount;
152
153
if
(numRows!=A.
NumMyRows
()) ierr = -1;
154
Epetra_SerialDenseVector
values(A.
MaxNumEntries
());
155
Epetra_IntSerialDenseVector
indices(A.
MaxNumEntries
());
156
//cout << "did proc0 setup\n";
157
for
(
int
i=0; i<numRows; i++) {
158
//cout << "extracting a row\n";
159
int
I = rowMap.
GID
(i);
160
int
numEntries = 0;
161
if
(A.
ExtractMyRowCopy
(i, values.
Length
(), numEntries,
162
values.
Values
(), indices.
Values
()))
return
(-1);
163
matlabAcolumnIndicesPtr[I - minAllGID] = valueCount;
// set the starting index of column I
164
double
* serialValuesPtr = values.
Values
();
165
for
(
int
j=0; j<numEntries; j++) {
166
int
J = colMap.
GID
(indices[j]);
167
*matlabAvaluesPtr = *serialValuesPtr++;
168
*matlabArowIndicesPtr = J;
169
// increment matlabA pointers
170
matlabAvaluesPtr++;
171
matlabArowIndicesPtr++;
172
valueCount++;
173
}
174
}
175
//cout << "proc0 row extraction for this chunck is done\n";
176
}
177
178
/*
179
if (comm.MyPID() == 0) {
180
cout << "printing matlabA pointers\n";
181
double* matlabAvaluesPtr = mxGetPr(matlabA);
182
int* matlabAcolumnIndicesPtr = mxGetJc(matlabA);
183
int* matlabArowIndicesPtr = mxGetIr(matlabA);
184
for(int i=0; i < numRows; i++) {
185
for(int j=0; j < A.MaxNumEntries(); j++) {
186
cout << "*matlabAvaluesPtr: " << *matlabAvaluesPtr++ << " *matlabAcolumnIndicesPtr: " << *matlabAcolumnIndicesPtr++ << " *matlabArowIndicesPtr" << *matlabArowIndicesPtr++ << "\n";
187
}
188
}
189
190
cout << "done printing matlabA pointers\n";
191
}
192
*/
193
194
int
ierrGlobal;
195
comm.
MinAll
(&ierr, &ierrGlobal, 1);
// If any processor has -1, all return -1
196
return
(ierrGlobal);
197
}
198
199
}
// namespace Matlab
EpetraExt_PutRowMatrix.h
Insert
Insert
Epetra_Comm.h
Epetra_CrsMatrix.h
Copy
Copy
Epetra_Import.h
Epetra_IntSerialDenseVector.h
Epetra_IntVector.h
Epetra_Map.h
Epetra_RowMatrix.h
Epetra_SerialDenseVector.h
Epetra_Vector.h
Epetra_BlockMap::MinAllGID
int MinAllGID() const
Epetra_BlockMap::GID
int GID(int LID) const
Epetra_BlockMap::Comm
const Epetra_Comm & Comm() const
Epetra_BlockMap::NumMyElements
int NumMyElements() const
Epetra_Comm
Epetra_Comm::NumProc
virtual int NumProc() const=0
Epetra_Comm::MinAll
virtual int MinAll(double *PartialMins, double *GlobalMins, int Count) const=0
Epetra_Comm::MyPID
virtual int MyPID() const=0
Epetra_CrsMatrix
Epetra_CrsMatrix::FillComplete
int FillComplete(bool OptimizeDataStorage=true)
Epetra_DistObject::Import
int Import(const Epetra_SrcDistObject &A, const Epetra_Import &Importer, Epetra_CombineMode CombineMode, const Epetra_OffsetIndex *Indexor=0)
Epetra_Import
Epetra_IntSerialDenseVector
Epetra_IntSerialDenseVector::Size
int Size(int Length_in)
Epetra_IntSerialDenseVector::Values
int * Values()
Epetra_IntVector
Epetra_IntVector::MyLength
int MyLength() const
Epetra_IntVector::Values
int * Values() const
Epetra_IntVector::GlobalLength
int GlobalLength() const
Epetra_Map
Epetra_RowMatrix
Epetra_RowMatrix::NumMyRows
virtual int NumMyRows() const=0
Epetra_RowMatrix::NumMyCols
virtual int NumMyCols() const=0
Epetra_RowMatrix::RowMatrixColMap
virtual const Epetra_Map & RowMatrixColMap() const=0
Epetra_RowMatrix::RowMatrixRowMap
virtual const Epetra_Map & RowMatrixRowMap() const=0
Epetra_RowMatrix::NumGlobalRows
virtual int NumGlobalRows() const=0
Epetra_RowMatrix::MaxNumEntries
virtual int MaxNumEntries() const=0
Epetra_RowMatrix::ExtractMyRowCopy
virtual int ExtractMyRowCopy(int MyRow, int Length, int &NumEntries, double *Values, int *Indices) const=0
Epetra_SerialDenseVector
Epetra_SerialDenseVector::Values
double * Values() const
Epetra_SerialDenseVector::Length
int Length() const
Matlab
Definition
EpetraExt_PutBlockMap.cpp:51
Matlab::DoCopyRowMatrix
int DoCopyRowMatrix(mxArray *matlabA, int &valueCount, const Epetra_RowMatrix &A)
Definition
EpetraExt_PutRowMatrix.cpp:128
Matlab::CopyRowMatrix
int CopyRowMatrix(mxArray *matlabA, const Epetra_RowMatrix &A)
Definition
EpetraExt_PutRowMatrix.cpp:56
Generated by
1.17.0