[DIST] Enable multiple thread and tracker, make rabit and xgboost more thread-safe by using thread local variables.

This commit is contained in:
tqchen
2016-03-03 11:36:34 -08:00
parent 12dc92f7e0
commit e80d3db64b
17 changed files with 323 additions and 153 deletions

View File

@@ -4,12 +4,20 @@
* \brief Enable all kinds of global variables in common.
*/
#include "./random.h"
#include "./thread_local.h"
namespace xgboost {
namespace common {
/*! \brief thread local entry for random. */
struct RandomThreadLocalEntry {
/*! \brief the random engine instance. */
GlobalRandomEngine engine;
};
typedef ThreadLocalStore<RandomThreadLocalEntry> RandomThreadLocalStore;
GlobalRandomEngine& GlobalRandom() {
static GlobalRandomEngine inst;
return inst;
return RandomThreadLocalStore::Get()->engine;
}
}
} // namespace xgboost

View File

@@ -61,7 +61,8 @@ typedef RandomEngine GlobalRandomEngine;
/*!
* \brief global singleton of a random engine.
* Only use this engine when necessary, not thread-safe.
* This random engine is thread-local and
* only visible to current thread.
*/
GlobalRandomEngine& GlobalRandom(); // NOLINT(*)

View File

@@ -6,6 +6,8 @@
#ifndef XGBOOST_COMMON_THREAD_LOCAL_H_
#define XGBOOST_COMMON_THREAD_LOCAL_H_
#include <dmlc/base.h>
#if DMLC_ENABLE_STD_THREAD
#include <mutex>
#endif

View File

@@ -15,6 +15,7 @@
#include <utility>
#include <string>
#include <limits>
#include "../common/common.h"
namespace xgboost {
namespace gbm {
@@ -265,13 +266,11 @@ class GBTree : public GradientBooster {
inline void InitUpdater() {
if (updaters.size() != 0) return;
std::string tval = tparam.updater_seq;
char *pstr;
pstr = std::strtok(&tval[0], ",");
while (pstr != nullptr) {
std::unique_ptr<TreeUpdater> up(TreeUpdater::Create(pstr));
std::vector<std::string> ups = common::Split(tval, ',');
for (const std::string& pstr : ups) {
std::unique_ptr<TreeUpdater> up(TreeUpdater::Create(pstr.c_str()));
up->Init(this->cfg);
updaters.push_back(std::move(up));
pstr = std::strtok(nullptr, ",");
}
}
// do group specific group