project tests refactoring

This commit is contained in:
Blaise Tine
2021-06-13 17:42:04 -07:00
parent 47c3234659
commit 03406c0a3f
631 changed files with 394471 additions and 653511 deletions

View File

@@ -0,0 +1,32 @@
LIB_PATH = ../../../runtime
COMP = /home/priya/dev/riscv_vec/riscv-gnu/bin/riscv32-unknown-elf-gcc
CC_FLAGS = -ffreestanding -O0 -Wl,--gc-sections -nostartfiles -nostdlib -nostartfiles -nodefaultlibs -Wl,-Bstatic,-T,$(LIB_PATH)/startup/vx_link.ld -march=rv32imv -mabi=ilp32
DMP = /home/priya/dev/riscv_vec/riscv-gnu/bin/riscv32-unknown-elf-objdump
CPY = /home/priya/dev/riscv_vec/riscv-gnu/bin/riscv32-unknown-elf-objcopy
# VX_STR = ../../startup/vx_start.S
NEWLIB = $(LIB_PATH)/newlib/newlib.c
VX_STR = $(LIB_PATH)/startup/vx_start.S
VX_INT = $(LIB_PATH)/intrinsics/vx_intrinsics.S
VX_IO = $(LIB_PATH)/io/vx_io.S $(LIB_PATH)/io/vx_io.c
VX_API = $(LIB_PATH)/vx_api/vx_api.c
VX_FIO = $(LIB_PATH)/fileio/fileio.S
VX_VEC = vx_vec_sgemm_nn.s #float --> int
LIBS = /home/priya/dev/riscv_vec/riscv-gnu/riscv32-unknown-elf/lib/libc.a /home/priya/dev/riscv_vec/riscv-gnu/riscv32-unknown-elf/lib/libstdc++.a -static-libgcc -lgcc
VX_MAIN = vx_vec_sgemm_nn
all: HEX DUMP ELF
DUMP: ELF
$(DMP) -D $(VX_MAIN).elf > $(VX_MAIN).dump
HEX: ELF
$(CPY) -O ihex $(VX_MAIN).elf $(VX_MAIN).hex
ELF:
$(COMP) $(CC_FLAGS) $(VX_STR) $(VX_VEC) $(VX_FIO) $(NEWLIB) $(VX_INT) $(VX_IO) $(VX_API) $(VX_MAIN).c $(LIBS) -Iinclude -o $(VX_MAIN).elf

View File

@@ -0,0 +1,38 @@
.type vx_vec_sgemm_nn, @function
.global vx_vec_sgemm_nn
#
# for (int n = 0; n < k; n++) {
# for (int m = 0; m < m; m++) {
# for (int i = 0; i < n;) {
#// d1[n*k+i] += a1[n*k+m]*b1[i*n+m];
# vx_vec_sgemm_nn(i, c, r, a1, b1, c1, ldc);
# i = i + 4;
# }
# }
# }
# a3 = a, a4 = b, a5 = c
# a0 = i, a1 = m, a2 = n
# a6 = ldc
vx_vec_sgemm_nn:
vsetvli t0, a6, e32
mul x1, a6, a2 # n*ldc
add x2, x1, a1 # i + (n*ldc)
add a3, x2, a3 # a[i+ n*ldc]
lw x3, (a3)
mul x4, a1, a6 # m*ldc
add x5, a0, x4 # i + m*ldc
add a4, x5, a4 # b[i + m*ldc]
# lw x6, (a4)
vlw.v v0, (a4)
vmul.vx v2, v1, x3
mul x6, a2, a6 # n*ldc
add x7, a0, x6 # i + n*ldc
add a5, x7, a5 # c[i + m*ldc]
vlw.v v3, (a5) #c
vadd.vv v3, v3, v2
ret

View File

