ParallelColumnDataWriter.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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, 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     //Construct the appropriate "scatter" object to concentrate the vector on the master
00072     if (mConcentrated==NULL)
00073     {
00074         VecScatterCreateToZero(petscVector, &mToMaster, &mConcentrated);
00075     }
00076 
00077 //    int size2;
00078 //    VecGetSize(mConcentrated, &size2);
00079 //    std::cout << "Vector size=" << size << "," << size2 << std::endl << std::flush;
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 //    std::cout << "Done scatter" << std::endl << std::flush;
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     // Put the stripe into its own 'unstriped' vector
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     // Put the unstriped vector
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         // Master process is allowed to write
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     // Make sure that everyone has queued their messages
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 }

Generated on Tue Aug 4 16:10:22 2009 for Chaste by  doxygen 1.5.5