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 "ParallelColumnDataWriter.hpp"
00030 #include "Exception.hpp"
00031 #include "DistributedVectorFactory.hpp"
00032
00033 ParallelColumnDataWriter::ParallelColumnDataWriter(const std::string& rDirectory,
00034 const std::string& rBaseName,
00035 bool cleanDirectory)
00036 : ColumnDataWriter::ColumnDataWriter(rDirectory, rBaseName, cleanDirectory),
00037 mConcentrated(NULL)
00038 {
00039 int num_procs, my_rank;
00040 MPI_Comm_size(PETSC_COMM_WORLD, &num_procs);
00041 if (num_procs==1)
00042 {
00043 mIsParallel = false;
00044 }
00045 else
00046 {
00047 mIsParallel = true;
00048 }
00049
00050 MPI_Comm_rank(PETSC_COMM_WORLD, &my_rank);
00051 if (my_rank==0)
00052 {
00053 mAmMaster = true;
00054 }
00055 else
00056 {
00057 mAmMaster = false;
00058 }
00059 }
00060
00061 void ParallelColumnDataWriter::PutVector(int variableID, Vec petscVector)
00062 {
00063 int size;
00064 VecGetSize(petscVector,&size);
00065
00066 if (size != mFixedDimensionSize)
00067 {
00068 EXCEPTION("Size of vector does not match FixedDimensionSize.");
00069 }
00070
00071
00072 if (mConcentrated==NULL)
00073 {
00074 VecScatterCreateToZero(petscVector, &mToMaster, &mConcentrated);
00075 }
00076
00077
00078
00079
00080
00081 #if (PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)
00082 VecScatterBegin(mToMaster, petscVector, mConcentrated, INSERT_VALUES, SCATTER_FORWARD);
00083 VecScatterEnd(mToMaster, petscVector, mConcentrated, INSERT_VALUES, SCATTER_FORWARD);
00084 #else
00085 VecScatterBegin(petscVector, mConcentrated, INSERT_VALUES, SCATTER_FORWARD, mToMaster);
00086 VecScatterEnd(petscVector, mConcentrated, INSERT_VALUES, SCATTER_FORWARD, mToMaster);
00087 #endif
00088
00089
00090
00091 if (mAmMaster)
00092 {
00093 double *concentrated_vector;
00094 VecGetArray(mConcentrated, &concentrated_vector);
00095 for (int i=0; i<size; i++)
00096 {
00097 ColumnDataWriter::PutVariable(variableID, concentrated_vector[i], i);
00098 }
00099 VecRestoreArray(mConcentrated, &concentrated_vector);
00100 }
00101
00102 }
00103
00104 void ParallelColumnDataWriter::PutVectorStripe(int variableId, DistributedVector::Stripe& rStripe)
00105 {
00106
00107 DistributedVectorFactory *p_factory = rStripe.GetFactory();
00108 Vec unstriped_petsc = p_factory->CreateVec();
00109 DistributedVector unstriped = p_factory->CreateDistributedVector(unstriped_petsc);
00110 for (DistributedVector::Iterator index = unstriped.Begin();
00111 index!= unstriped.End();
00112 ++index)
00113 {
00114 unstriped[index] = rStripe[index];
00115 }
00116
00117
00118 ParallelColumnDataWriter::PutVector(variableId, unstriped_petsc);
00119 VecDestroy(unstriped_petsc);
00120 }
00121
00122 void ParallelColumnDataWriter::EndDefineMode()
00123 {
00124 if (mAmMaster)
00125 {
00126 ColumnDataWriter::EndDefineMode();
00127 }
00128 else
00129 {
00130 mIsInDefineMode = false;
00131 }
00132 }
00133
00142 void ParallelColumnDataWriter::PutVariable(int variableID, double variableValue, long dimensionPosition)
00143 {
00144 if (mAmMaster)
00145 {
00146
00147 ColumnDataWriter::PutVariable(variableID, variableValue, dimensionPosition);
00148 }
00149 }
00150
00151 ParallelColumnDataWriter::~ParallelColumnDataWriter()
00152 {
00153 if (mConcentrated != NULL)
00154 {
00155 VecScatterDestroy(mToMaster);
00156 VecDestroy(mConcentrated);
00157 }
00158 Close();
00159 }
00160
00161 void ParallelColumnDataWriter::AdvanceAlongUnlimitedDimension()
00162 {
00163
00164 MPI_Barrier(PETSC_COMM_WORLD);
00165
00166 if (mAmMaster)
00167 {
00169 ColumnDataWriter::DoAdvanceAlongUnlimitedDimension();
00170 }
00171 }
00172
00173 void ParallelColumnDataWriter::Close()
00174 {
00175 MPI_Barrier(PETSC_COMM_WORLD);
00176
00178 if (mAmMaster)
00179 {
00180 ColumnDataWriter::Close();
00181 }
00182 }