Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_Workspace.cpp
Go to the documentation of this file.
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 #include "Teuchos_Workspace.hpp"
11 
12 namespace {
14 }
15 
16 // Global functions
17 
18 void Teuchos::set_default_workspace_store( const Teuchos::RCP<WorkspaceStore> &default_workspace_store_in )
19 {
20  default_workspace_store = default_workspace_store_in;
21 }
22 
24 {
25  return default_workspace_store;
26 }
27 
28 void Teuchos::print_memory_usage_stats( const WorkspaceStore* workspace_store, std::ostream& out )
29 {
30  if( workspace_store ) {
31  out
32  << "\n*** Statistics for autmatic array workspace:"
33  << "\n Number of megabytes of preallocated workspace = "
34  << (workspace_store->num_bytes_total()*1e-6)
35  << "\n Number of megabytes needed = "
36  << (workspace_store->num_max_bytes_needed()*1e-6)
37  << "\n Number of allocations using preallocated workspace = "
38  << workspace_store->num_static_allocations()
39  << "\n Number of dynamic allocations beyond preallocated workspace = "
40  << workspace_store->num_dyn_allocations()
41  << "\n";
42  }
43  else {
44  out
45  << "\n*** Statistics for autmatic array workspace:"
46  << "\n No workspace storage was allocated!\n";
47  }
48 }
49 
50 namespace Teuchos {
51 
52 // WorkspaceStore
53 
55  : workspace_begin_(NULL)
56  , workspace_end_(NULL)
57  , curr_ws_ptr_(NULL)
58  , num_static_allocations_(0)
59  , num_dyn_allocations_(0)
60  , num_current_bytes_total_(0)
61  , num_max_bytes_needed_(0)
62 {
63  if(num_bytes)
64  protected_initialize(num_bytes);
65 }
66 
68  if(workspace_begin_) delete [] workspace_begin_;
69 }
70 
72 {
74  curr_ws_ptr_ != workspace_begin_, std::logic_error
75  ,"WorkspaceStore::set_workspace_size(...) : Error, "
76  "You can not reset the workspace size when any RawWorkspace objects "
77  "are using workspace!" );
78  if(workspace_begin_) delete [] workspace_begin_;
79  workspace_begin_ = ::new char[num_bytes];
80  workspace_end_ = workspace_begin_ + num_bytes;
86 }
87 
88 // RawWorkspace
89 
90 RawWorkspace::RawWorkspace(WorkspaceStore* workspace_store, size_t num_bytes_in)
91 {
92  if(num_bytes_in) {
93  workspace_store_ = workspace_store;
94  if( !workspace_store_ || workspace_store_->num_bytes_remaining() < num_bytes_in ) {
95  workspace_begin_ = ::new char[num_bytes_in];
96  workspace_end_ = workspace_begin_ + num_bytes_in;
97  owns_memory_ = true;
100  }
101  else {
103  workspace_end_ = workspace_begin_ + num_bytes_in;
104  owns_memory_ = false;
105  workspace_store_->curr_ws_ptr_ += num_bytes_in;
107  }
108  }
109  else {
110  workspace_store_ = NULL;
111  workspace_begin_ = NULL;
112  workspace_end_ = NULL;
113  owns_memory_ = false;
114  }
115  if(workspace_store_) {
119  }
120 }
121 
123 {
124  if(workspace_store_)
126  if(owns_memory_) {
127  if(workspace_begin_) delete [] workspace_begin_;
128  }
129  else {
130  if(workspace_store_) {
133  ,"RawWorkspace::~RawWorkspace(...): Error, "
134  "Invalid usage of RawWorkspace class, corrupted WorspaceStore object!" );
136  }
137  }
138 }
139 
140 #ifdef __PGI // Should not have to define this since it should not be called!
141 void* RawWorkspace::operator new(size_t)
142 {
143  assert(0);
144  return NULL;
145 }
146 #endif
147 
148 } // end namespace Teuchos
WorkspaceStore * workspace_store_
TEUCHOSCORE_LIB_DLL_EXPORT void set_default_workspace_store(const Teuchos::RCP< WorkspaceStore > &default_workspace_store)
Set pointer to global workspace object.
#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.
~RawWorkspace()
Deallocate workspace.
WorkspaceStore(size_t num_bytes)
Workspace encapsulation class.
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...
#define TEUCHOS_TEST_FOR_TERMINATION(terminate_test, msg)
This macro is to be used instead of TEUCHOS_TEST_FOR_EXCEPTION() to report an error in situations whe...
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 protected_initialize(size_t num_bytes)
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.
TEUCHOSCORE_LIB_DLL_EXPORT Teuchos::RCP< WorkspaceStore > get_default_workspace_store()
Get the global workspace object set by set_default_workspace_store().