Skip to content

Commit

Permalink
add kernel test
Browse files Browse the repository at this point in the history
  • Loading branch information
jorendumoulin committed Nov 23, 2023
1 parent a9890a2 commit 6b9e51d
Show file tree
Hide file tree
Showing 10 changed files with 70,815 additions and 0 deletions.
33 changes: 33 additions & 0 deletions kernels/simple_copy/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Courtesy of Federico Ficarelli

.DEFAULT_GOAL := all

include ../../runtime/Makefile.rules

TESTS =
TESTS += baseline.x
TESTS += simple_copy.x

CFLAGS += -std=gnu11
CFLAGS += -Wall -Wextra

data.c data.h:
$(PYTHON) gendata.py

%.x: %.o main.o data.o
$(LD) $(LDFLAGS) $^ -o $@

sim_%: %
rm -fr ./logs/
$(VLTSIM) $<

RUN = $(addprefix run_, $(TESTS))
$(RUN): run_%: sim_%
mv logs $(subst sim_,,$<).logs

all: $(TESTS)

allrun: $(RUN)

clean:
rm -fr *.ll12 *.x *.o *.logs/ logs/ data.h data.c
9 changes: 9 additions & 0 deletions kernels/simple_copy/baseline.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "memref.h"
#include <snrt.h>
#include <stdint.h>

void _mlir_ciface_simple_copy(OneDMemrefI32_t *memrefA,
OneDMemrefI32_t *memrefB) {
snrt_dma_start_1d(memrefB->aligned_data, memrefA->aligned_data,
*(memrefA->shape[0]) * sizeof(int32_t));
}
36 changes: 36 additions & 0 deletions kernels/simple_copy/gendata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# simple script to generate inputs and expected outputs for simple_mult
import numpy as np
from numpy import typing as npt
from typing import Dict


def create_header(file_name: str, size: int, variables: Dict[str, npt.NDArray]) -> None:
with open(file_name, "w") as f:
includes = ["#include <stdint.h>", "#pragma once", "", f"#define N {size}", ""]
includes = "\n".join(includes)
variable_names = list(variables.keys())
variables_string = [f"extern const int32_t {i}[N];" for i in variable_names]
variables_string = "\n".join(variables_string)
f.write(includes)
f.write(variables_string)
f.write("\n")


def create_data(file_name: str, size: int, variables: Dict[str, npt.NDArray]):
includes = ['#include "data.h"', "", ""]
includes = "\n".join(includes)
with open(file_name, "w") as f:
f.write(includes)
for variable_name, variable_value in variables.items():
f.write(f"const int32_t {variable_name}[N] = " + "{\n")
variable_str = ["\t" + str(i) for i in variable_value]
f.write(",\n".join(variable_str))
f.write("\n};\n\n")


if __name__ == "__main__":
array_size = 10
A = np.linspace(1, array_size, array_size, dtype=np.int32)
variables = {"A": A}
create_header("data.h", array_size, variables)
create_data("data.c", array_size, variables)
59 changes: 59 additions & 0 deletions kernels/simple_copy/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "data.h"
#include "memref.h"
#include "stdint.h"
#include <snrt.h>

void _mlir_ciface_snax_dma_1d_transfer(OneDMemrefI32_t *a, OneDMemrefI32_t *b) {
snrt_dma_start_1d(b->aligned_data, a->aligned_data,
*(a->shape[0]) * sizeof(int32_t));
return;
}

int main() {

uint32_t constant_zero = 0;
uint32_t constant_size = N;

// create memref object for A
OneDMemrefI32_t memrefA = {
.data = &A,
.aligned_data = &A,
.offset = &constant_zero,
.shape[0] = &constant_size,
.stride[0] = &constant_zero,
};

// allocate memory in L1 for copy target
OneDMemrefI32_t memrefB = {
.data = (int32_t *)snrt_l1_next(),
.aligned_data = memrefB.data,
.offset = &constant_zero,
.shape[0] = &constant_size,
.stride[0] = &constant_zero,
};

// execute copy
if (snrt_is_dm_core()) {
_mlir_ciface_simple_copy(&memrefA, &memrefB);
// snrt_dma_start_1d((&memrefB)->aligned_data, (&memrefA)->aligned_data,
// *((&memrefA)->shape[0]) * sizeof(int32_t));
}

snrt_cluster_hw_barrier();

// check if result is okay with core 0

int thiscore = snrt_cluster_core_idx();
if (thiscore != 0)
return 0;

// Correctness check
int nerr = 0;
for (int i = 0; i < N; i++) {
int32_t error = memrefB.aligned_data[i] - A[i];
if (error != 0)
nerr += 1;
}

return nerr;
}
Loading

0 comments on commit 6b9e51d

Please sign in to comment.