9 #include <fei_ParameterSet.hpp>
10 #include "fei_Matrix_Local.hpp"
11 #include "fei_Matrix_core.hpp"
12 #include "fei_sstream.hpp"
13 #include "fei_fstream.hpp"
19 : matrixGraph_(matrixGraph),
20 sparseRowGraph_(sparseRowGraph),
21 coefs_(sparseRowGraph->packedColumnIndices.size(), 0.0),
28 Matrix_Local::~Matrix_Local()
46 {
return(
"fei::Matrix_Local" ); }
57 Matrix_Local::parameters(
int ,
const char*
const* )
63 Matrix_Local::getMatrixGraph()
const
64 {
return( matrixGraph_ ); }
68 { matrixGraph_ = matrixGraph; }
71 Matrix_Local::getGlobalNumRows()
const
72 {
return( sparseRowGraph_->rowNumbers.size() ); }
75 Matrix_Local::getLocalNumRows()
const
76 {
return( getGlobalNumRows() ); }
79 Matrix_Local::getRowIndex(
int rowNumber)
const
81 int* rows = &(sparseRowGraph_->rowNumbers[0]);
82 int numRows = getLocalNumRows();
87 Matrix_Local::getRowLength(
int row,
int& length)
const
89 int idx = getRowIndex(row);
90 if (idx < 0)
return(idx);
92 length = sparseRowGraph_->rowOffsets[idx+1] -
93 sparseRowGraph_->rowOffsets[idx];
98 Matrix_Local::putScalar(
double scalar)
100 for(
unsigned i=0; i<coefs_.size(); ++i) coefs_[i] = scalar;
101 stateChanged_ =
true;
106 Matrix_Local::copyOutRow(
int row,
int len,
double* coefs,
int* indices)
const
108 int idx = getRowIndex(row);
109 if (idx < 0)
return(idx);
111 int offset = sparseRowGraph_->rowOffsets[idx];
112 int length = sparseRowGraph_->rowOffsets[idx+1]-offset;
113 if (length > len) length = len;
115 for(
int i=0; i<length; ++i) {
116 indices[i] = sparseRowGraph_->packedColumnIndices[offset+i];
117 coefs[i] = coefs_[offset+i];
124 Matrix_Local::giveToMatrix(
int numRows,
const int* rows,
125 int numCols,
const int* cols,
126 const double*
const* values,
130 if (numRows == 0 || numCols == 0) {
134 if (format != FEI_DENSE_ROW && format != FEI_DENSE_COL) {
138 const double** myvalues =
const_cast<const double**
>(values);
139 if (format != FEI_DENSE_ROW) {
140 fei::Matrix_core::copyTransposeToWorkArrays(numRows, numCols, values,
141 work_data1D_, work_data2D_);
142 myvalues = &work_data2D_[0];
145 for(
int i=0; i<numRows; ++i) {
146 int idx = getRowIndex(rows[i]);
148 throw std::runtime_error(
"fei::Matrix_Local::sumIn ERROR, row not found.");
151 int offset = sparseRowGraph_->rowOffsets[idx];
152 int len = sparseRowGraph_->rowOffsets[idx+1] - offset;
154 int* colInds = &(sparseRowGraph_->packedColumnIndices[offset]);
155 double* coefs = &(coefs_[offset]);
157 for(
int j=0; j<numCols; ++j) {
160 throw std::runtime_error(
"fei::Matrix_Local::sumIn ERROR, col not found.");
164 coefs[idx2] += myvalues[i][j];
167 coefs[idx2] = myvalues[i][j];
172 stateChanged_ =
true;
177 Matrix_Local::sumIn(
int numRows,
const int* rows,
178 int numCols,
const int* cols,
179 const double*
const* values,
182 return( giveToMatrix(numRows, rows, numCols, cols, values,
187 Matrix_Local::copyIn(
int numRows,
const int* rows,
188 int numCols,
const int* cols,
189 const double*
const* values,
192 return( giveToMatrix(numRows, rows, numCols, cols, values,
197 Matrix_Local::sumInFieldData(
int fieldID,
201 const double*
const* data,
208 std::vector<int> indices(2*fieldSize);
211 for(
int i=1; i<fieldSize; ++i) {
212 indices[i] = indices[0]+i;
215 cspace->
getGlobalIndex(idType, colID, fieldID, indices[fieldSize]);
216 for(
int i=1; i<fieldSize; ++i) {
217 indices[fieldSize+i] = indices[fieldSize]+i;
220 return( giveToMatrix(fieldSize, &indices[0], fieldSize, &indices[fieldSize],
221 data,
true, format) );
225 Matrix_Local::sumInFieldData(
int fieldID,
235 std::vector<const double*> data2D(fieldSize);
238 for(
int i=0; i<fieldSize; ++i) {
239 data2D[i] = &data[offset];
243 return( sumInFieldData(fieldID, idType, rowID, colID,
244 &data2D[0], format) );
248 Matrix_Local::sumIn(
int blockID,
int connectivityID,
249 const double*
const* values,
252 int numIndices = matrixGraph_->getConnectivityNumIndices(blockID);
253 std::vector<int> indices(numIndices);
255 matrixGraph_->getConnectivityIndices(blockID, connectivityID,
256 numIndices, &indices[0], numIndices);
258 return( giveToMatrix(numIndices, &indices[0], numIndices, &indices[0],
259 values,
true, format) );
263 Matrix_Local::globalAssemble()
270 FEI_COUT <<
"fei::Matrix_Local::multiply NOT IMPLEMENTED."<<FEI_ENDL;
275 Matrix_Local::setCommSizes()
280 Matrix_Local::gatherFromOverlap(
bool accumulate)
287 Matrix_Local::writeToFile(
const char* filename,
288 bool matrixMarketFormat)
296 FEI_OSTRINGSTREAM osstr;
297 osstr << filename <<
"." << localProc <<
".mtx";
298 std::string fullname = osstr.str();
300 FEI_OFSTREAM ofstr(fullname.c_str(), IOS_OUT);
306 Matrix_Local::writeToStream(FEI_OSTREAM& ostrm,
307 bool matrixMarketFormat)
309 static char mmbanner[] =
"%%MatrixMarket matrix coordinate real general";
314 int numRows = getLocalNumRows();
316 int nnz = coefs_.size();
318 if (matrixMarketFormat) {
319 ostrm << mmbanner << FEI_ENDL;
320 ostrm << numRows <<
" " << numCols <<
" " << nnz << FEI_ENDL;
323 ostrm << numRows <<
" " << numCols <<
" "<< FEI_ENDL;
326 std::vector<int>& rowNumbers = sparseRowGraph_->rowNumbers;
327 std::vector<int>& rowOffsets = sparseRowGraph_->rowOffsets;
328 std::vector<int>& colIndices = sparseRowGraph_->packedColumnIndices;
330 ostrm.setf(IOS_SCIENTIFIC, IOS_FLOATFIELD);
334 for(
unsigned i=0; i<rowNumbers.size(); ++i) {
335 int rowlen = rowOffsets[i+1]-rowOffsets[i];
337 for(
int j=0; j<rowlen; ++j) {
338 if (matrixMarketFormat) {
339 ostrm << rowNumbers[i]+1 <<
" " << colIndices[offset]+1
340 <<
" " << coefs_[offset] << FEI_ENDL;
343 ostrm << rowNumbers[i] <<
" " << colIndices[offset]
344 <<
" " << coefs_[offset] << FEI_ENDL;
354 Matrix_Local::usingBlockEntryStorage()
358 Matrix_Local::markState()
360 stateChanged_ =
false;
364 Matrix_Local::changedSinceMark()
365 {
return(stateChanged_); }
367 const std::vector<int>&
368 Matrix_Local::getRowNumbers()
const
369 {
return( sparseRowGraph_->rowNumbers ); }
371 const std::vector<int>&
372 Matrix_Local::getRowOffsets()
const
373 {
return( sparseRowGraph_->rowOffsets ); }
375 const std::vector<int>&
376 Matrix_Local::getColumnIndices()
const
377 {
return( sparseRowGraph_->packedColumnIndices ); }
379 const std::vector<double>&
380 Matrix_Local::getCoefs()
const
381 {
return( coefs_ ); }
MPI_Comm getCommunicator() const
void writeToStream(snl_fei::RaggedTable< MAP_TYPE, SET_TYPE > &table, FEI_OSTREAM &os, const char *lineprefix=NULL)
virtual fei::SharedPtr< fei::SparseRowGraph > createGraph(bool blockEntryGraph, bool localRowGraph_includeSharedRows=false)=0
int getGlobalIndex(int idType, int ID, int fieldID, int fieldOffset, int whichComponentOfField, int &globalIndex)
int binarySearch(const T &item, const T *list, int len)
int localProc(MPI_Comm comm)
unsigned getFieldSize(int fieldID)
std::vector< int > & getEqnNumbers()
std::string typeName(const T &t)