Rate This Document
Findability
Accuracy
Completeness
Readability

Tensor

Tensor class in the KUPL matrix programming module, which is used to describe a matrix object, including the source data and its layout. You can create and define a KUPL Tensor so that KUPL can detect the matrix information for subsequent operations such as tensor operators, MMA, and copy.

Class Definition

template <typename dtype, typename Layout>

class Tensor {};

Class Member Variables

Table 1 Definition of class member variables

Member Variable

Type

Description

ptr_

dtype*

Stores the raw memory space of the user's matrix object, allowing subsequent operations in the matrix programming module to perceive it. Here, dtype is a class template parameter representing the precision type of the user's matrix object.

layout_

Layout

Describes the memory layout of the user's matrix object to ensure the correctness of subsequent operations within the matrix programming module.

Class Member Functions

1. Tensor(coord) for tensor object indexing and slicing.

Table 2 Input and output definitions of Tensor()

Parameter

Type

Description

Input/Output

coord

Coord

Specifies the coordinates to be obtained. If the input Coord type contains Underscore, it indicates tensor slicing.

Input

value/tensor

dtype/Tensor

Returns value details for the indexing and slicing capabilities of tensor objects: When the coordinate specifies a precise point, the element value at that specific coordinate is returned. When the input Coord contains an Underscore type, a sub-Tensor object representing the sliced tensor is returned.

Output

2. Element-wise addition of tensor objects: TensorC = TensorA + TensorB

Table 3 Input and output definitions of Tensor operator+

Parameter

Type

Description

Input/Output

tensorA

Tensor

Matrix A for element-wise addition of tensors

Input

tensorB

Tensor

Matrix B for element-wise addition of tensors

Input

tensorC

Tensor

Matrix C for the output of element-wise addition of tensors. Matrices A, B, and C have the same layout.

Output

3. Scalar multiplication of tensor objects: TensorC = A * TensorB or TensorC = TensorB * A

Table 4 Input and output definitions of Tensor operator*

Parameter

Type

Description

Input/Output

A

dtype

Scalar element for tensor scalar multiplication

Input

tensorB

Tensor

Multiplicand matrix for tensor scalar multiplication

Input

tensorC

Tensor

Matrix C for the output of tensor scalar multiplication. Matrices B and C have the same layout.

Output

Examples

#include "stdlib.h"
#include "kupl_mma.h"
using namespace kupl::tensor;

int main()
{
    constexpr int MATRIX_M  = 32;
    constexpr int MATRIX_N  = 16;
    double *data_a = (double *)malloc(sizeof(double) * MATRIX_M * MATRIX_N);
    double *data_b = (double *)malloc(sizeof(double) * MATRIX_M * MATRIX_N);
    double *data_c = (double *)malloc(sizeof(double) * MATRIX_M * MATRIX_N);

    auto shape = make_shape(Int<32>{}, Int<16>{});
    auto stride = make_stride(Int<16>{}, Int<1>{});
    auto layout = make_layout(shape, stride);
    atuo tensor_a = make_tensor(data_a, layout);
    auto tensor_b = make_tensor(data_b, layout);
    auto tensor_c = make_tensor(data_c, layout);

    // The following describes the indexing and slicing operations for a tensor object.
    // Obtain the tensor element whose index is (2, 2).
    auto coord1 = make_coord(Int<2>{}, Int<2>{});
    auto ret1 = tensor(coord1);
    // Use an Underscore variable to implement tensor slicing. coord2 indicates that all tensor elements in the second row are obtained.
    auto coord2 = make_coord(Int<2>{}, Underscore{});
    auto ret2 = tensor(coord2);
    auto coord3 = make_coord(Int<2>{});
    auto ret3 = ret2(coord3);

    // The following describes the element-wise addition operation for tensor objects.
    tensor_c = tensor_a + tensor_b;    

    // The following describes the scalar multiplication operation for tensor objects.
    tensor_c = tensor_b * 2.0;
    tensor_c = 2.0 * tensor_a;

    free(data_c);
    free(data_b);
    free(data_a);
    return 0;
}

The preceding example demonstrates the MMA workflow based on a 32*16*512_F64F64F64 matrix shape, where make_tensor is used to create tensor objects to be passed as parameters to subsequent mma or store interfaces.