add code for backtrace (#21)

* add code for backtrace

* Add: infini::Exception

```
Test project /home/zly/InfiniTensor_aux/build
    Start 1: test_graph
1/4 Test #1: test_graph .......................   Passed    0.05 sec
    Start 2: test_hash
2/4 Test #2: test_hash ........................   Passed    0.02 sec
    Start 3: test_conv
3/4 Test #3: test_conv ........................   Passed    4.40 sec
    Start 4: test_pooling
4/4 Test #4: test_pooling .....................   Passed    2.47 sec

100% tests passed, 0 tests failed out of 4

Total Test time (real) =   6.94 sec
```

* Fix: USE_BACKTRACE in cmake

Co-authored-by: wanghailu <wanghailu@qiyuanlab.com>
Co-authored-by: Liyan Zheng <liyan-zheng@outlook.com>
This commit is contained in:
Hardy 2022-09-01 20:30:12 +08:00 committed by GitHub
parent 48293576c0
commit 32a01efbbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 59 additions and 3 deletions

3
.gitmodules vendored
View File

@ -7,3 +7,6 @@
[submodule "3rd-party/googletest"]
path = 3rd-party/googletest
url = git@github.com:google/googletest.git
[submodule "3rd-party/backward-cpp"]
path = 3rd-party/backward-cpp
url = git@github.com:bombela/backward-cpp.git

1
3rd-party/backward-cpp vendored Submodule

@ -0,0 +1 @@
Subproject commit f30744bcf726ea3735df7ecf9e9de9ddac540283

View File

@ -5,7 +5,9 @@ project(InfiniTensor C CXX)
# Do not change these options in this file. Use cmake.config, cmake -DOPTION=VALUE, or ccmake to specify them.
option(USE_CUDA "Support CUDA GPU" ON)
option(USE_BACKTRACE "Print backtrace on exception and segmentation fault" ON)
option(BUILD_TEST "Build tests" ON)
cmake_dependent_option(BUILD_TEST_CORE "Build tests for core components" ON BUILD_TEST OFF)
cmake_dependent_option(BUILD_TEST_PET "Build tests for PET" OFF BUILD_TEST OFF)
cmake_dependent_option(BUILD_TEST_EINNET "Build tests for EINNET" OFF BUILD_TEST OFF)
@ -56,6 +58,14 @@ file(GLOB_RECURSE SRC src/*.cc src/*.cu)
add_library(InfiniTensor SHARED ${SRC})
if(USE_BACKTRACE)
add_definitions(-D BACKWARD_TRACE)
add_subdirectory(3rd-party/backward-cpp)
include_directories(3rd-party/backward-cpp)
add_backward(InfiniTensor)
target_link_libraries(InfiniTensor dw)
endif()
if(USE_CUDA)
# set(CUDA_HOST_COMPILER /home/spack/spack/opt/spack/linux-ubuntu22.04-broadwell/gcc-9.4.0/gcc-9.4.0-st36klijpsnquihiy463hmedsyhoc3g6/bin/gcc)
enable_language(CUDA)
@ -84,6 +94,9 @@ endfunction()
if(BUILD_TEST)
enable_testing()
if(USE_TRACE)
build_test(test/trace/*.cc)
endif()
if(BUILD_TEST_CORE)
build_test(test/core/*.cc)
build_test(test/operators/*.cc)

View File

@ -1,4 +1,5 @@
#pragma once
#include "utils/exception.h"
#include <cassert>
#include <functional>
#include <iostream>
@ -41,7 +42,7 @@ using HashType = uint64_t; // compatible with std::hash
#define _IT_ASSERT_2(name, info) \
(static_cast<bool>(name) \
? void(0) \
: throw std::runtime_error( \
: throw infini::Exception( \
std::string("[") + __FILE__ + ":" + std::to_string(__LINE__) + \
"] Assertion failed (" + #name + "): " + #info))
#define _IT_ASSERT_1(name) _IT_ASSERT_2(name, "");

12
include/utils/exception.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include <stdexcept>
#include <string>
namespace infini {
class Exception : public std::runtime_error {
public:
Exception(const std::string &msg);
};
} // namespace infini

View File

@ -21,4 +21,4 @@ double timeit(const std::function<void()> &func,
timingRounds;
}
} // namespace infini
} // namespace infini

26
src/utils/exception.cc Normal file
View File

@ -0,0 +1,26 @@
#include "utils/exception.h"
#ifdef BACKWARD_TRACE
#include "backward.hpp"
namespace backtrace = backward;
// signal handler
backtrace::SignalHandling sh;
namespace infini {
Exception::Exception(const std::string &msg) : std::runtime_error(msg) {
backtrace::StackTrace st;
st.load_here(32);
backtrace::Printer p;
p.print(st);
}
}; // namespace infini
#else
namespace infini {
Exception::Exception(const std::string &msg) : std::runtime_error(msg) {}
} // namespace infini
#endif

View File

@ -46,4 +46,4 @@ TEST(Graph, perf_engine) {
EXPECT_TRUE(matmul->getOutput()->equalData(ans));
}
} // namespace infini
} // namespace infini