Xgboost: Create a tutorial for using the C API in a C/C++ application

Created on 27 Sep 2020  路  17Comments  路  Source: dmlc/xgboost

It would be great to have a tutorial for using the C API of XGBoost in a C or C++ application. Some important components:

  • How to configure the CMakeLists.txt of your application to link with XGBoost (either statically or dynamically)
  • How to install XGBoost library into a system prefix or a Conda environment.
  • Useful tips, such as remembering to clean up allocated XGBoost object handles, or exception handling with XGBGetLastError().
  • Link to the demo: https://github.com/dmlc/xgboost/blob/master/demo/c-api/c-api-demo.c

Note to new contributors: Post a comment here if you'd like to work on this issue. Feel free to ping me for help.

doc good first issue hacktoberfest

All 17 comments

@hcho3 hello, sir
I'm efficient in C++ and would like to work on this? I will update you as soon as possible. How I can connect with you for help & guidance( like slack/discord/discourse/ or the discussion forum of XGBoost)? I've read the contributing guidelines & coding guidlines in C++ provided in the XGBoost documentation. Could you please assign it to me? I've started working on it.

@divya661 Thanks for volunteering. You have two options:

  • Use "@hcho3" here, as I check GitHub frequently.
  • E-mail me at [email protected], if you'd like to set up a virtual meeting with me.

@hcho3 Sir, could you please suggest me a few resources regarding the first 2 points of this issue?

@divya661 Take a look at https://github.com/dmlc/xgboost/blob/6bc41df2fefe5a1205f75d6b9bfd4d331f0d025f/.github/workflows/main.yml#L62-L78, which is an automated script to test the C API example (https://github.com/dmlc/xgboost/blob/master/demo/c-api/c-api-demo.c).

@hcho3 in point 2, by "system prefix" you mean with different OS like Windows, Mac OS & others? (want to confirm)

@divya661 No, the prefix refers to a directory where a package is to be installed. The term is used in GNU Autotools, e.g. https://www.gnu.org/prep/standards/html_node/Directory-Variables.html. The system prefix on Linux is /usr/local. It's called "system" prefix because the prefix is set system-wide.

Since writing to /usr/local requires the administrative privilege, it is often convenient to install a package into another prefix directory that is different from the system prefix (/usr/local). Creating a Conda environment creates a new prefix directory inside the user's home directory (usually $HOME/miniconda3/envs/[env_name]). Since this prefix is inside the home directory, the user is able to install packages into it without needing a permission from the administrator.

@divya661 Any updates? If you'd like further assistance, please do let me know.

@hcho3 I'll try to finish it in the coming next 2-3 days & submit my PR, sir. I apologize for the long delay, as I got busy with some urgent work.

@divya661 Thanks for the update. No need to apologize, as we all have our responsibilities. Do let me know if you need assistance.

@hcho3 Sir, I think this file https://github.com/dmlc/xgboost/blob/master/demo/c-api/README.md tells about configuring the cmakelist.txt file to link xgboost with external C/C++ application. Do I need to extend it for static & dynamic linking?

@divya661 My suggestion is to add a tutorial in the directory https://github.com/dmlc/xgboost/tree/master/doc/tutorials. To render the docs, go to the doc directory and run:

# Create Conda environment, with necessary Python packages.
# You need to do this only once
conda env create -n docgen --file=./xgboost_doc.yml

# Now activate the environment, to access the Python package
conda activate docgen

# This command will use Sphinx package to generate the docs.
make html

@hcho3 In the tutorial, should I also provide small examples to use various functions available in C API other than useful tips?

Yes that would be great.

@hcho3 Sir, could you please give me more hints on useful tips (other than exception handling using XGBGetLastError() & cleaning up Xgboost object handles) to work on, I have currently implemented only these two in my draft?

For now, you should submit a pull request with what you have. We can always add more material later.

6285 @hcho3 Sir, I have created the PR for this task. Please inform me regarding the changes I need to make to improve it or correct my mistakes in it.

Addressed in #6285

Was this page helpful?
0 / 5 - 0 ratings