@@ -0,0 +1,119 @@
#include <stdio.h>
#include <stdlib.h>
#include "../../../runtime/intrinsics/vx_intrinsics.h"
#include "vx_vec_sgemm_nn.h"
//---------------------------------------------------------------
/* # void sgemm_nn(size_t n, size_t m, size_t k,
# int *a, // m * k matri size_t lda,
# int *b, // k * n matrix size_t ldb,
# int *c, // m * n matrix size_t ldc)
# c += a*b (alpha=1, no transpose on input matrices)
# matrices stored in C row-major order */
//---------------------------------------------------------------
int main()
{
vx_tmc(1);
int m = 64;
int k = 64;
int n = 64;
int* a1 = (int*)malloc(sizeof(int) * m * k);
int* b1 = (int*)malloc(sizeof(int) * k * n);
int* c1 = (int*)malloc(sizeof(int) * m * n);
int* d1 = (int*)malloc(sizeof(int) * m * n); //verfication
for (int i = 0; i < (m * k); ++i) a1[i] = i;
for (int i = 0; i < (k * n); ++i) b1[i] = 1;
for (int i = 0; i < (m * n); ++i) c1[i] = 0;
for (int i = 0; i < (m * n); ++i) d1[i] = 0;
#if 0
printf("sgemm_nn\na[%d]:", m*k);
for (int i = 0; i < m*k; ++i) {
if(!(i % k)) printf("\n");
printf("%d ", a1[i]);
}
printf("\n\nb[%d]:", k*n);
for (int i = 0; i < k*n; ++i) {
if (!(i % n)) printf("\n");
printf("%d ", b1[i]);
}
#endif
int lda = 4;
int ldb = 4;
int ldc = 64; //64;
int vsize = 32;
int startCycles = vx_num_cycles();
int startInst = vx_num_instrs();
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
for (int i = 0; i < k;) {
// d1[r*k+i] += a1[r*k+c]*b1[i*n+c];
vx_vec_sgemm_nn(i, r, c, a1, b1, c1, ldc, vsize);
i = i + vsize;
}
}
}
int endCycles = vx_num_cycles();
int endInst = vx_num_instrs();
int totalInst = (endInst - startInst);
int totalCycles = (endCycles - startCycles);
printf("\nCycles = %d, Instructions = %d", totalCycles, totalInst);
// vx_vec_sgemm_nn(n, a1, b1, c1);
#if 0
printf("\n\nc[%d]:", m*n);
for (int i = 0; i < m*n; ++i) {
if (!(i % n)) printf("\n");
printf("%d ", c1[i]);
}
#endif
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
for (int i = 0; i < k; i++) {
d1[c*ldc+i] += a1[c*ldc+r]*b1[i + (r*ldc)];
//printf("d[%d] += a[%d]*b[%d]\n", c*ldc+i, c*ldc+r , i + (r*ldc));
//printf("%d %d %d\n", d1[c*ldc+i] , a1[c*ldc+r] , b1[i + (r*ldc)]);
}
}
}
#if 0
printf("\n\nc[%d]:\n", m*n);
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
printf("%d ", d1[i*m+j]);
}
printf("\n");
}
#endif
for(int i = 0; i < m*n; ++i)
{
if(c1[i] != d1[i])
{
printf("\n<sgemm_nn> FAILED at <index: %d>! \n", i);
return 1;
}
}
printf("\nPASS.......................... <sgemm_nn> \n");
free(a1); free(b1); free(c1);
vx_tmc(0);
return 0;
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,13 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
//void vx_vec_sgemm_nn(int n, int m, int k, int* a1, int lda, int* b1, int ldb, int* c1, int ldc);
void vx_vec_sgemm_nn(int n, int m, int k, int* a1, int* b1, int* c1, int ldc, int vsize);
//void vx_vec_sgemm_nn(int n, int* a1, int* b1, int* c1);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,42 @@
.type vx_vec_sgemm_nn, @function
.global vx_vec_sgemm_nn
#
# for (int n = 0; n < k; n++) {
# for (int m = 0; m < m; m++) {
# for (int i = 0; i < n;) {
#// d1[n*k+i] += a1[n*k+m]*b1[i*n+m];
# vx_vec_sgemm_nn(i, c, r, a1, b1, c1, ldc);
# i = i + 4;
# }
# }
# }
# a3 = a, a4 = b, a5 = c
# a0 = i, a1 = m, a2 = n
# a6 = ldc
vx_vec_sgemm_nn:
vsetvli t0, a7, e32
mul t1, a6, a2 # n*ldc
add t2, t1, a1 # i + (n*ldc)
slli t2, t2, 2
add a3, t2, a3 # a[i+ n*ldc]
lw t3, (a3)
mul t4, a1, a6 # m*ldc
add t5, a0, t4 # i + m*ldc
slli t5, t5, 2
add a4, t5, a4 # b[i + m*ldc]
# lw x6, (a4)
vlw.v v0, (a4)
vmul.vx v1, v0, t3
mul t6, a2, a6 # n*ldc
add t0, a0, t6 # i + n*ldc
slli t0, t0, 2
add a5, t0, a5 # c[i + m*ldc]
vlw.v v2, (a5) #c
vadd.vv v2, v2, v1
vsw.v v2, (a5)
ret