Crd2Idx
Product Support
Product |
Supported |
|---|---|
√ |
|
√ |
|
x |
|
x |
|
x |
|
x |
Function
The Crd2Idx function is used to convert a multidimensional coordinate (Coordinate) into a memory location index (Index) through a layout (Layout). The layout herein includes shape and stride information.
For a layout with the shape (d0, d1, ..., dn), stride (s0, s1, ..., sn), and coordinate (c0, c1, ..., cn), the conversion formula to the linear index is:

For example, for a layout with the shape (3, 4, 5), stride (20, 5, 1), and coordinate (1, 2, 3):
Dimension 0: c₀ × s₀ = 1 × 20 = 20 Dimension 1: c₁ × s₁ = 2 × 5 = 10 Dimension 2: c₂ × s₂ = 3 × 1 = 3 Index = 20 + 10 + 3 = 33
When the dimensions of the coordinate and the stride do not match, the delinearization method can be used to align the dimensions of the coordinate with those of the stride before the formula above is applied to calculate the final result.
The delinearization method is described as follows: For an n-dimensional array with the shape (d0, d1, ..., dn), the multidimensional coordinate (c0, c1, ..., cn) corresponding to the linear coordinate c can be converted using the following formula:

For example, given the shape ((2, 4), (3, 5)), stride ((3, 6), (1, 24)), layout ((2, 4), (3, 5)) : ((3, 6), (1, 24)), and coordinate (11, 12), following column-major order, the result of Crd2Idx is:
crd2idx = delinearize(11, 12) * stride = ((11 % 2, 11 / 2), (12 % 3, 12 / 3)) * ((3, 6), (1, 24)) = ((1, 5), (0, 4)) * ((3, 6), (1, 24)) = 1 * 3 + 5 * 6 + 0 * 1 + 4 * 24 = 129
The above process is summarized with the following formula:




Where (d0, d1, ..., dn) is the shape, (s0, s1, ..., sn) is the stride, and the delinearization formula is expanded as follows:

Prototype
// Layout input, converting coordinates to indexes template <typename CoordType, typename ShapeType, typename StrideType> __aicore__ inline constexpr auto Crd2Idx(const CoordType& coord, const Layout<ShapeType, StrideType>& layout) // Shape and stride input, converting coordinates to indexes template <typename CoordType, typename ShapeType, typename StrideType> __aicore__ inline constexpr auto Crd2Idx(const CoordType& coord, const ShapeType& shape, const StrideType& stride)
Parameters
Parameter |
Input/Output |
Description |
|---|---|---|
coord |
Input |
Std::tuple structure type, which is used to indicate the coordinates of a tensor in different dimensions. The input data type can be size_t or Std::Int. |
layout |
Input |
Input Layout object. The input data type can be size_t or Std::Int. |
shape |
Input |
Std::tuple structure type, which is used to define the logical shape of data, such as the number of rows and columns in a two-dimensional matrix or the size of each dimension in a multi-dimensional tensor. The input data type can be size_t or Std::Int. |
stride |
Input |
Std::tuple structure type, which is used to define the stride size of each dimension in memory, that is, the interval between adjacent elements in the same dimension in memory. The interval is measured in elements and corresponds to the dimension information of the shape. The input data type can be size_t or Std::Int. |
Returns
Returns the index value converted based on the coordinate information.
Restrictions
The input parameters must meet the corresponding data type requirements.
Example
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 | // Calculate the index value based on the layout input parameters. constexpr int M = 11; constexpr int N = 12; constexpr int blockM = 13; constexpr int blockN = 14; auto coord = AscendC::MakeCoord(AscendC::Std::Int<20>{}, AscendC::Std::Int<30>{}); auto shape = AscendC::MakeShape(AscendC::MakeShape(AscendC::Std::Int<blockM>{}, AscendC::Std::Int<M/blockM>{}), AscendC::MakeShape(AscendC::Std::Int<blockN>{}, AscendC::Std::Int<N/blockN>{})); auto stride = AscendC::MakeStride(AscendC::MakeStride(AscendC::Std::Int<blockN>{}, AscendC::Std::Int<blockM*blockN>{}),AscendC::MakeStride(AscendC::Std::Int<1>{}, AscendC::Std::Int<M*blockN>{})); auto layout = AscendC::MakeLayout(shape, stride); auto index = layout(coord); // decltype(index)::value = 590 index = AscendC::Crd2Idx(coord, layout); // decltype(index)::value = 590 // Calculate the index value based on the shape and stride input parameters. auto blockCoordM = AscendC::Std::Int<11>{}; auto blockCoordN = AscendC::Std::Int<12>{}; auto baseShapeM = AscendC::Std::Int<13>{}; auto baseShapeN = AscendC::Std::Int<14>{}; auto basestrideM = AscendC::Std::Int<15>{}; auto basestrideN = AscendC::Std::Int<16>{}; auto coord = AscendC::MakeCoord(AscendC::Std::Int<0>{}, blockCoordN); auto shape = AscendC::MakeShape(AscendC::MakeShape(baseShapeM, baseShapeM), AscendC::MakeShape(baseShapeN, baseShapeN)); auto stride = AscendC::MakeStride(AscendC::MakeStride(basestrideM, basestrideM),AscendC::MakeStride(basestrideN, basestrideN)); auto index = AscendC::Crd2Idx(coord, shape, stride); // decltype(index)::value = 192 |