modify_op_lock_and_record_event_pass.cc 2.3 KB
Newer Older
S
sneaxiy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// 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/fluid/framework/details/modify_op_lock_and_record_event_pass.h"
#include "paddle/fluid/framework/details/computation_op_handle.h"
#include "paddle/fluid/framework/details/multi_devices_helper.h"
S
sneaxiy 已提交
18
#include "paddle/fluid/framework/details/op_graph_view.h"
X
Xin Pan 已提交
19
#include "paddle/fluid/framework/ir/graph_helper.h"
S
sneaxiy 已提交
20 21 22 23 24 25

namespace paddle {
namespace framework {
namespace details {

static bool IsLockAndRecordEventFreeComputationOpHandle(
S
sneaxiy 已提交
26 27 28 29
    ComputationOpHandle *op, const OpGraphView &graph_view) {
  if (!platform::is_gpu_place(op->GetPlace())) return false;
  for (auto &pending_op : graph_view.PendingOps(op)) {
    auto *tmp = dynamic_cast<ComputationOpHandle *>(pending_op);
S
sneaxiy 已提交
30 31 32 33 34 35 36 37 38
    if (tmp == nullptr || !(tmp->GetPlace() == op->GetPlace())) {
      return false;
    }
  }
  return true;
}

std::unique_ptr<ir::Graph> ModifyOpLockAndRecordEventPass::ApplyImpl(
    std::unique_ptr<ir::Graph> ir_graph) const {
X
Xin Pan 已提交
39
  auto all_ops = ir::FilterByNodeWrapper<OpHandleBase>(*ir_graph);
S
sneaxiy 已提交
40
  OpGraphView graph_view(all_ops);
S
sneaxiy 已提交
41
  for (auto &op : all_ops) {
X
Xin Pan 已提交
42
    auto *compute_op = dynamic_cast<ComputationOpHandle *>(op);
S
sneaxiy 已提交
43 44
    if (compute_op == nullptr) continue;
    bool is_lock_and_record_event_free =
S
sneaxiy 已提交
45
        IsLockAndRecordEventFreeComputationOpHandle(compute_op, graph_view);
S
sneaxiy 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    compute_op->SetLockAndRecordEventFree(is_lock_and_record_event_free);
    if (is_lock_and_record_event_free) {
      VLOG(10) << "Set is_lock_and_record_event_free be true in op "
               << compute_op->DebugString();
    }
  }
  return ir_graph;
}

}  // namespace details
}  // namespace framework
}  // namespace paddle

REGISTER_PASS(modify_op_lock_and_record_event_pass,
              paddle::framework::details::ModifyOpLockAndRecordEventPass);