Teuchos_Workspace.cpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Teuchos_Workspace.hpp"
00030 
00031 namespace {
00032 Teuchos::RCP<Teuchos::WorkspaceStore>  default_workspace_store(Teuchos::null);
00033 }
00034 
00035 // Global functions
00036 
00037 void Teuchos::set_default_workspace_store( const Teuchos::RCP<WorkspaceStore> &default_workspace_store_in )
00038 {
00039   default_workspace_store = default_workspace_store_in;
00040 }
00041 
00042 Teuchos::RCP<Teuchos::WorkspaceStore> Teuchos::get_default_workspace_store()
00043 {
00044   return default_workspace_store;
00045 }
00046 
00047 void Teuchos::print_memory_usage_stats( const WorkspaceStore* workspace_store, std::ostream& out )
00048 {
00049   if( workspace_store ) {
00050     out
00051       << "\n*** Statistics for autmatic array workspace:"
00052       << "\n  Number of megabytes of preallocated workspace                = "
00053       << (workspace_store->num_bytes_total()*1e-6)
00054       << "\n  Number of megabytes needed                                   = "
00055       << (workspace_store->num_max_bytes_needed()*1e-6)
00056       << "\n  Number of allocations using preallocated workspace           = "
00057       << workspace_store->num_static_allocations()
00058       << "\n  Number of dynamic allocations beyond preallocated workspace  = "
00059       << workspace_store->num_dyn_allocations()
00060       << "\n";
00061   }
00062   else {
00063     out
00064       << "\n*** Statistics for autmatic array workspace:"
00065       << "\n  No workspace storage was allocated!\n";
00066   }
00067 }
00068 
00069 namespace Teuchos {
00070 
00071 // WorkspaceStore
00072 
00073 WorkspaceStore::WorkspaceStore(size_t num_bytes)
00074   : workspace_begin_(NULL)
00075   , workspace_end_(NULL)
00076   , curr_ws_ptr_(NULL)
00077   , num_static_allocations_(0)
00078   , num_dyn_allocations_(0)
00079   , num_current_bytes_total_(0)
00080   , num_max_bytes_needed_(0)
00081 {
00082   if(num_bytes)
00083     protected_initialize(num_bytes);
00084 }
00085 
00086 WorkspaceStore::~WorkspaceStore() {
00087   if(workspace_begin_) delete [] workspace_begin_;
00088 }
00089 
00090 void WorkspaceStore::protected_initialize(size_t num_bytes)
00091 {
00092   TEST_FOR_EXCEPTION(
00093     curr_ws_ptr_ != workspace_begin_, std::logic_error
00094     ,"WorkspaceStore::set_workspace_size(...) : Error, "
00095     "You can not reset the workspace size when any RawWorkspace objects "
00096     "are using workspace!" );
00097   if(workspace_begin_) delete [] workspace_begin_;
00098   workspace_begin_        = ::new char[num_bytes];
00099   workspace_end_          = workspace_begin_ + num_bytes;
00100   curr_ws_ptr_            = workspace_begin_;
00101   num_static_allocations_ = 0;
00102   num_dyn_allocations_    = 0;
00103   num_current_bytes_total_= 0;
00104   num_max_bytes_needed_   = 0;
00105 } 
00106 
00107 // RawWorkspace
00108 
00109 RawWorkspace::RawWorkspace(WorkspaceStore* workspace_store, size_t num_bytes_in)
00110 {
00111   if(num_bytes_in) {
00112     workspace_store_ = workspace_store;
00113     if( !workspace_store_ || workspace_store_->num_bytes_remaining() < num_bytes_in ) {
00114       workspace_begin_ = ::new char[num_bytes_in];
00115       workspace_end_   = workspace_begin_ + num_bytes_in;
00116       owns_memory_     = true;
00117       if(workspace_store_)
00118         workspace_store_->num_dyn_allocations_++;
00119     }
00120     else {
00121       workspace_begin_ = workspace_store_->curr_ws_ptr_;
00122       workspace_end_   = workspace_begin_ + num_bytes_in;
00123       owns_memory_     = false;
00124       workspace_store_->curr_ws_ptr_ += num_bytes_in;
00125       workspace_store_->num_static_allocations_++;
00126     }
00127   }
00128   else {
00129     workspace_store_ = NULL;
00130     workspace_begin_ = NULL;
00131     workspace_end_   = NULL;
00132     owns_memory_     = false;
00133   }
00134   if(workspace_store_) {
00135     workspace_store_->num_current_bytes_total_ += num_bytes_in;
00136     if( workspace_store_->num_current_bytes_total_ > workspace_store_->num_max_bytes_needed_ )
00137       workspace_store_->num_max_bytes_needed_ = workspace_store_->num_current_bytes_total_;
00138   }
00139 }
00140 
00141 RawWorkspace::~RawWorkspace()
00142 {
00143   if(workspace_store_)
00144     workspace_store_->num_current_bytes_total_ -= this->num_bytes();
00145   if(owns_memory_) {
00146     if(workspace_begin_) delete [] workspace_begin_;
00147   }
00148   else {
00149     if(workspace_store_) {
00150       TEST_FOR_EXCEPTION(
00151         workspace_store_->curr_ws_ptr_ != workspace_end_, std::logic_error
00152         ,"RawWorkspace::~RawWorkspace(...): Error, "
00153         "Invalid usage of RawWorkspace class, corrupted WorspaceStore object!" );
00154       workspace_store_->curr_ws_ptr_ = workspace_begin_;
00155     }
00156   }
00157 }
00158 
00159 #ifdef __PGI // Should not have to define this since it should not be called!
00160 void* RawWorkspace::operator new(size_t)
00161 {
00162   assert(0);
00163   return NULL;
00164 }
00165 #endif
00166 
00167 } // end namespace Teuchos

Generated on Wed May 12 21:40:33 2010 for Teuchos - Trilinos Tools Package by  doxygen 1.4.7