~ubuntu-branches/debian/sid/lammps/sid

« back to all changes in this revision

Viewing changes to lib/kokkos/core/src/impl/Kokkos_Error.cpp

  • Committer: Package Import Robot
  • Author(s): Anton Gladky
  • Date: 2015-04-29 23:44:49 UTC
  • mfrom: (5.1.3 experimental)
  • Revision ID: package-import@ubuntu.com-20150429234449-mbhy9utku6hp6oq8
Tags: 0~20150313.gitfa668e1-1
Upload into unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
//@HEADER
 
3
// ************************************************************************
 
4
//
 
5
//                             Kokkos
 
6
//         Manycore Performance-Portable Multidimensional Arrays
 
7
//
 
8
//              Copyright (2012) Sandia Corporation
 
9
//
 
10
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
 
11
// the U.S. Government retains certain rights in this software.
 
12
//
 
13
// Redistribution and use in source and binary forms, with or without
 
14
// modification, are permitted provided that the following conditions are
 
15
// met:
 
16
//
 
17
// 1. Redistributions of source code must retain the above copyright
 
18
// notice, this list of conditions and the following disclaimer.
 
19
//
 
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.
 
23
//
 
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.
 
27
//
 
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.
 
39
//
 
40
// Questions?  Contact  H. Carter Edwards (hcedwar@sandia.gov)
 
41
//
 
42
// ************************************************************************
 
43
//@HEADER
 
44
*/
 
45
 
 
46
#include <stdio.h>
 
47
#include <string.h>
 
48
#include <stdlib.h>
 
49
 
 
50
#include <ostream>
 
51
#include <sstream>
 
52
#include <iomanip>
 
53
#include <stdexcept>
 
54
#include <impl/Kokkos_Error.hpp>
 
55
 
 
56
//----------------------------------------------------------------------------
 
57
//----------------------------------------------------------------------------
 
58
 
 
59
namespace Kokkos {
 
60
namespace Impl {
 
61
 
 
62
void host_abort( const char * const message )
 
63
{
 
64
  fwrite(message,1,strlen(message),stderr);
 
65
  fflush(stderr);
 
66
  abort();
 
67
}
 
68
 
 
69
void throw_runtime_exception( const std::string & msg )
 
70
{
 
71
  std::ostringstream o ;
 
72
  o << msg ;
 
73
  traceback_callstack( o );
 
74
  throw std::runtime_error( o.str() );
 
75
}
 
76
 
 
77
 
 
78
std::string human_memory_size(size_t arg_bytes)
 
79
{
 
80
  double bytes = arg_bytes;
 
81
  const double K = 1024;
 
82
  const double M = K*1024;
 
83
  const double G = M*1024;
 
84
 
 
85
  std::ostringstream out;
 
86
  if (bytes < K) {
 
87
    out << std::setprecision(4) << bytes << " B";
 
88
  } else if (bytes < M) {
 
89
    bytes /= K;
 
90
    out << std::setprecision(4) << bytes << " K";
 
91
  } else if (bytes < G) {
 
92
    bytes /= M;
 
93
    out << std::setprecision(4) << bytes << " M";
 
94
  } else {
 
95
    bytes /= G;
 
96
    out << std::setprecision(4) << bytes << " G";
 
97
  }
 
98
  return out.str();
 
99
}
 
100
 
 
101
}
 
102
}
 
103
 
 
104
//----------------------------------------------------------------------------
 
105
//----------------------------------------------------------------------------
 
106
 
 
107
#if defined( __GNUC__ ) && defined( ENABLE_TRACEBACK )
 
108
 
 
109
/*  This is only known to work with GNU C++
 
110
 *  Must be compiled with '-rdynamic'
 
111
 *  Must be linked with   '-ldl'
 
112
 */
 
113
 
 
114
/* Print call stack into an error stream,
 
115
 * so one knows in which function the error occured.
 
116
 *
 
117
 * Code copied from:
 
118
 *   http://stupefydeveloper.blogspot.com/2008/10/cc-call-stack.html
 
119
 *
 
120
 * License on this site:
 
121
 *   This blog is licensed under a
 
122
 *   Creative Commons Attribution-Share Alike 3.0 Unported License.
 
123
 *
 
124
 *   http://creativecommons.org/licenses/by-sa/3.0/
 
125
 *
 
126
 * Modified to output to std::ostream.
 
127
 */
 
128
#include <signal.h>
 
129
#include <execinfo.h>
 
130
#include <cxxabi.h>
 
131
#include <dlfcn.h>
 
132
#include <stdlib.h>
 
133
 
 
134
namespace Kokkos {
 
135
namespace Impl {
 
136
 
 
137
void traceback_callstack( std::ostream & msg )
 
138
{
 
139
  using namespace abi;
 
140
 
 
141
  enum { MAX_DEPTH = 32 };
 
142
 
 
143
  void *trace[MAX_DEPTH];
 
144
  Dl_info dlinfo;
 
145
 
 
146
  int status;
 
147
 
 
148
  int trace_size = backtrace(trace, MAX_DEPTH);
 
149
 
 
150
  msg << std::endl << "Call stack {" << std::endl ;
 
151
 
 
152
  for (int i=1; i<trace_size; ++i)
 
153
  {
 
154
    if(!dladdr(trace[i], &dlinfo))
 
155
        continue;
 
156
 
 
157
    const char * symname = dlinfo.dli_sname;
 
158
 
 
159
    char * demangled = __cxa_demangle(symname, NULL, 0, &status);
 
160
 
 
161
    if ( status == 0 && demangled ) {
 
162
      symname = demangled;
 
163
    }
 
164
 
 
165
    if ( symname && *symname != 0 ) {
 
166
      msg << "  object: " << dlinfo.dli_fname
 
167
          << " function: " << symname
 
168
          << std::endl ;
 
169
    }
 
170
 
 
171
    if ( demangled ) {
 
172
        free(demangled);
 
173
    }
 
174
  }
 
175
  msg << "}" ;
 
176
}
 
177
 
 
178
}
 
179
}
 
180
 
 
181
#else
 
182
 
 
183
namespace Kokkos {
 
184
namespace Impl {
 
185
 
 
186
void traceback_callstack( std::ostream & msg )
 
187
{
 
188
  msg << std::endl << "Traceback functionality not available" << std::endl ;
 
189
}
 
190
 
 
191
}
 
192
}
 
193
 
 
194
#endif
 
195