PetscVecTools.hpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _PETSCVECTOOLS_HPP_
00030 #define _PETSCVECTOOLS_HPP_
00031
00032 #include "UblasVectorInclude.hpp"
00033
00034 #include <petscvec.h>
00035
00039 class PetscVecTools
00040 {
00041 public:
00042
00048 static void Finalise(Vec vector);
00049
00055 static void Display(Vec vector);
00056
00062 static void Zero(Vec vector);
00063
00071 static void SetElement(Vec vector, PetscInt row, double value);
00072
00080 static void AddToElement(Vec vector, PetscInt row, double value);
00081
00087 static unsigned GetSize(Vec vector);
00088
00096 static void GetOwnershipRange(Vec vector, PetscInt& lo, PetscInt& hi);
00097
00105 static double GetElement(Vec vector, PetscInt row);
00106
00114 static void AddScaledVector(Vec y, Vec x, double scaleFactor);
00115
00121 static void Scale(Vec vector, double scaleFactor);
00122
00131 static void WAXPY(Vec w, double a, Vec x, Vec y);
00132
00143 template<size_t VECTOR_SIZE>
00144 static void AddMultipleValues(Vec vector, unsigned* vectorIndices, c_vector<double, VECTOR_SIZE>& smallVector)
00145 {
00146 PetscInt indices_owned[VECTOR_SIZE];
00147 PetscInt num_indices_owned = 0;
00148 PetscInt global_row;
00149 PetscInt lo, hi;
00150 GetOwnershipRange(vector, lo, hi);
00151
00152 for (unsigned row = 0; row<VECTOR_SIZE; row++)
00153 {
00154 global_row = vectorIndices[row];
00155 if (global_row >=lo && global_row <hi)
00156 {
00157 indices_owned[num_indices_owned++] = global_row;
00158 }
00159 }
00160
00161 if (num_indices_owned == VECTOR_SIZE)
00162 {
00163 VecSetValues(vector,
00164 num_indices_owned,
00165 indices_owned,
00166 smallVector.data(),
00167 ADD_VALUES);
00168 }
00169 else
00170 {
00171
00172
00173
00174
00175 double values[VECTOR_SIZE];
00176 unsigned num_values_owned = 0;
00177
00178 for (unsigned row = 0; row<VECTOR_SIZE; row++)
00179 {
00180 global_row = vectorIndices[row];
00181 if (global_row >= lo && global_row < hi)
00182 {
00183 values[num_values_owned++] = smallVector(row);
00184 }
00185 }
00186
00187 VecSetValues(vector,
00188 num_indices_owned,
00189 indices_owned,
00190 values,
00191 ADD_VALUES);
00192 }
00193 }
00194
00203 static void SetupInterleavedVectorScatterGather(Vec interleavedVec, VecScatter& rFirstVariableScatterContext, VecScatter& rSecondVariableScatterContext);
00204
00214 static void DoInterleavedVecScatter(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContext, Vec secondVariableVec);
00215
00225 static void DoInterleavedVecGather(Vec interleavedVec, VecScatter firstVariableScatterContext, Vec firstVariableVec, VecScatter secondVariableScatterContext, Vec secondVariableVec);
00226 };
00227
00228 #endif //_PETSCVECTOOLS_HPP_