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 void DistributedVectorFactory::CalculateOwnership(Vec vec)
00034 {
00035 #ifndef NDEBUG
00036 if (!mPetscStatusKnown)
00037 {
00038 CheckForPetsc();
00039 }
00040 #endif
00041
00042 PetscInt petsc_lo, petsc_hi;
00043 VecGetOwnershipRange(vec, &petsc_lo, &petsc_hi);
00044 mLo = (unsigned)petsc_lo;
00045 mHi = (unsigned)petsc_hi;
00046
00047 PetscInt size;
00048 VecGetSize(vec, &size);
00049 mProblemSize = (unsigned) size;
00050 }
00051
00052 DistributedVectorFactory::DistributedVectorFactory(Vec vec) : mPetscStatusKnown(false)
00053 {
00054 CalculateOwnership(vec);
00055 }
00056
00057 DistributedVectorFactory::DistributedVectorFactory(unsigned size, PetscInt local) : mPetscStatusKnown(false)
00058 {
00059 #ifndef NDEBUG
00060 if (!mPetscStatusKnown)
00061 {
00062 CheckForPetsc();
00063 }
00064 #endif
00065 Vec vec;
00066 VecCreate(PETSC_COMM_WORLD, &vec);
00067 VecSetSizes(vec, local, size);
00068 VecSetFromOptions(vec);
00069 CalculateOwnership(vec);
00070 VecDestroy(vec);
00071 }
00072
00073
00074 void DistributedVectorFactory::CheckForPetsc()
00075 {
00076 assert(mPetscStatusKnown==false);
00077 PetscTruth petsc_is_initialised;
00078 PetscInitialized(&petsc_is_initialised);
00079
00080
00081
00082
00083 assert(petsc_is_initialised);
00084 mPetscStatusKnown=true;
00085 }
00086
00087 bool DistributedVectorFactory::IsGlobalIndexLocal(unsigned globalIndex)
00088 {
00089 return (mLo<=globalIndex && globalIndex<mHi);
00090 }
00091
00092 Vec DistributedVectorFactory::CreateVec()
00093 {
00094 Vec vec;
00095 VecCreate(PETSC_COMM_WORLD, &vec);
00096 VecSetSizes(vec, mHi-mLo, mProblemSize);
00097 VecSetFromOptions(vec);
00098 return vec;
00099 }
00100
00101 Vec DistributedVectorFactory::CreateVec(unsigned stride)
00102 {
00103 Vec vec;
00104 VecCreateMPI(PETSC_COMM_WORLD, stride*(mHi-mLo), stride*mProblemSize, &vec);
00105 return vec;
00106 }
00107
00108 DistributedVector DistributedVectorFactory::CreateDistributedVector(Vec vec)
00109 {
00110 DistributedVector dist_vector(vec, this);
00111 return dist_vector;
00112 }