You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

somas_stream.cc 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. * Unless required by applicable law or agreed to in writing, software
  8. * distributed under the License is distributed on an "AS IS" BASIS,
  9. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. * See the License for the specific language governing permissions and
  11. * limitations under the License.
  12. */
  13. #include "backend/optimizer/somas/somas_stream.h"
  14. namespace mindspore {
  15. namespace somas {
  16. void SomasStream::ComputeAncestorStreams() {
  17. // (Naive) algorithm: for a given stream, compute its ancestors assuming only distance 1 ancestors are known (handles
  18. // cycles between streams)
  19. std::set<SomasStreamPtr> current_level, temp_level, already_visited;
  20. auto thisPtr = std::make_shared<SomasStream>(id_);
  21. already_visited.insert(thisPtr);
  22. // Initialize current level to distance 2 ancestors
  23. for (auto stream1 : ancestor_streams_) {
  24. already_visited.insert(stream1);
  25. for (auto stream2 : stream1->ancestor_streams_) {
  26. if (std::find(already_visited.begin(), already_visited.end(), stream2) == already_visited.end())
  27. current_level.insert(stream2);
  28. }
  29. }
  30. while (!current_level.empty()) {
  31. // Push current level into ancestors
  32. for (auto stream1 : current_level) {
  33. ancestor_streams_.insert(stream1);
  34. already_visited.insert(stream1);
  35. // Keep next level of this ancestor
  36. for (auto stream2 : stream1->ancestor_streams_) {
  37. if (std::find(already_visited.begin(), already_visited.end(), stream2) == already_visited.end())
  38. temp_level.insert(stream2);
  39. }
  40. }
  41. current_level.clear();
  42. current_level = temp_level;
  43. temp_level.clear();
  44. }
  45. }
  46. } // namespace somas
  47. } // namespace mindspore