send_recv_util.h 1.7 KB
Newer Older
Q
Qiao Longfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15 16
#pragma once
#include <string>
X
Xin Pan 已提交
17
#include "paddle/fluid/framework/ir/node.h"
18

Q
Qiao Longfei 已提交
19 20 21 22 23
namespace paddle {
namespace operators {

inline bool NeedSend(const framework::Scope& scope,
                     const std::string& varname) {
Y
Yancey1989 已提交
24 25
  // dummy variable is only used in parallel executor to represent
  // some dependency relationship, we don't need to send/recv it.
X
Xin Pan 已提交
26
  // TODO(paddle-dev): Why would parallel executor logic leaked into here?
X
Xin Pan 已提交
27 28 29
  if (varname.find(framework::ir::Node::kControlDepVarName) !=
      std::string::npos)
    return false;
Q
Qiao Longfei 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
  auto* var = scope.FindVar(varname);
  PADDLE_ENFORCE_NOT_NULL(var, "Can not find variable '%s' in the send side.",
                          varname);
  if (var->IsType<framework::LoDTensor>()) {
    return var->Get<framework::LoDTensor>().IsInitialized();
  } else if (var->IsType<framework::SelectedRows>()) {
    return var->Get<framework::SelectedRows>().rows().size() > 0UL;
  } else {
    PADDLE_THROW(
        "Variable type in send side should be in "
        "[LodTensor, SelectedRows]");
  }
  return false;
}

}  // namespace operators
}  // namespace paddle