3
// ************************************************************************
6
// Manycore Performance-Portable Multidimensional Arrays
8
// Copyright (2012) Sandia Corporation
10
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
11
// the U.S. Government retains certain rights in this software.
13
// Redistribution and use in source and binary forms, with or without
14
// modification, are permitted provided that the following conditions are
17
// 1. Redistributions of source code must retain the above copyright
18
// notice, this list of conditions and the following disclaimer.
20
// 2. Redistributions in binary form must reproduce the above copyright
21
// notice, this list of conditions and the following disclaimer in the
22
// documentation and/or other materials provided with the distribution.
24
// 3. Neither the name of the Corporation nor the names of the
25
// contributors may be used to endorse or promote products derived from
26
// this software without specific prior written permission.
28
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
29
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
32
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
35
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
36
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40
// Questions? Contact H. Carter Edwards (hcedwar@sandia.gov)
42
// ************************************************************************
54
#include <impl/Kokkos_Error.hpp>
56
//----------------------------------------------------------------------------
57
//----------------------------------------------------------------------------
62
void host_abort( const char * const message )
64
fwrite(message,1,strlen(message),stderr);
69
void throw_runtime_exception( const std::string & msg )
71
std::ostringstream o ;
73
traceback_callstack( o );
74
throw std::runtime_error( o.str() );
78
std::string human_memory_size(size_t arg_bytes)
80
double bytes = arg_bytes;
81
const double K = 1024;
82
const double M = K*1024;
83
const double G = M*1024;
85
std::ostringstream out;
87
out << std::setprecision(4) << bytes << " B";
88
} else if (bytes < M) {
90
out << std::setprecision(4) << bytes << " K";
91
} else if (bytes < G) {
93
out << std::setprecision(4) << bytes << " M";
96
out << std::setprecision(4) << bytes << " G";
104
//----------------------------------------------------------------------------
105
//----------------------------------------------------------------------------
107
#if defined( __GNUC__ ) && defined( ENABLE_TRACEBACK )
109
/* This is only known to work with GNU C++
110
* Must be compiled with '-rdynamic'
111
* Must be linked with '-ldl'
114
/* Print call stack into an error stream,
115
* so one knows in which function the error occured.
118
* http://stupefydeveloper.blogspot.com/2008/10/cc-call-stack.html
120
* License on this site:
121
* This blog is licensed under a
122
* Creative Commons Attribution-Share Alike 3.0 Unported License.
124
* http://creativecommons.org/licenses/by-sa/3.0/
126
* Modified to output to std::ostream.
129
#include <execinfo.h>
137
void traceback_callstack( std::ostream & msg )
141
enum { MAX_DEPTH = 32 };
143
void *trace[MAX_DEPTH];
148
int trace_size = backtrace(trace, MAX_DEPTH);
150
msg << std::endl << "Call stack {" << std::endl ;
152
for (int i=1; i<trace_size; ++i)
154
if(!dladdr(trace[i], &dlinfo))
157
const char * symname = dlinfo.dli_sname;
159
char * demangled = __cxa_demangle(symname, NULL, 0, &status);
161
if ( status == 0 && demangled ) {
165
if ( symname && *symname != 0 ) {
166
msg << " object: " << dlinfo.dli_fname
167
<< " function: " << symname
186
void traceback_callstack( std::ostream & msg )
188
msg << std::endl << "Traceback functionality not available" << std::endl ;