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 17 #include "frontend/parallel/tensor_layout/array.h" 18 #include <utility> 19 #include "frontend/parallel/status.h" 20 #include "utils/log_adapter.h" 21 22 namespace mindspore { 23 namespace parallel { ToString() const24std::string Array::ToString() const { 25 std::ostringstream buffer; 26 buffer << "[ "; 27 for (auto &element : array_) { 28 buffer << (std::to_string(element) + " "); 29 } 30 buffer << "]"; 31 return buffer.str(); 32 } 33 Init(const Shape & array)34Status Array::Init(const Shape &array) { 35 array_ = array; 36 return IsvalidArray() ? Status::SUCCESS : Status::FAILED; 37 } 38 IsvalidArray() const39bool Array::IsvalidArray() const { return true; } 40 GetDimByIdx(size_t idx) const41int64_t Array::GetDimByIdx(size_t idx) const { 42 size_t mod_idx = idx; 43 if (idx >= GetDimSize()) { 44 MS_LOG(EXCEPTION) << "idx is " << idx << ", but array size is " << GetDimSize(); 45 } 46 return array_[mod_idx]; 47 } 48 GetDimByReverseIdx(size_t idx) const49int64_t Array::GetDimByReverseIdx(size_t idx) const { 50 size_t mod_idx = idx; 51 if (idx >= GetDimSize()) { 52 MS_LOG(EXCEPTION) << "idx is " << idx << " but array size is " << GetDimSize(); 53 } 54 return array_[GetDimSize() - 1 - mod_idx]; 55 } 56 operator ==(const Array & shape) const57bool Array::operator==(const Array &shape) const { 58 if (GetDimSize() != shape.GetDimSize()) { 59 return false; 60 } 61 for (uint64_t i = 0; i < GetDimSize(); i++) { 62 if (GetDimByIdx(i) != shape.GetDimByIdx(i)) { 63 return false; 64 } 65 } 66 return true; 67 } 68 } // namespace parallel 69 } // namespace mindspore 70