Chaste Release::3.1
ReplicatableVector.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 "ReplicatableVector.hpp"
00037 #include "Exception.hpp"
00038 #include "PetscTools.hpp"
00039 
00040 #include <cassert>
00041 #include <iostream>
00042 
00043 // Private methods
00044 
00045 void ReplicatableVector::RemovePetscContext()
00046 {
00047     if (mToAll != NULL)
00048     {
00049         VecScatterDestroy(PETSC_DESTROY_PARAM(mToAll));
00050         mToAll = NULL;
00051     }
00052 
00053     if (mReplicated != NULL)
00054     {
00055         PetscTools::Destroy(mReplicated);
00056         mReplicated = NULL;
00057     }
00058 
00059     if (mpData != NULL)
00060     {
00061         delete[] mpData;
00062         mpData = NULL;
00063     }
00064 }
00065 
00066 // Constructors & destructors
00067 
00068 ReplicatableVector::ReplicatableVector()
00069     : mpData(NULL),
00070       mSize(0),
00071       mToAll(NULL),
00072       mReplicated(NULL)
00073 {
00074 }
00075 
00076 ReplicatableVector::ReplicatableVector(Vec vec)
00077     : mpData(NULL),
00078       mSize(0),
00079       mToAll(NULL),
00080       mReplicated(NULL)
00081 {
00082     ReplicatePetscVector(vec);
00083 }
00084 
00085 ReplicatableVector::ReplicatableVector(unsigned size)
00086     : mpData(NULL),
00087       mSize(0),
00088       mToAll(NULL),
00089       mReplicated(NULL)
00090 {
00091     Resize(size);
00092 }
00093 
00094 ReplicatableVector::~ReplicatableVector()
00095 {
00096     RemovePetscContext();
00097 }
00098 
00099 // Vector interface methods
00100 
00101 unsigned ReplicatableVector::GetSize()
00102 {
00103     return mSize;
00104 }
00105 
00106 void ReplicatableVector::Resize(unsigned size)
00107 {
00108     // PETSc stuff will be out of date
00109     RemovePetscContext();
00110 
00111     mSize = size;
00112 
00113     try
00114     {
00115         mpData = new double[mSize];
00116     }
00117     catch(std::bad_alloc &badAlloc)
00118     {
00119 #define COVERAGE_IGNORE
00120         std::cout << "Failed to allocate a ReplicatableVector of size " << size  << std::endl;
00121         PetscTools::ReplicateException(true);
00122         throw badAlloc;
00123 #undef COVERAGE_IGNORE
00124     }
00125     PetscTools::ReplicateException(false);
00126 }
00127 
00128 double& ReplicatableVector::operator[](unsigned index)
00129 {
00130     assert(index < mSize);
00131     return mpData[index];
00132 }
00133 
00134 // The workhorse methods
00135 
00136 void ReplicatableVector::Replicate(unsigned lo, unsigned hi)
00137 {
00138     // Create a PetSC vector with the array containing the distributed data
00139     Vec distributed_vec;
00140     VecCreateMPIWithArray(PETSC_COMM_WORLD, hi-lo, this->GetSize(), &mpData[lo], &distributed_vec);
00141 
00142     // Now do the real replication
00143     ReplicatePetscVector(distributed_vec);
00144 
00145     // Clean up
00146     PetscTools::Destroy(distributed_vec);
00147 }
00148 
00149 void ReplicatableVector::ReplicatePetscVector(Vec vec)
00150 {
00151     // If the size has changed then we'll need to make a new context
00152     PetscInt isize;
00153     VecGetSize(vec, &isize);
00154     unsigned size = isize;
00155 
00156     if (this->GetSize() != size)
00157     {
00158         Resize(size);
00159     }
00160     if (mReplicated == NULL)
00161     {
00162         // This creates mToAll (the scatter context) and mReplicated (to store values)
00163         VecScatterCreateToAll(vec, &mToAll, &mReplicated);
00164     }
00165 
00166     // Replicate the data
00167 //PETSc-3.x.x or PETSc-2.3.3
00168 #if ( (PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
00169     VecScatterBegin(mToAll, vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD);
00170     VecScatterEnd  (mToAll, vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD);
00171 #else
00172 //PETSc-2.3.2 or previous
00173     VecScatterBegin(vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD, mToAll);
00174     VecScatterEnd  (vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD, mToAll);
00175 #endif
00176 
00177     // Information is now in mReplicated PETSc vector
00178     // Copy into mData
00179     double* p_replicated;
00180     VecGetArray(mReplicated, &p_replicated);
00181     for (unsigned i=0; i<size; i++)
00182     {
00183         mpData[i] = p_replicated[i];
00184     }
00185 }