Chaste  Release::2017.1
PetscVecTools.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 "PetscVecTools.hpp"
37 #include "PetscTools.hpp"
38 #include <petscviewer.h>
39 #include <cassert>
40 #include "DistributedVectorFactory.hpp"
41 #include "DistributedVector.hpp"
42 #include "PetscException.hpp"
43 
45 // Implementation
47 
49 {
50  VecAssemblyBegin(vector);
51  VecAssemblyEnd(vector);
52 }
53 
54 void PetscVecTools::SetElement(Vec vector, PetscInt row, double value)
55 {
56  PetscInt lo, hi;
57  GetOwnershipRange(vector, lo, hi);
58 
59  if (row >= lo && row < hi)
60  {
61  VecSetValues(vector, 1, &row, &value, INSERT_VALUES);
62  }
63 }
64 
65 void PetscVecTools::AddToElement(Vec vector, PetscInt row, double value)
66 {
67  PetscInt lo, hi;
68  GetOwnershipRange(vector, lo, hi);
69 
70  if (row >= lo && row < hi)
71  {
72  VecSetValues(vector, 1, &row, &value, ADD_VALUES);
73  }
74 }
75 
77 {
78  //Give full precision, scientific notation
79 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 7) // PETSc 3.7+
80  PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_MATLAB);
81 #else
82  PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_MATLAB);
83 #endif
84  VecView(vector, PETSC_VIEWER_STDOUT_WORLD);
85 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 7) // PETSc 3.7+
86  PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
87 #endif
88 }
89 
91 {
92 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
93  PetscScalar zero = 0.0;
94  VecSet(&zero, vector);
95 #else
96  VecZeroEntries(vector);
97 #endif
98 }
99 
100 unsigned PetscVecTools::GetSize(Vec vector)
101 {
102  PetscInt size;
103  VecGetSize(vector, &size);
104  return (unsigned) size;
105 }
106 
108 {
109  VecGetOwnershipRange(vector, &lo, &hi);
110 }
111 
113 {
114  PetscInt lo, hi;
115  GetOwnershipRange(vector, lo, hi);
116  assert(lo <= row && row < hi);
117 
118  double* p_vector;
119  PetscInt local_index = row-lo;
120  VecGetArray(vector, &p_vector);
121  double answer = p_vector[local_index];
122  VecRestoreArray(vector, &p_vector);
123 
124  return answer;
125 }
126 
127 void PetscVecTools::AddScaledVector(Vec y, Vec x, double scaleFactor)
128 {
129 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
130  VecAXPY(&scaleFactor, x, y);
131 #else
132  VecAXPY(y, scaleFactor, x);
133 #endif
134 }
135 
136 void PetscVecTools::Scale(Vec vector, double scaleFactor)
137 {
138 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
139  PETSCEXCEPT( VecScale(&scaleFactor, vector) );
140 #else
141  PETSCEXCEPT( VecScale(vector, scaleFactor) );
142 #endif
143 }
144 
145 void PetscVecTools::WAXPY(Vec w, double a, Vec x, Vec y)
146 {
147 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
148  PETSCEXCEPT( VecWAXPY(&a, x, y, w) );
149 #else
150  PETSCEXCEPT( VecWAXPY(w, a, x, y) );
151 #endif
152 }
153 
154 void PetscVecTools::SetupInterleavedVectorScatterGather(Vec interleavedVec, VecScatter& rFirstVariableScatterContext, VecScatter& rSecondVariableScatterContext)
155 {
156  PetscInt num_rows, num_local_rows;
157 
158  VecGetSize(interleavedVec, &num_rows);
159  VecGetLocalSize(interleavedVec, &num_local_rows);
160 
161  IS A11_rows, A22_rows;
162  ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 0, 2, &A11_rows);
163  ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 1, 2, &A22_rows);
164 
165  IS all_vector;
166  ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 0, 1, &all_vector);
167 
168  unsigned subvector_num_rows = num_rows/2;
169  unsigned subvector_local_rows = num_local_rows/2;
170  Vec x1_subvector = PetscTools::CreateVec(subvector_num_rows, subvector_local_rows);
171  Vec x2_subvector = PetscTools::CreateVec(subvector_num_rows, subvector_local_rows);
172 
173  VecScatterCreate(interleavedVec, A11_rows, x1_subvector, all_vector, &rFirstVariableScatterContext);
174  VecScatterCreate(interleavedVec, A22_rows, x2_subvector, all_vector, &rSecondVariableScatterContext);
175 
176  PetscTools::Destroy(x1_subvector);
177  PetscTools::Destroy(x2_subvector);
178 
179  ISDestroy(PETSC_DESTROY_PARAM(A11_rows));
180  ISDestroy(PETSC_DESTROY_PARAM(A22_rows));
181  ISDestroy(PETSC_DESTROY_PARAM(all_vector));
182 }
183 
184 void PetscVecTools::DoInterleavedVecScatter(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContefirstVariableScatterContextt, Vec secondVariableVec)
185 {
186  PetscScalar *p_interleaved_vec;
187  PetscScalar *p_1st_variable_vec;
188  PetscScalar *p_2nd_variable_vec;
189 
190  // // Note that the first vector is normally locked read-only when this is used
191  // PetscInt lock;
192  // VecLockGet(interleavedVec, &lock);
193  // assert(lock == 1);
194 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
195  // Request read-only access properly
196  VecGetArrayRead(interleavedVec, (const PetscScalar**)&p_interleaved_vec);
197 #else
198  VecGetArray(interleavedVec, &p_interleaved_vec);
199 #endif
200  VecGetArray(firstVariableVec, &p_1st_variable_vec);
201  VecGetArray(secondVariableVec, &p_2nd_variable_vec);
202 
203  PetscInt vec_local_size;
204  VecGetLocalSize(interleavedVec, &vec_local_size);
205  assert(vec_local_size%2 == 0);
206 
207  for (PetscInt local_index=0; local_index<vec_local_size/2; local_index++)
208  {
209  p_1st_variable_vec[local_index] = p_interleaved_vec[2*local_index];
210  p_2nd_variable_vec[local_index] = p_interleaved_vec[2*local_index+1];
211  }
212 
213  VecRestoreArray(interleavedVec, &p_interleaved_vec);
214  VecRestoreArray(firstVariableVec, &p_1st_variable_vec);
215  VecRestoreArray(secondVariableVec, &p_2nd_variable_vec);
216 
217 // DistributedVectorFactory factory(vec_size/2);
218 //
219 // DistributedVector dist_inter_vec = factory.CreateDistributedVector(interleavedVec);
220 // DistributedVector::Stripe dist_inter_vec_1st_var(dist_inter_vec, 0);
221 // DistributedVector::Stripe dist_inter_vec_2nd_var(dist_inter_vec, 1);
222 //
223 // DistributedVector dist_1st_var_vec = factory.CreateDistributedVector(firstVariableVec);
224 // DistributedVector dist_2nd_var_vec = factory.CreateDistributedVector(secondVariableVec);
225 //
226 // for (DistributedVector::Iterator index = dist_1st_var_vec.Begin();
227 // index!= dist_1st_var_vec.End();
228 // ++index)
229 // {
230 // dist_1st_var_vec[index] = dist_inter_vec_1st_var[index];
231 // dist_2nd_var_vec[index] = dist_inter_vec_2nd_var[index];
232 // }
233 
234 
235 // //PETSc-3.x.x or PETSc-2.3.3
236 //#if ((PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
237 // VecScatterBegin(firstVariableScatterContext, interleavedVec, firstVariableVec, INSERT_VALUES, SCATTER_FORWARD);
238 // VecScatterEnd(firstVariableScatterContext, interleavedVec, firstVariableVec, INSERT_VALUES, SCATTER_FORWARD);
239 //#else
240 // VecScatterBegin(interleavedVec, firstVariableVec, INSERT_VALUES, SCATTER_FORWARD, firstVariableScatterContext);
241 // VecScatterEnd(interleavedVec, firstVariableVec, INSERT_VALUES, SCATTER_FORWARD, firstVariableScatterContext);
242 //#endif
243 //
244 // //PETSc-3.x.x or PETSc-2.3.3
245 //#if ((PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
246 // VecScatterBegin(secondVariableScatterContefirstVariableScatterContextt, interleavedVec, secondVariableVec, INSERT_VALUES, SCATTER_FORWARD);
247 // VecScatterEnd(secondVariableScatterContefirstVariableScatterContextt, interleavedVec, secondVariableVec, INSERT_VALUES, SCATTER_FORWARD);
248 //#else
249 // VecScatterBegin(interleavedVec, secondVariableVec, INSERT_VALUES, SCATTER_FORWARD, secondVariableScatterContefirstVariableScatterContextt);
250 // VecScatterEnd(interleavedVec, secondVariableVec, INSERT_VALUES, SCATTER_FORWARD, secondVariableScatterContefirstVariableScatterContextt);
251 //#endif
252 }
253 
254 void PetscVecTools::DoInterleavedVecGather(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContext, Vec secondVariableVec)
255 {
256  PetscScalar *p_interleaved_vec;
257  PetscScalar *p_1st_variable_vec;
258  PetscScalar *p_2nd_variable_vec;
259 
260  VecGetArray(interleavedVec, &p_interleaved_vec);
261  VecGetArray(firstVariableVec, &p_1st_variable_vec);
262  VecGetArray(secondVariableVec, &p_2nd_variable_vec);
263 
264  PetscInt vec_local_size;
265  VecGetLocalSize(interleavedVec, &vec_local_size);
266  assert(vec_local_size%2 == 0);
267 
268  for (PetscInt local_index=0; local_index<vec_local_size/2; local_index++)
269  {
270  p_interleaved_vec[2*local_index] = p_1st_variable_vec[local_index];
271  p_interleaved_vec[2*local_index+1] = p_2nd_variable_vec[local_index];
272  }
273 
274  VecRestoreArray(interleavedVec, &p_interleaved_vec);
275  VecRestoreArray(firstVariableVec, &p_1st_variable_vec);
276  VecRestoreArray(secondVariableVec, &p_2nd_variable_vec);
277 
278 //#if ((PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
279 // VecScatterBegin(firstVariableScatterContext, firstVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE);
280 // VecScatterEnd(firstVariableScatterContext, firstVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE);
281 //#else
282 // VecScatterBegin(firstVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE, firstVariableScatterContext);
283 // VecScatterEnd(firstVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE, firstVariableScatterContext);
284 //#endif
285 //
287 //#if ((PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
288 // VecScatterBegin(secondVariableScatterContext, secondVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE);
289 // VecScatterEnd(secondVariableScatterContext, secondVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE);
290 //#else
291 // VecScatterBegin(secondVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE, secondVariableScatterContext);
292 // VecScatterEnd(secondVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE, secondVariableScatterContext);
293 //#endif
294 }
static unsigned GetSize(Vec vector)
static void SetupInterleavedVectorScatterGather(Vec interleavedVec, VecScatter &rFirstVariableScatterContext, VecScatter &rSecondVariableScatterContext)
static double GetElement(Vec vector, PetscInt row)
#define PETSC_DESTROY_PARAM(x)
Definition: PetscTools.hpp:70
static void SetElement(Vec vector, PetscInt row, double value)
static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries=true)
Definition: PetscTools.cpp:214
static void DoInterleavedVecScatter(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContext, Vec secondVariableVec)
static void Scale(Vec vector, double scaleFactor)
static void GetOwnershipRange(Vec vector, PetscInt &lo, PetscInt &hi)
static void AddScaledVector(Vec y, Vec x, double scaleFactor)
static void Zero(Vec vector)
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:352
static void WAXPY(Vec w, double a, Vec x, Vec y)
static void DoInterleavedVecGather(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContext, Vec secondVariableVec)
static void Display(Vec vector)
static void AddToElement(Vec vector, PetscInt row, double value)
static void Finalise(Vec vector)