EpetraExt Package Browser (Single Doxygen Collection) Development
EpetraExt_MultiVectorOut.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_MultiVectorOut.h"
00042 #include "EpetraExt_mmio.h"
00043 #include "Epetra_Comm.h"
00044 #include "Epetra_BlockMap.h"
00045 #include "Epetra_Map.h"
00046 #include "Epetra_Vector.h"
00047 #include "Epetra_IntVector.h"
00048 #include "Epetra_LongLongVector.h"
00049 #include "Epetra_GIDTypeVector.h"
00050 #include "Epetra_SerialDenseVector.h"
00051 #include "Epetra_IntSerialDenseVector.h"
00052 #include "Epetra_LongLongSerialDenseVector.h"
00053 #include "Epetra_GIDTypeSerialDenseVector.h"
00054 #include "Epetra_Import.h"
00055 #include "Epetra_CrsMatrix.h"
00056 
00057 using namespace EpetraExt;
00058 namespace EpetraExt {
00059 
00060 int MultiVectorToMatlabFile( const char *filename, const Epetra_MultiVector & A) {
00061 
00062   std::FILE * handle = 0;
00063   if (A.Map().Comm().MyPID()==0) { // Only PE 0 does this section
00064     handle = fopen(filename,"w");
00065     if (!handle) return(-1);
00066   }
00067   if (MultiVectorToMatlabHandle(handle, A)) return(-1); // Everybody calls this routine
00068 
00069   if (A.Map().Comm().MyPID()==0) // Only PE 0 opened a file
00070     if (fclose(handle)) return(-1);
00071   return(0);
00072 }
00073 
00074 int MultiVectorToMatrixMarketFile( const char *filename, const Epetra_MultiVector & A, 
00075          const char * matrixName,
00076          const char *matrixDescription, 
00077          bool writeHeader) {
00078   long long M = A.GlobalLength64();
00079   int N = A.NumVectors();
00080 
00081   FILE * handle = 0;
00082 
00083   if (A.Map().Comm().MyPID()==0) { // Only PE 0 does this section
00084 
00085     handle = fopen(filename,"w");
00086     if (!handle) return(-1);
00087     MM_typecode matcode;
00088     mm_initialize_typecode(&matcode);
00089     mm_set_matrix(&matcode);
00090     mm_set_array(&matcode);
00091     mm_set_real(&matcode);
00092 
00093     if (writeHeader==true) { // Only write header if requested (true by default)
00094     
00095       if (mm_write_banner(handle, matcode)) return(-1);
00096       
00097       if (matrixName!=0) fprintf(handle, "%% \n%% %s\n", matrixName);
00098       if (matrixDescription!=0) fprintf(handle, "%% %s\n%% \n", matrixDescription);
00099       
00100       if (mm_write_mtx_array_size(handle, M, N)) return(-1);
00101     }
00102   }
00103     
00104   if (MultiVectorToMatrixMarketHandle(handle, A)) return(-1); // Everybody calls this routine
00105 
00106   if (A.Map().Comm().MyPID()==0) // Only PE 0 opened a file
00107     if (fclose(handle)) return(-1);
00108   return(0);
00109 }
00110 
00111 int MultiVectorToMatlabHandle(FILE * handle, const Epetra_MultiVector & A) {
00112   return(MultiVectorToHandle(handle, A, false));
00113 }
00114 int MultiVectorToMatrixMarketHandle(FILE * handle, const Epetra_MultiVector & A) {
00115   return(MultiVectorToHandle(handle, A, true));
00116 }
00117 
00118 template<typename int_type>
00119 int TMultiVectorToHandle(FILE * handle, const Epetra_MultiVector & A, bool mmFormat) {
00120 
00121   Epetra_BlockMap bmap = A.Map();
00122   const Epetra_Comm & comm = bmap.Comm();
00123   int numProc = comm.NumProc();
00124 
00125   if (numProc==1)
00126     writeMultiVector(handle, A, mmFormat);
00127   else {
00128 
00129     // In the case of more than one column in the multivector, and writing to MatrixMarket
00130     // format, we call this function recursively, passing each vector of the multivector
00131     // individually so that we can get all of it written to file before going on to the next 
00132     // multivector
00133     if (A.NumVectors()>1 && mmFormat) {
00134       for (int i=0; i<A.NumVectors(); i++)
00135   if (MultiVectorToHandle(handle, *(A(i)), mmFormat)) return(-1);
00136       return(0);
00137     }
00138 
00139     Epetra_Map map((int_type) -1, bmap.NumMyPoints(), (int_type) 0, comm);
00140     // Create a veiw of this multivector using a map (instead of block map)
00141     Epetra_MultiVector A1(View, map, A.Pointers(), A.NumVectors());
00142     int numRows = map.NumMyElements();
00143     
00144     Epetra_Map allGidsMap((int_type) -1, numRows, (int_type) 0,comm);
00145     
00146     typename Epetra_GIDTypeVector<int_type>::impl allGids(allGidsMap);
00147     for (int i=0; i<numRows; i++) allGids[i] = (int_type) map.GID64(i);
00148     
00149     // Now construct a MultiVector on PE 0 by strip-mining the rows of the input matrix A.
00150     int numChunks = numProc;
00151     int stripSize = allGids.GlobalLength64()/numChunks;
00152     int remainder = allGids.GlobalLength64()%numChunks;
00153     int curStart = 0;
00154     int curStripSize = 0;
00155     typename Epetra_GIDTypeSerialDenseVector<int_type>::impl importGidList;
00156     if (comm.MyPID()==0) 
00157       importGidList.Size(stripSize+1); // Set size of vector to max needed
00158     for (int i=0; i<numChunks; i++) {
00159       if (comm.MyPID()==0) { // Only PE 0 does this part
00160   curStripSize = stripSize;
00161   if (i<remainder) curStripSize++; // handle leftovers
00162   for (int j=0; j<curStripSize; j++) importGidList[j] = j + curStart;
00163   curStart += curStripSize;
00164       }
00165       // The following import map will be non-trivial only on PE 0.
00166       Epetra_Map importGidMap((int_type) -1, curStripSize, importGidList.Values(), (int_type) 0, comm);
00167       Epetra_Import gidImporter(importGidMap, allGidsMap);
00168       typename Epetra_GIDTypeVector<int_type>::impl importGids(importGidMap);
00169       if (importGids.Import(allGids, gidImporter, Insert)) return(-1); 
00170 
00171       // importGids now has a list of GIDs for the current strip of matrix rows.
00172       // Use these values to build another importer that will get rows of the matrix.
00173 
00174       // The following import map will be non-trivial only on PE 0.
00175       Epetra_Map importMap((int_type) -1, importGids.MyLength(), importGids.Values(), (int_type) 0, comm);
00176       Epetra_Import importer(importMap, map);
00177       Epetra_MultiVector importA(importMap, A1.NumVectors());
00178       if (importA.Import(A1, importer, Insert)) return(-1); 
00179 
00180       // Finally we are ready to write this strip of the matrix to ostream
00181       if (writeMultiVector(handle, importA, mmFormat)) return(-1);
00182     }
00183   }
00184   return(0);
00185 }
00186 
00187 int MultiVectorToHandle(FILE * handle, const Epetra_MultiVector & A, bool mmFormat) {
00188 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
00189   if(A.Map().GlobalIndicesInt()) {
00190     return TMultiVectorToHandle<int>(handle, A, mmFormat);
00191   }
00192   else
00193 #endif
00194 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
00195   if(A.Map().GlobalIndicesLongLong()) {
00196     return TMultiVectorToHandle<long long>(handle, A, mmFormat);
00197   }
00198   else
00199 #endif
00200     throw "EpetraExt::MultiVectorToHandle: GlobalIndices type unknown";
00201 }
00202 
00203 int writeMultiVector(FILE * handle, const Epetra_MultiVector & A, bool mmFormat) {
00204 
00205   int ierr = 0;
00206   long long length = A.GlobalLength64();
00207   int numVectors = A.NumVectors();
00208   const Epetra_Comm & comm = A.Map().Comm();
00209   if (comm.MyPID()!=0) {
00210     if (A.MyLength()!=0) ierr = -1;
00211   }
00212   else {
00213     if (length!=A.MyLength()) ierr = -1;
00214     for (int j=0; j<numVectors; j++) {
00215       for (long long i=0; i<length; i++) {
00216   double val = A[j][i];
00217   if (mmFormat)
00218     fprintf(handle, "%22.16e\n", val);
00219   else
00220     fprintf(handle, "%22.16e ", val);
00221       }
00222       if (!mmFormat) fprintf(handle, "%s", "\n");
00223     }
00224   }
00225   int ierrGlobal;
00226   comm.MinAll(&ierr, &ierrGlobal, 1); // If any processor has -1, all return -1
00227   return(ierrGlobal);
00228 }
00229 } // namespace EpetraExt
00230 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines