Integers and i-tuples
Integer and integer-tuple types for control flow, tiling factors, and operations with mdspan.
Overview
In this section, you will learn how to define integers and integer-tuples (i-tuples) in Croktile code and understand their usage.
Integers for Program Control
In high-performance computation kernels, integers are typically used for program control rather than computation. In Croktile tileflow programs, integers are used exclusively for loop control and array indexing. Given the limited value ranges in these scenarios, Croktile has provided a single integer type -- the 32-bit signed integer -- to simplify the design for this domain-specific use.
In Croktile, an integer-typed variable is defined similar to C/C++:
int a = 1;However, since Croktile can infer the type from the initialization expression, you can also define an integer variable in another style:
a = 1;Similar to C/C++, you can define multiple integers in a single line:
int a = 1, b = 2, c = 3;
d = 4, e = 5;In Croktile, the mdspan-typed shape can not be used as the function parameter. However, integers can be passed from the host to the Tileflow program, and from the Tileflow program to the device:
__co__ void foo(int a) {
// ...
call kernel(a, 3);
}Unlike C/C++, Croktile does not allow the declaration of integers without initialization or the reassignment of an integer. For example:
int a; // error: declaration without initialization
int b = 1;
b = 3; // error: re-assignmentThis enforces that integers must be initialized at declaration and cannot be reassigned. In some contexts, these integers are referred to as "immutable" or "constant".
Group of Ranges and Group of Integers
In Croktile, mdspan can be used to represent dimensional shapes. However, as introduced, mdspan stands for Multi-Dimensional Span. The elements inside are Dimensional Spans, which imply ranges of values.
For example, an mdspan [7, 14] represents a group of two ranges [0, 7), [0, 14). Therefore, an mdspan does not represent a list of integer values. However, grouping integer values is useful in certain scenarios:
- When considering the delta of two mdspans, multiple integers are needed. In high-level semantics, the delta may reflect padding differences when adjusting shape dimensions.
- When tiling a shape, the multiple tiling factors are integers, not ranges.
These situations motivate Croktile to have a type for grouping integer values.
Group Integers as Integer-Tuple (I-Tuple)
Defining I-Tuples
In Croktile, multiple integers can be grouped into an integer-tuple, or i-tuple (or ituple). The keyword ituple can be used to define an i-tuple. The below code showcases the usage:
ituple a = {1, 2, 3};
b = {4, 5, 6}; // utilize the type inferenceSince Croktile can infer types from the initialization expression, programmers can often omit the ituple keyword. However, without explicit type annotation, an ituple variable definition might look similar to an mdspan definition if you are not yet familiar with Croktile. To distinguish them:
- The initialization expression of an ituple follows an assignment operation
=(like integers), whereas an mdspan is initialized after:; - The initialization expression of an ituple is enclosed by
{}, not[]as for mdspan.
Similar to mdspan, you may enforce rank check for ituples at compile time:
ituple<3> a = {1, 2}; // error: inconsistent rankOperations on I-Tuples
Operations on i-tuples are similar to those on mdspan. You can either use the element-of operation () to retrieve the element values or use ituple as a whole:
a = {3, 4};
b = {a(0), 1, a(1)}; // '()' to retrieve the element value
c = a {(0), (1), 2}; // syntax sugar
d = {a, 5, 6}; // concatenate
e = a + 1; // addition is applied elementwiseNote that, similar to integer and mdspan, an ituple variable must be initialized and cannot be reassigned.
In practice, ituples are often used with mdspans. For example:
shape : [7, 18, 28];
tiling_factors = {1, 2, 4};
tiled_shape : shape / tiling_factors;
padded_shape : shape + {2, 0, 2};In this code, a mdspan-typed shape is divided by an ituple-typed tiling factor to derive a tiled_shape. Additionally, a padded_shape is derived from the addition of the initial shape and an anonymous ituple. Note that when a mdspan is added by an ituple, their rank must be consistent. Or else, an error will occur.
shape : [7, 8, 9] + {1, 2}; // error: inconsistent rankEvaluation of Integers and I-Tuples
Similar to mdspan, both integers and i-tuples incur minimal runtime cost. Their values are evaluated at compile time whenever possible, so programmers can generally ignore their cost.
Look-Ahead: Bounded I-Tuples
I-tuples are not frequently used in real Croktile code. However, their variants, bounded i-tuples, which bind an i-tuple with an mdspan to indicate its possible range, are essential for constructing Croktile loops. We will cover bounded i-tuples in more detail in later chapters.
Quick Summary
In this section, we learned how to define integers and i-tuples in Croktile. The Integers follow C/C++ syntax. i-tuples follow a syntax similar to mdspan, but they can operate on mdspan to derive new values.
Both types require an initialization expression and cannot be reassigned, making them appear as constant or immutable values. In fact, all variables defined in Croktile tileflow programs are immutable, except for those of specific types. Therefore, the assignment operator = is used for variable initialization in most cases, which programs must be aware of.