mxnet
kvstore.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
25 #ifndef MXNET_KVSTORE_H_
26 #define MXNET_KVSTORE_H_
27 #include <dmlc/io.h>
28 #include <vector>
29 #include <utility>
30 #include <unordered_map>
31 #include <string>
32 #include <functional>
33 #include <atomic>
34 #include "../../src/kvstore/gradient_compression.h"
35 #include "./ndarray.h"
36 #if MXNET_USE_DIST_KVSTORE
37 #include "ps/ps.h"
38 #endif // MXNET_USE_DIST_KVSTORE
39 
40 namespace mxnet {
41 
51 };
52 
59 class KVStore {
60  public:
62  virtual ~KVStore() {}
63 
74  static KVStore *Create(const char *type = "local");
75 
79  inline const std::string& type() { return type_; }
80 
86  virtual void SetGradientCompression(const std::vector<std::pair<std::string, std::string> >
87  & kwargs) = 0;
88 
105  virtual void Init(const std::vector<int>& keys,
106  const std::vector<NDArray>& values) = 0;
112  virtual void Init(const std::vector<std::string>& str_keys,
113  const std::vector<NDArray>& values) = 0;
150  virtual void Push(const std::vector<int>& keys,
151  const std::vector<NDArray>& values,
152  int priority = 0) = 0;
153 
160  virtual void Push(const std::vector<std::string>& str_keys,
161  const std::vector<NDArray>& values,
162  int priority = 0) = 0;
187  virtual void Pull(const std::vector<int>& keys,
188  const std::vector<NDArray*>& values,
189  int priority = 0, bool ignore_sparse = true) = 0;
197  virtual void Pull(const std::vector<std::string>& str_keys,
198  const std::vector<NDArray*>& values,
199  int priority = 0, bool ignore_sparse = true) = 0;
200 
209  virtual void Broadcast(const std::vector<int>& vkeys,
210  const std::vector<int>& okeys,
211  const std::vector<NDArray>& values,
212  const std::vector<NDArray*>& outs,
213  int priority = 0) = 0;
214 
223  virtual void Broadcast(const std::vector<std::string>& str_vkeys,
224  const std::vector<std::string>& str_okeys,
225  const std::vector<NDArray>& values,
226  const std::vector<NDArray*>& outs,
227  int priority = 0) = 0;
228 
237  virtual void PushPull(const std::vector<int>& vkeys,
238  const std::vector<int>& okeys,
239  const std::vector<NDArray>& values,
240  const std::vector<NDArray*>& outs,
241  int priority = 0) = 0;
242 
251  virtual void PushPull(const std::vector<std::string>& str_vkeys,
252  const std::vector<std::string>& str_okeys,
253  const std::vector<NDArray>& values,
254  const std::vector<NDArray*>& outs,
255  int priority = 0) = 0;
264  virtual void PullRowSparse(const std::vector<int>& str_keys,
265  const std::vector<std::pair<NDArray*, NDArray>>& val_rowids,
266  int priority = 0) = 0;
267 
276  virtual void PullRowSparse(const std::vector<std::string>& str_keys,
277  const std::vector<std::pair<NDArray*, NDArray>>& val_rowids,
278  int priority = 0) = 0;
279 
283  typedef std::function<void(int, const NDArray&, NDArray*)> Updater;
287  typedef std::function<void(const std::string&, const NDArray&, NDArray*)> StrUpdater;
297  virtual void set_updater(const Updater& updater) {
298  CHECK(updater) << "invalid updater";
299  updater_ = updater;
300  }
301 
311  virtual void set_updater(const StrUpdater& updater) {
312  CHECK(updater) << "invalid updater";
313  str_updater_ = updater;
314  }
315 
316  /******************************************************
317  * the following are used for multi-machines.
318  ******************************************************/
319 
324  static void InitPSEnv(const std::unordered_map<std::string, std::string>& envs) {
325 #if MXNET_USE_DIST_KVSTORE
326  ps::Environment::Init(envs);
327 #else
328  LOG(FATAL) << "compile with USE_DIST_KVSTORE=1 to init parameter server's environment";
329 #endif // MXNET_USE_DIST_KVSTORE
330  }
331 
337  static bool IsWorkerNode() {
338 #if MXNET_USE_DIST_KVSTORE
339  const char* role_str = ps::Environment::Get()->find("DMLC_ROLE");
340  return (role_str == nullptr) || (!strcmp(role_str, "worker"));
341 #else
342  return true;
343 #endif // MXNET_USE_DIST_KVSTORE
344  }
345 
351  static bool IsServerNode() {
352 #if MXNET_USE_DIST_KVSTORE
353  const char* role_str = ps::Environment::Get()->find("DMLC_ROLE");
354  return (role_str != nullptr) && (!strcmp(role_str, "server"));
355 #else
356  return false;
357 #endif // MXNET_USE_DIST_KVSTORE
358  }
359 
360  void set_barrier_before_exit(const bool barrier_before_exit) {
361 #if MXNET_USE_DIST_KVSTORE
362  if (!IsWorkerNode()) LOG(FATAL) << "barrier_before_exit takes effect only on worker nodes";
363  barrier_before_exit_ = barrier_before_exit;
364 #else
365  LOG(FATAL) << "compile with USE_DIST_KVSTORE=1 to enable barrier";
366 #endif
367  }
368 
374  static bool IsSchedulerNode() {
375 #if MXNET_USE_DIST_KVSTORE
376  const char* role_str = ps::Environment::Get()->find("DMLC_ROLE");
377  return (role_str != nullptr) && (!strcmp(role_str, "scheduler"));
378 #else
379  return false;
380 #endif // MXNET_USE_DIST_KVSTORE
381  }
382 
389  virtual int get_rank() const {
390  return 0;
391  }
392 
396  virtual int get_group_size() const {
397  return 1;
398  }
399 
408  virtual int get_num_dead_node(int node_id, int timeout = 60) const {
409  return 0;
410  }
411 
419  virtual void Barrier() { }
420 
432  virtual void SendCommandToServers(int cmd_id, const std::string& cmd_body) { }
433 
441  const std::string& params) {
442  LOG(INFO) << "Unable to pass server the profiler command. If you are using "
443  << "distributed kvstore, you need to compile with USE_DIST_KVSTORE=1."
444  << "If you are training on single machine, then there is no server process"
445  << "to profile. Please profile the worker process instead.";
446  }
447 
451  typedef std::function<void(int, const std::string&)> Controller;
452 
466  virtual void RunServer(const Controller& controller) { }
467 
468  protected:
472  Updater updater_;
473 
477  StrUpdater str_updater_;
478 
482  std::string type_;
483 
488  std::shared_ptr<kvstore::GradientCompression> gradient_compression_;
489 
493  std::atomic<bool> barrier_before_exit_{true};
494 };
495 
496 } // namespace mxnet
497 #endif // MXNET_KVSTORE_H_
distributed key-value store
Definition: kvstore.h:59
std::function< void(int, const NDArray &, NDArray *)> Updater
the prototype of user-defined updater
Definition: kvstore.h:283
namespace of mxnet
Definition: api_registry.h:33
virtual int get_rank() const
Definition: kvstore.h:389
virtual void set_updater(const StrUpdater &updater)
set an updater with string keys
Definition: kvstore.h:311
Updater updater_
the user-defined updater
Definition: kvstore.h:472
const std::string & type()
return the type
Definition: kvstore.h:79
static bool IsSchedulerNode()
Definition: kvstore.h:374
virtual void Barrier()
global barrier among all worker machines
Definition: kvstore.h:419
static void InitPSEnv(const std::unordered_map< std::string, std::string > &envs)
initalize ps-lite environment variables
Definition: kvstore.h:324
static bool IsWorkerNode()
Definition: kvstore.h:337
virtual ~KVStore()
virtual destructor
Definition: kvstore.h:62
void set_barrier_before_exit(const bool barrier_before_exit)
Definition: kvstore.h:360
virtual void SetServerProfilerCommand(const KVStoreServerProfilerCommand type, const std::string &params)
Sends server profiler commands to all server nodes Only the worker with rank=0 sends the command whic...
Definition: kvstore.h:440
StrUpdater str_updater_
the user-defined updater with string keys
Definition: kvstore.h:477
virtual int get_num_dead_node(int node_id, int timeout=60) const
Definition: kvstore.h:408
std::shared_ptr< kvstore::GradientCompression > gradient_compression_
Gradient compression object starts with GC_NONE mode Used if SetGradientCompression sets the type...
Definition: kvstore.h:488
virtual void RunServer(const Controller &controller)
Run as server (or scheduler)
Definition: kvstore.h:466
std::function< void(const std::string &, const NDArray &, NDArray *)> StrUpdater
the prototype of user-defined updater with string keys
Definition: kvstore.h:287
virtual void SendCommandToServers(int cmd_id, const std::string &cmd_body)
Send a command to all server nodes.
Definition: kvstore.h:432
std::string type_
the kvstore type
Definition: kvstore.h:482
std::function< void(int, const std::string &)> Controller
the prototype of a server controller
Definition: kvstore.h:451
virtual void set_updater(const Updater &updater)
set an updater
Definition: kvstore.h:297
virtual int get_group_size() const
Definition: kvstore.h:396
KVStoreServerProfilerCommand
enum to denote types of commands kvstore sends to server regarding profiler kSetConfig sets profiler ...
Definition: kvstore.h:49
static bool IsServerNode()
Definition: kvstore.h:351