ParallelNeuralNetwork.h 3.1 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */


#pragma once

#include "NeuralNetwork.h"

namespace paddle {

class ParallelThread;

enum TaskType {
  TASK_FORWARD = 0,
  TASK_BACKWARD = 1,
  TASK_END_LAYER = 2,
  TASK_THREAD_FINISH = 3,
};

/**
 * A ParallelNeuralNetwork is capable of calculating a neural network through
 * multiple threads in parallel.
 */
class ParallelNeuralNetwork : public NeuralNetwork {
public:
  ParallelNeuralNetwork(std::string subModelName = "",
      NeuralNetwork* rootNetwork = nullptr)
    : NeuralNetwork(subModelName, rootNetwork) {}

  virtual void init(
      const ModelConfig &config, ParamInitCallback callback = nullptr,
      const std::vector<ParameterType> &
          parameterTypes = std::vector<ParameterType>{PARAMETER_VALUE,
                                                      PARAMETER_GRADIENT,
                                                      PARAMETER_MOMENTUM},
      bool useGpu = FLAGS_use_gpu);

  virtual void forward(const std::vector<Argument> &inArgs,
                       std::vector<Argument> *outArgs, PassType passType);

  virtual void backward(const UpdateCallback &callback = nullptr);

  void forwardBackward(const std::vector<Argument> &inArgs,
                       std::vector<Argument> *outArgs, PassType passType,
                       const UpdateCallback &callback = NULL);

  virtual void start(const TrainerConfig &config, DataProviderPtr dataProvider);

  void addComputeThread(int deviceId);

  void dispatchByDeviceId(int deviceId, LayerPtr layer, TaskType task);

  void waitAllThread();

  // virtual void eval(Evaluator* evaluator);

protected:
  bool useGpu_;
  int numDevices_; /* number of gpu devices */
  std::vector<std::unique_ptr<ParallelThread>> threads_;
};

class ParallelThread {
public:
  ParallelThread(int threadId, int deviceId, bool useGpu);
  ~ParallelThread();
  void jobEnqueue(LayerPtr layer, TaskType task);
  void start();
  void stop();
  int getDeviceId() const { return deviceId_; }

  void setBackwardCallback(const UpdateCallback &callback) {
    backwardCallback_ = callback;
  }
  void setForwardPassType(PassType passType) { passType_ = passType; }

protected:
  void computeThread();

public:
  struct Job {
    LayerPtr layer_;
    TaskType task_;
  };
  typedef Queue<Job> JobQueue;
  JobQueue queue_;

protected:
  int threadId_;  // from 0 to #threads-1
  int deviceId_;  // the GPU device Id which the computeThread_ used
  bool useGpu_;
  std::unique_ptr<std::thread> computeThread_;
  bool stopping_;  // whether the thread should stop
  UpdateCallback backwardCallback_;
  PassType passType_;
};
}  // namespace paddle