# On the Safe Deployment of Matrix Multiplication in Massively Parallel Safety-Related Systems
## 3 Enhancing MMM Safety
### Diagnostic Techniques  
The main idea is to compute an array of ESs at runtime to enhance the matrix-matrix multiplication execution with error-detection capabilities. We propose to compute this array of ESs and protect the data employed at the arithmetic operation level by including a diagnostic mechanism or a combination of diagnostic mechanisms. These diagnostic techniques can be integrated using different safety architectural patterns, as will be explained in Section 3.4.  
Figure 2: Cutlass GEMM hierarchy [13].  
A set of existing checksum techniques provide varying degrees of DC and performance impacts [19; 20]. Their usage has been focused on assuring integrity in data transmission [21], and as a result, their error-detection effectiveness has been widely studied. We refer the interested readers to references [19; 20] for an in-depth discussion on checksums effectiveness from which we select the following: XOR, one's and two's complement, Fletcher, and cyclic redundancy check (CRC). Furthermore, we adapt these checksums to detect the execution errors of the matrix-matrix multiplication function extracted from CUTLASS [13] based on the single-instruction multiple-thread paradigm. At thread level, the CUTLASS library performs a matrix-multiply-accumulate operation in a triple-nested loop denoted as inner (I), intermediate (M), and external (E), as shown in Algorithm 1.  
```
1:for each column of a tile from A matrix do
2: External loop statements
3:for each column of a tile from B matrix do
4: Intermediate loop statements
5:for each row of a tile from A matrix do
6: Internal loop statements (compute the multiplication)
7: [Checksum (I)]
8:endfor
9: [Checksum (M)]
10:endfor
11: [Checksum (E)]
12:endfor
```  
**Algorithm 1** MMM loops computed by a thread  
Depending on the employed checksum and the matrix-matrix multiplication loop at which it is applied, we obtain a wide catalog of solutions with varying levels of DC and performance impact. We classify them into two categories: (i) individual--in which we implement a single checksum in each matrix-matrix multiplication loop, and (ii) combinations--in which we implement combinations of two different checksums in the inner and intermediate loops. We design this latter category to offer additional trade-offs between DC and performance impact. In this way, we give the option to the safety engineer to choose from the different solutions of the catalog based on the specific DC and performance requirements of each safety application.  
The GPU implementation of the checksums is performed as follows. XOR and one's and two's complement checksums and employ a low-level parallel thread execution (PTX) [22]. We code these checksums with an instruction set provided by this intermediate language, aiming to be portable across multiple GPU architectures. Among the benefits, the extended-precision integer arithmetic instructions allow holding carry-in and carry-out with a carry-bit flag in an integer-addition operation. This feature is highly desirable in one's complement checksum as it reduces the performance impact incurred by its inclusion. In Algorithm 2, readers can observe the one's complement checksum implementation based on PTX instructions:  
```
1:function_a1c(uint32_tui32_a,uint32_tui32_b)
2:uint32_tacc;
3:asm (\"add.cc.u32 %0, %1, %2,\"
4: \"addc.u32 %0, %0, 0,\"
5: \"not.b32 %0, %0;\"
6: \"=r\" (acc)
7: \"=r\" (ui32_a), \"r\"(ui32_b));
8:returnacc;
9:endfunction
```  
**Algorithm 2** One's complement checksumFor the Fletcher, we employ the implementations provided by reference [14]. Finally, we resort to lookup tables for implementing the CRC. This method accelerates the protection of multiply-accumulate operations as follows: (1) we precompute the ES applying the CRC algorithm to all chunk's possible values of a prefixed number of bits (\(n\)). These values are stored in a \(2^{n}\) lookup table; (2) we access these CRC values at runtime.