4#if defined(PETSC_D) || defined(PETSC_Z)
6#ifndef DISTRIBUTEDVECTOR_H
7#define DISTRIBUTEDVECTOR_H
18 template <
typename _Type>
19 class DistributedVector {
25 DistributedVector(
const PetscInt& length) {
28 MPI_Initialized(&flag);
31 problem =
"DistributedVector<> needs PETSc and therefore you must call \n";
32 problem +=
"PetscSession before instantiating the object.\n";
33 throw ExceptionRuntime(problem);
37 VecCreate(PETSC_COMM_WORLD,&m_B);
38 VecSetSizes(m_B,PETSC_DECIDE,length);
40 VecSetFromOptions(m_B);
43 MPI_Comm_size(PETSC_COMM_WORLD,&m_size);
44 MPI_Comm_rank(PETSC_COMM_WORLD,&m_rank);
47 PetscPrintf(PETSC_COMM_WORLD,
"[DEBUG] Creating a distributed vector\n");
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);
56 ~DistributedVector() {
62 DistributedVector(DistributedVector<_Type>& source){
64 VecDuplicate(
source.m_B, &m_B);
72 std::size_t size()
const {
74 VecGetSize(m_B, &nnz);
75 return std::size_t(nnz);
79 PetscReal one_norm()
const {
81 VecNorm(m_B,NORM_1,&norm);
86 PetscReal two_norm()
const {
88 VecNorm(m_B,NORM_2,&norm);
93 PetscReal inf_norm()
const {
95 VecNorm(m_B,NORM_INFINITY,&norm);
106 void operator()(
const PetscInt& i,
const PetscScalar& value) {
108 VecGetOwnershipRange(m_B,&start,&end);
109 if ( ( i >= start ) && ( i < end ) ) {
110 VecSetValues(m_B,1,&i,&value,INSERT_VALUES);
117 void set_elt(
const PetscInt& i,
const PetscScalar& value) {
119 VecGetOwnershipRange(m_B,&start,&end);
120 if ( ( i >= start ) && ( i < end ) ) {
121 VecSetValues(m_B,1,&i,&value,INSERT_VALUES);
128 void set(
const DenseVector<int>& elts,
const DenseVector<_Type>& values) {
130 VecGetOwnershipRange(m_B,&start,&end);
131 PetscInt nnz_elts = elts.size();
132 VecSetValues(m_B,nnz_elts,&elts[0],&values[0],INSERT_VALUES);
136 void final_assembly() {
137 VecAssemblyBegin(m_B);
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);
157 PetscMPIInt m_rank, m_size;
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)
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...