ParallelNeuralNetwork.h 3.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
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

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

Y
Yu Yang 已提交
40 41 42 43 44 45 46
  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);
Z
zhangjinchao01 已提交
47 48

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

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

  void forwardBackward(const std::vector<Argument> &inArgs,
55 56
                       std::vector<Argument> *outArgs,
                       PassType passType,
Z
zhangjinchao01 已提交
57 58
                       const UpdateCallback &callback = NULL);

59
  virtual void start();
Z
zhangjinchao01 已提交
60 61 62 63 64 65 66 67 68 69 70

  void addComputeThread(int deviceId);

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

  void waitAllThread();

  // virtual void eval(Evaluator* evaluator);

protected:
  bool useGpu_;
71 72
  /// number of gpu devices
  int numDevices_;
Z
zhangjinchao01 已提交
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
  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:
102 103 104 105
  /// from 0 to threads-1
  int threadId_;
  /// the GPU device Id which the computeThread_ used
  int deviceId_;
Z
zhangjinchao01 已提交
106 107
  bool useGpu_;
  std::unique_ptr<std::thread> computeThread_;
108 109
  /// whether the thread should stop
  bool stopping_;
Z
zhangjinchao01 已提交
110 111 112 113
  UpdateCallback backwardCallback_;
  PassType passType_;
};
}  // namespace paddle