Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
17 namespace Teuchos {
18 
19 class WorkspaceStore;
20 class RawWorkspace;
21 
33 
52 TEUCHOSCORE_LIB_DLL_EXPORT void set_default_workspace_store( const Teuchos::RCP<WorkspaceStore> &default_workspace_store );
53 
57 
65 TEUCHOSCORE_LIB_DLL_EXPORT void print_memory_usage_stats( const WorkspaceStore* workspace_store, std::ostream& out );
66 
71 class TEUCHOSCORE_LIB_DLL_EXPORT RawWorkspace {
72 public:
74  friend class WorkspaceStore;
97  RawWorkspace(WorkspaceStore* workspace_store, size_t num_bytes);
99  ~RawWorkspace();
101  size_t num_bytes() const;
103  char* workspace_ptr();
105  const char* workspace_ptr() const;
106 private:
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();
114  RawWorkspace(const RawWorkspace&);
115  RawWorkspace& operator=(const RawWorkspace&);
116  static void* operator new(size_t);
117  static void operator delete(void*);
118 }; // end class RawWorkspace
119 
144 template<class T>
145 class Workspace {
146 public:
178  Workspace(WorkspaceStore* workspace_store, size_t num_elements, bool call_constructors = true);
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;
213 private:
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 
235 class TEUCHOSCORE_LIB_DLL_EXPORT WorkspaceStore {
236 public:
238  friend class RawWorkspace;
240  ~WorkspaceStore();
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;
267 protected:
269  WorkspaceStore(size_t num_bytes);
271  void protected_initialize(size_t num_bytes);
272 private:
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 {
301 public:
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 
320 template<class T>
321 inline
322 Workspace<T>::Workspace(WorkspaceStore* workspace_store, size_t num_elements, bool call_constructors)
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 
332 template<class T>
333 inline
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 
344 template<class T>
345 inline
346 size_t Workspace<T>::size() const
347 {
348  return raw_workspace_.num_bytes() / sizeof(T);
349 }
350 
351 template<class T>
352 inline
354 {
355  return ( size() ? &(*this)[0] : 0 );
356 }
357 
358 template<class T>
359 inline
360 const T* Workspace<T>::getRawPtr() const
361 {
362  return ( size() ? &(*this)[0] : 0 );
363 }
364 
365 template<class T>
366 inline
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 
375 template<class T>
376 inline
377 const T& Workspace<T>::operator[](size_t i) const
378 {
379  return const_cast<Workspace<T>*>(this)->operator[](i);
380 }
381 
382 template<class T>
383 inline
385 {
386  if (size()==0)
387  return Teuchos::null;
388  return arrayView<T>( &(*this)[0], size() );
389 }
390 
391 template<class T>
392 inline
395 {
396  if (size()==0)
397  return Teuchos::null;
398  return arrayView<const T>( &(*this)[0], size() );
399 }
400 
401 template<class T>
402 inline
404 {
405  return (*this)();
406 }
407 
408 template<class T>
409 inline
411 {
412  return (*this)();
413 }
414 
415 #ifdef __PGI // Should not have to define this but pgCC is complaining!
416 template<class T>
417 inline
418 void* Workspace<T>::operator new(size_t)
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!
426 template<class T>
427 inline
428 void Workspace<T>::operator delete(void*)
429 {
430  assert(0);
431 }
432 
433 // /////////////////////////////////////
434 // Inline members for WorkspaceStore
435 
436 inline
438 {
439  return workspace_end_ - workspace_begin_;
440 }
441 
442 inline
444 {
445  return workspace_end_ - curr_ws_ptr_;
446 }
447 
448 inline
450 {
451  return num_static_allocations_;
452 }
453 
454 inline
456 {
457  return num_dyn_allocations_;
458 }
459 
460 inline
462 {
463  return num_current_bytes_total_;
464 }
465 
466 inline
468 {
469  return num_max_bytes_needed_;
470 }
471 
472 // /////////////////////////////////////////////////
473 // Inline members for WorkspaceStoreInitializeable
474 
475 inline
477  : WorkspaceStore(num_bytes)
478 {}
479 
480 inline
482 {
483  protected_initialize(num_bytes);
484 }
485 
486 // /////////////////////////////////////
487 // Inline members for RawWorkspace
488 
489 inline
491 {
492  return workspace_end_ - workspace_begin_;
493 }
494 
495 inline
497 {
498  return workspace_begin_;
499 }
500 
501 inline
502 const char* RawWorkspace::workspace_ptr() const
503 {
504  return workspace_begin_;
505 }
506 
507 // should not have to define this but the gcc-2.95.2 compiler is complaining!
508 inline
509 void RawWorkspace::operator delete(void*)
510 {
511  assert(0);
512 }
513 
514 } // end namespace Teuchos
515 
516 #endif // TEUCHOS_WORKSPACE_HPP
WorkspaceStoreInitializeable(size_t num_bytes=0)
Default constructs to no memory set and will dynamically allocate all memory requested.
size_t size() const
Return the number of elements in the array.
Encapulsation object for raw temporary workspace that has been allocated. These objects can only be c...
Templated class for workspace creation.
ArrayView< T > operator()()
Return a non-const array view.
~Workspace()
The destructor on the elements will only be called if call_constructors == true was passed to the con...
TEUCHOSCORE_LIB_DLL_EXPORT void set_default_workspace_store(const Teuchos::RCP< WorkspaceStore > &default_workspace_store)
Set pointer to global workspace object.
WorkspaceStore class that can be used to actually reinitialize memory.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
size_t num_bytes() const
Return the number of bytes of raw workspace.
size_t num_current_bytes_total()
Return the total number of bytes currently allocated.. This is the total number of bytes currently be...
T & operator[](size_t i)
Non-const zero based element access.
T * getRawPtr()
Return a raw pointer to the beginning of the array or null if unsized.
Workspace encapsulation class.
Nonowning array view.
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...
int num_static_allocations() const
Return the number of static memory allocations granted thus far. This is the number of memory allocat...
Smart reference counting pointer class for automatic garbage collection.
void initialize(size_t num_bytes)
Set the size block of memory to be given as workspace.
void protected_initialize(size_t num_bytes)
Partial specialization of ArrayView for const T.
char * workspace_ptr()
Give a raw pointer to the beginning of the workspace.
size_t num_bytes_remaining() const
Return the number of bytes remaining currently.
size_t num_bytes_total() const
Return the total number of bytes that where initially allocated.
TEUCHOSCORE_LIB_DLL_EXPORT void print_memory_usage_stats(const WorkspaceStore *workspace_store, std::ostream &out)
Print statistics on memory usage.
Reference-counted pointer class and non-member templated function implementations.
TEUCHOSCORE_LIB_DLL_EXPORT Teuchos::RCP< WorkspaceStore > get_default_workspace_store()
Get the global workspace object set by set_default_workspace_store().