Skip to content

Commit

Permalink
ADD: update build system
Browse files Browse the repository at this point in the history
  • Loading branch information
T-K-233 committed Jun 12, 2024
1 parent 990b7e9 commit 459d3e5
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 122 deletions.
34 changes: 34 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

# Options
option(BUILD_SHARED_LIBS "Build using shared libraries" OFF )
option(CMAKE_BUILD_TYPE "Build type" Debug )
option(STATIC_LINKING "Link statically" TRUE )

option(X86 "Build for x86" OFF )
option(RISCV "Build for RISC-V" OFF )


add_library(target-riscv INTERFACE)
target_compile_features(target-riscv INTERFACE c_std_11)

add_library(target-x86 INTERFACE)
target_compile_features(target-x86 INTERFACE c_std_11)

target_link_options(target-x86 INTERFACE -static)
target_link_options(target-riscv INTERFACE -static)

target_compile_options(target-x86 INTERFACE -march=native)
target_link_options(target-x86 INTERFACE -march=native)


set(WRAP_SPECS_FILE "htif_wrap.specs")
set(SPECS_FILE "htif_nano.specs")
set(LIBGLOSS_DIR "$ENV{RISCV}/riscv64-unknown-elf/lib/")

target_compile_options(target-riscv INTERFACE -fno-common -fno-builtin-printf)
target_compile_options(target-riscv INTERFACE -mcmodel=medany -march=rv64gcv_zfh -mabi=lp64d)
target_compile_options(target-riscv INTERFACE -Wl,-Map=output.map -specs=${SPECS_FILE} -specs=${WRAP_SPECS_FILE} -T ${CMAKE_SOURCE_DIR}/htif.ld)
target_link_options(target-x86 INTERFACE -fno-common -fno-builtin-printf)
target_link_options(target-riscv INTERFACE -mcmodel=medany -march=rv64gcv_zfh -mabi=lp64d)
target_link_options(target-riscv INTERFACE -Wl,-Map=output.map -specs=${SPECS_FILE} -specs=${WRAP_SPECS_FILE} -T ${CMAKE_SOURCE_DIR}/htif.ld)

41 changes: 9 additions & 32 deletions example/fast-depth/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,34 +1,19 @@
# the CMake files are still very hacky
# TODO: organize the build system properly

cmake_minimum_required(VERSION 3.10)

project(fast-depth LANGUAGES C)


# Options
option(BUILD_SHARED_LIBS "Build using shared libraries" OFF )
option(CMAKE_BUILD_TYPE "Build type" Debug )
option(STATIC_LINKING "Link statically" TRUE )

option(X86 "Build for x86" OFF )
option(RISCV "Build for RISC-V" OFF )


add_executable(fast-depth main.c)
target_include_directories(fast-depth PUBLIC inc)

target_compile_features(fast-depth INTERFACE c_std_11)


set(CMAKE_C_FLAGS "-O3 -Wall -Wextra")
set(CMAKE_EXE_LINKER_FLAGS "-static")

if (X86)
message("Building for x86")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native")
message("fast-depth: building for x86")
target_link_libraries(fast-depth PUBLIC target-x86)

elseif (RISCV)
message("Building for RISC-V")
message("fast-depth: building for RISC-V")
# CMake toolchain definition for RISC-V GCC toolchain
set(CMAKE_SYSTEM_NAME "Generic" CACHE STRING "")
set(CMAKE_SYSTEM_PROCESSOR "riscv" CACHE STRING "")
Expand All @@ -43,25 +28,17 @@ elseif (RISCV)
set(CMAKE_OBJCOPY "${TOOLCHAIN_PREFIX}objcopy")
set(CMAKE_SIZE "${TOOLCHAIN_PREFIX}size")
set(CMAKE_STRIP "${TOOLCHAIN_PREFIX}ld")

set(WRAP_SPECS_FILE "htif_wrap.specs")
set(SPECS_FILE "htif_nano.specs")
set(LIBGLOSS_DIR "$ENV{RISCV}/riscv64-unknown-elf/lib/")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-common -fno-builtin-printf")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mcmodel=medany -march=rv64gcv_zfh -mabi=lp64d")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,-Map=output.map -specs=${SPECS_FILE} -specs=${WRAP_SPECS_FILE} -T ${CMAKE_SOURCE_DIR}/htif.ld")

target_link_libraries(fast-depth PUBLIC target-riscv)
endif ()

add_compile_options(-O3 -Wall -Wextra)

target_compile_options(fast-depth PRIVATE -u _printf_float)

add_subdirectory(../../ ./build/)

