Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_Workspace.hpp
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef TEUCHOS_WORKSPACE_HPP
11#define TEUCHOS_WORKSPACE_HPP
12
13#include "Teuchos_RCP.hpp"
14#include "Teuchos_ArrayView.hpp"
15#include "Teuchos_Assert.hpp"
16
17namespace Teuchos {
18
19class WorkspaceStore;
20class RawWorkspace;
21
33
52TEUCHOSCORE_LIB_DLL_EXPORT void set_default_workspace_store( const Teuchos::RCP<WorkspaceStore> &default_workspace_store );
53
57
65TEUCHOSCORE_LIB_DLL_EXPORT void print_memory_usage_stats( const WorkspaceStore* workspace_store, std::ostream& out );
66
71class TEUCHOSCORE_LIB_DLL_EXPORT RawWorkspace {
72public:
74 friend class WorkspaceStore;
101 size_t num_bytes() const;
103 char* workspace_ptr();
105 const char* workspace_ptr() const;
106private:
107 WorkspaceStore *workspace_store_;
108 char *workspace_begin_;
109 char *workspace_end_;
110 bool owns_memory_; // If true then the pointed to memory was allocated with
111 // new so we need to call delete on it when we are destroyed.
112 // not defined and not to be called
113 RawWorkspace();
115 RawWorkspace& operator=(const RawWorkspace&);
116 static void* operator new(size_t);
117 static void operator delete(void*);
118}; // end class RawWorkspace
119
144template<class T>
146public:
182 ~Workspace();
184 size_t size() const;
187 T* getRawPtr();
190 const T* getRawPtr() const;
197 T& operator[](size_t i);
204 const T& operator[](size_t i) const;
210 operator ArrayView<T>();
212 operator ArrayView<const T>() const;
213private:
214 RawWorkspace raw_workspace_;
215 bool call_constructors_;
216 // not defined and not to be called
217 Workspace();
218 Workspace(const RawWorkspace&);
219 Workspace& operator=(const RawWorkspace&);
220 static void* operator new(size_t);
221 static void operator delete(void*);
222}; // end class Workspace
223
235class TEUCHOSCORE_LIB_DLL_EXPORT WorkspaceStore {
236public:
238 friend class RawWorkspace;
243 size_t num_bytes_total() const;
246 size_t num_bytes_remaining() const;
252 int num_static_allocations() const;
258 int num_dyn_allocations() const;
262 size_t num_current_bytes_total();
266 size_t num_max_bytes_needed() const;
267protected:
269 WorkspaceStore(size_t num_bytes);
271 void protected_initialize(size_t num_bytes);
272private:
273 char *workspace_begin_; // Points to the beginning of raw allocated workspace.
274 // If NULL then no workspace has been allocated yet.
275 char *workspace_end_; // Points to one past the last byte of allocated workspace.
276 // workspace_end_ >= workspace_begin_
277 char *curr_ws_ptr_; // Points to the first available byte of workspace.
278 // workspace_begin_ <= curr_ws_ptr_ <= workspace_end_
279 int num_static_allocations_; // Number of workspace allocation using already
280 // allocated memory.
281 int num_dyn_allocations_; // Number of workspace allocations using dynamic
282 // memory because the current workspace store was
283 // overridden
284 size_t num_current_bytes_total_; // Total bytes currently being used
285 size_t num_max_bytes_needed_; // Maximum number of bytes of storage needed
286 // Not definted and not to be called
288 WorkspaceStore& operator=(const WorkspaceStore&);
289}; // end class WorkspaceStore
290
299 : public WorkspaceStore
300{
301public:
305 WorkspaceStoreInitializeable(size_t num_bytes = 0);
312 void initialize(size_t num_bytes);
313}; // end class WorkspaceStoreInitializeable
314
316
317// /////////////////////////////////////
318// Inline members for Workspace<T>
319
320template<class T>
321inline
323 : raw_workspace_(workspace_store,sizeof(T)*num_elements), call_constructors_(call_constructors)
324{
325 if(call_constructors_) {
326 char* raw_ptr = raw_workspace_.workspace_ptr();
327 for( size_t k = 0; k < num_elements; ++k, raw_ptr += sizeof(T) )
328 ::new (raw_ptr) T(); // placement new
329 }
330}
331
332template<class T>
333inline
335{
336 if(call_constructors_) {
337 const size_t num_elements = this->size();
338 char* raw_ptr = raw_workspace_.workspace_ptr();
339 for( size_t k = 0; k < num_elements; ++k, raw_ptr += sizeof(T) )
340 reinterpret_cast<T*>(raw_ptr)->~T();
341 }
342}
343
344template<class T>
345inline
346size_t Workspace<T>::size() const
347{
348 return raw_workspace_.num_bytes() / sizeof(T);
349}
350
351template<class T>
352inline
354{
355 return ( size() ? &(*this)[0] : 0 );
356}
357
358template<class T>
359inline
361{
362 return ( size() ? &(*this)[0] : 0 );
363}
364
365template<class T>
366inline
368{
369#ifdef TEUCHOS_DEBUG
370 TEUCHOS_TEST_FOR_EXCEPTION( !( i < this->size() ), std::invalid_argument, "Workspace<T>::operator[](i): Error!" );
371#endif
372 return reinterpret_cast<T*>(raw_workspace_.workspace_ptr())[i];
373}
374
375template<class T>
376inline
377const T& Workspace<T>::operator[](size_t i) const
378{
379 return const_cast<Workspace<T>*>(this)->operator[](i);
380}
381
382template<class T>
383inline
385{
386 if (size()==0)
387 return Teuchos::null;
388 return arrayView<T>( &(*this)[0], size() );
389}
390
391template<class T>
392inline
395{
396 if (size()==0)
397 return Teuchos::null;
398 return arrayView<const T>( &(*this)[0], size() );
399}
400
401template<class T>
402inline
404{
405 return (*this)();
406}
407
408template<class T>
409inline
411{
412 return (*this)();
413}
414
415#ifdef __PGI // Should not have to define this but pgCC is complaining!
416template<class T>
417inline
419{
420 assert(0);
421 return NULL;
422}
423#endif
424
425// should not have to define this but the gcc-2.95.2 compiler is complaining!
426template<class T>
427inline
428void Workspace<T>::operator delete(void*)
429{
430 assert(0);
431}
432
433// /////////////////////////////////////
434// Inline members for WorkspaceStore
435
436inline
438{
439 return workspace_end_ - workspace_begin_;
440}
441
442inline
444{
445 return workspace_end_ - curr_ws_ptr_;
446}
447
448inline
450{
451 return num_static_allocations_;
452}
453
454inline
456{
457 return num_dyn_allocations_;
458}
459
460inline
462{
463 return num_current_bytes_total_;
464}
465
466inline
468{
469 return num_max_bytes_needed_;
470}
471
472// /////////////////////////////////////////////////
473// Inline members for WorkspaceStoreInitializeable
474
475inline
479
480inline
482{
483 protected_initialize(num_bytes);
484}
485
486// /////////////////////////////////////
487// Inline members for RawWorkspace
488
489inline
491{
492 return workspace_end_ - workspace_begin_;
493}
494
495inline
497{
498 return workspace_begin_;
499}
500
501inline
503{
504 return workspace_begin_;
505}
506
507// should not have to define this but the gcc-2.95.2 compiler is complaining!
508inline
509void RawWorkspace::operator delete(void*)
510{
511 assert(0);
512}
513
514} // end namespace Teuchos
515
516#endif // TEUCHOS_WORKSPACE_HPP
Reference-counted pointer class and non-member templated function implementations.
Smart reference counting pointer class for automatic garbage collection.
Encapulsation object for raw temporary workspace that has been allocated. These objects can only be c...
size_t num_bytes() const
Return the number of bytes of raw workspace.
char * workspace_ptr()
Give a raw pointer to the beginning of the workspace.
WorkspaceStore class that can be used to actually reinitialize memory.
WorkspaceStoreInitializeable(size_t num_bytes=0)
Default constructs to no memory set and will dynamically allocate all memory requested.
void initialize(size_t num_bytes)
Set the size block of memory to be given as workspace.
Workspace encapsulation class.
void protected_initialize(size_t num_bytes)
int num_static_allocations() const
Return the number of static memory allocations granted thus far. This is the number of memory allocat...
size_t num_max_bytes_needed() const
Return the maximum storage in bytes needed. This is the maximum total amount of * storage that was ne...
int num_dyn_allocations() const
Return the number of dynamic memory allocations granted thus far. This is the number of memory alloca...
size_t num_bytes_total() const
Return the total number of bytes that where initially allocated.
size_t num_bytes_remaining() const
Return the number of bytes remaining currently.
size_t num_current_bytes_total()
Return the total number of bytes currently allocated.. This is the total number of bytes currently be...
Templated class for workspace creation.
~Workspace()
The destructor on the elements will only be called if call_constructors == true was passed to the con...
T * getRawPtr()
Return a raw pointer to the beginning of the array or null if unsized.
ArrayView< T > operator()()
Return a non-const array view.
T & operator[](size_t i)
Non-const zero based element access.
size_t size() const
Return the number of elements in the array.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOSCORE_LIB_DLL_EXPORT void print_memory_usage_stats(const WorkspaceStore *workspace_store, std::ostream &out)
Print statistics on memory usage.
TEUCHOSCORE_LIB_DLL_EXPORT Teuchos::RCP< WorkspaceStore > get_default_workspace_store()
Get the global workspace object set by set_default_workspace_store().
TEUCHOSCORE_LIB_DLL_EXPORT void set_default_workspace_store(const Teuchos::RCP< WorkspaceStore > &default_workspace_store)
Set pointer to global workspace object.