Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "MOR_EpetraLocalMapMVMatrixMarketUtils.hpp"
00008
00009 #include "EpetraExt_MultiVectorOut.h"
00010 #include "EpetraExt_MultiVectorIn.h"
00011
00012 #include "Epetra_LocalMap.h"
00013 #include "Epetra_Export.h"
00014 #include "Epetra_Import.h"
00015
00016 #include "Teuchos_TestForException.hpp"
00017
00018 namespace MOR {
00019
00020 namespace Detail {
00021
00022 bool isRegularMap(const Epetra_BlockMap &candidate)
00023 {
00024 return candidate.ConstantElementSize() && candidate.ElementSize() == 1;
00025 }
00026
00027 bool hasMatchingLocalAndGlobalIDs(const Epetra_BlockMap &candidate)
00028 {
00029 return candidate.MinAllGID() == 0 && (candidate.MaxAllGID() + 1 == candidate.NumGlobalElements());
00030 }
00031
00032 bool isRegularMapLocal(const Epetra_BlockMap ®ularCandidate)
00033 {
00034 return hasMatchingLocalAndGlobalIDs(regularCandidate) && !regularCandidate.DistributedGlobal();
00035 }
00036
00037 bool isLocalMap(const Epetra_Map &candidate)
00038 {
00039 return isRegularMapLocal(candidate);
00040 }
00041
00042 bool isLocalMap(const Epetra_BlockMap &candidate)
00043 {
00044 return isRegularMap(candidate) && isRegularMapLocal(candidate);
00045 }
00046
00047 Epetra_Map makeMasterMap(const Epetra_Comm &comm, int vectorSize)
00048 {
00049 const int myElementCount = comm.MyPID() == 0 ? vectorSize : 0;
00050 return Epetra_Map(vectorSize, myElementCount, 0, comm);
00051 }
00052
00053 }
00054
00055 using namespace Detail;
00056
00057 void writeLocalMapMultiVectorToMatrixMarket(
00058 const std::string &fileName,
00059 const Epetra_MultiVector &localMapMv)
00060 {
00061 const Epetra_BlockMap sourceMap = localMapMv.Map();
00062 TEUCHOS_TEST_FOR_EXCEPT(!isLocalMap(sourceMap));
00063
00064 const Epetra_BlockMap masterMap = makeMasterMap(sourceMap.Comm(), sourceMap.NumGlobalElements());
00065 const Epetra_Export exportFromSourceToMaster(sourceMap, masterMap);
00066
00067 Epetra_MultiVector masterMv(masterMap, localMapMv.NumVectors(), true);
00068 masterMv.Export(localMapMv, exportFromSourceToMaster, Insert);
00069
00070 {
00071 const int ierr = EpetraExt::MultiVectorToMatrixMarketFile(fileName.c_str(), masterMv);
00072 TEUCHOS_TEST_FOR_EXCEPT(ierr != 0);
00073 }
00074 }
00075
00076
00077 Teuchos::RCP<Epetra_MultiVector> readLocalMapMultiVectorFromMatrixMarket(
00078 const std::string &fileName, const Epetra_Comm &comm, int vectorSize)
00079 {
00080 const Epetra_Map masterMap = makeMasterMap(comm, vectorSize);
00081
00082 Teuchos::RCP<Epetra_MultiVector> masterMv;
00083 {
00084 Epetra_MultiVector *masterMvRawPtr;
00085 const int ierr = EpetraExt::MatrixMarketFileToMultiVector(fileName.c_str(), masterMap, masterMvRawPtr);
00086 TEUCHOS_TEST_FOR_EXCEPT(ierr != 0);
00087 masterMv = Teuchos::rcp(masterMvRawPtr);
00088 }
00089
00090 const Epetra_LocalMap localMap(vectorSize, 0, comm);
00091 const Epetra_Import importer(localMap, masterMap);
00092 const Teuchos::RCP<Epetra_MultiVector> result(new Epetra_MultiVector(localMap, masterMv->NumVectors()));
00093 result->Import(*masterMv, importer, Insert);
00094
00095 return result;
00096 }
00097
00098 }