message("Compiler Flags: ${CMAKE_C_FLAGS}")
message("Linker Flags: ${CMAKE_EXE_LINKER_FLAGS}")


add_subdirectory(../../nn ./build/nn)
add_subdirectory(../../nn/ ./build/nn)
target_link_libraries(fast-depth PUBLIC nn)

target_link_libraries(fast-depth PUBLIC m)
Expand Down
18 changes: 9 additions & 9 deletions example/fast-depth/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ void showASCIIImage(Tensor *tensor) {
}
}

static void enable_vector_operations() {
unsigned long mstatus;
asm volatile("csrr %0, mstatus" : "=r"(mstatus));
mstatus |= 0x00000600 | 0x00006000 | 0x00018000;
asm volatile("csrw mstatus, %0"::"r"(mstatus));
}
// static void enable_vector_operations() {
// unsigned long mstatus;
// asm volatile("csrr %0, mstatus" : "=r"(mstatus));
// mstatus |= 0x00000600 | 0x00006000 | 0x00018000;
// asm volatile("csrw mstatus, %0"::"r"(mstatus));
// }

int main() {

enable_vector_operations();
// enable_vector_operations();

Model *model = malloc(sizeof(Model));

Expand All @@ -89,9 +89,9 @@ int main() {
// NN_fill_F32(&model->x, 0.0);
memcpy((uint8_t *)model->x.data, (uint8_t *)model_input_data, (size_t)model_input_end - (size_t)model_input_start);

cycles = READ_CSR("mcycle");
// cycles = READ_CSR("mcycle");
forward(model);
cycles = READ_CSR("mcycle") - cycles;
// cycles = READ_CSR("mcycle") - cycles;

printf("cycles: %lu\n", cycles);

Expand Down
77 changes: 0 additions & 77 deletions test/CMakeLists.txt

This file was deleted.

45 changes: 45 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
cmake_minimum_required(VERSION 3.10)

project(test LANGUAGES C)


add_executable(test src/main.c)
target_include_directories(test PUBLIC inc)

target_compile_features(test INTERFACE c_std_11)

if (X86)
message("test: building for x86")
target_link_libraries(test PUBLIC target-x86)

elseif (RISCV)
message("test: building for RISC-V")
# CMake toolchain definition for RISC-V GCC toolchain
set(CMAKE_SYSTEM_NAME "Generic" CACHE STRING "")
set(CMAKE_SYSTEM_PROCESSOR "riscv" CACHE STRING "")

set(TOOLCHAIN_PREFIX "riscv64-unknown-elf-")

set(CMAKE_C_COMPILER "${TOOLCHAIN_PREFIX}gcc")
set(CMAKE_ASM_COMPILER "${TOOLCHAIN_PREFIX}gcc")
set(CMAKE_CXX_COMPILER "${TOOLCHAIN_PREFIX}g++")
set(CMAKE_AR "${TOOLCHAIN_PREFIX}ar")
set(CMAKE_LINKER "{TOOLCHAIN_PREFIX}ld")
set(CMAKE_OBJCOPY "${TOOLCHAIN_PREFIX}objcopy")
set(CMAKE_SIZE "${TOOLCHAIN_PREFIX}size")
set(CMAKE_STRIP "${TOOLCHAIN_PREFIX}ld")

target_link_libraries(test PUBLIC target-riscv)
endif ()

add_compile_options(-O3 -Wall -Wextra)

target_compile_options(test PRIVATE -u _printf_float)

add_subdirectory(../ ./build/)

add_subdirectory(../nn/ ./build/nn)
target_link_libraries(test PUBLIC nn)

target_link_libraries(test PUBLIC m)

File renamed without changes.
14 changes: 10 additions & 4 deletions test/src/main.c → tests/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <rv.h>

#include "nn.h"
#include "riscv_vector.h"
// #include "riscv_vector.h"


void print_bits_half(float16_t x) {
Expand Down Expand Up @@ -46,16 +46,22 @@ uint8_t compareResult(float golden, float actual) {
}

int main() {
for (size_t i = 0; i < 100; i += 1) {
float x = rand() / (float)RAND_MAX * 1000.0f;
// for (size_t i = 0; i < 100; i += 1) {
// float x = rand() / (float)RAND_MAX * 1000.0f;

float x = (float)(0x47ca9334);

float16_t x_compressed = NN_floatToHalf(x);
float x_decompressed = NN_halfToFloat(x_compressed);

print_bits(x);
print_bits_half(x_compressed);
print_bits(x_decompressed);

printf("%f\t", x);
printf("%f\n", x_decompressed);

compareResult(x, x_decompressed);
}
// }
return 0;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 459d3e5

Please sign in to comment.