DistributedVectorFactory.cpp
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 mLo = (unsigned)petsc_lo;
00050 mHi = (unsigned)petsc_hi;
00051
00052 PetscInt size;
00053 VecGetSize(vec, &size);
00054 mProblemSize = (unsigned) size;
00055 mNumProcs = PetscTools::GetNumProcs();
00056 }
00057
00058
00059 void DistributedVectorFactory::SetFromFactory(DistributedVectorFactory* pFactory)
00060 {
00061 if (pFactory->GetNumProcs() != mNumProcs)
00062 {
00063 EXCEPTION("Cannot set from a factory for a different number of processes.");
00064 }
00065 if (pFactory->GetProblemSize() != mProblemSize)
00066 {
00067 EXCEPTION("Cannot set from a factory for a different problem size.");
00068 }
00069 mLo = pFactory->GetLow();
00070 mHi = pFactory->GetHigh();
00071 }
00072
00073 DistributedVectorFactory::DistributedVectorFactory(Vec vec)
00074 : mPetscStatusKnown(false),
00075 mpOriginalFactory(NULL)
00076 {
00077 CalculateOwnership(vec);
00078 }
00079
00080 DistributedVectorFactory::DistributedVectorFactory(unsigned size, PetscInt local)
00081 : mPetscStatusKnown(false),
00082 mpOriginalFactory(NULL)
00083 {
00084 #ifndef NDEBUG
00085 CheckForPetsc();
00086 #endif
00087 Vec vec;
00088 VecCreate(PETSC_COMM_WORLD, &vec);
00089 VecSetSizes(vec, local, size);
00090 VecSetFromOptions(vec);
00091 CalculateOwnership(vec);
00092 VecDestroy(vec);
00093 }
00094
00095 DistributedVectorFactory::DistributedVectorFactory(DistributedVectorFactory* pOriginalFactory)
00096 : mPetscStatusKnown(false),
00097 mpOriginalFactory(pOriginalFactory)
00098 {
00099 assert(mpOriginalFactory != NULL);
00100 Vec vec;
00101 VecCreate(PETSC_COMM_WORLD, &vec);
00102 VecSetSizes(vec, PETSC_DECIDE, mpOriginalFactory->GetProblemSize());
00103 VecSetFromOptions(vec);
00104 CalculateOwnership(vec);
00105 VecDestroy(vec);
00106 }
00107
00108 DistributedVectorFactory::DistributedVectorFactory(unsigned lo, unsigned hi, unsigned size, unsigned numProcs)
00109 : mLo(lo),
00110 mHi(hi),
00111 mProblemSize(size),
00112 mNumProcs(numProcs),
00113 mPetscStatusKnown(false),
00114 mpOriginalFactory(NULL)
00115 {
00116 #ifndef NDEBUG
00117 CheckForPetsc();
00118 #endif
00119 }
00120
00121 DistributedVectorFactory::~DistributedVectorFactory()
00122 {
00123 delete mpOriginalFactory;
00124 }
00125
00126
00127 void DistributedVectorFactory::CheckForPetsc()
00128 {
00129 assert(mPetscStatusKnown==false);
00130 PetscTruth petsc_is_initialised;
00131 PetscInitialized(&petsc_is_initialised);
00132
00133
00134
00135
00136 assert(petsc_is_initialised);
00137 mPetscStatusKnown=true;
00138 }
00139
00140 bool DistributedVectorFactory::IsGlobalIndexLocal(unsigned globalIndex)
00141 {
00142 return (mLo<=globalIndex && globalIndex<mHi);
00143 }
00144
00145 Vec DistributedVectorFactory::CreateVec()
00146 {
00147 Vec vec;
00148 VecCreate(PETSC_COMM_WORLD, &vec);
00149 VecSetSizes(vec, mHi-mLo, mProblemSize);
00150 VecSetFromOptions(vec);
00151 return vec;
00152 }
00153
00154 Vec DistributedVectorFactory::CreateVec(unsigned stride)
00155 {
00156 Vec vec;
00157 VecCreateMPI(PETSC_COMM_WORLD, stride*(mHi-mLo), stride*mProblemSize, &vec);
00158 return vec;
00159 }
00160
00161 DistributedVector DistributedVectorFactory::CreateDistributedVector(Vec vec)
00162 {
00163 DistributedVector dist_vector(vec, this);
00164 return dist_vector;
00165 }
00166
00167
00168
00169 #include "SerializationExportWrapperForCpp.hpp"
00170 CHASTE_CLASS_EXPORT(DistributedVectorFactory);