EpetraExt Package Browser (Single Doxygen Collection) Development
EpetraExt_RowMatrixOut.cpp
Go to the documentation of this file.
00001 //@HEADER
00002 // ***********************************************************************
00003 //
00004 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00005 //                 Copyright (2011) Sandia Corporation
00006 //
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 //@HEADER
00041 #include "EpetraExt_RowMatrixOut.h"
00042 #include "EpetraExt_mmio.h"
00043 #include "Epetra_Comm.h"
00044 #include "Epetra_Map.h"
00045 #include "Epetra_Vector.h"
00046 #include "Epetra_IntVector.h"
00047 #include "Epetra_SerialDenseVector.h"
00048 #include "Epetra_IntSerialDenseVector.h"
00049 #include "Epetra_Import.h"
00050 #include "Epetra_CrsMatrix.h"
00051 
00052 using namespace EpetraExt;
00053 namespace EpetraExt {
00054 
00055 int RowMatrixToMatlabFile( const char *filename, const Epetra_RowMatrix & A) {
00056 
00057   // Simple wrapper to make it clear what can be used to write to Matlab format
00058   return(RowMatrixToMatrixMarketFile(filename, A, 0, 0, false));
00059 }
00060 
00061 int RowMatrixToMatrixMarketFile( const char *filename, const Epetra_RowMatrix & A, 
00062          const char * matrixName,
00063          const char *matrixDescription, 
00064          bool writeHeader) {
00065   int M = A.NumGlobalRows();
00066   int N = A.NumGlobalCols();
00067   int nz = A.NumGlobalNonzeros();
00068 
00069   FILE * handle = 0;
00070 
00071   if (A.RowMatrixRowMap().Comm().MyPID()==0) { // Only PE 0 does this section
00072 
00073     handle = fopen(filename,"w");
00074     if (!handle) {EPETRA_CHK_ERR(-1);}
00075     MM_typecode matcode;
00076     mm_initialize_typecode(&matcode);
00077     mm_set_matrix(&matcode);
00078     mm_set_coordinate(&matcode);
00079     mm_set_real(&matcode);
00080 
00081     if (writeHeader==true) { // Only write header if requested (true by default)
00082     
00083       if (mm_write_banner(handle, matcode)!=0) {EPETRA_CHK_ERR(-1);}
00084       
00085       if (matrixName!=0) fprintf(handle, "%% \n%% %s\n", matrixName);
00086       if (matrixDescription!=0) fprintf(handle, "%% %s\n%% \n", matrixDescription);
00087       
00088       if (mm_write_mtx_crd_size(handle, M, N, nz)!=0) {EPETRA_CHK_ERR(-1);}
00089     }
00090   }
00091     
00092   if (RowMatrixToHandle(handle, A)!=0) {EPETRA_CHK_ERR(-1);}// Everybody calls this routine
00093 
00094   if (A.RowMatrixRowMap().Comm().MyPID()==0) // Only PE 0 opened a file
00095     if (fclose(handle)!=0) {EPETRA_CHK_ERR(-1);}
00096   return(0);
00097 }
00098 
00099 int RowMatrixToHandle(FILE * handle, const Epetra_RowMatrix & A) {
00100 
00101   Epetra_Map map = A.RowMatrixRowMap();
00102   const Epetra_Comm & comm = map.Comm();
00103   int numProc = comm.NumProc();
00104 
00105   if (numProc==1 || !A.Map().DistributedGlobal())
00106     writeRowMatrix(handle, A);
00107   else {
00108     int numRows = map.NumMyElements();
00109     
00110     Epetra_Map allGidsMap(-1, numRows, 0,comm);
00111     
00112     Epetra_IntVector allGids(allGidsMap);
00113     for (int i=0; i<numRows; i++) allGids[i] = map.GID(i);
00114     
00115     // Now construct a RowMatrix on PE 0 by strip-mining the rows of the input matrix A.
00116     int numChunks = numProc;
00117     int stripSize = allGids.GlobalLength()/numChunks;
00118     int remainder = allGids.GlobalLength()%numChunks;
00119     int curStart = 0;
00120     int curStripSize = 0;
00121     Epetra_IntSerialDenseVector importGidList;
00122     if (comm.MyPID()==0) 
00123       importGidList.Size(stripSize+1); // Set size of vector to max needed
00124     for (int i=0; i<numChunks; i++) {
00125       if (comm.MyPID()==0) { // Only PE 0 does this part
00126   curStripSize = stripSize;
00127   if (i<remainder) curStripSize++; // handle leftovers
00128   for (int j=0; j<curStripSize; j++) importGidList[j] = j + curStart;
00129   curStart += curStripSize;
00130       }
00131       // The following import map will be non-trivial only on PE 0.
00132       if (comm.MyPID()>0) assert(curStripSize==0);
00133       Epetra_Map importGidMap(-1, curStripSize, importGidList.Values(), 0, comm);
00134       Epetra_Import gidImporter(importGidMap, allGidsMap);
00135       Epetra_IntVector importGids(importGidMap);
00136       if (importGids.Import(allGids, gidImporter, Insert)!=0) {EPETRA_CHK_ERR(-1); }
00137 
00138       // importGids now has a list of GIDs for the current strip of matrix rows.
00139       // Use these values to build another importer that will get rows of the matrix.
00140 
00141       // The following import map will be non-trivial only on PE 0.
00142       Epetra_Map importMap(-1, importGids.MyLength(), importGids.Values(), map.IndexBase(), comm);
00143       Epetra_Import importer(importMap, map);
00144       Epetra_CrsMatrix importA(Copy, importMap, 0);
00145       if (importA.Import(A, importer, Insert)!=0) {EPETRA_CHK_ERR(-1); }
00146       if (importA.FillComplete(A.OperatorDomainMap(), importMap)!=0) {EPETRA_CHK_ERR(-1);}
00147 
00148       // Finally we are ready to write this strip of the matrix to ostream
00149       if (writeRowMatrix(handle, importA)!=0) {EPETRA_CHK_ERR(-1);}
00150     }
00151   }
00152   return(0);
00153 }
00154 int writeRowMatrix(FILE * handle, const Epetra_RowMatrix & A) {
00155 
00156   int numRows = A.NumGlobalRows();
00157   Epetra_Map rowMap = A.RowMatrixRowMap();
00158   Epetra_Map colMap = A.RowMatrixColMap();
00159   const Epetra_Comm & comm = rowMap.Comm();
00160   int ioffset = 1 - rowMap.IndexBase(); // Matlab indices start at 1
00161   int joffset = 1 - colMap.IndexBase(); // Matlab indices start at 1
00162   if (comm.MyPID()!=0) {
00163     if (A.NumMyRows()!=0) {EPETRA_CHK_ERR(-1);}
00164     if (A.NumMyCols()!=0) {EPETRA_CHK_ERR(-1);}
00165   }
00166   else {
00167     if (numRows!=A.NumMyRows()) {EPETRA_CHK_ERR(-1);}
00168     Epetra_SerialDenseVector values(A.MaxNumEntries());
00169     Epetra_IntSerialDenseVector indices(A.MaxNumEntries());
00170     for (int i=0; i<numRows; i++) {
00171       int I = rowMap.GID(i) + ioffset;
00172       int numEntries;
00173       if (A.ExtractMyRowCopy(i, values.Length(), numEntries, 
00174            values.Values(), indices.Values())!=0) {EPETRA_CHK_ERR(-1);}
00175       for (int j=0; j<numEntries; j++) {
00176   int J = colMap.GID(indices[j]) + joffset;
00177   double val = values[j];
00178   fprintf(handle, "%d %d %22.16e\n", I, J, val);
00179       }
00180     }
00181   }
00182   return(0);
00183 }
00184 } // namespace EpetraExt
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines