> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/NVIDIA/cutlass/llms.txt
> Use this file to discover all available pages before exploring further.

# cutlass::layout Types

> Layout functions mapping logical coordinates to linear memory

## Overview

Layout functions define how logical coordinates in a tensor's index space map to offsets in the 1D array held in memory. All layout functions implement the interface defined by `IdentityTensorLayout<>` and are used by `TensorRef` and derived classes.

**Header:** `cutlass/layout/matrix.h`

## RowMajor

Mapping function for row-major matrices.

### Class Definition

```cpp theme={null}
class RowMajor {
public:
  static int const kRank = 2;
  static int const kStrideRank = 1;
  
  using Index = int32_t;
  using LongIndex = int64_t;
  using TensorCoord = MatrixCoord;
  using Stride = Coord<kStrideRank, LongIndex>;
};
```

### Constructors

```cpp theme={null}
CUTLASS_HOST_DEVICE
RowMajor(LongIndex ldm = 0);

CUTLASS_HOST_DEVICE
RowMajor(Stride stride);
```

### Member Functions

#### packed

```cpp theme={null}
CUTLASS_HOST_DEVICE
static RowMajor packed(MatrixCoord const &extent);
```

Returns a layout to a tightly packed tensor.

#### operator()

```cpp theme={null}
CUTLASS_HOST_DEVICE
LongIndex operator()(MatrixCoord const &coord) const;
```

Returns the offset of a coordinate in linear memory. Assumes coordinate has convention (row, column).

**Implementation:**

```cpp theme={null}
return LongIndex(coord.row()) * LongIndex(stride_[0]) + coord.column();
```

#### inverse

```cpp theme={null}
CUTLASS_HOST_DEVICE
MatrixCoord inverse(LongIndex offset) const;
```

Inverse of layout function, mapping linear offset to logical coordinate.

#### stride

```cpp theme={null}
CUTLASS_HOST_DEVICE
Stride stride() const;

CUTLASS_HOST_DEVICE
Stride & stride();

CUTLASS_HOST_DEVICE
typename Stride::Index stride(int idx) const;

CUTLASS_HOST_DEVICE
typename Stride::Index & stride(int idx);
```

#### capacity

```cpp theme={null}
CUTLASS_HOST_DEVICE
LongIndex capacity(MatrixCoord const &extent) const;
```

Computes the number of contiguous elements needed to store a tensor with the given size.

### Example

```cpp theme={null}
// Create row-major layout with leading dimension
cutlass::layout::RowMajor layout(lda);

// Create tensor reference
cutlass::TensorRef<float, cutlass::layout::RowMajor> tensor(ptr, layout);

// Access element at (i, j)
float value = tensor.at(cutlass::MatrixCoord(i, j));
```

## ColumnMajor

Mapping function for column-major matrices.

### Class Definition

```cpp theme={null}
class ColumnMajor {
public:
  static int const kRank = 2;
  static int const kStrideRank = 1;
  
  using Index = int32_t;
  using LongIndex = int64_t;
  using TensorCoord = MatrixCoord;
  using Stride = Coord<kStrideRank, LongIndex>;
};
```

### Constructors

```cpp theme={null}
CUTLASS_HOST_DEVICE
ColumnMajor(LongIndex ldm = 0);

CUTLASS_HOST_DEVICE
ColumnMajor(Stride stride);
```

### Member Functions

#### packed

```cpp theme={null}
CUTLASS_HOST_DEVICE
static ColumnMajor packed(MatrixCoord const &extent);
```

#### operator()

```cpp theme={null}
CUTLASS_HOST_DEVICE
LongIndex operator()(MatrixCoord const &coord) const;
```

Returns the offset of a coordinate in linear memory.

**Implementation:**

```cpp theme={null}
return LongIndex(coord.column()) * LongIndex(stride_[0]) + coord.row();
```

#### inverse

```cpp theme={null}
CUTLASS_HOST_DEVICE
MatrixCoord inverse(LongIndex offset) const;
```

#### stride

```cpp theme={null}
CUTLASS_HOST_DEVICE
Stride stride() const;

CUTLASS_HOST_DEVICE
Stride & stride();
```

#### capacity

```cpp theme={null}
CUTLASS_HOST_DEVICE
LongIndex capacity(MatrixCoord const &extent) const;
```

### Example

```cpp theme={null}
// Column-major with leading dimension
cutlass::layout::ColumnMajor layout(lda);
cutlass::TensorRef<float, cutlass::layout::ColumnMajor> tensor(ptr, layout);
```

## RowMajorInterleaved

Mapping function for interleaved matrices. Matrix is structured as row-major arrangement of fixed-size columns.

### Template Signature

```cpp theme={null}
template <int Interleave>
struct RowMajorInterleaved;
```

### Template Parameters

<ParamField path="Interleave" type="int">
  Size of interleaved columns
</ParamField>

### Class Definition

```cpp theme={null}
template <int Interleave>
struct RowMajorInterleaved {
  static int const kRank = 2;
  static int const kStrideRank = 1;
  static int const kInterleave = Interleave;
  
  using Index = int32_t;
  using LongIndex = int64_t;
  using TensorCoord = MatrixCoord;
  using Stride = Coord<kStrideRank, LongIndex>;
};
```

### operator()

```cpp theme={null}
CUTLASS_HOST_DEVICE
LongIndex operator()(MatrixCoord const &coord) const;
```

**Implementation:**

```cpp theme={null}
Index row_major = coord.row() / kInterleave;
Index row_minor = coord.row() % kInterleave;
return LongIndex(row_major) * LongIndex(stride_[0]) + 
       LongIndex(coord.column()) * kInterleave + row_minor;
```

### Example

