1 #ifndef DUNE_FEM_ARRAYS_HH 2 #define DUNE_FEM_ARRAYS_HH 14 #include <dune/common/genericiterator.hh> 15 #include <dune/common/exceptions.hh> 16 #include <dune/common/version.hh> 20 #include <dune/fem/solver/oemsolver/cblas.h> 35 template <
class T,
class AllocatorType = DefaultDofAllocator<T> >
38 template<
class ArrayType>
50 T* p =
new T [ nmemb ] ;
62 static T*
realloc (T* oldMem,
size_t oldSize ,
size_t nmemb)
67 const size_t copySize =
std::min( oldSize, nmemb );
68 std::copy( oldMem, oldMem+copySize, p );
83 T* p = (T *) std::malloc(nmemb *
sizeof(T));
96 static T*
realloc (T* oldMem,
size_t oldSize ,
size_t nmemb)
100 T * p = (T *) std::realloc(oldMem , nmemb*
sizeof(T));
186 : vec_( const_cast< T * > (vec) )
194 return DofIteratorType(*
this, 0);
198 ConstDofIteratorType
begin()
const {
199 return ConstDofIteratorType(*
this, 0);
204 return DofIteratorType(*
this, size_);
208 ConstDofIteratorType
end()
const {
209 return ConstDofIteratorType(*
this, size_);
213 size_t size ()
const {
return size_; }
216 void assertIndex (
const size_t i )
const 221 std::cerr << std::endl;
222 std::cerr <<
"Error in StaticArray: Index out of Range: " << i << std::endl;
223 std::cerr <<
" Size of array: " << size() << std::endl;
231 T& operator [] (
const size_t i )
238 const T& operator [] (
const size_t i )
const 245 ThisType& operator= (
const ThisType & org)
247 assert(org.
size_ >= size() );
248 assert( ( size_ > 0 ) ? vec_ != 0 :
true );
250 std::copy(org.
vec_, org.
vec_ + size_, vec_ );
255 ThisType& operator += (
const ThisType & org)
257 assert(org.
size_ >= size() );
258 const size_t s = size();
259 const T * ov = org.
vec_;
260 for(
size_t i=0; i<s; ++i) vec_[i] += ov[i];
265 ThisType& operator -= (
const ThisType& org)
267 assert(org.
size() >= size() );
268 const size_t s = size();
269 const T * ov = org.
vec_;
270 for(
size_t i=0; i<s; ++i) vec_[i] -= ov[i];
275 ThisType& operator *= (
const T scalar)
277 const size_t s = size();
278 for(
size_t i=0; i<s; ++i) vec_[i] *= scalar;
283 ThisType& operator /= (
const T scalar)
285 const T scalar_1 = (((T) 1)/scalar);
286 const size_t s = size();
287 for(
size_t i=0; i<s; ++i) vec_[i] *= scalar_1;
292 ThisType& operator= (
const T scalar)
294 const size_t s = size();
295 for(
size_t i=0; i<s; ++i) vec_[i] = scalar;
300 void axpy (
const ThisType& org,
const T scalar)
302 const size_t s = size();
303 const T * ov = org.
vec_;
304 for(
size_t i=0; i<s; ++i) vec_[i] += scalar*ov[i];
310 const size_t s = size();
311 for(
size_t i=0; i<s; ++i) vec_[i] = 0;
315 void memmove(
const int length,
const int oldStartIdx,
const int newStartIdx)
317 void * dest = ((
void *) (&vec_[newStartIdx]));
318 const void * src = ((
const void *) (&vec_[oldStartIdx]));
319 std::memmove(dest, src, length *
sizeof(T));
327 return vec_ == other.
vec_;
338 const T*
data()
const {
return vec_; }
341 template <
class StreamTraits>
344 const uint64_t len = size_;
346 for(
size_t i=0; i<size_; ++i)
354 template <
class StreamTraits>
362 DUNE_THROW(InvalidStateException,
"StaticArray::read: internal size " << size_ <<
" and size to read " << len <<
" not equal!");
365 for(
size_t i=0; i<size_; ++i)
375 s <<
"Print StaticArray(addr = "<<
this <<
") (size = " << size_ <<
")\n";
376 for(
size_t i=0; i<size(); ++i)
378 s << vec_[i] <<
"\n";
388 DuneCBlas :: daxpy( size() , scalar, org.
vec_, 1 , vec_, 1);
390 const size_t s = size();
391 const double* ov = org.
vec_;
392 for(
size_t i=0; i<s; ++i) vec_[i] += scalar * ov[i];
400 std::memset(vec_, 0 , size() *
sizeof(
int));
405 std::memset(vec_, 0 , size() *
sizeof(
double));
417 template <
class T,
class AllocatorType>
424 using BaseType :: size_ ;
425 using BaseType :: vec_ ;
434 using BaseType :: size ;
438 : BaseType(0, (T *) 0)
446 : BaseType(0, (T *) 0),
458 ((T *) (size == 0) ? 0 : AllocatorType ::
malloc (size)))
467 memoryFactor_ = memFactor;
480 ThisType& operator= (
const ThisType & org)
484 assert( ( size_ > 0 ) ? vec_ != 0 :
true );
485 std::copy(org.
vec_, org.
vec_ + size_, vec_ );
495 if( (nsize <= memSize_) && (nsize >= (memSize_/2)) )
509 adjustMemory( nsize );
521 if( mSize <= memSize_ )
527 adjustMemory( mSize );
533 return memSize_ *
sizeof(T) +
sizeof(ThisType);
540 assert( memoryFactor_ >= 1.0 );
541 const double overEstimate = memoryFactor_ * mSize;
542 const size_t nMemSize = (size_t) std::ceil( overEstimate );
543 assert( nMemSize >= mSize );
548 vec_ = AllocatorType :: malloc(nMemSize);
552 assert( nMemSize > 0 );
559 vec_ = AllocatorType :: realloc (vec_,memSize_,nMemSize);
571 AllocatorType :: free ( vec_ );
580 template<
class ValueType>
584 static size_t used(
const ArrayType & array)
595 const size_t oldStartIdx,
596 const size_t newStartIdx)
598 assert( newStartIdx >= oldStartIdx );
601 size_t newIdx = newStartIdx + length - 1;
602 assert( newIdx < array.
size() );
604 for(
size_t oldIdx = oldStartIdx + length-1; oldIdx >= oldStartIdx; --oldIdx, --newIdx )
606 assert( oldIdx < array.
size() );
608 array[newIdx] = array[oldIdx];
611 array[oldIdx ] = 0.0;
617 const size_t oldStartIdx,
618 const size_t newStartIdx)
620 assert( newStartIdx <= oldStartIdx );
622 const size_t upperBound = oldStartIdx + length;
624 size_t newIdx = newStartIdx;
625 for(
size_t oldIdx = oldStartIdx; oldIdx<upperBound; ++oldIdx, ++newIdx )
628 array[newIdx] = array[oldIdx];
637 void assign( ArrayType& array,
const int newIndex,
const int oldIndex )
639 array[ newIndex ] = array[ oldIndex ];
646 #endif // #ifndef DUNE_FEM_ARRAYS_HH static T * realloc(T *oldMem, size_t oldSize, size_t nmemb)
allocate array of nmemb objects of type T
Definition: arrays.hh:96
size_t capacity() const
return number of total enties of array
Definition: arrays.hh:477
void print(std::ostream &s) const
print array
Definition: arrays.hh:373
void clear()
set all entries to zero
Definition: arrays.hh:308
bool read(InStreamInterface< StreamTraits > &in)
write to stream
Definition: arrays.hh:355
static void setMemoryFactor(ArrayType &array, const double memFactor)
Definition: arrays.hh:588
size_t size() const
return number of enties of array
Definition: arrays.hh:213
MutableArray< ValueType > ArrayType
Definition: arrays.hh:583
static constexpr T min(T a)
Definition: utility.hh:81
void adjustMemory(size_t mSize)
adjust the memory
Definition: arrays.hh:538
static void assign(ArrayType &array, const int newIndex, const int oldIndex)
Definition: arrays.hh:637
size_t usedMemorySize() const
return size of vector in bytes
Definition: arrays.hh:531
bool write(OutStreamInterface< StreamTraits > &out) const
write to stream
Definition: arrays.hh:342
T FieldType
Definition: arrays.hh:154
size_t size_
Definition: arrays.hh:150
void resize(size_t nsize)
Definition: arrays.hh:491
GenericIterator< const ThisType, const T > ConstDofIteratorType
Const DofIterator.
Definition: arrays.hh:168
bool operator==(const ThisType &other) const
Definition: arrays.hh:325
T * data()
return leak pointer for usage in BLAS routines
Definition: arrays.hh:336
size_t size_type
type of unsigned integral type of indexing
Definition: arrays.hh:174
static void free(T *p)
release memory previously allocated with malloc member
Definition: arrays.hh:89
StaticArray(const size_t size, T *vec)
create array of length size and store vec as pointer to memory
Definition: arrays.hh:177
oriented to the STL Allocator funtionality
Definition: arrays.hh:33
void axpy(const ThisType &org, const T scalar)
axpy operation
Definition: arrays.hh:300
DofIteratorType begin()
iterator pointing to begin of array
Definition: arrays.hh:193
MutableArray()
create array of length 0
Definition: arrays.hh:437
StaticArray< T > ThisType
Definition: arrays.hh:144
static void memMoveBackward(ArrayType &array, const size_t length, const size_t oldStartIdx, const size_t newStartIdx)
Definition: arrays.hh:593
StaticArray< T > BaseType
Definition: arrays.hh:422
SpecialArrayFeatures is a wrapper class to extend some array classes with some special features neede...
Definition: arrays.hh:39
DofIteratorType end()
iterator pointing to end of array
Definition: arrays.hh:203
const T * leakPointer() const
return leak pointer for usage in BLAS routines
Definition: arrays.hh:333
static T * malloc(size_t nmemb)
allocate array of nmemb objects of type T
Definition: arrays.hh:47
static void free(T *p)
release memory previously allocated with malloc member
Definition: arrays.hh:56
T * vec_
Definition: arrays.hh:147
double memoryFactor_
Definition: arrays.hh:428
Definition: coordinate.hh:4
static size_t used(const ArrayType &array)
Definition: arrays.hh:584
T value_type
definition conforming to STL
Definition: arrays.hh:156
abstract interface for an input stream
Definition: streams.hh:177
Static Array Wrapper for simple C Vectors like double* and int*. This also works as base class for th...
Definition: arrays.hh:141
MutableArray< T, AllocatorType > ThisType
Definition: arrays.hh:421
void reserve(size_t mSize)
Definition: arrays.hh:517
T block_type
definition conforming to ISTL
Definition: arrays.hh:159
~MutableArray()
Destructor.
Definition: arrays.hh:471
size_t memSize_
Definition: arrays.hh:431
ConstDofIteratorType const_iterator
make compatible with std::vector
Definition: arrays.hh:171
void setMemoryFactor(const double memFactor)
set memory factor
Definition: arrays.hh:465
StaticArray(const size_t size, const T *vec)
create array of length size and store vec as pointer to memory
Definition: arrays.hh:185
T * leakPointer()
return leak pointer for usage in BLAS routines
Definition: arrays.hh:331
MutableArray(const MutableArray &other)
copy constructor
Definition: arrays.hh:445
GenericIterator< ThisType, T > DofIteratorType
DofIterator.
Definition: arrays.hh:162
static T * malloc(size_t nmemb)
allocate array of nmemb objects of type T
Definition: arrays.hh:80
void freeMemory()
Definition: arrays.hh:567
ConstDofIteratorType begin() const
const iterator pointing to begin of array
Definition: arrays.hh:198
DofIteratorType iterator
make compatible with std::vector
Definition: arrays.hh:165
void memmove(const int length, const int oldStartIdx, const int newStartIdx)
move memory from old to new destination
Definition: arrays.hh:315
ConstDofIteratorType end() const
const iterator pointing to end of array
Definition: arrays.hh:208
static void memMoveForward(ArrayType &array, const size_t length, const size_t oldStartIdx, const size_t newStartIdx)
Definition: arrays.hh:615
static T * realloc(T *oldMem, size_t oldSize, size_t nmemb)
allocate array of nmemb objects of type T
Definition: arrays.hh:62
MutableArray(const size_t size)
create array of length size
Definition: arrays.hh:455
abstract interface for an output stream
Definition: streams.hh:44
const T * data() const
return leak pointer for usage in BLAS routines
Definition: arrays.hh:338