LCOV - code coverage report
Current view: top level - legacy/ascend910/algorithm/base/alg_template - nonuniform_hierarchical_ring_v1_base.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 73 0
Test Date: 2026-07-28 12:11:00 Functions: 0.0 % 18 0

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "nonuniform_hierarchical_ring_v1_base.h"
      12              : 
      13              : namespace hccl {
      14              : 
      15            0 : RingInfo::RingInfo(u32 rankSize)
      16            0 :     : rankSize_(rankSize)
      17              : {
      18            0 :     sqrtRankSize_ = static_cast<u32>(std::sqrt(rankSize));
      19              : 
      20            0 :     u32 left = sqrtRankSize_;
      21            0 :     u32 right = sqrtRankSize_;
      22              : 
      23            0 :     while (left > 0 && right < rankSize) {
      24            0 :         if (left * right < rankSize) {
      25            0 :             right++;
      26            0 :         } else if (left * right > rankSize) {
      27            0 :             left--;
      28              :         } else {
      29            0 :             break;
      30              :         }
      31              :     }
      32              : 
      33            0 :     if ((right - left) < threshold) {
      34            0 :         colSize_ = left;
      35            0 :         rowSize_ = right;
      36            0 :         extraRowSize_ = 0;
      37            0 :         extraColSize_ = 0;
      38            0 :         rankOffset_ = colSize_ * rowSize_;
      39              :     } else {
      40              :         // 保证除了最后一列之外的其他列size相等
      41            0 :         u32 extraSize = rankSize - sqrtRankSize_ * sqrtRankSize_;
      42            0 :         extraRowSize_ = (extraSize >= sqrtRankSize_) ? sqrtRankSize_ : 0;
      43            0 :         extraColSize_ = extraSize - extraRowSize_;
      44              : 
      45            0 :         colSize_ = sqrtRankSize_ + ((extraRowSize_ == 0) ? 0 : 1);
      46            0 :         rowSize_ = sqrtRankSize_;
      47              : 
      48              :         // 当rank < rankOffset_时,rank所处的行size为sqrtRankSize_ + 1
      49              :         // 当rank >= rankOffset_时,rank所处的行size为sqrtRankSize_
      50            0 :         if (extraColSize_ == 0) {
      51            0 :             rankOffset_ = colSize_ * rowSize_;
      52              :         } else {
      53            0 :             rankOffset_ = (sqrtRankSize_ + 1) * extraColSize_;
      54              :         }
      55              :     }
      56            0 : }
      57              : 
      58            0 : RingInfo::~RingInfo()
      59              : {
      60            0 : }
      61              : 
      62            0 : u32 RingInfo::GetRankSize() const
      63              : {
      64            0 :     return rankSize_;
      65              : }
      66              : 
      67            0 : u32 RingInfo::GetRankOffset() const
      68              : {
      69            0 :     return rankOffset_;
      70              : }
      71              : 
      72            0 : u32 RingInfo::GetSqrtRankSize() const
      73              : {
      74            0 :     return sqrtRankSize_;
      75              : }
      76              : 
      77            0 : u32 RingInfo::GetRowSize() const
      78              : {
      79            0 :     return rowSize_;
      80              : }
      81              : 
      82            0 : u32 RingInfo::GetColSize() const
      83              : {
      84            0 :     return colSize_;
      85              : }
      86              : 
      87            0 : u32 RingInfo::GetVIndex(u32 rank) const
      88              : {
      89            0 :     if (rank < rankOffset_) {
      90            0 :         return rank / (rowSize_ + ((extraColSize_ == 0) ? 0 : 1));
      91              :     } else {
      92            0 :         return extraColSize_ + (rank - rankOffset_) / sqrtRankSize_;
      93              :     }
      94              : }
      95              : 
      96            0 : u32 RingInfo::GetHIndex(u32 rank) const
      97              : {
      98            0 :     if (rank < rankOffset_) {
      99            0 :         return rank % (rowSize_ + ((extraColSize_ == 0) ? 0 : 1));
     100              :     } else {
     101            0 :         return (rank - rankOffset_) % sqrtRankSize_;
     102              :     }
     103              : }
     104              : 
     105            0 : u32 RingInfo::GetVSizeByHIndex(u32 hIndex) const
     106              : {
     107            0 :     if (rankOffset_ == rankSize_) {
     108            0 :         return colSize_;
     109            0 :     } else if (hIndex == sqrtRankSize_) {
     110            0 :         return extraColSize_;
     111              :     } else {
     112            0 :         return sqrtRankSize_ + static_cast<u32>(hIndex < extraRowSize_);
     113              :     }
     114              : }
     115              : 
     116            0 : u32 RingInfo::GetVSizeByRank(u32 rank) const
     117              : {
     118            0 :     return GetVSizeByHIndex(GetHIndex(rank));
     119              : }
     120              : 
     121            0 : u32 RingInfo::GetHSizeByVIndex(u32 vIndex) const
     122              : {
     123            0 :     if (rankOffset_ == rankSize_) {
     124            0 :         return rowSize_;
     125              :     } else {
     126            0 :         return sqrtRankSize_ + static_cast<u32>(vIndex < extraColSize_);
     127              :     }
     128              : }
     129              : 
     130            0 : u32 RingInfo::GetHSizeByRank(u32 rank) const
     131              : {
     132            0 :     return GetHSizeByVIndex(GetVIndex(rank));
     133              : }
     134              : 
     135            0 : u32 RingInfo::GetRank(u32 vIndex, u32 hIndex) const
     136              : {
     137            0 :     if (rankOffset_ == rankSize_) {
     138            0 :         return vIndex * rowSize_ + hIndex;
     139            0 :     } else if (vIndex < extraColSize_) {
     140            0 :         return (sqrtRankSize_ + 1) * vIndex + hIndex;
     141              :     } else {
     142            0 :         return rankOffset_ + sqrtRankSize_ * (vIndex - extraColSize_) + hIndex;
     143              :     }
     144              : }
     145              : 
     146            0 : NHRV1Base::NHRV1Base(const HcclDispatcher dispatcher)
     147            0 :     : AlgTemplateBase(dispatcher)
     148              : {
     149            0 : }
     150              : 
     151            0 : NHRV1Base::~NHRV1Base()
     152              : {
     153            0 : }
     154              : 
     155            0 : RingInfo NHRV1Base::GetRingInfo(u32 rankSize)
     156              : {
     157            0 :     return RingInfo(rankSize);
     158              : }
     159              : 
     160              : 
     161              : }   // ~~ namespace hccl
        

Generated by: LCOV version 2.0-1