diff --git a/sw/applications/example_matmul/gen_stimuly.py b/sw/applications/example_matmul/gen_stimuly.py new file mode 100644 index 000000000..30c9ad62e --- /dev/null +++ b/sw/applications/example_matmul/gen_stimuly.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python + +import sys +import random + +# Copyright 2017 ETH Zurich and University of Bologna. +# Copyright and related rights are licensed under the Solderpad Hardware +# License, Version 0.51 (the License); you may not use this file except in +# compliance with the License. You may obtain a copy of the License at +# http://solderpad.org/licenses/SHL-0.51. Unless required by applicable law +# or agreed to in writing, software, hardware and materials distributed under +# this License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR +# CONDITIONS OF ANY KIND, either express or implied. See the License for the +# specific language governing permissions and limitations under the License. + +def write_arr(f, name, arr, ctype, size): + f.write("const " + ctype + " " + name + "[] = {\n") + i = 1 + for v in arr: + if i % size == 0: + f.write('%d,\n' % (v)) + else: + f.write('%d,' % (v)) + i+=1 + f.write('};\n\n') + return + + +################################################################################ +f = open('matrixMul8.h', 'w') +f.write('#ifndef _MATMUL8_\n') +f.write('#define _MATMUL8_\n') +f.write('// This file is automatically generated\n') + + +SIZE = 16 +RANGE = 4 + +m_a = [] +m_b = [] +m_exp = [] + +for i in range(0,SIZE): + for j in range(0,SIZE): + a = random.randint(-RANGE, RANGE-1) + b = random.randint(-RANGE, RANGE-1) + + m_a.append(a) + m_b.append(b) + +for i in range(0,SIZE): + for j in range(0,SIZE): + r = 0 + + for k in range (0,SIZE): + r = r + m_a[i * SIZE + k] * m_b[k * SIZE + j] + + m_exp.append(r) + + +write_arr(f, 'm_a', m_a, 'int8_t', SIZE) +write_arr(f, 'm_b_transposed', m_b, 'int8_t', SIZE) +write_arr(f, 'm_exp', m_exp, 'int32_t', SIZE) + +f.write('#define SIZE %d\n' % SIZE) + + +f.write('#endif') diff --git a/sw/applications/example_matmul/main.c b/sw/applications/example_matmul/main.c new file mode 100644 index 000000000..789f41132 --- /dev/null +++ b/sw/applications/example_matmul/main.c @@ -0,0 +1,80 @@ +// Copyright 2024 EPFL +// Solderpad Hardware License, Version 2.1, see LICENSE.md for details. +// SPDX-License-Identifier: Apache-2.0 WITH SHL-2.1 + +#include +#include +#include "csr.h" +#include "matrixMul8.h" +#include "x-heep.h" + +/* By default, printfs are activated for FPGA and disabled for simulation. */ +#define PRINTF_IN_FPGA 1 +#define PRINTF_IN_SIM 0 + +#if TARGET_SIM && PRINTF_IN_SIM + #define PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__) +#elif TARGET_PYNQ_Z2 && PRINTF_IN_FPGA + #define PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__) +#else + #define PRINTF(...) +#endif + +void __attribute__ ((noinline)) matrixMul8(int8_t * A, int8_t * Bt, int32_t * C, int N); + +uint32_t check_results(int32_t * C, int N); + +int32_t m_c[SIZE*SIZE]; + +int main() +{ + + uint32_t errors = 0; + unsigned int instr, cycles; + + //enable mcycle csr + CSR_CLEAR_BITS(CSR_REG_MCOUNTINHIBIT, 0x1); + + CSR_WRITE(CSR_REG_MCYCLE, 0); + + //execute the kernel + matrixMul8(m_a, m_b_transposed, m_c, SIZE); + + CSR_READ(CSR_REG_MCYCLE, &cycles); + + errors = check_results(m_c, SIZE); + + PRINTF("program finished with %d errors and %d cycles\n\r", errors, cycles); + return errors; +} + +void __attribute__ ((noinline)) matrixMul8(int8_t * A, int8_t * Bt, int32_t * C, int N) +{ + for(int i = 0; i < N; i++) { + for(int j = 0; j < N; j++) { + int32_t acc = 0; + for(int k = 0; k < N; k++) { + acc+= A[i*N+k] * Bt[k*N+j]; + } + C[i*N+j] = acc; + } + } +} + +uint32_t check_results(int32_t * C, int N) +{ + // check + int i, j; + uint32_t err = 0; + + for(i = 0; i < N; i++) { + for(j = 0; j < N; j++) { + if(C[i*N+j] != m_exp[i*N+j]) { + err++; + PRINTF("Error at index %d, %d, expected %d, got %d\n\r", i, j, m_exp[i*N+j], C[i*N+j]); + } + } + } + + return err; +} diff --git a/sw/applications/example_matmul/matrixMul8.h b/sw/applications/example_matmul/matrixMul8.h new file mode 100644 index 000000000..2725551b0 --- /dev/null +++ b/sw/applications/example_matmul/matrixMul8.h @@ -0,0 +1,62 @@ +#ifndef _MATMUL8_ +#define _MATMUL8_ +// This file is automatically generated +const int8_t m_a[] = { +-2,3,-1,-1,-3,2,0,2,-2,-1,-3,-3,1,3,2,1, +1,-3,1,-4,-1,2,1,0,-4,-1,-1,-3,-2,-1,0,-2, +1,0,-1,2,1,3,2,-3,0,3,-1,3,-2,3,2,2, +-3,1,0,0,1,0,-4,-4,0,-2,-3,-2,-1,-1,3,2, +-2,0,-1,-3,-4,-1,-3,0,3,-1,2,-1,0,-2,1,1, +0,-2,-4,1,1,-4,1,1,0,-2,0,-4,-4,-1,2,-4, +-2,-1,-2,-4,0,-2,2,3,-2,2,-2,-1,2,3,1,-3, +-1,-2,3,-4,0,3,-4,-3,1,-4,0,-1,-2,0,1,-1, +1,-3,0,1,-3,-3,0,-2,2,1,-3,1,1,-4,3,0, +2,1,1,2,-1,-4,3,-3,-3,1,-2,-1,1,3,-2,3, +-1,-1,-1,-1,-3,1,-2,3,-1,0,-2,3,1,3,-3,-4, +-1,-1,-1,-1,-2,3,-3,3,-3,-1,-4,-2,-3,3,0,-2, +2,3,-4,0,3,-4,-4,3,3,2,-1,-2,0,-1,-4,-4, +3,-1,-1,-3,2,-2,3,1,-2,2,3,0,-2,-2,-2,0, +-4,1,2,2,3,-2,2,1,-2,0,-4,2,-2,-2,3,-2, +1,3,-2,0,1,3,2,-3,-4,0,0,3,3,-1,3,3, +}; + +const int8_t m_b_transposed[] = { +-1,-2,-1,3,-1,-3,2,-3,1,0,2,1,2,-4,1,-1, +2,-4,2,3,-4,-3,3,-2,2,1,-1,-2,-4,-4,-4,2, +-4,-4,-4,1,-4,0,1,0,-2,1,-4,2,-3,2,-3,-4, +2,2,-4,0,-2,-4,-3,-3,-3,-4,-3,-3,-3,2,1,0, +-2,1,-2,-4,2,3,1,-4,2,2,2,-2,-2,-4,-1,-3, +3,2,3,2,3,0,2,-2,-3,1,1,3,3,2,2,2, +-3,-4,-2,1,-1,2,3,-2,-3,0,-1,-2,3,-3,2,-4, +1,-2,-1,3,-1,-3,-4,2,-4,3,2,-4,1,1,-2,-2, +-2,2,3,-3,0,2,-1,1,2,-2,-2,3,-2,-2,-3,-4, +-1,-3,-3,1,3,3,3,1,3,-4,0,-4,-2,-3,0,3, +-1,1,-4,0,-1,-2,-3,2,1,1,-4,2,1,0,1,-1, +-2,-4,0,-3,-1,2,0,-1,2,-3,-3,-4,0,-3,-3,-2, +3,1,2,3,2,-3,1,2,2,-4,-4,3,-2,3,-4,1, +-3,-1,-3,-1,-2,3,-2,-4,2,-4,-1,0,-2,1,-1,2, +-1,0,-2,-3,3,3,0,-1,3,2,3,-3,3,1,-1,3, +3,3,-1,-3,-3,2,-3,0,2,-2,1,-2,3,1,0,-4, +}; + +const int32_t m_exp[] = { +33,0,23,29,-4,-7,0,-3,-11,8,25,-4,8,35,-6,44, +-7,-4,7,25,23,5,22,9,-34,50,40,27,43,18,42,16, +-14,-10,-23,-30,8,47,18,-49,22,-39,5,-34,12,-26,14,10, +23,44,26,-39,11,22,0,-4,25,12,25,11,-8,25,-3,25, +17,28,36,-7,4,-4,-22,53,17,13,-2,32,12,23,-9,8, +-15,20,-8,-13,20,-3,-17,1,-18,43,45,-13,15,-9,39,13, +-16,-28,3,16,30,24,12,14,5,5,23,-19,3,1,-7,31, +-11,31,31,-23,12,21,1,8,1,40,12,66,13,32,5,4, +-2,6,11,-16,20,8,8,25,12,-18,3,-5,12,6,0,-2, +-8,-23,-35,10,-41,0,12,-27,10,-44,-15,-18,-16,-9,7,-7, +2,-21,25,29,5,-15,-8,13,-20,-17,-10,4,-15,19,-12,30, +17,7,20,21,13,-2,-11,-6,-35,26,49,-1,13,37,21,49, +9,1,29,20,8,-36,0,11,19,5,18,-7,-56,-48,-22,14, +-24,-27,-24,9,2,5,15,6,0,26,17,-16,27,-48,26,-26, +-20,-33,-19,-25,2,24,14,-17,-17,22,12,-61,-18,-10,-14,-6, +31,-8,7,0,12,5,34,-29,27,-12,4,-24,28,-14,-1,22, +}; + +#define SIZE 16 +#endif \ No newline at end of file