Permalink
Browse files

refactor: librabit

  • Loading branch information...
1 parent 73b6e9b commit 7479791f6a3922bbca3187a3d712c3a1ebaa384a @tqchen tqchen committed Feb 27, 2016
View
@@ -22,17 +22,16 @@ BPATH=.
# objectives that makes up rabit library
MPIOBJ= $(BPATH)/engine_mpi.o
OBJ= $(BPATH)/allreduce_base.o $(BPATH)/allreduce_robust.o $(BPATH)/engine.o $(BPATH)/engine_empty.o $(BPATH)/engine_mock.o\
- $(BPATH)/rabit_wrapper.o $(BPATH)/engine_base.o
-SLIB= wrapper/librabit_wrapper.so wrapper/librabit_wrapper_mock.so wrapper/librabit_wrapper_mpi.so
+ $(BPATH)/c_api.o $(BPATH)/engine_base.o
+SLIB= lib/librabit.so lib/librabit_mpi.so lib/librabit_mock.so lib/librabit_base.so
ALIB= lib/librabit.a lib/librabit_mpi.a lib/librabit_empty.a lib/librabit_mock.a lib/librabit_base.a
-HEADERS=src/*.h include/*.h include/rabit/*.h
+HEADERS=src/*.h include/rabit/*.h include/rabit/internal/*.h
DMLC=dmlc-core
.PHONY: clean all install mpi python lint doc doxygen
-all: lib/librabit.a lib/librabit_mock.a wrapper/librabit_wrapper.so wrapper/librabit_wrapper_mock.so lib/librabit_base.a
-mpi: lib/librabit_mpi.a wrapper/librabit_wrapper_mpi.so
-python: wrapper/librabit_wrapper.so wrapper/librabit_wrapper_mock.so
+all: lib/librabit.a lib/librabit_mock.a lib/librabit.so lib/librabit_base.a lib/librabit_mock.so
+mpi: lib/librabit_mpi.a lib/librabit_mpi.so
$(BPATH)/allreduce_base.o: src/allreduce_base.cc $(HEADERS)
$(BPATH)/engine.o: src/engine.cc $(HEADERS)
@@ -41,17 +40,13 @@ $(BPATH)/engine_mpi.o: src/engine_mpi.cc $(HEADERS)
$(BPATH)/engine_empty.o: src/engine_empty.cc $(HEADERS)
$(BPATH)/engine_mock.o: src/engine_mock.cc $(HEADERS)
$(BPATH)/engine_base.o: src/engine_base.cc $(HEADERS)
+$(BPATH)/c_api.o: src/c_api.cc $(HEADERS)
-lib/librabit.a: $(BPATH)/allreduce_base.o $(BPATH)/allreduce_robust.o $(BPATH)/engine.o
-lib/librabit_base.a: $(BPATH)/allreduce_base.o $(BPATH)/engine_base.o
-lib/librabit_mock.a: $(BPATH)/allreduce_base.o $(BPATH)/allreduce_robust.o $(BPATH)/engine_mock.o
-lib/librabit_empty.a: $(BPATH)/engine_empty.o
-lib/librabit_mpi.a: $(MPIOBJ)
-# wrapper code
-$(BPATH)/rabit_wrapper.o: wrapper/rabit_wrapper.cc
-wrapper/librabit_wrapper.so: $(BPATH)/rabit_wrapper.o lib/librabit.a
-wrapper/librabit_wrapper_mock.so: $(BPATH)/rabit_wrapper.o lib/librabit_mock.a
-wrapper/librabit_wrapper_mpi.so: $(BPATH)/rabit_wrapper.o lib/librabit_mpi.a
+lib/librabit.a lib/librabit.so: $(BPATH)/allreduce_base.o $(BPATH)/allreduce_robust.o $(BPATH)/engine.o $(BPATH)/c_api.o
+lib/librabit_base.a lib/librabit_base.so: $(BPATH)/allreduce_base.o $(BPATH)/engine_base.o $(BPATH)/c_api.o
+lib/librabit_mock.a lib/librabit_mock.so: $(BPATH)/allreduce_base.o $(BPATH)/allreduce_robust.o $(BPATH)/engine_mock.o $(BPATH)/c_api.o
+lib/librabit_empty.a: $(BPATH)/engine_empty.o $(BPATH)/c_api.o
+lib/librabit_mpi.a lib/librabit_mpi.so: $(MPIOBJ)
$(OBJ) :
$(CXX) -c $(CFLAGS) -o $@ $(firstword $(filter %.cpp %.c %.cc, $^) )
@@ -66,11 +61,10 @@ $(SLIB) :
$(CXX) $(CFLAGS) -shared -o $@ $(filter %.cpp %.o %.c %.cc %.a, $^) $(LDFLAGS)
lint:
- $(DMLC)/scripts/lint.py rabit $(LINT_LANG) src include wrapper
+ $(DMLC)/scripts/lint.py rabit $(LINT_LANG) src include
doc doxygen:
cd include; doxygen ../doc/Doxyfile; cd -
clean:
- $(RM) $(OBJ) $(MPIOBJ) $(ALIB) $(MPIALIB) $(SLIB) *~ src/*~ include/*~ include/*/*~ wrapper/*~
-
+ $(RM) $(OBJ) $(MPIOBJ) $(ALIB) $(MPIALIB) $(SLIB) *~ src/*~ include/*~ include/*/*~
View
@@ -95,7 +95,7 @@ WARN_LOGFILE =
#---------------------------------------------------------------------------
# configuration options related to the input files
#---------------------------------------------------------------------------
-INPUT = . dmlc
+INPUT = rabit dmlc
INPUT_ENCODING = UTF-8
FILE_PATTERNS =
RECURSIVE = NO
View
@@ -7,7 +7,7 @@
import numpy as np
# import rabit, the tracker script will setup the lib path correctly
# for normal run without tracker script, add following line
-# sys.path.append(os.path.dirname(__file__) + '/../wrapper')
+# sys.path.append(os.path.dirname(__file__) + '/../python')
import rabit
rabit.init()
@@ -16,7 +16,7 @@
a = np.zeros(n)
for i in xrange(n):
a[i] = rank + i
-
+
print '@node[%d] before-allreduce: a=%s' % (rank, str(a))
a = rabit.allreduce(a, rabit.MAX)
print '@node[%d] after-allreduce-max: a=%s' % (rank, str(a))
View
@@ -1,7 +0,0 @@
-Library Header Files
-====
-* This folder contains all the header needed to use the library
-* To use it, add the "include" folder to the search path of the compiler
-* User only needs to know [rabit.h](rabit.h) and [rabit_serializable.h](rabit_serializable.h) in order to use the library
-* Folder [rabit](rabit) contains headers for internal engine and template's implementation
-* Not all .h files in the project are in the "include" folder, .h files that are internally used by the library remain at [src](../src)
View
@@ -0,0 +1,135 @@
+/*!
+ * Copyright by Contributors
+ * \file rabit_c_api.h
+ * \author Tianqi Chen
+ * \brief a C style API of rabit.
+ */
+#ifndef RABIT_C_API_H_
+#define RABIT_C_API_H_
+
+#ifdef __cplusplus
+#define RABIT_EXTERN_C extern "C"
+#endif
+
+#if defined(_MSC_VER) || defined(_WIN32)
+#define RABIT_DLL RABIT_EXTERN_C __declspec(dllexport)
+#else
+#define RABIT_DLL RABIT_EXTERN_C
+#endif
+
+// manually define unsign long
+typedef unsigned long rbt_ulong; // NOLINT(*)
+
+/*!
+ * \brief intialize the rabit module,
+ * call this once before using anything
+ * The additional arguments is not necessary.
+ * Usually rabit will detect settings
+ * from environment variables.
+ * \param argc number of arguments in argv
+ * \param argv the array of input arguments
+ */
+RABIT_DLL void RabitInit(int argc, char *argv[]);
+
+/*!
+ * \brief finalize the rabit engine,
+ * call this function after you finished all jobs.
+ */
+RABIT_DLL void RabitFinalize();
+
+/*! \brief get rank of current process */
+RABIT_DLL int RabitGetRank();
+
+/*! \brief get total number of process */
+RABIT_DLL int RabitGetWorldSize();
+
+/*!
+ * \brief print the msg to the tracker,
+ * this function can be used to communicate the information of the progress to
+ * the user who monitors the tracker
+ * \param msg the message to be printed
+ */
+RABIT_DLL void RabitTrackerPrint(const char *msg);
+/*!
+ * \brief get name of processor
+ * \param out_name hold output string
+ * \param out_len hold length of output string
+ * \param max_len maximum buffer length of input
+ */
+RABIT_DLL void RabitGetProcessorName(char *out_name,
+ rbt_ulong *out_len,
+ rbt_ulong max_len);
+/*!
+ * \brief broadcast an memory region to all others from root
+ *
+ * Example: int a = 1; Broadcast(&a, sizeof(a), root);
+ * \param sendrecv_data the pointer to send or recive buffer,
+ * \param size the size of the data
+ * \param root the root of process
+ */
+RABIT_DLL void RabitBroadcast(void *sendrecv_data,
+ rbt_ulong size, int root);
+/*!
+ * \brief perform in-place allreduce, on sendrecvbuf
+ * this function is NOT thread-safe
+ *
+ * Example Usage: the following code gives sum of the result
+ * vector<int> data(10);
+ * ...
+ * Allreduce<op::Sum>(&data[0], data.size());
+ * ...
+ * \param sendrecvbuf buffer for both sending and recving data
+ * \param count number of elements to be reduced
+ * \param enum_dtype the enumeration of data type, see rabit::engine::mpi::DataType in engine.h of rabit include
+ * \param enum_op the enumeration of operation type, see rabit::engine::mpi::OpType in engine.h of rabit
+ * \param prepare_fun Lazy preprocessing function, if it is not NULL, prepare_fun(prepare_arg)
+ * will be called by the function before performing Allreduce, to intialize the data in sendrecvbuf_.
+ * If the result of Allreduce can be recovered directly, then prepare_func will NOT be called
+ * \param prepare_arg argument used to passed into the lazy preprocessing function
+ */
+RABIT_DLL void RabitAllreduce(void *sendrecvbuf,
+ size_t count,
+ int enum_dtype,
+ int enum_op,
+ void (*prepare_fun)(void *arg),
+ void *prepare_arg);
+
+/*!
+ * \brief load latest check point
+ * \param out_global_model hold output of serialized global_model
+ * \param out_global_len the output length of serialized global model
+ * \param out_local_model hold output of serialized local_model, can be NULL
+ * \param out_local_len the output length of serialized local model, can be NULL
+ *
+ * \return the version number of check point loaded
+ * if returned version == 0, this means no model has been CheckPointed
+ * nothing will be touched
+ */
+RABIT_DLL int RabitLoadCheckPoint(char **out_global_model,
+ rbt_ulong *out_global_len,
+ char **out_local_model,
+ rbt_ulong *out_local_len);
+/*!
+ * \brief checkpoint the model, meaning we finished a stage of execution
+ * every time we call check point, there is a version number which will increase by one
+ *
+ * \param global_model hold content of serialized global_model
+ * \param global_len the content length of serialized global model
+ * \param local_model hold content of serialized local_model, can be NULL
+ * \param local_len the content length of serialized local model, can be NULL
+ *
+ * NOTE: local_model requires explicit replication of the model for fault-tolerance, which will
+ * bring replication cost in CheckPoint function. global_model do not need explicit replication.
+ * So only CheckPoint with global_model if possible
+ */
+RABIT_DLL void RabitCheckPoint(const char *global_model,
+ rbt_ulong global_len,
+ const char *local_model,
+ rbt_ulong local_len);
+/*!
+ * \return version number of current stored model,
+ * which means how many calls to CheckPoint we made so far
+ */
+RABIT_DLL int RabitVersionNumber();
+
+#endif // RABIT_C_API_H_
@@ -4,10 +4,10 @@
* \brief This file defines the core interface of rabit library
* \author Tianqi Chen, Nacho, Tianyi
*/
-#ifndef RABIT_ENGINE_H_
-#define RABIT_ENGINE_H_
+#ifndef RABIT_INTERNAL_ENGINE_H_
+#define RABIT_INTERNAL_ENGINE_H_
#include <string>
-#include "../rabit_serializable.h"
+#include "../serializable.h"
namespace MPI {
/*! \brief MPI data type just to be compatible with MPI reduce function*/
@@ -241,7 +241,8 @@ class ReduceHandle {
* \param prepare_arg argument used to pass into the lazy preprocessing function
*/
void Allreduce(void *sendrecvbuf,
- size_t type_nbytes, size_t count,
+ size_t type_nbytes,
+ size_t count,
IEngine::PreprocFunction prepare_fun = NULL,
void *prepare_arg = NULL);
/*! \return the number of bytes occupied by the type */
@@ -259,4 +260,4 @@ class ReduceHandle {
};
} // namespace engine
} // namespace rabit
-#endif // RABIT_ENGINE_H_
+#endif // RABIT_INTERNAL_ENGINE_H_
@@ -4,15 +4,15 @@
* \brief utilities with different serializable implementations
* \author Tianqi Chen
*/
-#ifndef RABIT_IO_H_
-#define RABIT_IO_H_
+#ifndef RABIT_INTERNAL_IO_H_
+#define RABIT_INTERNAL_IO_H_
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include "./utils.h"
-#include "../rabit_serializable.h"
+#include "../serializable.h"
namespace rabit {
namespace utils {
@@ -103,4 +103,4 @@ struct MemoryBufferStream : public SeekStream {
}; // class MemoryBufferStream
} // namespace utils
} // namespace rabit
-#endif // RABIT_IO_H_
+#endif // RABIT_INTERNAL_IO_H_
@@ -5,8 +5,8 @@
*
* \author Tianqi Chen
*/
-#ifndef RABIT_RABIT_INL_H_
-#define RABIT_RABIT_INL_H_
+#ifndef RABIT_INTERNAL_RABIT_INL_H_
+#define RABIT_INTERNAL_RABIT_INL_H_
// use engine for implementation
#include <vector>
#include <string>
@@ -325,4 +325,4 @@ inline void SerializeReducer<DType>::Allreduce(DType *sendrecvobj,
}
#endif
} // namespace rabit
-#endif // RABIT_RABIT_INL_H_
+#endif // RABIT_INTERNAL_RABIT_INL_H_
@@ -4,8 +4,8 @@
* \brief This file defines the utils for timing
* \author Tianqi Chen, Nacho, Tianyi
*/
-#ifndef RABIT_TIMER_H_
-#define RABIT_TIMER_H_
+#ifndef RABIT_INTERNAL_TIMER_H_
+#define RABIT_INTERNAL_TIMER_H_
#include <time.h>
#ifdef __MACH__
#include <mach/clock.h>
@@ -38,4 +38,4 @@ inline double GetTime(void) {
}
} // namespace utils
} // namespace rabit
-#endif // RABIT_TIMER_H_
+#endif // RABIT_INTERNAL_TIMER_H_
@@ -4,8 +4,8 @@
* \brief simple utils to support the code
* \author Tianqi Chen
*/
-#ifndef RABIT_UTILS_H_
-#define RABIT_UTILS_H_
+#ifndef RABIT_INTERNAL_UTILS_H_
+#define RABIT_INTERNAL_UTILS_H_
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <string>
@@ -188,4 +188,4 @@ inline const char* BeginPtr(const std::string &str) {
return &str[0];
}
} // namespace rabit
-#endif // RABIT_UTILS_H_
+#endif // RABIT_INTERNAL_UTILS_H_
Oops, something went wrong.

0 comments on commit 7479791

Please sign in to comment.