Chaste  Release::3.4
PetscVecTools.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 "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  PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_MATLAB);
80  VecView(vector, PETSC_VIEWER_STDOUT_WORLD);
81 }
82 
84 {
85 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
86  PetscScalar zero = 0.0;
87  VecSet(&zero, vector);
88 #else
89  VecZeroEntries(vector);
90 #endif
91 }
92 
93 unsigned PetscVecTools::GetSize(Vec vector)
94 {
95  PetscInt size;
96  VecGetSize(vector, &size);
97  return (unsigned) size;
98 }
99 
101 {
102  VecGetOwnershipRange(vector, &lo, &hi);
103 }
104 
106 {
107  PetscInt lo, hi;
108  GetOwnershipRange(vector, lo, hi);
109  assert(lo <= row && row < hi);
110 
111  double* p_vector;
112  PetscInt local_index = row-lo;
113  VecGetArray(vector, &p_vector);
114  double answer = p_vector[local_index];
115  VecRestoreArray(vector, &p_vector);
116 
117  return answer;
118 }
119 
120 void PetscVecTools::AddScaledVector(Vec y, Vec x, double scaleFactor)
121 {
122 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
123  VecAXPY(&scaleFactor, x, y);
124 #else
125  VecAXPY(y, scaleFactor, x);
126 #endif
127 }
128 
129 void PetscVecTools::Scale(Vec vector, double scaleFactor)
130 {
131 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
132  PETSCEXCEPT( VecScale(&scaleFactor, vector) );
133 #else
134  PETSCEXCEPT( VecScale(vector, scaleFactor) );
135 #endif
136 }
137 
138 void PetscVecTools::WAXPY(Vec w, double a, Vec x, Vec y)
139 {
140 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
141  PETSCEXCEPT( VecWAXPY(&a, x, y, w) );
142 #else
143  PETSCEXCEPT( VecWAXPY(w, a, x, y) );
144 #endif
145 }
146 
147 void PetscVecTools::SetupInterleavedVectorScatterGather(Vec interleavedVec, VecScatter& rFirstVariableScatterContext, VecScatter& rSecondVariableScatterContext)
148 {
149  PetscInt num_rows, num_local_rows;
150 
151  VecGetSize(interleavedVec, &num_rows);
152  VecGetLocalSize(interleavedVec, &num_local_rows);
153 
154  IS A11_rows, A22_rows;
155  ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 0, 2, &A11_rows);
156  ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 1, 2, &A22_rows);
157 
158  IS all_vector;
159  ISCreateStride(PETSC_COMM_WORLD, num_rows/2, 0, 1, &all_vector);
160 
161  unsigned subvector_num_rows = num_rows/2;
162  unsigned subvector_local_rows = num_local_rows/2;
163  Vec x1_subvector = PetscTools::CreateVec(subvector_num_rows, subvector_local_rows);
164  Vec x2_subvector = PetscTools::CreateVec(subvector_num_rows, subvector_local_rows);
165 
166  VecScatterCreate(interleavedVec, A11_rows, x1_subvector, all_vector, &rFirstVariableScatterContext);
167  VecScatterCreate(interleavedVec, A22_rows, x2_subvector, all_vector, &rSecondVariableScatterContext);
168 
169  PetscTools::Destroy(x1_subvector);
170  PetscTools::Destroy(x2_subvector);
171 
172  ISDestroy(PETSC_DESTROY_PARAM(A11_rows));
173  ISDestroy(PETSC_DESTROY_PARAM(A22_rows));
174  ISDestroy(PETSC_DESTROY_PARAM(all_vector));
175 }
176 
177 void PetscVecTools::DoInterleavedVecScatter(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContefirstVariableScatterContextt, Vec secondVariableVec)
178 {
179  PetscScalar *p_interleaved_vec;
180  PetscScalar *p_1st_variable_vec;
181  PetscScalar *p_2nd_variable_vec;
182 
183  // // Note that the first vector is normally locked read-only when this is used
184  // PetscInt lock;
185  // VecLockGet(interleavedVec, &lock);
186  // assert(lock == 1);
187 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
188  // Request read-only access properly
189  VecGetArrayRead(interleavedVec, (const PetscScalar**)&p_interleaved_vec);
190 #else
191  VecGetArray(interleavedVec, &p_interleaved_vec);
192 #endif
193  VecGetArray(firstVariableVec, &p_1st_variable_vec);
194  VecGetArray(secondVariableVec, &p_2nd_variable_vec);
195 
196  PetscInt vec_local_size;
197  VecGetLocalSize(interleavedVec, &vec_local_size);
198  assert(vec_local_size%2 == 0);
199 
200  for (PetscInt local_index=0; local_index<vec_local_size/2; local_index++)
201  {
202  p_1st_variable_vec[local_index] = p_interleaved_vec[2*local_index];
203  p_2nd_variable_vec[local_index] = p_interleaved_vec[2*local_index+1];
204  }
205 
206  VecRestoreArray(interleavedVec, &p_interleaved_vec);
207  VecRestoreArray(firstVariableVec, &p_1st_variable_vec);
208  VecRestoreArray(secondVariableVec, &p_2nd_variable_vec);
209 
210 // DistributedVectorFactory factory(vec_size/2);
211 //
212 // DistributedVector dist_inter_vec = factory.CreateDistributedVector(interleavedVec);
213 // DistributedVector::Stripe dist_inter_vec_1st_var(dist_inter_vec, 0);
214 // DistributedVector::Stripe dist_inter_vec_2nd_var(dist_inter_vec, 1);
215 //
216 // DistributedVector dist_1st_var_vec = factory.CreateDistributedVector(firstVariableVec);
217 // DistributedVector dist_2nd_var_vec = factory.CreateDistributedVector(secondVariableVec);
218 //
219 // for (DistributedVector::Iterator index = dist_1st_var_vec.Begin();
220 // index!= dist_1st_var_vec.End();
221 // ++index)
222 // {
223 // dist_1st_var_vec[index] = dist_inter_vec_1st_var[index];
224 // dist_2nd_var_vec[index] = dist_inter_vec_2nd_var[index];
225 // }
226 
227 
228 // //PETSc-3.x.x or PETSc-2.3.3
229 //#if ( (PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
230 // VecScatterBegin(firstVariableScatterContext, interleavedVec, firstVariableVec, INSERT_VALUES, SCATTER_FORWARD);
231 // VecScatterEnd(firstVariableScatterContext, interleavedVec, firstVariableVec, INSERT_VALUES, SCATTER_FORWARD);
232 //#else
233 // VecScatterBegin(interleavedVec, firstVariableVec, INSERT_VALUES, SCATTER_FORWARD, firstVariableScatterContext);
234 // VecScatterEnd(interleavedVec, firstVariableVec, INSERT_VALUES, SCATTER_FORWARD, firstVariableScatterContext);
235 //#endif
236 //
237 // //PETSc-3.x.x or PETSc-2.3.3
238 //#if ( (PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
239 // VecScatterBegin(secondVariableScatterContefirstVariableScatterContextt, interleavedVec, secondVariableVec, INSERT_VALUES, SCATTER_FORWARD);
240 // VecScatterEnd(secondVariableScatterContefirstVariableScatterContextt, interleavedVec, secondVariableVec, INSERT_VALUES, SCATTER_FORWARD);
241 //#else
242 // VecScatterBegin(interleavedVec, secondVariableVec, INSERT_VALUES, SCATTER_FORWARD, secondVariableScatterContefirstVariableScatterContextt);
243 // VecScatterEnd(interleavedVec, secondVariableVec, INSERT_VALUES, SCATTER_FORWARD, secondVariableScatterContefirstVariableScatterContextt);
244 //#endif
245 }
246 
247 void PetscVecTools::DoInterleavedVecGather(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContext, Vec secondVariableVec)
248 {
249  PetscScalar *p_interleaved_vec;
250  PetscScalar *p_1st_variable_vec;
251  PetscScalar *p_2nd_variable_vec;
252 
253  VecGetArray(interleavedVec, &p_interleaved_vec);
254  VecGetArray(firstVariableVec, &p_1st_variable_vec);
255  VecGetArray(secondVariableVec, &p_2nd_variable_vec);
256 
257  PetscInt vec_local_size;
258  VecGetLocalSize(interleavedVec, &vec_local_size);
259  assert(vec_local_size%2 == 0);
260 
261  for (PetscInt local_index=0; local_index<vec_local_size/2; local_index++)
262  {
263  p_interleaved_vec[2*local_index] = p_1st_variable_vec[local_index];
264  p_interleaved_vec[2*local_index+1] = p_2nd_variable_vec[local_index];
265  }
266 
267  VecRestoreArray(interleavedVec, &p_interleaved_vec);
268  VecRestoreArray(firstVariableVec, &p_1st_variable_vec);
269  VecRestoreArray(secondVariableVec, &p_2nd_variable_vec);
270 
271 //#if ( (PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
272 // VecScatterBegin(firstVariableScatterContext, firstVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE);
273 // VecScatterEnd(firstVariableScatterContext, firstVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE);
274 //#else
275 // VecScatterBegin(firstVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE, firstVariableScatterContext);
276 // VecScatterEnd(firstVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE, firstVariableScatterContext);
277 //#endif
278 //
280 //#if ( (PETSC_VERSION_MAJOR == 3) || (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 3 && PETSC_VERSION_SUBMINOR == 3)) //2.3.3 or 3.x.x
281 // VecScatterBegin(secondVariableScatterContext, secondVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE);
282 // VecScatterEnd(secondVariableScatterContext, secondVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE);
283 //#else
284 // VecScatterBegin(secondVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE, secondVariableScatterContext);
285 // VecScatterEnd(secondVariableVec, interleavedVec, INSERT_VALUES, SCATTER_REVERSE, secondVariableScatterContext);
286 //#endif
287 }
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:69
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:351
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)