|
EpetraExt
Development
|
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_LongLongVector.h" 00048 #include "Epetra_GIDTypeVector.h" 00049 #include "Epetra_SerialDenseVector.h" 00050 #include "Epetra_IntSerialDenseVector.h" 00051 #include "Epetra_LongLongSerialDenseVector.h" 00052 #include "Epetra_GIDTypeSerialDenseVector.h" 00053 #include "Epetra_Import.h" 00054 #include "Epetra_CrsMatrix.h" 00055 #include <limits> 00056 00057 using namespace EpetraExt; 00058 namespace EpetraExt { 00059 00060 int RowMatrixToMatlabFile( const char *filename, const Epetra_RowMatrix & A) { 00061 00062 // Simple wrapper to make it clear what can be used to write to Matlab format 00063 return(RowMatrixToMatrixMarketFile(filename, A, 0, 0, false)); 00064 } 00065 00066 int RowMatrixToMatrixMarketFile( const char *filename, const Epetra_RowMatrix & A, 00067 const char * matrixName, 00068 const char *matrixDescription, 00069 bool writeHeader) { 00070 long long M = A.NumGlobalRows64(); 00071 long long N = A.NumGlobalCols64(); 00072 long long nz = A.NumGlobalNonzeros64(); 00073 00074 FILE * handle = 0; 00075 00076 if (A.RowMatrixRowMap().Comm().MyPID()==0) { // Only PE 0 does this section 00077 00078 handle = fopen(filename,"w"); 00079 if (!handle) {EPETRA_CHK_ERR(-1);} 00080 MM_typecode matcode; 00081 mm_initialize_typecode(&matcode); 00082 mm_set_matrix(&matcode); 00083 mm_set_coordinate(&matcode); 00084 mm_set_real(&matcode); 00085 00086 if (writeHeader==true) { // Only write header if requested (true by default) 00087 00088 if (mm_write_banner(handle, matcode)!=0) {EPETRA_CHK_ERR(-1);} 00089 00090 if (matrixName!=0) fprintf(handle, "%% \n%% %s\n", matrixName); 00091 if (matrixDescription!=0) fprintf(handle, "%% %s\n%% \n", matrixDescription); 00092 00093 if (mm_write_mtx_crd_size(handle, M, N, nz)!=0) {EPETRA_CHK_ERR(-1);} 00094 } 00095 } 00096 00097 if (RowMatrixToHandle(handle, A)!=0) {EPETRA_CHK_ERR(-1);}// Everybody calls this routine 00098 00099 if (A.RowMatrixRowMap().Comm().MyPID()==0) // Only PE 0 opened a file 00100 if (fclose(handle)!=0) {EPETRA_CHK_ERR(-1);} 00101 return(0); 00102 } 00103 00104 template<typename int_type> 00105 int RowMatrixToHandle(FILE * handle, const Epetra_RowMatrix & A) { 00106 00107 Epetra_Map map = A.RowMatrixRowMap(); 00108 const Epetra_Comm & comm = map.Comm(); 00109 int numProc = comm.NumProc(); 00110 00111 if (numProc==1 || !A.Map().DistributedGlobal()) 00112 writeRowMatrix(handle, A); 00113 else { 00114 int numRows = map.NumMyElements(); 00115 00116 Epetra_Map allGidsMap((int_type) -1, numRows, (int_type) 0,comm); 00117 00118 typename Epetra_GIDTypeVector<int_type>::impl allGids(allGidsMap); 00119 for (int i=0; i<numRows; i++) allGids[i] = (int_type) map.GID64(i); 00120 00121 // Now construct a RowMatrix on PE 0 by strip-mining the rows of the input matrix A. 00122 int numChunks = numProc; 00123 int stripSize = allGids.GlobalLength64()/numChunks; 00124 int remainder = allGids.GlobalLength64()%numChunks; 00125 int curStart = 0; 00126 int curStripSize = 0; 00127 typename Epetra_GIDTypeSerialDenseVector<int_type>::impl importGidList; 00128 if (comm.MyPID()==0) 00129 importGidList.Size(stripSize+1); // Set size of vector to max needed 00130 for (int i=0; i<numChunks; i++) { 00131 if (comm.MyPID()==0) { // Only PE 0 does this part 00132 curStripSize = stripSize; 00133 if (i<remainder) curStripSize++; // handle leftovers 00134 for (int j=0; j<curStripSize; j++) importGidList[j] = j + curStart; 00135 curStart += curStripSize; 00136 } 00137 // The following import map will be non-trivial only on PE 0. 00138 if (comm.MyPID()>0) assert(curStripSize==0); 00139 Epetra_Map importGidMap(-1, curStripSize, importGidList.Values(), 0, comm); 00140 Epetra_Import gidImporter(importGidMap, allGidsMap); 00141 typename Epetra_GIDTypeVector<int_type>::impl importGids(importGidMap); 00142 if (importGids.Import(allGids, gidImporter, Insert)!=0) {EPETRA_CHK_ERR(-1); } 00143 00144 // importGids now has a list of GIDs for the current strip of matrix rows. 00145 // Use these values to build another importer that will get rows of the matrix. 00146 00147 // The following import map will be non-trivial only on PE 0. 00148 Epetra_Map importMap(-1, importGids.MyLength(), importGids.Values(), map.IndexBase64(), comm); 00149 Epetra_Import importer(importMap, map); 00150 Epetra_CrsMatrix importA(Copy, importMap, 0); 00151 if (importA.Import(A, importer, Insert)!=0) {EPETRA_CHK_ERR(-1); } 00152 if (importA.FillComplete(A.OperatorDomainMap(), importMap)!=0) {EPETRA_CHK_ERR(-1);} 00153 00154 // Finally we are ready to write this strip of the matrix to ostream 00155 if (writeRowMatrix(handle, importA)!=0) {EPETRA_CHK_ERR(-1);} 00156 } 00157 } 00158 return(0); 00159 } 00160 00161 int RowMatrixToHandle(FILE * handle, const Epetra_RowMatrix & A) { 00162 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES 00163 if(A.RowMatrixRowMap().GlobalIndicesInt()) { 00164 return RowMatrixToHandle<int>(handle, A); 00165 } 00166 else 00167 #endif 00168 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES 00169 if(A.RowMatrixRowMap().GlobalIndicesLongLong()) { 00170 return RowMatrixToHandle<long long>(handle, A); 00171 } 00172 else 00173 #endif 00174 throw "EpetraExt::RowMatrixToHandle: GlobalIndices type unknown"; 00175 } 00176 00177 int writeRowMatrix(FILE * handle, const Epetra_RowMatrix & A) { 00178 00179 long long numRows_LL = A.NumGlobalRows64(); 00180 if(numRows_LL > std::numeric_limits<int>::max()) 00181 throw "EpetraExt::writeRowMatrix: numRows_LL > std::numeric_limits<int>::max()"; 00182 00183 int numRows = static_cast<int>(numRows_LL); 00184 Epetra_Map rowMap = A.RowMatrixRowMap(); 00185 Epetra_Map colMap = A.RowMatrixColMap(); 00186 const Epetra_Comm & comm = rowMap.Comm(); 00187 long long ioffset = 1 - rowMap.IndexBase64(); // Matlab indices start at 1 00188 long long joffset = 1 - colMap.IndexBase64(); // Matlab indices start at 1 00189 if (comm.MyPID()!=0) { 00190 if (A.NumMyRows()!=0) {EPETRA_CHK_ERR(-1);} 00191 if (A.NumMyCols()!=0) {EPETRA_CHK_ERR(-1);} 00192 } 00193 else { 00194 if (numRows!=A.NumMyRows()) {EPETRA_CHK_ERR(-1);} 00195 Epetra_SerialDenseVector values(A.MaxNumEntries()); 00196 Epetra_IntSerialDenseVector indices(A.MaxNumEntries()); 00197 for (int i=0; i<numRows; i++) { 00198 long long I = rowMap.GID64(i) + ioffset; 00199 int numEntries; 00200 if (A.ExtractMyRowCopy(i, values.Length(), numEntries, 00201 values.Values(), indices.Values())!=0) {EPETRA_CHK_ERR(-1);} 00202 for (int j=0; j<numEntries; j++) { 00203 long long J = colMap.GID64(indices[j]) + joffset; 00204 double val = values[j]; 00205 fprintf(handle, "%lld %lld %22.16e\n", I, J, val); 00206 } 00207 } 00208 } 00209 return(0); 00210 } 00211 } // namespace EpetraExt
1.7.6.1