Chaste  Release::2017.1
ReplicatableVector.cpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "ReplicatableVector.hpp"
37 #include "Exception.hpp"
38 #include "PetscTools.hpp"
39 
40 #include <cassert>
41 #include <iostream>
42 
43 // Private methods
44 
46 {
47  if (mToAll != nullptr)
48  {
49  VecScatterDestroy(PETSC_DESTROY_PARAM(mToAll));
50  mToAll = nullptr;
51  }
52 
53  if (mReplicated != nullptr)
54  {
56  mReplicated = nullptr;
57  }
58 
59  if (mpData != nullptr)
60  {
61  delete[] mpData;
62  mpData = nullptr;
63  }
64 }
65 
66 // Constructors & destructors
67 
69  : mpData(nullptr),
70  mSize(0),
71  mToAll(nullptr),
72  mReplicated(nullptr)
73 {
74 }
75 
77  : mpData(nullptr),
78  mSize(0),
79  mToAll(nullptr),
80  mReplicated(nullptr)
81 {
83 }
84 
86  : mpData(nullptr),
87  mSize(0),
88  mToAll(nullptr),
89  mReplicated(nullptr)
90 {
91  Resize(size);
92 }
93 
95 {
97 }
98 
99 // Vector interface methods
100 
102 {
103  return mSize;
104 }
105 
106 void ReplicatableVector::Resize(unsigned size)
107 {
108  // PETSc stuff will be out of date
110 
111  mSize = size;
112 
113  try
114  {
115  mpData = new double[mSize];
116  }
117 // LCOV_EXCL_START
118  catch(std::bad_alloc &badAlloc)
119  {
120  std::cout << "Failed to allocate a ReplicatableVector of size " << size << std::endl;
122  throw badAlloc;
123  }
124 // LCOV_EXCL_STOP
125 
127 }
128 
129 double& ReplicatableVector::operator[](unsigned index)
130 {
131  assert(index < mSize);
132  return mpData[index];
133 }
134 
135 // The workhorse methods
136 
137 void ReplicatableVector::Replicate(unsigned lo, unsigned hi)
138 {
139  // Create a PetSC vector with the array containing the distributed data
140  Vec distributed_vec;
141 
142 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 3) //PETSc 3.3 or later
143  //Extra argument is block size
144  VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, hi-lo, this->GetSize(), &mpData[lo], &distributed_vec);
145 #else
146  VecCreateMPIWithArray(PETSC_COMM_WORLD, hi-lo, this->GetSize(), &mpData[lo], &distributed_vec);
147 #endif
148 #if (PETSC_VERSION_MAJOR == 3) //PETSc 3.x.x
149  VecSetOption(distributed_vec, VEC_IGNORE_OFF_PROC_ENTRIES, PETSC_TRUE);
150 #else
151  VecSetOption(distributed_vec, VEC_IGNORE_OFF_PROC_ENTRIES);
152 #endif
153  // Now do the real replication
154  ReplicatePetscVector(distributed_vec);
155 
156  // Clean up
157  PetscTools::Destroy(distributed_vec);
158 }
159 
161 {
162  // If the size has changed then we'll need to make a new context
163  PetscInt isize;
164  VecGetSize(vec, &isize);
165  unsigned size = isize;
166 
167  if (this->GetSize() != size)
168  {
169  Resize(size);
170  }
171  if (mReplicated == nullptr)
172  {
173  // This creates mToAll (the scatter context) and mReplicated (to store values)
174  VecScatterCreateToAll(vec, &mToAll, &mReplicated);
175  }
176 
177  // Replicate the data
178 //PETSc-3.x.x or PETSc-2.3.3
179 #if ((PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
180  VecScatterBegin(mToAll, vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD);
181  VecScatterEnd (mToAll, vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD);
182 #else
183 //PETSc-2.3.2 or previous
184  VecScatterBegin(vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD, mToAll);
185  VecScatterEnd (vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD, mToAll);
186 #endif
187 
188  // Information is now in mReplicated PETSc vector
189  // Copy into mData
190  double* p_replicated;
191  VecGetArray(mReplicated, &p_replicated);
192  for (unsigned i=0; i<size; i++)
193  {
194  mpData[i] = p_replicated[i];
195  }
196 }
void Replicate(unsigned lo, unsigned hi)
double & operator[](unsigned index)
#define PETSC_DESTROY_PARAM(x)
Definition: PetscTools.hpp:70
static void ReplicateException(bool flag)
Definition: PetscTools.cpp:198
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:352
void ReplicatePetscVector(Vec vec)
void Resize(unsigned size)