ParallelNeuralNetwork.h 3.2 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
/* 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 = "",
37 38
                        NeuralNetwork *rootNetwork = nullptr)
      : NeuralNetwork(subModelName, rootNetwork) {}
Z
zhangjinchao01 已提交
39 40

  virtual void init(
41 42 43 44 45 46
      const ModelConfig &config,
      ParamInitCallback callback = nullptr,
      const std::vector<ParameterType>
          &parameterTypes = std::vector<ParameterType>{PARAMETER_VALUE,
                                                       PARAMETER_GRADIENT,
                                                       PARAMETER_MOMENTUM},
Z
zhangjinchao01 已提交
47 48 49
      bool useGpu = FLAGS_use_gpu);

  virtual void forward(const std::vector<Argument> &inArgs,
50 51
                       std::vector<Argument> *outArgs,
                       PassType passType);
Z
zhangjinchao01 已提交
52 53 54 55

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

  void forwardBackward(const std::vector<Argument> &inArgs,
56 57
                       std::vector<Argument> *outArgs,
                       PassType passType,
Z
zhangjinchao01 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71
                       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_;
72 73
  /// number of gpu devices
  int numDevices_;
Z
zhangjinchao01 已提交
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
  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:
103 104 105 106
  /// from 0 to threads-1
  int threadId_;
  /// the GPU device Id which the computeThread_ used
  int deviceId_;
Z
zhangjinchao01 已提交
107 108
  bool useGpu_;
  std::unique_ptr<std::thread> computeThread_;
109 110
  /// whether the thread should stop
  bool stopping_;
Z
zhangjinchao01 已提交
111 112 113 114
  UpdateCallback backwardCallback_;
  PassType passType_;
};
}  // namespace paddle