Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Allocator_atexit.cpp
Go to the documentation of this file.
1// @HEADER
2// ***********************************************************************
3//
4// Teuchos: Common Tools Package
5// Copyright (2004) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38//
39// ***********************************************************************
40// @HEADER
41
44#include <sstream>
45#include <vector>
46
47#ifdef HAVE_TEUCHOS_MPI
48# include "mpi.h"
49#endif // HAVE_TEUCHOS_MPI
50
51// This test "passes" if the atexit() hook runs and prints the right
52// thing. _exit() skips over any atexit() hooks, so it is perfect for
53// making the test fail if something goes wrong before main() is done.
54// However, _exit() might not exist. See the following man page to
55// learn how to test whether _exit() exists.
56//
57// http://man7.org/linux/man-pages/man2/_exit.2.html
58
59namespace { // (anonymous)
61 bool iPrint_;
62
64 std::string prefix_;
65
66 // Demonstration of an atexit() hook that uses
67 // Teuchos::Details::AllocationLogger to report maximum and current
68 // memory usage at exit from main().
69 void allocationLoggerHook () {
70 if (iPrint_) {
72 using std::cout;
73 using std::endl;
74
75 // If printing on multiple MPI processes, print to a string
76 // first, to hinder interleaving of output from different
77 // processes.
78 std::ostringstream os;
79 os << prefix_ << "Teuchos allocation tracking: "
80 << "Maximum allocation (B): "
81 << AllocationLogger::maxAllocInBytes ()
82 << ", Current allocation (B): "
83 << AllocationLogger::curAllocInBytes () << endl;
84 cout << os.str ();
85 }
86 }
87} // namespace (anonymous)
88
89#ifdef HAVE_TEUCHOS_MPI
90int main (int argc, char* argv[])
91#else
92int main (int, char*[])
93#endif // HAVE_TEUCHOS_MPI
94{
95 typedef std::vector<float, Teuchos::Details::Allocator<float> > float_vec_type;
96 typedef std::vector<long, Teuchos::Details::Allocator<long> > long_vec_type;
97
98#ifdef HAVE_TEUCHOS_MPI
99 (void) MPI_Init (&argc, &argv);
100 int myRank = 0;
101 (void) MPI_Comm_rank (MPI_COMM_WORLD, &myRank);
102 // Only let Process 0 print. This ensures that the atexit() hook
103 // won't print multiple times, with the possibility of interleaved
104 // output and a test that fails unjustifiedly (CTest determines
105 // passing by searching the test output for a string).
106 iPrint_ = (myRank == 0);
107#else
108 iPrint_ = true;
109#endif // HAVE_TEUCHOS_MPI
110 prefix_ = std::string ("Proc 0: ");
111
112 // Register the atexit() "hook" (the function to be called when
113 // main() exists).
114 //
115 // It's possible for atexit() to fail. In that case, it returns a
116 // nonzero error code. The failure mode to expect is that too many
117 // hooks have been set and the system has no more memory to store
118 // them. In that case, we simply accept that no printing will
119 // happen at exit. It doesn't make sense to retry; hooks can't be
120 // _unset_, so memory will never get freed up.
121 (void) atexit (allocationLoggerHook);
122
123 const float_vec_type::size_type numEntries = 10;
124 float_vec_type x_f (numEntries, 42.0);
125 long_vec_type x_l (numEntries);
126 std::copy (x_f.begin (), x_f.end (), x_l.begin ());
127
128#ifdef HAVE_TEUCHOS_MPI
129 (void) MPI_Finalize ();
130#endif // HAVE_TEUCHOS_MPI
131
132 return EXIT_SUCCESS;
133}
134
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Declaration of Teuchos::Details::Allocator, a tracking and logging implementation of the C++ Standard...
Logging implementation used by Allocator (see below).
int main()
Definition evilMain.cpp:75