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 
16 int TreeliteRegisterLogCallback(void (*callback)(const char*)) {
17  API_BEGIN();
19  registry->Register(callback);
20  API_END();
21 }
22 
24  const void* data, const char* data_type_str, const uint32_t* col_ind, const size_t* row_ptr,
25  size_t num_row, size_t num_col, DMatrixHandle* out) {
26  API_BEGIN();
27  TypeInfo data_type = GetTypeInfoByName(data_type_str);
28  std::unique_ptr<DMatrix> matrix
29  = CSRDMatrix::Create(data_type, data, col_ind, row_ptr, num_row, num_col);
30  *out = static_cast<DMatrixHandle>(matrix.release());
31  API_END();
32 }
33 
35  const void* data, const char* data_type_str, size_t num_row, size_t num_col,
36  const void* missing_value, DMatrixHandle* out) {
37  API_BEGIN();
38  TypeInfo data_type = GetTypeInfoByName(data_type_str);
39  std::unique_ptr<DMatrix> matrix
40  = DenseDMatrix::Create(data_type, data, missing_value, num_row, num_col);
41  *out = static_cast<DMatrixHandle>(matrix.release());
42  API_END();
43 }
44 
46  size_t* out_num_row,
47  size_t* out_num_col,
48  size_t* out_nelem) {
49  API_BEGIN();
50  const DMatrix* dmat = static_cast<DMatrix*>(handle);
51  *out_num_row = dmat->GetNumRow();
52  *out_num_col = dmat->GetNumCol();
53  *out_nelem = dmat->GetNumElem();
54  API_END();
55 }
56 
58  API_BEGIN();
59  delete static_cast<DMatrix*>(handle);
60  API_END();
61 }
#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:57
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:23
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
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:16
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:45
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:34
#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