CppNoddy  0.92
Loading...
Searching...
No Matches
DistributedVector.h
Go to the documentation of this file.
1/// \file DistributedVector.h
2/// A class that constructs a SPARSE/DISTRIBUTED vector using PETSc
3
4#if defined(PETSC_D) || defined(PETSC_Z)
5
6#ifndef DISTRIBUTEDVECTOR_H
7#define DISTRIBUTEDVECTOR_H
8
9#include <Exceptions.h>
10#include <DenseVector.h>
11#include <PetscSession.h>
12#include "petsc.h"
13
14
15namespace CppNoddy {
16
17 /// A class that constructs a SPARSE/DISTRIBUTED vector
18 template <typename _Type>
19 class DistributedVector {
20
21 public:
22
23 /// Construct with a set number of elements
24 /// \param length The number of elements in the matrix
25 DistributedVector(const PetscInt& length) {
26#if defined(PARANOID)
27 int flag(0);
28 MPI_Initialized(&flag);
29 if(flag != 1) {
30 std::string problem;
31 problem = "DistributedVector<> needs PETSc and therefore you must call \n";
32 problem += "PetscSession before instantiating the object.\n";
33 throw ExceptionRuntime(problem);
34 }
35#endif
36 // create and size a vector m_B
37 VecCreate(PETSC_COMM_WORLD,&m_B);
38 VecSetSizes(m_B,PETSC_DECIDE,length);
39 // add any command line configuration
40 VecSetFromOptions(m_B);
41
42 // store the rank/size in the object
43 MPI_Comm_size(PETSC_COMM_WORLD,&m_size);
44 MPI_Comm_rank(PETSC_COMM_WORLD,&m_rank);
45
46#if defined(DEBUG)
47 PetscPrintf(PETSC_COMM_WORLD, "[DEBUG] Creating a distributed vector\n");
48 PetscInt start, end;
49 VecGetOwnershipRange(m_B,&start,&end);
50 PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[DEBUG] Rank = %D Start = %D End = %D \n", m_rank, start, end );
51 PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT);
52#endif
53 }
54
55 /// D-tor, make sure we clean up PETSc objects on exit
56 ~DistributedVector() {
57 // have to destroy on all processes (I think?!)
58 VecDestroy(&m_B);
59 }
60
61 /// Copy constructor
62 DistributedVector(DistributedVector<_Type>& source){
63 // VecDuplicate just copies distribution and memory allocation
64 VecDuplicate(source.m_B, &m_B);
65 m_rank = source.m_rank;
66 m_size = source.m_size;
67 //
68 // VecCopy copies data
69 VecCopy(source.m_B, m_B);
70 }
71
72 std::size_t size() const {
73 PetscInt nnz(0);
74 VecGetSize(m_B, &nnz);
75 return std::size_t(nnz);
76 }
77
78 /// \return 1-norm of the distributed vector
79 PetscReal one_norm() const {
80 PetscReal norm(0.0);
81 VecNorm(m_B,NORM_1,&norm);
82 return norm;
83 }
84
85 /// \return 2-norm of the distributed vector
86 PetscReal two_norm() const {
87 PetscReal norm(0.0);
88 VecNorm(m_B,NORM_2,&norm);
89 return norm;
90 }
91
92 /// \return infinity-norm of the distributed vector
93 PetscReal inf_norm() const {
94 PetscReal norm(0.0);
95 VecNorm(m_B,NORM_INFINITY,&norm);
96 return norm;
97 }
98
99 /// \return A pointer to the PETSc Vector
100 Vec* get_pVec(){
101 return &m_B;
102 }
103
104 /// \param i The index of the element to set
105 /// \param value The value to be put into element i
106 void operator()(const PetscInt& i, const PetscScalar& value) {
107 PetscInt start, end;
108 VecGetOwnershipRange(m_B,&start,&end);
109 if ( ( i >= start ) && ( i < end ) ) {
110 VecSetValues(m_B,1,&i,&value,INSERT_VALUES);
111 } else {
112 }
113 }
114
115 /// \param i The index of the element to set
116 /// \param value The value to be put into element i
117 void set_elt(const PetscInt& i, const PetscScalar& value) {
118 PetscInt start, end;
119 VecGetOwnershipRange(m_B,&start,&end);
120 if ( ( i >= start ) && ( i < end ) ) {
121 VecSetValues(m_B,1,&i,&value,INSERT_VALUES);
122 } else {
123 }
124 }
125
126 /// \param i The vector of elements to set
127 /// \param value The vector of values to be put in the above indices
128 void set(const DenseVector<int>& elts, const DenseVector<_Type>& values) {
129 PetscInt start, end;
130 VecGetOwnershipRange(m_B,&start,&end);
131 PetscInt nnz_elts = elts.size();
132 VecSetValues(m_B,nnz_elts,&elts[0],&values[0],INSERT_VALUES);
133 }
134
135 /// Assemble the matrix
136 void final_assembly() {
137 VecAssemblyBegin(m_B);
138 VecAssemblyEnd(m_B);
139 }
140
141 /// View the matrix on stdout
142 void view() {
143 PetscInt start, end;
144 VecGetOwnershipRange(m_B,&start,&end);
145 PetscPrintf(PETSC_COMM_WORLD, "Vector view to stdout:\n");
146 PetscSynchronizedPrintf(PETSC_COMM_WORLD, "Processor rank = %D, start = %D end = %D \n", m_rank, start, end-1 );
147 PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT);
148 VecView(m_B,PETSC_VIEWER_STDOUT_WORLD);
149 }
150
151 private:
152
153 // PETSc matrix
154 Vec m_B;
155
156 // processor rank
157 PetscMPIInt m_rank, m_size;
158
159 }
160 ; // END CLASS
161} // end namespace
162
163#endif // include guard
164
165#endif // check for PETSC_D or PETSC_Z
Specification for a templated DenseVector class – a dense, dynamic, vector object.
The collection of CppNoddy exceptions.
double source(const double &x, const double &y, const double &t)
Definition: IBVPLinear.cpp:26
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt