channel_send_op.cc 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Copyright (c) 2016 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/channel.h"
#include <paddle/fluid/framework/lod_rank_table.h>
#include <paddle/fluid/framework/lod_tensor_array.h>
#include <paddle/fluid/framework/reader.h>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/var_type.h"
T
Thuan Nguyen 已提交
21
#include "paddle/fluid/operators/concurrency/channel_util.h"
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "paddle/fluid/operators/math/math_function.h"

static constexpr char Channel[] = "Channel";
static constexpr char X[] = "X";

namespace paddle {
namespace operators {

class ChannelSendOp : public framework::OperatorBase {
 public:
  ChannelSendOp(const std::string &type,
                const framework::VariableNameMap &inputs,
                const framework::VariableNameMap &outputs,
                const framework::AttributeMap &attrs)
      : framework::OperatorBase(type, inputs, outputs, attrs) {}

  void InferShape(framework::InferShapeContext *ctx) const {
    PADDLE_ENFORCE(ctx->HasInput(Channel),
                   "Input(Channel) of ChannelSendOp should not be null.");
    PADDLE_ENFORCE(ctx->HasInput(X),
                   "Input(X) of ChannelSendOp should not be null.");
  }

 private:
  void RunImpl(const framework::Scope &scope,
               const platform::Place &dev_place) const override {
    // Get the channel holder created by channel_create op, passed as input.
    framework::ChannelHolder *ch =
        scope.FindVar(Input(Channel))->GetMutable<framework::ChannelHolder>();
    auto input_var = scope.FindVar(Input(X));

    // Send the input data through the channel.
54
    concurrency::ChannelSend(ch, input_var);
55 56 57 58 59
  }
};

class ChannelSendOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
60
  void Make() override {
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    AddInput(Channel,
             "(Channel) A variable which \"sends\" the passed in value to "
             "a listening receiver.")
        .AsDuplicable();
    AddInput(X, "(Variable) The value which gets sent by the channel.")
        .AsDuplicable();
    AddComment(R"DOC(
)DOC");
  }
};
}  // namespace operators
}  // namespace paddle

REGISTER_OPERATOR(channel_send, paddle::operators::ChannelSendOp,
                  paddle::framework::EmptyGradOpMaker,
                  paddle::operators::ChannelSendOpMaker);