24 #ifndef MXNET_KVSTORE_H_ 25 #define MXNET_KVSTORE_H_ 29 #include <unordered_map> 34 #if MXNET_USE_DIST_KVSTORE 36 #endif // MXNET_USE_DIST_KVSTORE 83 virtual void Init(
const std::vector<int>& keys,
84 const std::vector<NDArray>& values) = 0;
90 virtual void Init(
const std::vector<std::string>& str_keys,
91 const std::vector<NDArray>& values) = 0;
128 virtual void Push(
const std::vector<int>& keys,
129 const std::vector<NDArray>& values,
130 int priority = 0) = 0;
138 virtual void Push(
const std::vector<std::string>& str_keys,
139 const std::vector<NDArray>& values,
140 int priority = 0) = 0;
164 virtual void Pull(
const std::vector<int>& keys,
165 const std::vector<NDArray*>& values,
166 int priority = 0) = 0;
173 virtual void Pull(
const std::vector<std::string>& str_keys,
174 const std::vector<NDArray*>& values,
175 int priority = 0) = 0;
186 const std::vector<std::pair<NDArray*, NDArray>>& val_rowids,
187 int priority = 0) = 0;
197 virtual void PullRowSparse(
const std::vector<std::string>& str_keys,
198 const std::vector<std::pair<NDArray*, NDArray>>& val_rowids,
199 int priority = 0) = 0;
204 typedef std::function<void(int, const NDArray&, NDArray*)>
Updater;
208 typedef std::function<void(const std::string&, const NDArray&, NDArray*)>
StrUpdater;
219 CHECK(updater) <<
"invalid updater";
232 CHECK(updater) <<
"invalid updater";
244 static void InitPSEnv(
const std::unordered_map<std::string, std::string>& envs) {
245 #if MXNET_USE_DIST_KVSTORE 246 ps::Environment::Init(envs);
248 LOG(FATAL) <<
"compile with USE_DIST_KVSTORE=1 to init parameter server's environment";
249 #endif // MXNET_USE_DIST_KVSTORE 258 #if MXNET_USE_DIST_KVSTORE 259 const char* role_str = ps::Environment::Get()->find(
"DMLC_ROLE");
260 return (role_str ==
nullptr) || (!strcmp(role_str,
"worker"));
263 #endif // MXNET_USE_DIST_KVSTORE 272 #if MXNET_USE_DIST_KVSTORE 273 const char* role_str = ps::Environment::Get()->find(
"DMLC_ROLE");
274 return (role_str !=
nullptr) && (!strcmp(role_str,
"server"));
277 #endif // MXNET_USE_DIST_KVSTORE 281 #if MXNET_USE_DIST_KVSTORE 282 if (!
IsWorkerNode()) LOG(FATAL) <<
"barrier_before_exit takes effect only on worker nodes";
285 LOG(FATAL) <<
"compile with USE_DIST_KVSTORE=1 to enable barrier";
295 #if MXNET_USE_DIST_KVSTORE 296 const char* role_str = ps::Environment::Get()->find(
"DMLC_ROLE");
297 return (role_str !=
nullptr) && (!strcmp(role_str,
"scheduler"));
300 #endif // MXNET_USE_DIST_KVSTORE 357 typedef std::function<void(int, const std::string&)>
Controller;
372 virtual void RunServer(
const Controller& controller) { }
397 #endif // MXNET_KVSTORE_H_ distributed key-value store
Definition: kvstore.h:45
std::function< void(int, const NDArray &, NDArray *)> Updater
the prototype of user-defined updater
Definition: kvstore.h:204
namespace of mxnet
Definition: base.h:126
virtual int get_rank() const
Definition: kvstore.h:309
static KVStore * Create(const char *type="local")
Factory function to create a new KVStore.
virtual void set_updater(const StrUpdater &updater)
set an updater with string keys
Definition: kvstore.h:231
Updater updater_
the user-defined updater
Definition: kvstore.h:378
virtual void PullRowSparse(const std::vector< int > &str_keys, const std::vector< std::pair< NDArray *, NDArray >> &val_rowids, int priority=0)=0
pull a list of key-value pairs from the store. The NDArray pulled back will be in row_sparse storage ...
const std::string & type()
return the type
Definition: kvstore.h:65
virtual void Pull(const std::vector< int > &keys, const std::vector< NDArray * > &values, int priority=0)=0
pull a list of key-value pairs from the store
static bool IsSchedulerNode()
Definition: kvstore.h:294
virtual void Barrier()
global barrier among all worker machines
Definition: kvstore.h:339
static void InitPSEnv(const std::unordered_map< std::string, std::string > &envs)
initalize ps-lite environment variables
Definition: kvstore.h:244
virtual void Init(const std::vector< int > &keys, const std::vector< NDArray > &values)=0
Initialize a list of key-value pair to the store.
static bool IsWorkerNode()
Definition: kvstore.h:257
virtual ~KVStore()
virtual destructor
Definition: kvstore.h:48
void set_barrier_before_exit(const bool barrier_before_exit)
Definition: kvstore.h:280
StrUpdater str_updater_
the user-defined updater with string keys
Definition: kvstore.h:383
virtual int get_num_dead_node(int node_id, int timeout=60) const
Definition: kvstore.h:328
virtual void RunServer(const Controller &controller)
Run as server (or scheduler)
Definition: kvstore.h:372
std::function< void(const std::string &, const NDArray &, NDArray *)> StrUpdater
the prototype of user-defined updater with string keys
Definition: kvstore.h:208
virtual void Push(const std::vector< int > &keys, const std::vector< NDArray > &values, int priority=0)=0
push a list of key-value pairs into the store
virtual void SendCommandToServers(int cmd_id, const std::string &cmd_body)
Send a command to all server nodes.
Definition: kvstore.h:352
std::string type_
the kvstore type
Definition: kvstore.h:388
std::function< void(int, const std::string &)> Controller
the prototype of a server controller
Definition: kvstore.h:357
virtual void set_updater(const Updater &updater)
set an updater
Definition: kvstore.h:218
virtual int get_group_size() const
Definition: kvstore.h:316
std::atomic< bool > barrier_before_exit_
whether to do barrier when finalize
Definition: kvstore.h:393
static bool IsServerNode()
Definition: kvstore.h:271