Treelite
c_api_common.cc
Go to the documentation of this file.
1 
8 #include <dmlc/thread_local.h>
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
23 using TreeliteAPIThreadLocalStore
24  = dmlc::ThreadLocalStore<TreeliteAPIThreadLocalEntry>;
25 
26 int TreeliteRegisterLogCallback(void (*callback)(const char*)) {
27  API_BEGIN();
28  LogCallbackRegistry* registry = LogCallbackRegistryStore::Get();
29  registry->Register(callback);
30  API_END();
31 }
32 
34  const char* path, const char* format, const char* data_type, int nthread, int verbose,
35  DMatrixHandle* out) {
36  API_BEGIN();
37  std::unique_ptr<DMatrix> mat = CSRDMatrix::Create(path, format, data_type, nthread, verbose);
38  *out = static_cast<DMatrixHandle>(mat.release());
39  API_END();
40 }
41 
43  const void* data, const char* data_type_str, const uint32_t* col_ind, const size_t* row_ptr,
44  size_t num_row, size_t num_col, DMatrixHandle* out) {
45  API_BEGIN();
46  TypeInfo data_type = GetTypeInfoByName(data_type_str);
47  std::unique_ptr<DMatrix> matrix
48  = CSRDMatrix::Create(data_type, data, col_ind, row_ptr, num_row, num_col);
49  *out = static_cast<DMatrixHandle>(matrix.release());
50  API_END();
51 }
52 
54  const void* data, const char* data_type_str, size_t num_row, size_t num_col,
55  const void* missing_value, DMatrixHandle* out) {
56  API_BEGIN();
57  TypeInfo data_type = GetTypeInfoByName(data_type_str);
58  std::unique_ptr<DMatrix> matrix
59  = DenseDMatrix::Create(data_type, data, missing_value, num_row, num_col);
60  *out = static_cast<DMatrixHandle>(matrix.release());
61  API_END();
62 }
63 
65  size_t* out_num_row,
66  size_t* out_num_col,
67  size_t* out_nelem) {
68  API_BEGIN();
69  const DMatrix* dmat = static_cast<DMatrix*>(handle);
70  *out_num_row = dmat->GetNumRow();
71  *out_num_col = dmat->GetNumCol();
72  *out_nelem = dmat->GetNumElem();
73  API_END();
74 }
75 
77  API_BEGIN();
78  delete static_cast<DMatrix*>(handle);
79  API_END();
80 }
#define API_BEGIN()
macro to guard beginning and end section of all functions
Definition: c_api_error.h:15
int TreeliteDMatrixFree(DMatrixHandle handle)
delete DMatrix from memory
Definition: c_api_common.cc:76
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:42
Input data structure of Treelite.
int TreeliteDMatrixCreateFromFile(const char *path, const char *format, const char *data_type, int nthread, int verbose, DMatrixHandle *out)
create a sparse DMatrix from a file
Definition: c_api_common.cc:33
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
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:26
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:64
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:53
#define API_END()
every function starts with API_BEGIN(); and finishes with API_END() or API_END_HANDLE_ERROR ...
Definition: c_api_error.h:18