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