PetscVecTools.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #ifndef _PETSCVECTOOLS_HPP_
00030 #define _PETSCVECTOOLS_HPP_
00031 
00032 #include "UblasVectorInclude.hpp" // needs to be 'first'
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              * We need continuous data, if some of the rows do not belong
00173              * to the processor their values are not passed to VecSetValues.
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_
Generated on Thu Dec 22 13:00:07 2011 for Chaste by  doxygen 1.6.3