ParallelColumnDataWriter.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
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;
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 
00051 void ParallelColumnDataWriter::PutVector(int variableID, Vec petscVector)
00052 {
00053     int size;
00054     VecGetSize(petscVector,&size);
00055 
00056     if (size != mFixedDimensionSize)
00057     {
00058         EXCEPTION("Size of vector does not match FixedDimensionSize.");
00059     }
00060 
00061     //Construct the appropriate "scatter" object to concentrate the vector on the master
00062     if (mConcentrated==NULL)
00063     {
00064         VecScatterCreateToZero(petscVector, &mToMaster, &mConcentrated);
00065     }
00066 
00067 //    int size2;
00068 //    VecGetSize(mConcentrated, &size2);
00069 //    std::cout << "Vector size=" << size << "," << size2 << std::endl << std::flush;
00070 
00071 //PETSc-3.x.x or PETSc-2.3.3
00072 #if ( (PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
00073     VecScatterBegin(mToMaster, petscVector, mConcentrated, INSERT_VALUES, SCATTER_FORWARD);
00074     VecScatterEnd(mToMaster, petscVector, mConcentrated, INSERT_VALUES, SCATTER_FORWARD);
00075 #else
00076     VecScatterBegin(petscVector, mConcentrated, INSERT_VALUES, SCATTER_FORWARD, mToMaster);
00077     VecScatterEnd(petscVector, mConcentrated, INSERT_VALUES, SCATTER_FORWARD, mToMaster);
00078 #endif
00079 
00080 //    std::cout << "Done scatter" << std::endl << std::flush;
00081 
00082     if (PetscTools::AmMaster())
00083     {
00084         double *concentrated_vector;
00085         VecGetArray(mConcentrated, &concentrated_vector);
00086         for (int i=0; i<size; i++)
00087         {
00088             ColumnDataWriter::PutVariable(variableID, concentrated_vector[i], i);
00089         }
00090         VecRestoreArray(mConcentrated, &concentrated_vector);
00091     }
00092 
00093 }
00094 
00095 void ParallelColumnDataWriter::PutVectorStripe(int variableId, DistributedVector::Stripe& rStripe)
00096 {
00097     // Put the stripe into its own 'unstriped' vector
00098     DistributedVectorFactory* p_factory = rStripe.GetFactory();
00099     Vec unstriped_petsc = p_factory->CreateVec();
00100     DistributedVector unstriped = p_factory->CreateDistributedVector(unstriped_petsc);
00101     for (DistributedVector::Iterator index = unstriped.Begin();
00102          index!= unstriped.End();
00103          ++index)
00104     {
00105         unstriped[index] = rStripe[index];
00106     }
00107 
00108     // Put the unstriped vector
00109     ParallelColumnDataWriter::PutVector(variableId, unstriped_petsc);
00110     VecDestroy(unstriped_petsc);
00111 }
00112 
00113 void ParallelColumnDataWriter::EndDefineMode()
00114 {
00115     if (PetscTools::AmMaster())
00116     {
00117         ColumnDataWriter::EndDefineMode();
00118     }
00119     else
00120     {
00121         mIsInDefineMode = false;
00122     }
00123 }
00124 
00133 void ParallelColumnDataWriter::PutVariable(int variableID, double variableValue, long dimensionPosition)
00134 {
00135     if (PetscTools::AmMaster())
00136     {
00137         // Master process is allowed to write
00138         ColumnDataWriter::PutVariable(variableID, variableValue, dimensionPosition);
00139     }
00140 }
00141 
00142 ParallelColumnDataWriter::~ParallelColumnDataWriter()
00143 {
00144     if (mConcentrated != NULL)
00145     {
00146         VecScatterDestroy(mToMaster);
00147         VecDestroy(mConcentrated);
00148     }
00149     Close();
00150 }
00151 
00152 void ParallelColumnDataWriter::AdvanceAlongUnlimitedDimension()
00153 {
00154     // Paranoia
00155     PetscTools::Barrier("ParallelColumnDataWriter::AdvanceAlongUnlimitedDimension");
00156 
00157     if (PetscTools::AmMaster())
00158     {
00159         ColumnDataWriter::DoAdvanceAlongUnlimitedDimension();
00160     }
00161 }
00162 
00163 void ParallelColumnDataWriter::Close()
00164 {
00165     // Paranoia
00166     PetscTools::Barrier("ParallelColumnDataWriter::Close");
00167 
00168     if (PetscTools::AmMaster())
00169     {
00170         ColumnDataWriter::Close();
00171     }
00172 }

Generated on Mon Apr 18 11:35:28 2011 for Chaste by  doxygen 1.5.5