executor.h 4.2 KB
Newer Older
Q
qijun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2016 PaddlePaddle Authors. 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

D
dzhwinter 已提交
17 18 19
#include <map>
#include <unordered_map>

Y
Yang Yang 已提交
20
#include "paddle/framework/op_info.h"
21
#include "paddle/framework/program_desc.h"
Q
qijun 已提交
22 23
#include "paddle/framework/scope.h"
#include "paddle/framework/tensor.h"
D
dzhwinter 已提交
24
#include "paddle/platform/device_context.h"
Q
qijun 已提交
25 26 27 28

namespace paddle {
namespace framework {

D
dzhwinter 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42
class DeviceContextPool {
 public:
  static DeviceContextPool& Get() {
    PADDLE_ENFORCE_NOT_NULL(pool, "Need to Create DeviceContextPool first!");
    return *pool;
  }

  static DeviceContextPool& Create(const std::vector<platform::Place>& places) {
    if (pool == nullptr) {
      pool = new DeviceContextPool(places);
    }
    return *pool;
  }

D
dzhwinter 已提交
43 44 45 46 47 48 49 50 51 52
  const platform::DeviceContext* Borrow(const platform::Place& place) {
    auto range = device_contexts_.equal_range(place);
    if (range.first == range.second) {
      PADDLE_THROW(
          "'Place' is not supported, Please re-compile with WITH_GPU "
          "option");
    }
    return range.first->second;
  }

D
dzhwinter 已提交
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
  std::vector<const platform::DeviceContext*> Borrow(
      const std::vector<platform::Place>& places) {
    PADDLE_ENFORCE_GT(places.size(), 0);
    PADDLE_ENFORCE_LE(places.size(), device_contexts_.size());
    std::vector<const platform::DeviceContext*> borrowed_contexts;
    for (auto& place : places) {
      auto range = device_contexts_.equal_range(place);
      if (range.first == range.second) {
        PADDLE_THROW(
            "'Place' is not supported, Please re-compile with WITH_GPU "
            "option");
      }
      // TODO(dzhwinter) : assign the first found device. Will enhanced later.
      // device load balancer maybe useful here.
      borrowed_contexts.emplace_back(range.first->second);
    }
    return borrowed_contexts;
  }

  explicit DeviceContextPool(const std::vector<platform::Place>& places) {
    PADDLE_ENFORCE_GT(places.size(), 0);
    for (size_t i = 0; i < places.size(); i++) {
      if (platform::is_cpu_place(places[i])) {
        device_contexts_.emplace(
            places[i], new platform::CPUDeviceContext(
                           boost::get<platform::CPUPlace>(places[i])));
      } else if (platform::is_gpu_place(places[i])) {
#ifdef PADDLE_WITH_CUDA
        device_contexts_.emplace(
            places[i], new platform::CUDADeviceContext(
                           boost::get<platform::GPUPlace>(places[i])));
#else
        PADDLE_THROW(
            "'GPUPlace' is not supported, Please re-compile with WITH_GPU "
            "option");
#endif
      }
    }
  }

  ~DeviceContextPool() {}

 private:
  static DeviceContextPool* pool;
  struct Hash {
    std::hash<int> hash_;
    size_t operator()(const platform::Place& place) const {
      return hash_(place.which());
    }
  };
  std::unordered_multimap<const platform::Place, const platform::DeviceContext*,
                          Hash>
      device_contexts_;
  DISABLE_COPY_AND_ASSIGN(DeviceContextPool);
};

Q
qijun 已提交
109 110
class Executor {
 public:
D
dzhwinter 已提交
111 112 113 114 115 116 117
  // TODO(dzhwinter) : Do not rely on this function, it will be removed
  explicit Executor(const platform::DeviceContext& device)
      : Executor(std::vector<platform::Place>({device.GetPlace()})) {}

  explicit Executor(const platform::Place& place)
      : Executor(std::vector<platform::Place>({place})) {}

Q
qijun 已提交
118
  explicit Executor(const std::vector<platform::Place>& places);
Y
Yang Yang 已提交
119 120 121 122 123 124 125 126

  /* @Brief
   * Runtime evaluation of the given ProgramDesc under certain Scope
   *
   * @param
   *  ProgramDesc
   *  Scope
   */
127
  void Run(const ProgramDesc&, Scope*, int, bool create_local_scope = true,
T
typhoonzero 已提交
128
           bool create_vars = true);
Q
qijun 已提交
129

Q
qijun 已提交
130
 private:
Y
Yu Yang 已提交
131
  std::vector<const platform::DeviceContext*> device_contexts_;
Q
qijun 已提交
132
};
Q
qijun 已提交
133 134 135

}  // namespace framework
}  // namespace paddle