anchor.cc 12.4 KB
Newer Older
L
lujiale 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 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 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
/**
 * Copyright 2019-2020 Huawei Technologies Co., Ltd
 *
 * 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 "graph/anchor.h"

#include <algorithm>
#include <cstring>

#include "debug/ge_util.h"
#include "framework/common/debug/ge_log.h"
#include "graph/node.h"

namespace ge {
Anchor::Anchor(const NodePtr &owner_node, int idx) : owner_node_(owner_node), idx_(idx) {}

bool Anchor::IsTypeOf(TYPE type) const { return strcmp(Anchor::TypeOf<Anchor>(), type) == 0; }

Anchor::Vistor<AnchorPtr> Anchor::GetPeerAnchors() const {
  vector<AnchorPtr> ret;
  for (const auto &anchor : peer_anchors_) {
    ret.push_back(anchor.lock());
  }
  return Anchor::Vistor<AnchorPtr>(shared_from_this(), ret);
}

AnchorPtr Anchor::GetFirstPeerAnchor() const {
  if (peer_anchors_.empty()) {
    return nullptr;
  } else {
    return Anchor::DynamicAnchorCast<Anchor>(peer_anchors_.begin()->lock());
  }
}

NodePtr Anchor::GetOwnerNode() const { return owner_node_.lock(); }

void Anchor::UnlinkAll() noexcept {
  if (!peer_anchors_.empty()) {
    do {
      auto peer_anchor_ptr = peer_anchors_.begin()->lock();
      if (Unlink(peer_anchor_ptr) != GRAPH_SUCCESS) {
        GELOGW("unlink peer_anchor_ptr failed.");
      }
    } while (!peer_anchors_.empty());
  }
}

graphStatus Anchor::Unlink(const AnchorPtr &peer) {
  if (peer == nullptr) {
    GELOGE(GRAPH_FAILED, "peer anchor is invalid.");
    return GRAPH_FAILED;
  }
  auto it = std::find_if(peer_anchors_.begin(), peer_anchors_.end(), [peer](const std::weak_ptr<Anchor> &an) {
    auto anchor = an.lock();
    return peer->Equal(anchor);
  });

  GE_IF_BOOL_EXEC(it == peer_anchors_.end(), GELOGW("this anchor is not connected to peer"); return GRAPH_FAILED);

  auto it_peer =
      std::find_if(peer->peer_anchors_.begin(), peer->peer_anchors_.end(), [this](const std::weak_ptr<Anchor> &an) {
        auto anchor = an.lock();
        return Equal(anchor);
      });

  GE_CHK_BOOL_RET_STATUS(it_peer != peer->peer_anchors_.end(), GRAPH_FAILED, "peer is not connected to this anchor");

  (void)peer_anchors_.erase(it);
  (void)peer->peer_anchors_.erase(it_peer);
  return GRAPH_SUCCESS;
}

graphStatus Anchor::ReplacePeer(const AnchorPtr &old_peer, const AnchorPtr &first_peer, const AnchorPtr &second_peer) {
  GE_CHK_BOOL_RET_STATUS(old_peer != nullptr, GRAPH_FAILED, "this old peer anchor is nullptr");
  GE_CHK_BOOL_RET_STATUS(first_peer != nullptr, GRAPH_FAILED, "this first peer anchor is nullptr");
  GE_CHK_BOOL_RET_STATUS(second_peer != nullptr, GRAPH_FAILED, "this second peer anchor is nullptr");
  auto this_it = std::find_if(peer_anchors_.begin(), peer_anchors_.end(), [old_peer](const std::weak_ptr<Anchor> &an) {
    auto anchor = an.lock();
    return old_peer->Equal(anchor);
  });

  GE_CHK_BOOL_RET_STATUS(this_it != peer_anchors_.end(), GRAPH_FAILED, "this anchor is not connected to old_peer");

  auto old_it = std::find_if(old_peer->peer_anchors_.begin(), old_peer->peer_anchors_.end(),
                             [this](const std::weak_ptr<Anchor> &an) {
                               auto anchor = an.lock();
                               return Equal(anchor);
                             });

  GE_CHK_BOOL_RET_STATUS(old_it != old_peer->peer_anchors_.end(), GRAPH_FAILED,
                         "old_peer is not connected to this anchor");
  *this_it = first_peer;
  first_peer->peer_anchors_.push_back(shared_from_this());
  *old_it = second_peer;
  second_peer->peer_anchors_.push_back(old_peer);
  return GRAPH_SUCCESS;
}

bool Anchor::IsLinkedWith(const AnchorPtr &peer) {
  auto it = std::find_if(peer_anchors_.begin(), peer_anchors_.end(), [peer](const std::weak_ptr<Anchor> &an) {
    auto anchor = an.lock();
    GE_CHK_BOOL_RET_STATUS(peer != nullptr, false, "this old peer anchor is nullptr");
    return peer->Equal(anchor);
  });
  return (it != peer_anchors_.end());
}

int Anchor::GetIdx() const { return idx_; }

void Anchor::SetIdx(int index) { idx_ = index; }

DataAnchor::DataAnchor(const NodePtr &owner_node, int idx) : Anchor(owner_node, idx) {}

bool DataAnchor::IsTypeOf(TYPE type) const {
  if (strcmp(Anchor::TypeOf<DataAnchor>(), type) == 0) {
    return true;
  }
  return Anchor::IsTypeOf(type);
}

InDataAnchor::InDataAnchor(const NodePtr &owner_node, int idx) : DataAnchor(owner_node, idx) {}

OutDataAnchorPtr InDataAnchor::GetPeerOutAnchor() const {
  if (peer_anchors_.empty()) {
    return nullptr;
  } else {
    return Anchor::DynamicAnchorCast<OutDataAnchor>(peer_anchors_.begin()->lock());
  }
}

graphStatus InDataAnchor::LinkFrom(const OutDataAnchorPtr &src) {
  // InDataAnchor must be only linkfrom once
  if (src == nullptr || !peer_anchors_.empty()) {
    GELOGE(GRAPH_FAILED, "src anchor is invalid or the peerAnchors is not empty.");
    return GRAPH_FAILED;
  }
  peer_anchors_.push_back(src);
  src->peer_anchors_.push_back(shared_from_this());
  return GRAPH_SUCCESS;
}

bool InDataAnchor::Equal(AnchorPtr anchor) const {
  auto in_data_anchor = Anchor::DynamicAnchorCast<InDataAnchor>(anchor);
  if (in_data_anchor != nullptr) {
    if (GetOwnerNode() == in_data_anchor->GetOwnerNode() && GetIdx() == in_data_anchor->GetIdx()) {
      return true;
    }
  }
  return false;
}

bool InDataAnchor::IsTypeOf(TYPE type) const {
  if (strcmp(Anchor::TypeOf<InDataAnchor>(), type) == 0) {
    return true;
  }
  return DataAnchor::IsTypeOf(type);
}

OutDataAnchor::OutDataAnchor(const NodePtr &owner_node, int idx) : DataAnchor(owner_node, idx) {}

OutDataAnchor::Vistor<InDataAnchorPtr> OutDataAnchor::GetPeerInDataAnchors() const {
  vector<InDataAnchorPtr> ret;
  for (const auto &anchor : peer_anchors_) {
    auto in_data_anchor = Anchor::DynamicAnchorCast<InDataAnchor>(anchor.lock());
    if (in_data_anchor != nullptr) {
      ret.push_back(in_data_anchor);
    }
  }
  return OutDataAnchor::Vistor<InDataAnchorPtr>(shared_from_this(), ret);
}

uint32_t OutDataAnchor::GetPeerInDataNodesSize() const {
  uint32_t out_nums = 0;
  for (const auto &anchor : peer_anchors_) {
    auto in_data_anchor = Anchor::DynamicAnchorCast<InDataAnchor>(anchor.lock());
    if (in_data_anchor != nullptr && in_data_anchor->GetOwnerNode() != nullptr) {
      out_nums++;
    }
  }
  return out_nums;
}

OutDataAnchor::Vistor<InControlAnchorPtr> OutDataAnchor::GetPeerInControlAnchors() const {
  vector<InControlAnchorPtr> ret;
  for (const auto &anchor : peer_anchors_) {
    auto in_control_anchor = Anchor::DynamicAnchorCast<InControlAnchor>(anchor.lock());
    if (in_control_anchor != nullptr) {
      ret.push_back(in_control_anchor);
    }
  }
  return OutDataAnchor::Vistor<InControlAnchorPtr>(shared_from_this(), ret);
}

graphStatus OutDataAnchor::LinkTo(const InDataAnchorPtr &dest) {
  if (dest == nullptr || !dest->peer_anchors_.empty()) {
    GELOGE(GRAPH_FAILED, "dest anchor is invalid or the peerAnchors is not empty.");
    return GRAPH_FAILED;
  }
  peer_anchors_.push_back(dest);
  dest->peer_anchors_.push_back(shared_from_this());
  return GRAPH_SUCCESS;
}

graphStatus OutDataAnchor::LinkTo(const InControlAnchorPtr &dest) {
  if (dest == nullptr) {
    GELOGE(GRAPH_FAILED, "dest anchor is invalid.");
    return GRAPH_FAILED;
  }
  peer_anchors_.push_back(dest);
  dest->peer_anchors_.push_back(shared_from_this());
  return GRAPH_SUCCESS;
}

graphStatus OutControlAnchor::LinkTo(const InDataAnchorPtr &dest) {
  if (dest == nullptr) {
    GELOGE(GRAPH_FAILED, "dest anchor is invalid.");
    return GRAPH_FAILED;
  }
  peer_anchors_.push_back(dest);
  dest->peer_anchors_.push_back(shared_from_this());
  return GRAPH_SUCCESS;
}

bool OutDataAnchor::Equal(AnchorPtr anchor) const {
  CHECK_FALSE_EXEC(anchor != nullptr, return false);
  auto out_data_anchor = Anchor::DynamicAnchorCast<OutDataAnchor>(anchor);
  if (out_data_anchor != nullptr) {
    if (GetOwnerNode() == out_data_anchor->GetOwnerNode() && GetIdx() == out_data_anchor->GetIdx()) {
      return true;
    }
  }
  return false;
}

bool OutDataAnchor::IsTypeOf(TYPE type) const {
  if (strcmp(Anchor::TypeOf<OutDataAnchor>(), type) == 0) {
    return true;
  }
  return DataAnchor::IsTypeOf(type);
}

ControlAnchor::ControlAnchor(const NodePtr &owner_node) : Anchor(owner_node, -1) {}

ControlAnchor::ControlAnchor(const NodePtr &owner_node, int idx) : Anchor(owner_node, idx) {}

bool ControlAnchor::IsTypeOf(TYPE type) const {
  if (strcmp(Anchor::TypeOf<ControlAnchor>(), type) == 0) {
    return true;
  }
  return Anchor::IsTypeOf(type);
}

InControlAnchor::InControlAnchor(const NodePtr &owner_node) : ControlAnchor(owner_node) {}

InControlAnchor::InControlAnchor(const NodePtr &owner_node, int idx) : ControlAnchor(owner_node, idx) {}

InControlAnchor::Vistor<OutControlAnchorPtr> InControlAnchor::GetPeerOutControlAnchors() const {
  vector<OutControlAnchorPtr> ret;
  for (const auto &anchor : peer_anchors_) {
    auto out_control_anchor = Anchor::DynamicAnchorCast<OutControlAnchor>(anchor.lock());
    if (out_control_anchor != nullptr) {
      ret.push_back(out_control_anchor);
    }
  }
  return InControlAnchor::Vistor<OutControlAnchorPtr>(shared_from_this(), ret);
}

InControlAnchor::Vistor<OutDataAnchorPtr> InControlAnchor::GetPeerOutDataAnchors() const {
  vector<OutDataAnchorPtr> ret;
  for (const auto &anchor : peer_anchors_) {
    auto out_data_anchor = Anchor::DynamicAnchorCast<OutDataAnchor>(anchor.lock());
    if (out_data_anchor != nullptr) {
      ret.push_back(out_data_anchor);
    }
  }
  return InControlAnchor::Vistor<OutDataAnchorPtr>(shared_from_this(), ret);
}

graphStatus InControlAnchor::LinkFrom(const OutControlAnchorPtr &src) {
  if (src == nullptr) {
    GELOGE(GRAPH_FAILED, "src anchor is invalid.");
    return GRAPH_FAILED;
  }
  peer_anchors_.push_back(src);
  src->peer_anchors_.push_back(shared_from_this());
  return GRAPH_SUCCESS;
}

bool InControlAnchor::Equal(AnchorPtr anchor) const {
  CHECK_FALSE_EXEC(anchor != nullptr, return false);
  auto in_control_anchor = Anchor::DynamicAnchorCast<InControlAnchor>(anchor);
  if (in_control_anchor != nullptr) {
    if (GetOwnerNode() == in_control_anchor->GetOwnerNode()) {
      return true;
    }
  }
  return false;
}

bool InControlAnchor::IsTypeOf(TYPE type) const {
  if (strcmp(Anchor::TypeOf<InControlAnchor>(), type) == 0) {
    return true;
  }
  return ControlAnchor::IsTypeOf(type);
}

OutControlAnchor::OutControlAnchor(const NodePtr &owner_node) : ControlAnchor(owner_node) {}

OutControlAnchor::OutControlAnchor(const NodePtr &owner_node, int idx) : ControlAnchor(owner_node, idx) {}

OutControlAnchor::Vistor<InControlAnchorPtr> OutControlAnchor::GetPeerInControlAnchors() const {
  vector<InControlAnchorPtr> ret;
  for (const auto &anchor : peer_anchors_) {
    auto in_control_anchor = Anchor::DynamicAnchorCast<InControlAnchor>(anchor.lock());
    if (in_control_anchor != nullptr) {
      ret.push_back(in_control_anchor);
    }
  }
  return OutControlAnchor::Vistor<InControlAnchorPtr>(shared_from_this(), ret);
}

OutControlAnchor::Vistor<InDataAnchorPtr> OutControlAnchor::GetPeerInDataAnchors() const {
  vector<InDataAnchorPtr> ret;
  for (const auto &anchor : peer_anchors_) {
    auto in_data_anchor = Anchor::DynamicAnchorCast<InDataAnchor>(anchor.lock());
    if (in_data_anchor != nullptr) {
      ret.push_back(in_data_anchor);
    }
  }
  return OutControlAnchor::Vistor<InDataAnchorPtr>(shared_from_this(), ret);
}

graphStatus OutControlAnchor::LinkTo(const InControlAnchorPtr &dest) {
  if (dest == nullptr) {
    GELOGE(GRAPH_FAILED, "dest anchor is invalid.");
    return GRAPH_FAILED;
  }
  peer_anchors_.push_back(dest);
  dest->peer_anchors_.push_back(shared_from_this());
  return GRAPH_SUCCESS;
}

bool OutControlAnchor::Equal(AnchorPtr anchor) const {
  auto out_control_anchor = Anchor::DynamicAnchorCast<OutControlAnchor>(anchor);
  if (out_control_anchor != nullptr) {
    if (GetOwnerNode() == out_control_anchor->GetOwnerNode()) {
      return true;
    }
  }
  return false;
}

bool OutControlAnchor::IsTypeOf(TYPE type) const {
  if (strcmp(Anchor::TypeOf<OutControlAnchor>(), type) == 0) {
    return true;
  }
  return ControlAnchor::IsTypeOf(type);
}
}  // namespace ge