00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <cassert>
00030
00031 #include "DistributedVectorFactory.hpp"
00032
00033
00034 bool DistributedVectorFactory::msCheckNumberOfProcessesOnLoad = true;
00035
00036
00037
00038 void DistributedVectorFactory::CalculateOwnership(Vec vec)
00039 {
00040 #ifndef NDEBUG
00041 if (!mPetscStatusKnown)
00042 {
00043 CheckForPetsc();
00044 }
00045 #endif
00046
00047 PetscInt petsc_lo, petsc_hi;
00048 VecGetOwnershipRange(vec, &petsc_lo, &petsc_hi);
00049 mGlobalLows.clear();
00050 mLo = (unsigned)petsc_lo;
00051 mHi = (unsigned)petsc_hi;
00052
00053 PetscInt size;
00054 VecGetSize(vec, &size);
00055 mProblemSize = (unsigned) size;
00056 mNumProcs = PetscTools::GetNumProcs();
00057 }
00058
00059
00060 void DistributedVectorFactory::SetFromFactory(DistributedVectorFactory* pFactory)
00061 {
00062 if (pFactory->GetNumProcs() != mNumProcs)
00063 {
00064 EXCEPTION("Cannot set from a factory for a different number of processes.");
00065 }
00066 if (pFactory->GetProblemSize() != mProblemSize)
00067 {
00068 EXCEPTION("Cannot set from a factory for a different problem size.");
00069 }
00070 mGlobalLows.clear();
00071 mLo = pFactory->GetLow();
00072 mHi = pFactory->GetHigh();
00073 }
00074
00075 DistributedVectorFactory::DistributedVectorFactory(Vec vec)
00076 : mPetscStatusKnown(false),
00077 mpOriginalFactory(NULL)
00078 {
00079 CalculateOwnership(vec);
00080 }
00081
00082 DistributedVectorFactory::DistributedVectorFactory(unsigned size, PetscInt local)
00083 : mPetscStatusKnown(false),
00084 mpOriginalFactory(NULL)
00085 {
00086 #ifndef NDEBUG
00087 CheckForPetsc();
00088 #endif
00089 Vec vec=PetscTools::CreateVec(size, local);
00090 CalculateOwnership(vec);
00091 VecDestroy(vec);
00092 }
00093
00094 DistributedVectorFactory::DistributedVectorFactory(DistributedVectorFactory* pOriginalFactory)
00095 : mPetscStatusKnown(false),
00096 mpOriginalFactory(pOriginalFactory)
00097 {
00098 assert(mpOriginalFactory != NULL);
00099
00100
00101 Vec vec=PetscTools::CreateVec(mpOriginalFactory->GetProblemSize());
00102
00103 CalculateOwnership(vec);
00104 VecDestroy(vec);
00105 }
00106
00107 DistributedVectorFactory::DistributedVectorFactory(unsigned lo, unsigned hi, unsigned size, unsigned numProcs)
00108 : mLo(lo),
00109 mHi(hi),
00110 mProblemSize(size),
00111 mNumProcs(numProcs),
00112 mPetscStatusKnown(false),
00113 mpOriginalFactory(NULL)
00114 {
00115 #ifndef NDEBUG
00116 CheckForPetsc();
00117 #endif
00118 }
00119
00120 DistributedVectorFactory::~DistributedVectorFactory()
00121 {
00122 delete mpOriginalFactory;
00123 }
00124
00125
00126 void DistributedVectorFactory::CheckForPetsc()
00127 {
00128 assert(mPetscStatusKnown==false);
00129 PetscTruth petsc_is_initialised;
00130 PetscInitialized(&petsc_is_initialised);
00131
00132
00133
00134
00135 assert(petsc_is_initialised);
00136 mPetscStatusKnown=true;
00137 }
00138
00139 bool DistributedVectorFactory::IsGlobalIndexLocal(unsigned globalIndex)
00140 {
00141 return (mLo<=globalIndex && globalIndex<mHi);
00142 }
00143
00144 Vec DistributedVectorFactory::CreateVec()
00145 {
00146 Vec vec=PetscTools::CreateVec(mProblemSize, mHi-mLo);
00147 return vec;
00148 }
00149
00150 Vec DistributedVectorFactory::CreateVec(unsigned stride)
00151 {
00152 Vec vec;
00153 VecCreateMPI(PETSC_COMM_WORLD, stride*(mHi-mLo), stride*mProblemSize, &vec);
00154 return vec;
00155 }
00156
00157 DistributedVector DistributedVectorFactory::CreateDistributedVector(Vec vec)
00158 {
00159 DistributedVector dist_vector(vec, this);
00160 return dist_vector;
00161 }
00162
00163 std::vector<unsigned> &DistributedVectorFactory::rGetGlobalLows()
00164 {
00165 if (mGlobalLows.size() != PetscTools::GetNumProcs())
00166 {
00167 assert( mGlobalLows.empty());
00168 mGlobalLows.resize(PetscTools::GetNumProcs());
00169
00170 MPI_Allgather( &mLo, 1, MPI_UNSIGNED, &mGlobalLows[0], 1, MPI_UNSIGNED, PETSC_COMM_WORLD);
00171 }
00172
00173 return mGlobalLows;
00174 }
00175
00176 #include "SerializationExportWrapperForCpp.hpp"
00177 CHASTE_CLASS_EXPORT(DistributedVectorFactory)