Treelite
c_api_common.cc
Go to the documentation of this file.
1 
9 #include <treelite/logging.h>
10 #include <treelite/data.h>
11 #include <treelite/c_api_common.h>
12 #include <treelite/c_api_error.h>
13 
14 using namespace treelite;
15 
19  std::string ret_str;
20 };
21 
22 // define threadlocal store for returning information
24 
25 int TreeliteRegisterLogCallback(void (*callback)(const char*)) {
26  API_BEGIN();
28  registry->Register(callback);
29  API_END();
30 }
31 
33  const void* data, const char* data_type_str, const uint32_t* col_ind, const size_t* row_ptr,
34  size_t num_row, size_t num_col, DMatrixHandle* out) {
35  API_BEGIN();
36  TypeInfo data_type = GetTypeInfoByName(data_type_str);
37  std::unique_ptr<DMatrix> matrix
38  = CSRDMatrix::Create(data_type, data, col_ind, row_ptr, num_row, num_col);
39  *out = static_cast<DMatrixHandle>(matrix.release());
40  API_END();
41 }
42 
44  const void* data, const char* data_type_str, size_t num_row, size_t num_col,
45  const void* missing_value, DMatrixHandle* out) {
46  API_BEGIN();
47  TypeInfo data_type = GetTypeInfoByName(data_type_str);
48  std::unique_ptr<DMatrix> matrix
49  = DenseDMatrix::Create(data_type, data, missing_value, num_row, num_col);
50  *out = static_cast<DMatrixHandle>(matrix.release());
51  API_END();
52 }
53 
55  size_t* out_num_row,
56  size_t* out_num_col,
57  size_t* out_nelem) {
58  API_BEGIN();
59  const DMatrix* dmat = static_cast<DMatrix*>(handle);
60  *out_num_row = dmat->GetNumRow();
61  *out_num_col = dmat->GetNumCol();
62  *out_nelem = dmat->GetNumElem();
63  API_END();
64 }
65 
67  API_BEGIN();
68  delete static_cast<DMatrix*>(handle);
69  API_END();
70 }
#define API_BEGIN()
macro to guard beginning and end section of all functions
Definition: c_api_error.h:14
int TreeliteDMatrixFree(DMatrixHandle handle)
delete DMatrix from memory
Definition: c_api_common.cc:66
int TreeliteDMatrixCreateFromCSR(const void *data, const char *data_type_str, const uint32_t *col_ind, const size_t *row_ptr, size_t num_row, size_t num_col, DMatrixHandle *out)
create DMatrix from a (in-memory) CSR matrix
Definition: c_api_common.cc:32
Input data structure of Treelite.
logging facility for Treelite
TypeInfo
Types used by thresholds and leaf outputs.
Definition: typeinfo.h:22
void * DMatrixHandle
handle to a data matrix
Definition: c_api_common.h:30
entry to to easily hold returning information
Definition: c_api_common.cc:17
Helper class for thread-local storage.
Error handling for C API.
int TreeliteRegisterLogCallback(void(*callback)(const char *))
register callback function for LOG(INFO) messages – helpful messages that are not errors...
Definition: c_api_common.cc:25
TypeInfo GetTypeInfoByName(const std::string &str)
conversion table from string to TypeInfo, defined in tables.cc
Definition: typeinfo.cc:16
C API of Treelite, used for interfacing with other languages This header is used by both the runtime ...
int TreeliteDMatrixGetDimension(DMatrixHandle handle, size_t *out_num_row, size_t *out_num_col, size_t *out_nelem)
get dimensions of a DMatrix
Definition: c_api_common.cc:54
A thread-local storage.
Definition: thread_local.h:17
std::string ret_str
result holder for returning string
Definition: c_api_common.cc:19
int TreeliteDMatrixCreateFromMat(const void *data, const char *data_type_str, size_t num_row, size_t num_col, const void *missing_value, DMatrixHandle *out)
create DMatrix from a (in-memory) dense matrix
Definition: c_api_common.cc:43
#define API_END()
every function starts with API_BEGIN(); and finishes with API_END() or API_END_HANDLE_ERROR ...
Definition: c_api_error.h:17