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 PushPull(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 PushPull(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;
236  virtual void PullRowSparse(const std::vector<int>& str_keys,
237  const std::vector<std::pair<NDArray*, NDArray>>& val_rowids,
238  int priority = 0) = 0;
239 
248  virtual void PullRowSparse(const std::vector<std::string>& str_keys,
249  const std::vector<std::pair<NDArray*, NDArray>>& val_rowids,
250  int priority = 0) = 0;
251 
255  typedef std::function<void(int, const NDArray&, NDArray*)> Updater;
259  typedef std::function<void(const std::string&, const NDArray&, NDArray*)> StrUpdater;
269  virtual void set_updater(const Updater& updater) {
270  CHECK(updater) << "invalid updater";
271  updater_ = updater;
272  }
273 
283  virtual void set_updater(const StrUpdater& updater) {
284  CHECK(updater) << "invalid updater";
285  str_updater_ = updater;
286  }
287 
288  /******************************************************
289  * the following are used for multi-machines.
290  ******************************************************/
291 
296  static void InitPSEnv(const std::unordered_map<std::string, std::string>& envs) {
297 #if MXNET_USE_DIST_KVSTORE
298  ps::Environment::Init(envs);
299 #else
300  LOG(FATAL) << "compile with USE_DIST_KVSTORE=1 to init parameter server's environment";
301 #endif // MXNET_USE_DIST_KVSTORE
302  }
303 
309  static bool IsWorkerNode() {
310 #if MXNET_USE_DIST_KVSTORE
311  const char* role_str = ps::Environment::Get()->find("DMLC_ROLE");
312  return (role_str == nullptr) || (!strcmp(role_str, "worker"));
313 #else
314  return true;
315 #endif // MXNET_USE_DIST_KVSTORE
316  }
317 
323  static bool IsServerNode() {
324 #if MXNET_USE_DIST_KVSTORE
325  const char* role_str = ps::Environment::Get()->find("DMLC_ROLE");
326  return (role_str != nullptr) && (!strcmp(role_str, "server"));
327 #else
328  return false;
329 #endif // MXNET_USE_DIST_KVSTORE
330  }
331 
332  void set_barrier_before_exit(const bool barrier_before_exit) {
333 #if MXNET_USE_DIST_KVSTORE
334  if (!IsWorkerNode()) LOG(FATAL) << "barrier_before_exit takes effect only on worker nodes";
335  barrier_before_exit_ = barrier_before_exit;
336 #else
337  LOG(FATAL) << "compile with USE_DIST_KVSTORE=1 to enable barrier";
338 #endif
339  }
340 
346  static bool IsSchedulerNode() {
347 #if MXNET_USE_DIST_KVSTORE
348  const char* role_str = ps::Environment::Get()->find("DMLC_ROLE");
349  return (role_str != nullptr) && (!strcmp(role_str, "scheduler"));
350 #else
351  return false;
352 #endif // MXNET_USE_DIST_KVSTORE
353  }
354 
361  virtual int get_rank() const {
362  return 0;
363  }
364 
368  virtual int get_group_size() const {
369  return 1;
370  }
371 
380  virtual int get_num_dead_node(int node_id, int timeout = 60) const {
381  return 0;
382  }
383 
391  virtual void Barrier() { }
392 
404  virtual void SendCommandToServers(int cmd_id, const std::string& cmd_body) { }
405 
413  const std::string& params) {
414  LOG(INFO) << "Unable to pass server the profiler command. If you are using "
415  << "distributed kvstore, you need to compile with USE_DIST_KVSTORE=1."
416  << "If you are training on single machine, then there is no server process"
417  << "to profile. Please profile the worker process instead.";
418  }
419 
423  typedef std::function<void(int, const std::string&)> Controller;
424 
438  virtual void RunServer(const Controller& controller) { }
439 
440  protected:
444  Updater updater_;
445 
449  StrUpdater str_updater_;
450 
454  std::string type_;
455 
460  std::shared_ptr<kvstore::GradientCompression> gradient_compression_;
461 
465  std::atomic<bool> barrier_before_exit_{true};
466 };
467 
468 } // namespace mxnet
469 #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:255
namespace of mxnet
Definition: base.h:89
virtual int get_rank() const
Definition: kvstore.h:361
virtual void set_updater(const StrUpdater &updater)
set an updater with string keys
Definition: kvstore.h:283
Updater updater_
the user-defined updater
Definition: kvstore.h:444
const std::string & type()
return the type
Definition: kvstore.h:79
static bool IsSchedulerNode()
Definition: kvstore.h:346
virtual void Barrier()
global barrier among all worker machines
Definition: kvstore.h:391
static void InitPSEnv(const std::unordered_map< std::string, std::string > &envs)
initalize ps-lite environment variables
Definition: kvstore.h:296
static bool IsWorkerNode()
Definition: kvstore.h:309
virtual ~KVStore()
virtual destructor
Definition: kvstore.h:62
void set_barrier_before_exit(const bool barrier_before_exit)
Definition: kvstore.h:332
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:412
StrUpdater str_updater_
the user-defined updater with string keys
Definition: kvstore.h:449
virtual int get_num_dead_node(int node_id, int timeout=60) const
Definition: kvstore.h:380
std::shared_ptr< kvstore::GradientCompression > gradient_compression_
Gradient compression object starts with GC_NONE mode Used if SetGradientCompression sets the type...
Definition: kvstore.h:460
virtual void RunServer(const Controller &controller)
Run as server (or scheduler)
Definition: kvstore.h:438
std::function< void(const std::string &, const NDArray &, NDArray *)> StrUpdater
the prototype of user-defined updater with string keys
Definition: kvstore.h:259
virtual void SendCommandToServers(int cmd_id, const std::string &cmd_body)
Send a command to all server nodes.
Definition: kvstore.h:404
std::string type_
the kvstore type
Definition: kvstore.h:454
std::function< void(int, const std::string &)> Controller
the prototype of a server controller
Definition: kvstore.h:423
virtual void set_updater(const Updater &updater)
set an updater
Definition: kvstore.h:269
virtual int get_group_size() const
Definition: kvstore.h:368
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:323