Browse Source

feat(lite): add get last error code interface in lite c

GitOrigin-RevId: 280cc88092
tags/v1.9.0
Megvii Engine Team 3 years ago
parent
commit
597efed40b
5 changed files with 47 additions and 5 deletions
  1. +9
    -0
      lite/include/lite/common_enum_c.h
  2. +9
    -0
      lite/lite-c/include/lite-c/global_c.h
  3. +3
    -3
      lite/lite-c/include/lite-c/network_c.h
  4. +22
    -2
      lite/lite-c/src/global.cpp
  5. +4
    -0
      lite/test/test_tensor_c.cpp

+ 9
- 0
lite/include/lite/common_enum_c.h View File

@@ -22,6 +22,15 @@ typedef enum {
ERROR = 3, /*!< Print only errors */ ERROR = 3, /*!< Print only errors */
} LiteLogLevel; } LiteLogLevel;


/*!
* \brief The Error Code
*/
typedef enum {
OK = 0,
LITE_INTERNAL_ERROR = 1,
LITE_UNKNOWN_ERROR = 2,
} ErrorCode;

typedef enum { typedef enum {
LITE_DEFAULT = 0, //! default backend is mge LITE_DEFAULT = 0, //! default backend is mge
} LiteBackend; } LiteBackend;


+ 9
- 0
lite/lite-c/include/lite-c/global_c.h View File

@@ -23,6 +23,15 @@ extern "C" {
*/ */
LITE_API int LITE_get_version(int* major, int* minor, int* patch); LITE_API int LITE_get_version(int* major, int* minor, int* patch);


/*! \brief Get the last error code.
* \return the current error code
*/
LITE_API ErrorCode LITE_get_last_error_code();

/*! \brief Clear the last error code and error message.
*/
LITE_API void LITE_clear_last_error();

/*! \brief Get the last error message. /*! \brief Get the last error message.
* \return the message pointer * \return the message pointer
*/ */


+ 3
- 3
lite/lite-c/include/lite-c/network_c.h View File

@@ -75,7 +75,7 @@ extern "C" {
* mask 0b10: async if there are multiple comp nodes with * mask 0b10: async if there are multiple comp nodes with
* mask 0b100: always async * mask 0b100: always async
*/ */
typedef struct Options {
typedef struct {
int weight_preprocess; int weight_preprocess;
int fuse_preprocess; int fuse_preprocess;
int fake_next_exec; int fake_next_exec;
@@ -128,7 +128,7 @@ LITE_API LiteConfig* default_config();
* \brief config the network input and output item * \brief config the network input and output item
* *
*/ */
typedef struct LiteIO {
typedef struct {
//! the tensor name in the graph corresponding to the IO //! the tensor name in the graph corresponding to the IO
const char* name; const char* name;


@@ -157,7 +157,7 @@ extern LITE_API const LiteIO default_io;
* \brief the input and output information when load the network * \brief the input and output information when load the network
* the NetworkIO will remain in the network until the network is destroyed * the NetworkIO will remain in the network until the network is destroyed
*/ */
typedef struct LiteNetworkIO {
typedef struct {
LiteIO* inputs; LiteIO* inputs;
LiteIO* outputs; LiteIO* outputs;
size_t input_size; //! the number IO in inputs size_t input_size; //! the number IO in inputs


+ 22
- 2
lite/lite-c/src/global.cpp View File

@@ -17,10 +17,20 @@ namespace {
class ErrorMsg { class ErrorMsg {
public: public:
std::string& get_error_msg() { return error_msg; } std::string& get_error_msg() { return error_msg; }
void set_error_msg(const std::string& msg) { error_msg = msg; }
ErrorCode get_error_code() { return error_code; }
void set_error_msg(const std::string& msg, ErrorCode code) {
error_msg = msg + ", Error Code: " + std::to_string(code);
error_code = code;
}

void clear_error() {
error_code = ErrorCode::OK;
error_msg.clear();
}


private: private:
std::string error_msg; std::string error_msg;
ErrorCode error_code;
}; };


static LITE_MUTEX mtx_error; static LITE_MUTEX mtx_error;
@@ -32,10 +42,20 @@ ErrorMsg& get_global_error() {


int LiteHandleException(const std::exception& e) { int LiteHandleException(const std::exception& e) {
LITE_LOCK_GUARD(mtx_error); LITE_LOCK_GUARD(mtx_error);
get_global_error().set_error_msg(e.what());
get_global_error().set_error_msg(e.what(), ErrorCode::LITE_INTERNAL_ERROR);
return -1; return -1;
} }


ErrorCode LITE_get_last_error_code() {
LITE_LOCK_GUARD(mtx_error);
return get_global_error().get_error_code();
}

void LITE_clear_last_error() {
LITE_LOCK_GUARD(mtx_error);
get_global_error().clear_error();
}

const char* LITE_get_last_error() { const char* LITE_get_last_error() {
LITE_LOCK_GUARD(mtx_error); LITE_LOCK_GUARD(mtx_error);
return get_global_error().get_error_msg().c_str(); return get_global_error().get_error_msg().c_str();


+ 4
- 0
lite/test/test_tensor_c.cpp View File

@@ -56,7 +56,11 @@ TEST(TestCapiTensor, Basic) {
//! test error //! test error
ASSERT_EQ(LITE_is_pinned_host(c_tensor0, nullptr), -1); ASSERT_EQ(LITE_is_pinned_host(c_tensor0, nullptr), -1);
ASSERT_NE(strlen(LITE_get_last_error()), 0); ASSERT_NE(strlen(LITE_get_last_error()), 0);
ASSERT_EQ(LITE_get_last_error_code(), ErrorCode::LITE_INTERNAL_ERROR);
printf("The last error is: %s\n", LITE_get_last_error()); printf("The last error is: %s\n", LITE_get_last_error());
LITE_clear_last_error();
ASSERT_EQ(strlen(LITE_get_last_error()), 0);
ASSERT_EQ(LITE_get_last_error_code(), ErrorCode::OK);


LITE_destroy_tensor(c_tensor0); LITE_destroy_tensor(c_tensor0);
LITE_destroy_tensor(c_tensor1); LITE_destroy_tensor(c_tensor1);


Loading…
Cancel
Save