executor.cc 2.7 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 32
      device_contexts_[i] = new platform::CPUDeviceContext(
          boost::get<platform::CPUPlace>(places[i]));
    } else if (platform::is_gpu_place(places[i])) {
Q
qijun 已提交
33
#ifdef PADDLE_WITH_GPU
Q
qijun 已提交
34 35
      device_contexts_[i] = new platform::CUDADeviceContext(
          boost::get<platform::GPUPlace>(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 45 46 47 48 49 50
Executor::~Executor() {
  for (auto& device_context : device_contexts_) {
    if (device_context) {
      delete device_context;
    }
  }
}

Q
qijun 已提交
51
void Executor::Run(const ProgramDesc& pdesc, Scope* scope) {
Y
Yang Yang 已提交
52 53 54
  // TODO(tonyyang-svail):
  //    - only runs the first block
  //    - only runs on the first device
Y
Yang Yang 已提交
55
  //    - test on gpu
Y
Yang Yang 已提交
56
  auto& block = pdesc.blocks(0);
Y
Yang Yang 已提交
57
  auto& device = device_contexts_[0];
Y
Yang Yang 已提交
58

Y
Yang Yang 已提交
59 60 61
  // TODO(tonyyang-svail):
  //    - runs on a new local scope
  // Scope& local_scope = scope->NewScope();
Y
Yang Yang 已提交
62 63 64 65 66 67

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

  for (auto& op_desc : block.ops()) {
Y
Yang Yang 已提交
68
    auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
Q
qijun 已提交
69
    std::cout << op->DebugString() << std::endl;
Y
Yang Yang 已提交
70
    op->Run(*scope, *device);
Y
Yang Yang 已提交
71 72 73
  }

  // TODO(tonyyang-svail): need to test gpu device
Q
qijun 已提交
74
  for (auto& device_context : device_contexts_) {
Q
qijun 已提交
75
    device_context->Wait();
Q
qijun 已提交
76
  }
Y
Yang Yang 已提交
77
  // // print tensor value
Q
qijun 已提交
78 79 80 81 82 83 84 85 86
  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 已提交
87 88 89 90
}

}  // namespace framework
}  // namespace paddle