```cpp theme={null}
// 32-way interleaved row-major layout
using Layout = cutlass::layout::RowMajorInterleaved<32>;
Layout layout(ldm);
cutlass::TensorRef<int8_t, Layout> tensor(ptr, layout);
```

## ColumnMajorInterleaved

Mapping function for interleaved matrices. Matrix is structured as column-major arrangement of fixed-size rows.

### Template Signature

```cpp theme={null}
template <int Interleave>
struct ColumnMajorInterleaved;
```

### Template Parameters

<ParamField path="Interleave" type="int">
  Size of interleaved rows
</ParamField>

### Class Definition

```cpp theme={null}
template <int Interleave>
struct ColumnMajorInterleaved {
  static int const kRank = 2;
  static int const kStrideRank = 1;
  static int const kInterleave = Interleave;
  
  using Index = int32_t;
  using LongIndex = int64_t;
  using TensorCoord = MatrixCoord;
  using Stride = Coord<kStrideRank, LongIndex>;
};
```

### operator()

```cpp theme={null}
CUTLASS_HOST_DEVICE
LongIndex operator()(MatrixCoord const &coord) const;
```

**Implementation:**

```cpp theme={null}
Index column_major = coord.column() / kInterleave;
Index column_minor = coord.column() % kInterleave;
return LongIndex(column_major) * LongIndex(stride_[0]) + 
       LongIndex(coord.row()) * kInterleave + column_minor;
```

## ContiguousMatrix

Mapping function for scenarios where layout is row-major or column-major but this information is only available at runtime.

### Enumeration

```cpp theme={null}
enum class Matrix {
  kColumnMajor,  ///< leading dimension refers to stride between columns
  kRowMajor      ///< leading dimension refers to stride between rows
};
```

### Class Definition

```cpp theme={null}
struct ContiguousMatrix {
  static int const kRank = 2;
  static int const kStrideRank = 1;
  
  using Index = int32_t;
  using LongIndex = int64_t;
  using TensorCoord = MatrixCoord;
  using Stride = Coord<kStrideRank, LongIndex>;
};
```

### Constructor

```cpp theme={null}
CUTLASS_HOST_DEVICE
ContiguousMatrix(
  Index ldm = 0,
  Matrix layout = Matrix::kColumnMajor
);
```

### operator()

```cpp theme={null}
CUTLASS_HOST_DEVICE
LongIndex operator()(MatrixCoord const &coord) const;
```

**Implementation:**

```cpp theme={null}
if (layout_ == Matrix::kColumnMajor) {
  return coord.row() + coord.column() * stride_[0];
}
else if (layout_ == Matrix::kRowMajor) {
  return coord.row() * stride_[0] + coord.column();
}
```

### Example

```cpp theme={null}
// Runtime layout selection
cutlass::layout::Matrix kind = is_column_major ? 
  cutlass::layout::Matrix::kColumnMajor : 
  cutlass::layout::Matrix::kRowMajor;

cutlass::layout::ContiguousMatrix layout(ldm, kind);
cutlass::TensorRef<float, cutlass::layout::ContiguousMatrix> tensor(ptr, layout);
```

## AffineRank2ColumnMajor

Mapping function for scenarios where both rows and columns are separated by a stride. Row stride is smaller than column stride.

### Class Definition

```cpp theme={null}
struct AffineRank2ColumnMajor {
  static int const kRank = 2;
  static int const kStrideRank = 2;
  
  using Index = int32_t;
  using LongIndex = int64_t;
  using TensorCoord = MatrixCoord;
  using Stride = Coord<kStrideRank, LongIndex>;
};
```

### Constructors

```cpp theme={null}
CUTLASS_HOST_DEVICE
AffineRank2ColumnMajor(Stride const &stride = Stride());

CUTLASS_HOST_DEVICE
AffineRank2ColumnMajor(
  LongIndex row_stride,
  LongIndex column_stride
);

CUTLASS_HOST_DEVICE
AffineRank2ColumnMajor(LongIndex stride);
```

### operator()

```cpp theme={null}
CUTLASS_HOST_DEVICE
LongIndex operator()(MatrixCoord const &coord) const;
```

**Implementation:**

```cpp theme={null}
return dot(coord, stride_);
```

## AffineRank2RowMajor

Mapping function where both rows and columns are separated by a stride. Column stride is smaller than row stride.

### Class Definition

```cpp theme={null}
struct AffineRank2RowMajor {
  static int const kRank = 2;
  static int const kStrideRank = 2;
  
  using Index = int32_t;
  using LongIndex = int64_t;
  using TensorCoord = MatrixCoord;
  using Stride = Coord<kStrideRank, LongIndex>;
};
```

### Constructors

```cpp theme={null}
CUTLASS_HOST_DEVICE
AffineRank2RowMajor(Stride const &stride = Stride());

CUTLASS_HOST_DEVICE
AffineRank2RowMajor(
  LongIndex row_stride,
  LongIndex column_stride
);
```

## Layout Transpose

Defines transposes of matrix layouts.

```cpp theme={null}
template <typename Layout>
struct LayoutTranspose;

template <>
struct LayoutTranspose<layout::RowMajor> {
  using type = layout::ColumnMajor;
};

template <>
struct LayoutTranspose<layout::ColumnMajor> {
  using type = layout::RowMajor;
};
```

### Example

```cpp theme={null}
// Get transpose layout type
using TransposedLayout = typename cutlass::layout::LayoutTranspose<
  cutlass::layout::RowMajor>::type;
// TransposedLayout is cutlass::layout::ColumnMajor
```

## See Also

* [TensorRef](/api/cpp/tensor-ref) - Uses layout functions to map coordinates
* [Gemm](/api/cpp/gemm) - Accepts layout template parameters
