-
Notifications
You must be signed in to change notification settings - Fork 298
/
mycuda.cpp
238 lines (198 loc) · 7.07 KB
/
mycuda.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/helper/AdvancedTimer.h>
#include <sofa/helper/BackTrace.h>
#include <iostream>
#include <fstream>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include "mycuda.h"
#include <sofa/helper/StringUtils.h>
#include <sofa/helper/logging/Messaging.h>
#if defined(__GNUC__) && !defined(__APPLE__) && !defined(WIN32)
#define SOFA_GPU_DEBUG_STACK_TRACE 10
#ifdef SOFA_GPU_DEBUG_STACK_TRACE
#include <stdexcept>
#include <string>
#include <execinfo.h>
#include <cxxabi.h>
#include <cstdlib>
class rtstack: public std::runtime_error
{
public:
explicit rtstack(const std::string & __arg) : std::runtime_error(__arg + '\n' + __getStack()) {};
private:
std::string __getStack() const throw()
{
// 100 = profondeur max de la pile qui sera affichee (arbitraire)
// On recupere la pile d'appels et les symboles associes
// ATTENTION Utiliser le switch -rdynamic sinon on n'a pas les symboles
void *traces[100];
size_t size = backtrace (traces, 100);
char** symbols = backtrace_symbols (traces, size);
std::string stacktrace;
int nbdisp = 0;
bool startSofa = false;
for (size_t i = 0; i < size; i++)
{
// stacktrace += symbols[i];
// On a typiquemenet des chaines de cette sorte: ./a.out(_Z1ev+0x180) [0x401704]
// On recherche les caracteres ( et +
std::string fct_name;
char *begin=NULL, *end=NULL;
for (char *j=symbols[i]; *j; ++j)
{
if (*j=='(') begin = j;
else if (*j=='+') end = j;
}
// si ( et + ont ete trouves
if (begin && end)
{
*begin++ = 0;
*end = 0;
// on a trouve le nom "mangled", on va maintenant le demangler
int status;
char *ret = abi::__cxa_demangle(begin,NULL,NULL,&status);
// Si ret != 0, on a demangle qq chose
if (ret)
{
fct_name = ret;
free(ret);
ret = NULL;
}
else // demangling failed, just pretend it's a C function with no args
{
fct_name = begin;
fct_name += "::()";
}
if (!startSofa && std::string::npos != fct_name.find(std::string("displayStack")))
{
startSofa = true; // wait to see displayStack() before printing the stack
}
else if (startSofa && std::string::npos != fct_name.find(std::string("::")))
{
stacktrace += fct_name;
stacktrace += '\n';
nbdisp++;
if (nbdisp>=SOFA_GPU_DEBUG_STACK_TRACE)
{
free (symbols);
return stacktrace;
}
}
}
}
free (symbols);
return stacktrace;
}
};
#endif
#endif
namespace sofa::gpu::cuda
{
extern "C"
{
MycudaVerboseLevel mycudaVerboseLevel = LOG_ERR;
//MycudaVerboseLevel mycudaVerboseLevel = LOG_INFO;
//MycudaVerboseLevel mycudaVerboseLevel = LOG_TRACE;
}
static void timerSyncCB(void*)
{
mycudaThreadSynchronize();
}
void mycudaPrivateInit(int /*device*/)
{
const char* sync = getenv("CUDA_TIMER_SYNC");
if (!sync || !*sync || atoi(sync))
sofa::helper::AdvancedTimer::setSyncCallBack(timerSyncCB, NULL);
const char* verbose = getenv("CUDA_VERBOSE");
if (verbose && *verbose)
mycudaVerboseLevel = (MycudaVerboseLevel) atoi(verbose);
}
void mycudaLogError(const char* err, const char* src)
{
msg_error("SofaCUDA") << "CUDA error: "<< err <<" returned from "<< src;
sofa::helper::BackTrace::dump();
assert(0);
exit(1);
}
std::string
format(const char *const format, va_list args)
{
// Determine the size needed for the buffer
va_list args_copy;
va_copy(args_copy, args);
int size = vsnprintf(nullptr, 0, format, args_copy);
va_end(args_copy);
// Allocate memory for the buffer
std::string result(size + 1, '0');
result[size] = '\0';
// Format the string into the buffer
vsprintf(&result[0], format, args);
// Resize the string to exclude the null terminator
result.resize(size);
return result;
}
int mycudaPrintf(const char* fmt, ...)
{
va_list args;
va_start( args, fmt );
auto message = format(fmt, args);
va_end( args );
message = sofa::helper::removeTrailingCharacter(message, '\n');
msg_info("SofaCUDA") << message;
return static_cast<int>(message.size());
}
int mycudaPrintfError(const char* fmt, ...)
{
va_list args;
va_start( args, fmt );
auto message = format(fmt, args);
va_end( args );
message = sofa::helper::removeTrailingCharacter(message, '\n');
msg_error("SofaCUDA") << message;
return static_cast<int>(message.size());
}
const char* mygetenv(const char* name)
{
return getenv(name);
}
#if defined(SOFA_GPU_DEBUG_STACK_TRACE) && defined(__GNUC__) && !defined(__APPLE__) && !defined(WIN32)
void displayStack(const char * name)
{
try
{
throw (rtstack(name));
}
catch (std::exception &exc)
{
std::cerr << exc.what();
};
}
#else
void displayStack(const char * /*name*/)
{
}
#endif
} // namespace sofa::gpu::cuda