EpetraExt Development
EpetraExt_PutMultiVector.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 
00042 #include "EpetraExt_PutMultiVector.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_IntSerialDenseVector.h"
00049 #include "Epetra_Import.h"
00050 
00051 using namespace Matlab;
00052 namespace Matlab {
00053 int CopyMultiVector(double** matlabApr, const Epetra_MultiVector& A) {
00054 
00055   Epetra_BlockMap bmap = A.Map();
00056   const Epetra_Comm & comm = bmap.Comm();
00057   int numProc = comm.NumProc();
00058 
00059   if (numProc==1)
00060     DoCopyMultiVector(matlabApr, A);
00061   else {
00062 
00063     // In the case of more than one column in the multivector, and writing to MatrixMarket
00064     // format, we call this function recursively, passing each vector of the multivector
00065     // individually so that we can get all of it written to file before going on to the next 
00066     // multivector
00067     if (A.NumVectors() > 1) {
00068       for (int i=0; i < A.NumVectors(); i++)
00069   if (CopyMultiVector(matlabApr, *(A(i)))) return(-1);
00070       return(0);
00071     }
00072 
00073     Epetra_Map map(-1, bmap.NumMyPoints(), 0, comm);
00074     // Create a veiw of this multivector using a map (instead of block map)
00075     Epetra_MultiVector A1(View, map, A.Pointers(), A.NumVectors());
00076     int numRows = map.NumMyElements();
00077     
00078     Epetra_Map allGidsMap(-1, numRows, 0,comm);
00079     
00080     Epetra_IntVector allGids(allGidsMap);
00081     for (int i=0; i<numRows; i++) allGids[i] = map.GID(i);
00082     
00083     // Now construct a MultiVector on PE 0 by strip-mining the rows of the input matrix A.
00084     int numChunks = numProc;
00085     int stripSize = allGids.GlobalLength()/numChunks;
00086     int remainder = allGids.GlobalLength()%numChunks;
00087     int curStart = 0;
00088     int curStripSize = 0;
00089     Epetra_IntSerialDenseVector importGidList;
00090     int numImportGids = 0;
00091     if (comm.MyPID()==0) 
00092       importGidList.Size(stripSize+1); // Set size of vector to max needed
00093     for (int i=0; i<numChunks; i++) {
00094       if (comm.MyPID()==0) { // Only PE 0 does this part
00095   curStripSize = stripSize;
00096   if (i<remainder) curStripSize++; // handle leftovers
00097   for (int j=0; j<curStripSize; j++) importGidList[j] = j + curStart;
00098   curStart += curStripSize;
00099       }
00100       // The following import map will be non-trivial only on PE 0.
00101       Epetra_Map importGidMap(-1, curStripSize, importGidList.Values(), 0, comm);
00102       Epetra_Import gidImporter(importGidMap, allGidsMap);
00103       Epetra_IntVector importGids(importGidMap);
00104       if (importGids.Import(allGids, gidImporter, Insert)) return(-1); 
00105 
00106       // importGids now has a list of GIDs for the current strip of matrix rows.
00107       // Use these values to build another importer that will get rows of the matrix.
00108 
00109       // The following import map will be non-trivial only on PE 0.
00110       Epetra_Map importMap(-1, importGids.MyLength(), importGids.Values(), 0, comm);
00111       Epetra_Import importer(importMap, map);
00112       Epetra_MultiVector importA(importMap, A1.NumVectors());
00113       if (importA.Import(A1, importer, Insert)) return(-1); 
00114 
00115       // Finally we are ready to write this strip of the matrix to ostream
00116       if (DoCopyMultiVector(matlabApr, importA)) return(-1);
00117     }
00118   }
00119   return(0);
00120 }
00121 
00122 int DoCopyMultiVector(double** matlabApr, const Epetra_MultiVector& A) {
00123 
00124   int ierr = 0;
00125   int length = A.GlobalLength();
00126   int numVectors = A.NumVectors();
00127   const Epetra_Comm & comm = A.Map().Comm();
00128   if (comm.MyPID()!=0) {
00129     if (A.MyLength()!=0) ierr = -1;
00130   }
00131   else {
00132     if (length!=A.MyLength()) ierr = -1;
00133     double* matlabAvalues = *matlabApr;
00134     double* Aptr = A.Values();
00135     memcpy((void *)matlabAvalues, (void *)Aptr, sizeof(*Aptr) * length * numVectors);
00136     *matlabApr += length;   
00137   }
00138   int ierrGlobal;
00139   comm.MinAll(&ierr, &ierrGlobal, 1); // If any processor has -1, all return -1
00140   return(ierrGlobal);
00141 }
00142 } // namespace Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines