Chaste  Release::3.4
ReplicatableVector.cpp
1 /*
2 
3 Copyright (c) 2005-2016, 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 != NULL)
48  {
49  VecScatterDestroy(PETSC_DESTROY_PARAM(mToAll));
50  mToAll = NULL;
51  }
52 
53  if (mReplicated != NULL)
54  {
56  mReplicated = NULL;
57  }
58 
59  if (mpData != NULL)
60  {
61  delete[] mpData;
62  mpData = NULL;
63  }
64 }
65 
66 // Constructors & destructors
67 
69  : mpData(NULL),
70  mSize(0),
71  mToAll(NULL),
72  mReplicated(NULL)
73 {
74 }
75 
77  : mpData(NULL),
78  mSize(0),
79  mToAll(NULL),
80  mReplicated(NULL)
81 {
83 }
84 
86  : mpData(NULL),
87  mSize(0),
88  mToAll(NULL),
89  mReplicated(NULL)
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  catch(std::bad_alloc &badAlloc)
118  {
119 #define COVERAGE_IGNORE
120  std::cout << "Failed to allocate a ReplicatableVector of size " << size << std::endl;
122  throw badAlloc;
123 #undef COVERAGE_IGNORE
124  }
126 }
127 
128 double& ReplicatableVector::operator[](unsigned index)
129 {
130  assert(index < mSize);
131  return mpData[index];
132 }
133 
134 // The workhorse methods
135 
136 void ReplicatableVector::Replicate(unsigned lo, unsigned hi)
137 {
138  // Create a PetSC vector with the array containing the distributed data
139  Vec distributed_vec;
140 
141 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 3) //PETSc 3.3 or later
142  //Extra argument is block size
143  VecCreateMPIWithArray(PETSC_COMM_WORLD, 1, hi-lo, this->GetSize(), &mpData[lo], &distributed_vec);
144 #else
145  VecCreateMPIWithArray(PETSC_COMM_WORLD, hi-lo, this->GetSize(), &mpData[lo], &distributed_vec);
146 #endif
147 #if (PETSC_VERSION_MAJOR == 3) //PETSc 3.x.x
148  VecSetOption(distributed_vec, VEC_IGNORE_OFF_PROC_ENTRIES, PETSC_TRUE);
149 #else
150  VecSetOption(distributed_vec, VEC_IGNORE_OFF_PROC_ENTRIES);
151 #endif
152  // Now do the real replication
153  ReplicatePetscVector(distributed_vec);
154 
155  // Clean up
156  PetscTools::Destroy(distributed_vec);
157 }
158 
160 {
161  // If the size has changed then we'll need to make a new context
162  PetscInt isize;
163  VecGetSize(vec, &isize);
164  unsigned size = isize;
165 
166  if (this->GetSize() != size)
167  {
168  Resize(size);
169  }
170  if (mReplicated == NULL)
171  {
172  // This creates mToAll (the scatter context) and mReplicated (to store values)
173  VecScatterCreateToAll(vec, &mToAll, &mReplicated);
174  }
175 
176  // Replicate the data
177 //PETSc-3.x.x or PETSc-2.3.3
178 #if ( (PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
179  VecScatterBegin(mToAll, vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD);
180  VecScatterEnd (mToAll, vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD);
181 #else
182 //PETSc-2.3.2 or previous
183  VecScatterBegin(vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD, mToAll);
184  VecScatterEnd (vec, mReplicated, INSERT_VALUES, SCATTER_FORWARD, mToAll);
185 #endif
186 
187  // Information is now in mReplicated PETSc vector
188  // Copy into mData
189  double* p_replicated;
190  VecGetArray(mReplicated, &p_replicated);
191  for (unsigned i=0; i<size; i++)
192  {
193  mpData[i] = p_replicated[i];
194  }
195 }
void Replicate(unsigned lo, unsigned hi)
double & operator[](unsigned index)
#define PETSC_DESTROY_PARAM(x)
Definition: PetscTools.hpp:69
static void ReplicateException(bool flag)
Definition: PetscTools.cpp:198
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:351
void ReplicatePetscVector(Vec vec)
void Resize(unsigned size)