channel_util.cc 5.4 KB
Newer Older
T
Thuan Nguyen 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "paddle/fluid/operators/concurrency/channel_util.h"
T
Thuan Nguyen 已提交
16 17 18 19
#include "paddle/fluid/framework/var_type.h"

namespace poc = paddle::operators::concurrency;

20
void poc::ChannelSend(framework::ChannelHolder *ch, framework::Variable *var) {
T
Thuan Nguyen 已提交
21 22
  auto type = framework::ToVarType(var->Type());
  if (type == framework::proto::VarType_Type_LOD_TENSOR)
23
    ch->Send(var->GetMutable<framework::LoDTensor>());
T
Thuan Nguyen 已提交
24
  else if (type == framework::proto::VarType_Type_LOD_RANK_TABLE)
25
    ch->Send(var->GetMutable<framework::LoDRankTable>());
T
Thuan Nguyen 已提交
26
  else if (type == framework::proto::VarType_Type_LOD_TENSOR_ARRAY)
27
    ch->Send(var->GetMutable<framework::LoDTensorArray>());
T
Thuan Nguyen 已提交
28
  else if (type == framework::proto::VarType_Type_SELECTED_ROWS)
29
    ch->Send(var->GetMutable<framework::SelectedRows>());
T
Thuan Nguyen 已提交
30
  else if (type == framework::proto::VarType_Type_READER)
31
    ch->Send(var->GetMutable<framework::ReaderHolder>());
T
Thuan Nguyen 已提交
32
  else if (type == framework::proto::VarType_Type_CHANNEL)
33
    ch->Send(var->GetMutable<framework::ChannelHolder>());
T
Thuan Nguyen 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  else
    PADDLE_THROW("ChannelSend:Unsupported type");
}

bool poc::ChannelReceive(framework::ChannelHolder *ch,
                         framework::Variable *var) {
  // Get type of channel and use that to call mutable data for Variable
  auto type = framework::ToVarType(ch->Type());
  if (type == framework::proto::VarType_Type_LOD_TENSOR)
    return ch->Receive(var->GetMutable<framework::LoDTensor>());
  else if (type == framework::proto::VarType_Type_LOD_RANK_TABLE)
    return ch->Receive(var->GetMutable<framework::LoDRankTable>());
  else if (type == framework::proto::VarType_Type_LOD_TENSOR_ARRAY)
    return ch->Receive(var->GetMutable<framework::LoDTensorArray>());
  else if (type == framework::proto::VarType_Type_SELECTED_ROWS)
    return ch->Receive(var->GetMutable<framework::SelectedRows>());
  else if (type == framework::proto::VarType_Type_READER)
    return ch->Receive(var->GetMutable<framework::ReaderHolder>());
  else if (type == framework::proto::VarType_Type_CHANNEL)
    return ch->Receive(var->GetMutable<framework::ChannelHolder>());
  else
    PADDLE_THROW("ChannelReceive:Unsupported type");
}

void poc::ChannelAddToSendQ(framework::ChannelHolder *ch, const void *referrer,
                            framework::Variable *var,
                            std::shared_ptr<std::condition_variable_any> cond,
                            std::function<bool(framework::ChannelAction)> cb) {
  auto type = framework::ToVarType(var->Type());
  if (type == framework::proto::VarType_Type_LOD_TENSOR) {
    ch->AddToSendQ(referrer, var->GetMutable<framework::LoDTensor>(), cond, cb);
  } else if (type == framework::proto::VarType_Type_LOD_RANK_TABLE) {
    ch->AddToSendQ(referrer, var->GetMutable<framework::LoDRankTable>(), cond,
                   cb);
  } else if (type == framework::proto::VarType_Type_LOD_TENSOR_ARRAY) {
    ch->AddToSendQ(referrer, var->GetMutable<framework::LoDTensorArray>(), cond,
                   cb);
  } else if (type == framework::proto::VarType_Type_SELECTED_ROWS) {
    ch->AddToSendQ(referrer, var->GetMutable<framework::SelectedRows>(), cond,
                   cb);
  } else if (type == framework::proto::VarType_Type_READER) {
    ch->AddToSendQ(referrer, var->GetMutable<framework::ReaderHolder>(), cond,
                   cb);
  } else if (type == framework::proto::VarType_Type_CHANNEL) {
    ch->AddToSendQ(referrer, var->GetMutable<framework::ChannelHolder>(), cond,
                   cb);
  } else {
    PADDLE_THROW("ChannelAddToSendQ:Unsupported type");
  }
}

void poc::ChannelAddToReceiveQ(
    framework::ChannelHolder *ch, const void *referrer,
    framework::Variable *var, std::shared_ptr<std::condition_variable_any> cond,
    std::function<bool(framework::ChannelAction)> cb) {
  auto type = framework::ToVarType(var->Type());
  if (type == framework::proto::VarType_Type_LOD_TENSOR) {
    ch->AddToReceiveQ(referrer, var->GetMutable<framework::LoDTensor>(), cond,
                      cb);
  } else if (type == framework::proto::VarType_Type_LOD_RANK_TABLE) {
    ch->AddToReceiveQ(referrer, var->GetMutable<framework::LoDRankTable>(),
                      cond, cb);
  } else if (type == framework::proto::VarType_Type_LOD_TENSOR_ARRAY) {
    ch->AddToReceiveQ(referrer, var->GetMutable<framework::LoDTensorArray>(),
                      cond, cb);
  } else if (type == framework::proto::VarType_Type_SELECTED_ROWS) {
    ch->AddToReceiveQ(referrer, var->GetMutable<framework::SelectedRows>(),
                      cond, cb);
  } else if (type == framework::proto::VarType_Type_READER) {
    ch->AddToReceiveQ(referrer, var->GetMutable<framework::ReaderHolder>(),
                      cond, cb);
  } else if (type == framework::proto::VarType_Type_CHANNEL) {
    ch->AddToReceiveQ(referrer, var->GetMutable<framework::ChannelHolder>(),
                      cond, cb);
  } else {
    PADDLE_THROW("ChannelAddToReceiveQ:Unsupported type");
  }
}