# rabit 的安装与使用

首先需要从 github 将这个项目的存储库克隆到本地目录中。

git clone https://github.com/dmlc/rabit.git

会在当前目录出现一个名为 rabit 的文件 之后从进入 rabit 文件,里边内容如下:

picture

产生 lib 文件

lib 文件夹原本是空的,在 lib 里 README.md 是这么写的:

This folder holds the library file generated by the compiler. To generate the library file, type make in the project root folder. If you want mpi compatible library, type make mpi

所以需要将文件进行编译,产生 lib 文件。

Makefile 文件已经存在 rabit 目录里了,我们只需要在 rabit 目录下输入 make 就可以了, 会出现如下库文件:

picture

这些文件的介绍如下:

List of Files

  • rabit.a The rabit package library Normally you need to link with this one
  • rabit_mock.a The rabit package library with mock test This library allows additional mock-test
  • rabit_mpi.a The MPI backed library
    1. Link against this library makes the program use MPI Allreduce
    2. This library is not fault-tolerant
  • rabit_empty.a Dummy package implementation
    1. This is an empty library that does not provide anything
    2. Only introduced to minimize code dependency for projects that only need single machine code

设置环境变量

库文件生成之后,需要添加环境变量 CPLUS_INCLUDE_PATHLIBRARY_PATHLD_LIBRARY_PATH

  • C_INCLUDE_PATH 为头文件的搜索路径
  • LIBRARY_PATH 为静态库搜索路径(编译时包含)
  • LD_LIBRARY_PATH 为动态库搜索路径(链接时包含)

这三项分别设置为:

export CPLUS_INCLUDE_PATH=/home/.../rabit/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=/home/.../rabit/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/home/.../rabit/lib:\$LD_LIBRARY_PATH
1
2
3

三种方法如下:

  1. 修改 profile 文件:
vi /etc/profile
1

在文档末尾写入

export CPLUS_INCLUDE_PATH=/home/.../rabit/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=/home/.../rabit/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/home/.../rabit/lib:\$LD_LIBRARY_PATH
1
2
3
  1. 修改.bashrc 文件:
vi ~/.bashrc
1

在文档末尾写入

export CPLUS_INCLUDE_PATH=/home/.../rabit/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=/home/.../rabit/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/home/.../rabit/lib:\$LD_LIBRARY_PATH
1
2
3
  1. 在 Terminal 命令行窗口中直接使用 export 命令,临时写入
export CPLUS_INCLUDE_PATH=/home/.../rabit/include:$CPLUS_INCLUDE_PATH
export LIBRARY_PATH=/home/.../rabit/lib:$LIBRARY_PATH
export LD_LIBRARY_PATH=/home/.../rabit/lib:\$LD_LIBRARY_PATH
1
2
3

设置完成之后测试是否成功: 编写测试文件 rabittest.cpp

/_ rabittest.cpp _/
#include <iostream>
#include <rabit.h>
int main(int argc,char \*argv[]) {
std::cout << "hellorabit\n";
rabit::Init(argc, argv);
std::cout << rabit::GetRank();
std::cout << "\n";
return(0);
}
1
2
3
4
5
6
7
8
9
10

使用如下命令编译:

g++ -o rabittest rabittest.cpp -lrabit -std=gnu++0x
1

如果成功, 会在当前目录出现一个可执行文件

picture

之后使用./rabittest 测试即可。 测试程序运行结果:

picture

常见错误: 错误 1:rabit.h: No such file or directory

picture

环境变量设置出现错误,需要重新设置。 错误 2:error: ‘nullptr’ was not declared in this scope

picture

网上说是关于 c++11 的原因,在 g++命令后加上-std=gnu++0x 这一项就可以了