1#ifndef DUNE_FEM_PETSCVECTOR_HH
2#define DUNE_FEM_PETSCVECTOR_HH
38 template<
class DFSpace >
57 template <
class DiscreteFunctionSpace,
class Mapper >
58 class PetscManagedDofStorage
59 :
public ManagedDofStorageImplementation< typename DiscreteFunctionSpace::GridPartType::GridType, Mapper, PetscVector< DiscreteFunctionSpace > >
61 typedef typename DiscreteFunctionSpace::GridPartType::GridType GridType;
62 typedef Mapper MapperType ;
63 typedef PetscVector< DiscreteFunctionSpace > DofArrayType ;
64 typedef ManagedDofStorageImplementation< GridType, MapperType, DofArrayType > BaseType;
67 typedef typename BaseType :: SizeType SizeType;
71 const MapperType& mapper,
73 : BaseType(
space.
grid(), mapper, myArray_ ),
74 myArray_(
space, backend )
78 void resize(
const bool )
override
82 void reserve(
const SizeType )
override
87 void dofCompress(
const bool clearResizedArrays )
override
90 if( clearResizedArrays )
97 void enableDofCompression()
override
102 DofArrayType myArray_;
121 template<
class DFSpace >
124 typedef PetscVector< DFSpace > ThisType;
125 friend class PetscDofBlock< ThisType >;
126 friend class PetscDofBlock< const ThisType >;
127 friend class PetscDofProxy< ThisType >;
128 friend class PetscDofProxy< const ThisType >;
131 typedef PetscScalar value_type ;
132 typedef Vec DofContainerType;
134 static constexpr int blockSize = DFSpace::localBlockSize;
135 typedef Hybrid::IndexRange< int, blockSize > BlockIndices;
137 typedef PetscDofBlock< ThisType > DofBlockType;
138 typedef PetscDofBlock< const ThisType > ConstDofBlockType;
139 typedef typename DofBlockType::DofIterator DofIteratorType;
140 typedef typename ConstDofBlockType::DofIterator ConstDofIteratorType;
141 typedef Envelope< DofBlockType > DofBlockPtrType;
142 typedef Envelope< ConstDofBlockType > ConstDofBlockPtrType;
143 typedef typename DofBlockType::IndexType
IndexType;
145 typedef DofIteratorType IteratorType;
146 typedef ConstDofIteratorType ConstIteratorType;
147 typedef typename DFSpace::RangeFieldType FieldType;
148 typedef int SizeType;
150 typedef PetscMappers< DFSpace > MappersType;
154 typedef DFSpace DiscreteFunctionSpaceType;
158 typedef typename DFSpace::template CommDataHandle< FakeDF >::OperationType DefaultCommunicationOperationType;
161 PetscVector (
const DFSpace& space, Vec vec )
162 : mappers_(
space ), vec_(vec),
163 backend_( getBackend( vec ) ),
166 static_assert( DefaultCommunicationOperationType::value == DFCommunicationOperation::copy ||
167 DefaultCommunicationOperationType::value == DFCommunicationOperation::add,
168 "only copy/add are available communication operations for petsc");
169 ::Dune::Petsc::VecGhostGetLocalForm( vec_, &ghostedVec_ );
172 PetscVector (
const DFSpace& space,
const int backend = ::Dune::Petsc::Backend::defaultBackend )
173 : mappers_(
space ), backend_( backend ), owner_(true)
175 static_assert( DefaultCommunicationOperationType::value == DFCommunicationOperation::copy ||
176 DefaultCommunicationOperationType::value == DFCommunicationOperation::add,
177 "only copy/add are available communication operations for petsc");
183 PetscVector (
const ThisType &other )
184 : mappers_( other.mappers_ ), backend_( other.backend_ ), owner_(true)
219 void hasBeenModified () { ++sequence_; }
223 communicateFlag_ =
true;
229 communicateIfNecessary();
233 const Vec* getVector ()
const
235 communicateIfNecessary();
240 const int backend()
const {
return backend_; }
245 communicateIfNecessary();
249 const Vec& array ()
const
251 communicateIfNecessary();
255 Vec* getGhostedVector ()
257 communicateIfNecessary();
261 const Vec* getGhostedVector ()
const
263 communicateIfNecessary();
269 ::Dune::Petsc::VecAssemblyBegin( vec_ );
272 void endAssemble(
const bool communicate )
274 ::Dune::Petsc::VecAssemblyEnd( vec_ );
276 communicateIfNecessary();
280 template <
class Operation>
281 void communicateNow (
const Operation& operation)
const
283 communicateFlag_ =
true;
285 communicateIfNecessary( operation );
289 ConstDofBlockType
operator[] (
const IndexType
index )
const {
return ConstDofBlockType( *
this,
index ); }
291 ConstDofBlockPtrType block ( IndexType
index )
const {
return blockPtr(
index ); }
292 DofBlockPtrType block ( IndexType
index ) {
return blockPtr(
index ); }
294 DofBlockPtrType blockPtr ( IndexType
index )
297 return DofBlockPtrType(
typename DofBlockType::UnaryConstructorParamType( *
this,
index ) );
300 ConstDofBlockPtrType blockPtr ( IndexType
index )
const
303 return ConstDofBlockPtrType(
typename ConstDofBlockType::UnaryConstructorParamType( *
this,
index ) );
306 DofIteratorType
begin () {
return DofIteratorType( *
this, 0, 0 ); }
307 ConstDofIteratorType
begin ()
const {
return ConstDofIteratorType( *
this, 0, 0 ); }
308 DofIteratorType
end () {
return DofIteratorType( *
this, mappers().
size() ); }
309 ConstDofIteratorType
end ()
const {
return ConstDofIteratorType( *
this, mappers().
size() ); }
313 DUNE_THROW(NotImplemented,
"PetscVector::beforeEnd is not implemented yet");
319 DUNE_THROW(NotImplemented,
"PetscVector::beforeEnd is not implemented yet");
325 DUNE_THROW(NotImplemented,
"PetscVector::beforeBegin is not implemented yet");
330 DUNE_THROW(NotImplemented,
"PetscVector::beforeBegin is not implemented yet");
334 DofIteratorType dbegin () {
return begin(); }
335 ConstDofIteratorType dbegin ()
const {
return begin(); }
336 DofIteratorType dend () {
return end(); }
337 ConstDofIteratorType dend ()
const {
return end(); }
341 ::Dune::Petsc::VecSet( *getVector(), 0. );
342 updateGhostRegions();
343 vectorIsUpToDateNow();
346 PetscScalar
operator* (
const ThisType &other )
const
349 ::Dune::Petsc::VecDot( *getVector(), *other.getVector(), &ret );
353 const ThisType&
operator+= (
const ThisType &other )
355 ::Dune::Petsc::VecAXPY( *getVector(), 1., *other.getVector() );
356 updateGhostRegions();
357 vectorIsUpToDateNow();
361 const ThisType&
operator-= (
const ThisType &other )
363 ::Dune::Petsc::VecAXPY( *getVector(), -1., *other.getVector() );
364 updateGhostRegions();
365 vectorIsUpToDateNow();
369 const ThisType&
operator*= ( PetscScalar scalar )
371 ::Dune::Petsc::VecScale( *getVector(), scalar );
372 updateGhostRegions();
373 vectorIsUpToDateNow();
377 const ThisType&
operator/= ( PetscScalar scalar )
379 assert( scalar != 0 );
383 void axpy (
const PetscScalar &scalar,
const ThisType &other )
385 ::Dune::Petsc::VecAXPY( *getVector(), scalar, *other.getVector() );
393 VecGetArray( ghostedVec_,&array );
394 std::fill_n( array + mappers().ghostMapper().interiorSize() * blockSize, mappers().ghostMapper().
ghostSize() * blockSize, PetscScalar( 0 ) );
396 VecRestoreArray( ghostedVec_, &array );
401 void printGlobal (
bool doit )
405 VecView( vec_, PETSC_VIEWER_STDOUT_WORLD );
408 void printGhost (
bool doit)
414 VecGetArray( ghostedVec_,&array );
417 PetscSynchronizedPrintf(PETSC_COMM_WORLD,
"%D %G\n",i,PetscRealPart(array[i]));
419 VecRestoreArray( ghostedVec_, &array );
420#if PETSC_VERSION_MAJOR <= 3 && PETSC_VERSION_MINOR < 5
421 PetscSynchronizedFlush( PETSC_COMM_WORLD );
423 PetscSynchronizedFlush( PETSC_COMM_WORLD, PETSC_STDOUT );
428 void assign(
const ThisType& other )
432 other.communicateIfNecessary();
435 ::Dune::Petsc::VecCopy( other.vec_, vec_ );
438 updateGhostRegions();
442 template <
class Container>
443 void assignVector(
const SimpleBlockVector< Container, blockSize >& other )
445 Vec& vec = *getGhostedVector();
448 const auto& mapping = mappers_.ghostMapper().mapping();
449 const size_t blocks = mapping.size();
450 assert( blocks == other.size() );
451 for(
size_t b=0, bs = 0; b<blocks; ++b, bs += blockSize)
453 PetscInt block = mapping[ b ];
454 const PetscScalar* values =
static_cast< const PetscScalar*
> (other.data()+bs);
455 ::Dune::Petsc::VecSetValuesBlocked( vec, 1, &block, values, INSERT_VALUES );
457 ::Dune::Petsc::VecGhostGetLocalForm( vec_, &ghostedVec_ );
459 updateGhostRegions();
463 template <
class DofBlock>
464 void assignVector(
const ISTLBlockVector< DofBlock >& other )
466 assert( DofBlock :: dimension == blockSize );
467 Vec& vec = *getGhostedVector();
470 const auto& mapping = mappers_.ghostMapper().mapping();
471 const size_t blocks = mapping.size();
472 assert( blocks == other.size() );
473 for(
size_t b=0; b<blocks; ++b )
475 PetscInt block = mapping[ b ];
476 const PetscScalar* values =
static_cast< const PetscScalar*
> (&(other[ b ][ 0 ])) ;
477 ::Dune::Petsc::VecSetValuesBlocked( vec, 1, &block, values, INSERT_VALUES );
479 ::Dune::Petsc::VecGhostGetLocalForm( vec_, &ghostedVec_ );
481 updateGhostRegions();
485 template <
class Container>
486 void copyTo( SimpleBlockVector< Container, blockSize >& other )
const
488 typedef typename Container::FieldType FieldType;
489 const PetscScalar *array =
nullptr;
490 VecGetArrayRead( ghostedVec_, &array );
493 const auto& mapping = mappers_.ghostMapper().mapping();
494 const size_t blocks = mapping.size();
495 assert( blocks == other.size() );
496 for(
size_t b=0; b<blocks; ++b )
498 const PetscScalar* petscBlock = array + (blockSize * mapping[ b ]);
499 FieldType* otherBlock = other.data() + (b * blockSize);
500 for(
int i=0; i<blockSize; ++i )
502 otherBlock[ i ] = petscBlock[ i ];
505 VecRestoreArrayRead( ghostedVec_, &array );
509 template <
class DofBlock>
510 void copyTo ( ISTLBlockVector< DofBlock >& other )
const
512 assert( DofBlock :: dimension == blockSize );
513 const PetscScalar *array =
nullptr;
514 VecGetArrayRead( ghostedVec_, &array );
517 const auto& mapping = mappers_.ghostMapper().mapping();
518 const size_t blocks = mapping.size();
519 assert( blocks == other.size() );
520 for(
size_t b=0; b<blocks; ++b )
522 const PetscScalar* petscBlock = array + (blockSize * mapping[ b ]);
523 DofBlock& otherBlock = other[ b ];
524 for(
int i=0; i<blockSize; ++i )
526 otherBlock[ i ] = petscBlock[ i ];
529 VecRestoreArrayRead( ghostedVec_, &array );
532 PetscVector&
operator= (
const ThisType& other )
538 const MappersType &mappers()
const {
return mappers_; }
542 return size() *
sizeof(value_type);
545 static void setMemoryFactor(
const double memFactor)
551 void memMoveBackward(
const int length,
const int oldStartIdx,
const int newStartIdx)
553 DUNE_THROW(NotImplemented,
"memMoveBackward is to be implemented");
557 void memMoveForward(
const int length,
const int oldStartIdx,
const int newStartIdx)
559 DUNE_THROW(NotImplemented,
"memMoveForward is to be implemented");
562 void copyContent(
const int newIndex,
const int oldIndex )
564 DUNE_THROW(NotImplemented,
"copyContent is to be implemented");
568 const int getBackend( Vec vec )
const
571 ::Dune::Petsc::VecGetType( vec, &vt );
573 return ::Dune::Petsc::Backend::toVecId( vt );
581 const PetscInt localBlocks = mappers_.ghostMapper().interiorSize();
582 const PetscInt numGhostBlocks = mappers_.ghostMapper().ghostSize();
584 const PetscInt localSize = localBlocks * blockSize;
585 const PetscInt globalSize = mappers_.parallelMapper().size() * blockSize;
588 const PetscInt *ghostBlocks = mappers_.parallelMapper().mapping().data() + localBlocks;
589 const auto&
comm = mappers_.space().gridPart().comm();
591 ::Dune::Petsc::VecCreateGhost( comm, localSize, globalSize, numGhostBlocks, ghostBlocks, &vec_ );
593 ::Dune::Petsc::VecCreateGhostBlock( comm, blockSize, localSize, globalSize, numGhostBlocks, ghostBlocks, &vec_ );
594 ::Dune::Petsc::VecSetType( vec_, ::Dune::Petsc::Backend::toVecType( backend_ ) );
595 ::Dune::Petsc::VecGhostGetLocalForm( vec_, &ghostedVec_ );
601 ::Dune::Petsc::VecGhostRestoreLocalForm( vec_, &ghostedVec_ );
602 ::Dune::Petsc::VecDestroy( &vec_ );
606 void communicateIfNecessary ()
const
608 DefaultCommunicationOperationType op;
609 communicateIfNecessary( op );
612 template <
class Operation>
613 void communicateIfNecessary (
const Operation& op)
const
616 if( communicateFlag_ && memorySequence_ < sequence_ )
618 if ( memorySequence_ < sequence_ )
620 if ( Operation::value == DFCommunicationOperation::add )
622 ::Dune::Petsc::VecGhostUpdateBegin( vec_, ADD_VALUES, SCATTER_REVERSE );
623 ::Dune::Petsc::VecGhostUpdateEnd( vec_, ADD_VALUES, SCATTER_REVERSE );
625 ::Dune::Petsc::VecGhostUpdateBegin( vec_, INSERT_VALUES, SCATTER_FORWARD );
626 ::Dune::Petsc::VecGhostUpdateEnd( vec_, INSERT_VALUES, SCATTER_FORWARD );
628 memorySequence_ = sequence_;
631 communicateFlag_ =
false;
636 void updateGhostRegions ()
638 ::Dune::Petsc::VecGhostUpdateBegin( vec_, INSERT_VALUES, SCATTER_FORWARD );
639 ::Dune::Petsc::VecGhostUpdateEnd( vec_, INSERT_VALUES, SCATTER_FORWARD );
642 void vectorIsUpToDateNow ()
const
644 memorySequence_ = sequence_;
645 communicateFlag_ =
false;
651 MappersType mappers_;
656 mutable unsigned long memorySequence_ = 0;
657 mutable unsigned long sequence_ = 0;
659 mutable bool communicateFlag_ =
false;
void axpy(const Ta &a, const type &y)
void init(const Matrix *matrix)
reference operator[](size_type i)
SLList< T, A > & operator=(const SLList< T, A > &other)
bigunsignedint< k > & operator/=(const bigunsignedint< k > &x)
bigunsignedint< k > & operator+=(const bigunsignedint< k > &x)
bigunsignedint< k > & operator-=(const bigunsignedint< k > &x)
bigunsignedint< k > & operator*=(const bigunsignedint< k > &x)
friend friend auto operator*(const DiagonalMatrix &matrixA, const DiagonalMatrix< OtherScalar, n > &matrixB)
std::ptrdiff_t index() const
#define DUNE_THROW(E,...)
typename Impl::Index< Range >::Type IndexType
Definition hybrid.hh:69
const Grid & grid() const
const Communication & comm() const
int ghostSize(int codim) const