Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Allocator_atexit.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_ConfigDefs.hpp>
12 #include <sstream>
13 #include <vector>
14 
15 #ifdef HAVE_TEUCHOS_MPI
16 # include "mpi.h"
17 #endif // HAVE_TEUCHOS_MPI
18 
19 // This test "passes" if the atexit() hook runs and prints the right
20 // thing. _exit() skips over any atexit() hooks, so it is perfect for
21 // making the test fail if something goes wrong before main() is done.
22 // However, _exit() might not exist. See the following man page to
23 // learn how to test whether _exit() exists.
24 //
25 // http://man7.org/linux/man-pages/man2/_exit.2.html
26 
27 namespace { // (anonymous)
29  bool iPrint_;
30 
32  std::string prefix_;
33 
34  // Demonstration of an atexit() hook that uses
35  // Teuchos::Details::AllocationLogger to report maximum and current
36  // memory usage at exit from main().
37  void allocationLoggerHook () {
38  if (iPrint_) {
40  using std::cout;
41  using std::endl;
42 
43  // If printing on multiple MPI processes, print to a string
44  // first, to hinder interleaving of output from different
45  // processes.
46  std::ostringstream os;
47  os << prefix_ << "Teuchos allocation tracking: "
48  << "Maximum allocation (B): "
49  << AllocationLogger::maxAllocInBytes ()
50  << ", Current allocation (B): "
51  << AllocationLogger::curAllocInBytes () << endl;
52  cout << os.str ();
53  }
54  }
55 } // namespace (anonymous)
56 
57 #ifdef HAVE_TEUCHOS_MPI
58 int main (int argc, char* argv[])
59 #else
60 int main (int, char*[])
61 #endif // HAVE_TEUCHOS_MPI
62 {
63  typedef std::vector<float, Teuchos::Details::Allocator<float> > float_vec_type;
64  typedef std::vector<long, Teuchos::Details::Allocator<long> > long_vec_type;
65 
66 #ifdef HAVE_TEUCHOS_MPI
67  (void) MPI_Init (&argc, &argv);
68  int myRank = 0;
69  (void) MPI_Comm_rank (MPI_COMM_WORLD, &myRank);
70  // Only let Process 0 print. This ensures that the atexit() hook
71  // won't print multiple times, with the possibility of interleaved
72  // output and a test that fails unjustifiedly (CTest determines
73  // passing by searching the test output for a string).
74  iPrint_ = (myRank == 0);
75 #else
76  iPrint_ = true;
77 #endif // HAVE_TEUCHOS_MPI
78  prefix_ = std::string ("Proc 0: ");
79 
80  // Register the atexit() "hook" (the function to be called when
81  // main() exists).
82  //
83  // It's possible for atexit() to fail. In that case, it returns a
84  // nonzero error code. The failure mode to expect is that too many
85  // hooks have been set and the system has no more memory to store
86  // them. In that case, we simply accept that no printing will
87  // happen at exit. It doesn't make sense to retry; hooks can't be
88  // _unset_, so memory will never get freed up.
89  (void) atexit (allocationLoggerHook);
90 
91  const float_vec_type::size_type numEntries = 10;
92  float_vec_type x_f (numEntries, 42.0);
93  long_vec_type x_l (numEntries);
94  std::copy (x_f.begin (), x_f.end (), x_l.begin ());
95 
96 #ifdef HAVE_TEUCHOS_MPI
97  (void) MPI_Finalize ();
98 #endif // HAVE_TEUCHOS_MPI
99 
100  return EXIT_SUCCESS;
101 }
102 
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
Logging implementation used by Allocator (see below).
int main(int argc, char *argv[])
Declaration of Teuchos::Details::Allocator, a tracking and logging implementation of the C++ Standard...