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.

array.cc 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "parallel/tensor_layout/array.h"
  17. #include <utility>
  18. #include "parallel/status.h"
  19. #include "utils/log_adapter.h"
  20. namespace mindspore {
  21. namespace parallel {
  22. std::string Array::ToString() const {
  23. std::ostringstream buffer;
  24. buffer << "[ ";
  25. for (auto &element : array_) {
  26. buffer << std::to_string(element) + " ";
  27. }
  28. buffer << "]";
  29. return buffer.str();
  30. }
  31. Status Array::Init(const std::vector<int32_t> &array) {
  32. array_ = array;
  33. return IsvalidArray() ? Status::SUCCESS : Status::FAILED;
  34. }
  35. bool Array::IsvalidArray() const { return true; }
  36. int32_t Array::GetDimByIdx(uint32_t idx) const {
  37. size_t mod_idx = idx;
  38. if (idx >= GetDimSize()) {
  39. MS_LOG(EXCEPTION) << "idx is " << idx << ", but array size is " << GetDimSize();
  40. }
  41. return array_[mod_idx];
  42. }
  43. int32_t Array::GetDimByReverseIdx(uint32_t idx) const {
  44. size_t mod_idx = idx;
  45. if (idx >= GetDimSize()) {
  46. MS_LOG(EXCEPTION) << "idx is " << idx << " but array size is " << GetDimSize();
  47. }
  48. return array_[GetDimSize() - 1 - mod_idx];
  49. }
  50. bool Array::operator==(const Array &shape) const {
  51. if (GetDimSize() != shape.GetDimSize()) {
  52. return false;
  53. }
  54. for (uint32_t i = 0; i < GetDimSize(); i++) {
  55. if (GetDimByIdx(i) != shape.GetDimByIdx(i)) {
  56. return false;
  57. }
  58. }
  59. return true;
  60. }
  61. } // namespace parallel
  62. } // namespace mindspore