-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cheshire] Back-ref sw compilation flow for fmatmul
- Loading branch information
Showing
12 changed files
with
211 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
# Build software for Cheshire Ara | ||
|
||
Compile the `.c` programs in this folder with: | ||
## Compile the vector code for Cheshire | ||
|
||
Compile the source files with the vector extension support enable: | ||
|
||
```bash | ||
make chs-sw-all | ||
``` | ||
|
||
This command will copy the necessary source files into Cheshire's `sw/tests` directory and compile them with the support for vector extension. | ||
This command will also copy the necessary dependencies to `sw/tests` and enable the vector extension at compile time. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../apps/common/encoding.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../apps/fmatmul/kernel/fmatmul.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../../apps/fmatmul/kernel/fmatmul.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2024 ETH Zurich and University of Bologna. | ||
// Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Matteo Perotti <[email protected]> | ||
// | ||
// Custom vector util | ||
|
||
#ifndef __VECTOR_UTIL_H__ | ||
#define __VECTOR_UTIL_H__ | ||
|
||
// Compile with version(GCC) >= 13 | ||
#include <riscv_vector.h> | ||
#include "encoding.h" | ||
|
||
#define FABS(x) ((x < 0) ? -x : x) | ||
|
||
unsigned int timer; | ||
|
||
// Return the current value of the cycle counter | ||
int64_t get_cycle_count() { | ||
int64_t cycle_count; | ||
// The fence is needed to be sure that Ara is idle, and it is not performing | ||
// the last vector stores when we read mcycle with stop_timer() | ||
asm volatile("fence; csrr %[cycle_count], cycle" : [cycle_count] "=r"(cycle_count)); | ||
return cycle_count; | ||
}; | ||
|
||
// Start and stop the counter | ||
void start_timer() { timer = -get_cycle_count(); } | ||
void stop_timer() { timer += get_cycle_count(); } | ||
|
||
// Get the value of the timer | ||
int64_t get_timer() { return timer; } | ||
|
||
inline void enable_rvv() { | ||
asm volatile ("li t0, %0" :: "i"(MSTATUS_VS)); | ||
asm volatile ("csrs mstatus, t0" ); | ||
} | ||
|
||
inline int similarity_check(double a, double b, double threshold) { | ||
double diff = a - b; | ||
if (FABS(diff) > threshold) | ||
return 0; | ||
else | ||
return 1; | ||
} | ||
|
||
inline int similarity_check_32b(float a, float b, float threshold) { | ||
float diff = a - b; | ||
if (FABS(diff) > threshold) | ||
return 0; | ||
else | ||
return 1; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright 2024 ETH Zurich and University of Bologna. | ||
// Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// Matteo Perotti <[email protected]> | ||
// | ||
// fmatmul wrapper for Cheshire | ||
|
||
#include "regs/cheshire.h" | ||
#include "dif/clint.h" | ||
#include "dif/uart.h" | ||
#include "params.h" | ||
#include "util.h" | ||
|
||
#include "cheshire_util.h" | ||
#include "vector_util.h" | ||
|
||
#include "fmatmul.c.h" | ||
|
||
#ifndef _MM_SIZE_ | ||
#define _MM_SIZE_ 32 | ||
#endif | ||
|
||
// Define Matrix dimensions: | ||
// C = AB with A=[MxN], B=[NxP], C=[MxP] | ||
uint64_t M = _MM_SIZE_; | ||
uint64_t N = _MM_SIZE_; | ||
uint64_t P = _MM_SIZE_; | ||
|
||
// Max matrix size: 256x256 | ||
double a[_MM_SIZE_*_MM_SIZE_] __attribute__((aligned(32 * NR_LANES))); | ||
double b[_MM_SIZE_*_MM_SIZE_] __attribute__((aligned(32 * NR_LANES))); | ||
double c[_MM_SIZE_*_MM_SIZE_] __attribute__((aligned(32 * NR_LANES))); | ||
// Gold results | ||
double g[_MM_SIZE_*_MM_SIZE_] __attribute__((aligned(32 * NR_LANES))); | ||
|
||
#define THRESHOLD 0.001 | ||
|
||
// Verify the matrix | ||
int verify_matrix(double *result, double *gold, size_t R, size_t C, | ||
double threshold) { | ||
for (uint64_t i = 0; i < R; ++i) { | ||
for (uint64_t j = 0; j < C; ++j) { | ||
int idx = i * C + j; | ||
if (!similarity_check(result[idx], gold[idx], threshold)) { | ||
return (i + j) == 0 ? -1 : idx; | ||
} | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
int main() { | ||
printf("fmatmul kernel:\r\n"); | ||
|
||
cheshire_start(); | ||
enable_rvv(); | ||
|
||
unsigned int s = M; | ||
|
||
// Initialize matrices | ||
for (unsigned int i = 0; i < s; ++i) { | ||
for (unsigned int k = 0; k < s; ++k) { | ||
a[k + i*s] = (double) (i + k); | ||
} | ||
} | ||
for (unsigned int k = 0; k < s; ++k) { | ||
for (unsigned int j = 0; j < s; ++j) { | ||
b[j + k*s] = (double) (k - j); | ||
} | ||
} | ||
|
||
// Run scalar check | ||
printf("Calculating fmatmul on scalar core...\r\n"); | ||
for (unsigned int i = 0; i < s; ++i) { | ||
for (unsigned int j = 0; j < s; ++j) { | ||
double sum = 0; | ||
for (unsigned int k = 0; k < s; ++k) { | ||
sum += a[k + i * s] * b[j + k * s]; | ||
} | ||
g[j + i * s] = sum; | ||
} | ||
} | ||
|
||
// Run vector kernel | ||
printf("Calculating fmatmul on vector core...\r\n"); | ||
start_timer(); | ||
fmatmul(c, a, b, s, s, s); | ||
stop_timer(); | ||
|
||
// Metrics | ||
int64_t runtime = get_timer(); | ||
float performance = 2.0 * s * s * s / runtime; | ||
float utilization = 100 * performance / (2.0 * NR_LANES); | ||
|
||
printf("The execution took %d cycles.\r\n", runtime); | ||
printf("The performance is %f FLOP/cycle (%f%% utilization).\r\n", | ||
performance, utilization); | ||
|
||
// Verify the result only for s == M (to keep it simple) | ||
if (s == M) { | ||
printf("Verifying result...\r\n"); | ||
int error = verify_matrix(c, g, s, s, THRESHOLD); | ||
if (error != 0) { | ||
printf("Error code %d\r\n", error); | ||
printf("c[%d]=%f != %f\r\n", error, c[error], g[error]); | ||
cheshire_end(); | ||
return error; | ||
} else { | ||
printf("Passed.\r\n"); | ||
} | ||
} | ||
|
||
|
||
cheshire_end(); | ||
|
||
return 0; | ||
} |
File renamed without changes.
This file was deleted.
Oops, something went wrong.