Chaste Release::3.1
ParallelColumnDataWriter.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "ParallelColumnDataWriter.hpp"
00037 #include "Exception.hpp"
00038 #include "DistributedVectorFactory.hpp"
00039 
00040 ParallelColumnDataWriter::ParallelColumnDataWriter(const std::string& rDirectory,
00041                                                    const std::string& rBaseName,
00042                                                    bool cleanDirectory)
00043     : ColumnDataWriter::ColumnDataWriter(rDirectory, rBaseName, cleanDirectory),
00044       mConcentrated(NULL)
00045 {
00046     int num_procs;
00047     MPI_Comm_size(PETSC_COMM_WORLD, &num_procs);
00048     if (num_procs==1)
00049     {
00050         mIsParallel = false;
00051     }
00052     else
00053     {
00054         mIsParallel = true;
00055     }
00056 }
00057 
00058 void ParallelColumnDataWriter::PutVector(int variableID, Vec petscVector)
00059 {
00060     int size;
00061     VecGetSize(petscVector,&size);
00062 
00063     if (size != mFixedDimensionSize)
00064     {
00065         EXCEPTION("Size of vector does not match FixedDimensionSize.");
00066     }
00067 
00068     // Construct the appropriate "scatter" object to concentrate the vector on the master
00069     if (mConcentrated==NULL)
00070     {
00071         VecScatterCreateToZero(petscVector, &mToMaster, &mConcentrated);
00072     }
00073 
00074 //    int size2;
00075 //    VecGetSize(mConcentrated, &size2);
00076 //    std::cout << "Vector size=" << size << "," << size2 << std::endl << std::flush;
00077 
00078 //PETSc-3.x.x or PETSc-2.3.3
00079 #if ( (PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
00080     VecScatterBegin(mToMaster, petscVector, mConcentrated, INSERT_VALUES, SCATTER_FORWARD);
00081     VecScatterEnd(mToMaster, petscVector, mConcentrated, INSERT_VALUES, SCATTER_FORWARD);
00082 #else
00083     VecScatterBegin(petscVector, mConcentrated, INSERT_VALUES, SCATTER_FORWARD, mToMaster);
00084     VecScatterEnd(petscVector, mConcentrated, INSERT_VALUES, SCATTER_FORWARD, mToMaster);
00085 #endif
00086 
00087 //    std::cout << "Done scatter" << std::endl << std::flush;
00088 
00089     if (PetscTools::AmMaster())
00090     {
00091         double *concentrated_vector;
00092         VecGetArray(mConcentrated, &concentrated_vector);
00093         for (int i=0; i<size; i++)
00094         {
00095             ColumnDataWriter::PutVariable(variableID, concentrated_vector[i], i);
00096         }
00097         VecRestoreArray(mConcentrated, &concentrated_vector);
00098     }
00099 }
00100 
00101 void ParallelColumnDataWriter::PutVectorStripe(int variableId, DistributedVector::Stripe& rStripe)
00102 {
00103     // Put the stripe into its own 'unstriped' vector
00104     DistributedVectorFactory* p_factory = rStripe.GetFactory();
00105     Vec unstriped_petsc = p_factory->CreateVec();
00106     DistributedVector unstriped = p_factory->CreateDistributedVector(unstriped_petsc);
00107     for (DistributedVector::Iterator index = unstriped.Begin();
00108          index!= unstriped.End();
00109          ++index)
00110     {
00111         unstriped[index] = rStripe[index];
00112     }
00113 
00114     // Put the unstriped vector
00115     ParallelColumnDataWriter::PutVector(variableId, unstriped_petsc);
00116     PetscTools::Destroy(unstriped_petsc);
00117 }
00118 
00119 void ParallelColumnDataWriter::EndDefineMode()
00120 {
00121     if (PetscTools::AmMaster())
00122     {
00123         ColumnDataWriter::EndDefineMode();
00124     }
00125     else
00126     {
00127         mIsInDefineMode = false;
00128     }
00129 }
00130 
00139 void ParallelColumnDataWriter::PutVariable(int variableID, double variableValue, long dimensionPosition)
00140 {
00141     if (PetscTools::AmMaster())
00142     {
00143         // Master process is allowed to write
00144         ColumnDataWriter::PutVariable(variableID, variableValue, dimensionPosition);
00145     }
00146 }
00147 
00148 ParallelColumnDataWriter::~ParallelColumnDataWriter()
00149 {
00150     if (mConcentrated != NULL)
00151     {
00152         VecScatterDestroy(PETSC_DESTROY_PARAM(mToMaster));
00153         PetscTools::Destroy(mConcentrated);
00154     }
00155     Close();
00156 }
00157 
00158 void ParallelColumnDataWriter::AdvanceAlongUnlimitedDimension()
00159 {
00160     // Paranoia
00161     PetscTools::Barrier("ParallelColumnDataWriter::AdvanceAlongUnlimitedDimension");
00162 
00163     if (PetscTools::AmMaster())
00164     {
00165         ColumnDataWriter::DoAdvanceAlongUnlimitedDimension();
00166     }
00167 }
00168 
00169 void ParallelColumnDataWriter::Close()
00170 {
00171     // Paranoia
00172     PetscTools::Barrier("ParallelColumnDataWriter::Close");
00173 
00174     if (PetscTools::AmMaster())
00175     {
00176         ColumnDataWriter::Close();
00177     }
00178 }