executor.cc 2.5 KB
Newer Older
Q
qijun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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. */

#include "paddle/framework/executor.h"
Y
Yang Yang 已提交
16
#include <iostream>
Q
qijun 已提交
17
#include <memory>
Y
Yang Yang 已提交
18
#include <vector>
Y
Yang Yang 已提交
19
#include "paddle/framework/lod_tensor.h"
Q
qijun 已提交
20
#include "paddle/framework/op_registry.h"
Q
qijun 已提交
21
#include "paddle/framework/scope.h"
Q
qijun 已提交
22 23 24 25

namespace paddle {
namespace framework {

Q
qijun 已提交
26
Executor::Executor(const std::vector<platform::Place>& places) {
Q
qijun 已提交
27
  device_contexts_.resize(places.size());
Q
qijun 已提交
28
  for (size_t i = 0; i < places.size(); i++) {
Q
qijun 已提交
29
    if (platform::is_cpu_place(places[i])) {
Q
qijun 已提交
30 31
      device_contexts_[i].reset(new platform::CPUDeviceContext(
          boost::get<platform::CPUPlace>(places[i])));
Q
qijun 已提交
32 33
    } else {
#ifndef PADDLE_ONLY_CPU
Q
qijun 已提交
34 35
      device_contexts_[i].reset(new platform::CUDADeviceContext(
          boost::get<platform::CPUPlace>(places[i])));
Q
qijun 已提交
36 37 38 39
#else
      PADDLE_THROW("'GPUPlace' is not supported in CPU only device.");
#endif
    }
Q
qijun 已提交
40 41 42
  }
}

Q
qijun 已提交
43 44
void Executor::Run(const ProgramDesc& pdesc, Scope* scope,
                   std::vector<Tensor>* outputs) {
Y
Yang Yang 已提交
45 46 47
  // TODO(tonyyang-svail):
  //    - only runs the first block
  //    - only runs on the first device
Y
Yang Yang 已提交
48
  //    - test on gpu
Y
Yang Yang 已提交
49
  auto& block = pdesc.blocks(0);
Y
Yang Yang 已提交
50
  auto& device = device_contexts_[0];
Y
Yang Yang 已提交
51

Y
Yang Yang 已提交
52 53 54
  // TODO(tonyyang-svail):
  //    - runs on a new local scope
  // Scope& local_scope = scope->NewScope();
Y
Yang Yang 已提交
55 56 57 58 59 60

  for (auto& var : block.vars()) {
    scope->NewVar(var.name());
  }

  for (auto& op_desc : block.ops()) {
Y
Yang Yang 已提交
61
    auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yang Yang 已提交
62
    op->Run(*scope, *device);
Y
Yang Yang 已提交
63 64 65
  }

  // TODO(tonyyang-svail): need to test gpu device
Q
qijun 已提交
66
  for (auto& device_context : device_contexts_) {
Q
qijun 已提交
67
    device_context->Wait();
Q
qijun 已提交
68
  }
Y
Yang Yang 已提交
69 70 71 72 73 74 75 76 77
  // // print tensor value
  // for (auto& var : block.vars()) {
  //   std::cout << var.name() << std::endl;
  //   auto v = scope->FindVar(var.name());
  //   const LoDTensor& t = v->Get<LoDTensor>();
  //   for (int i = 0; i < t.numel(); ++i)
  //     std::cout << t.data<float>()[i] << " ";
  //   std::cout << std::endl;
  // }
Q
qijun 已提交
78 79 80 81
}

}  // namespace framework
}  // namespace paddle