Add naive implementation

This commit is contained in:
Hugo Mårdbrink 2024-03-21 00:15:23 +01:00
commit 748b5ae9e4
5 changed files with 142 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
/stats
/build
*.out
.DS_Store
compile_commands.json

15
CMakeLists.txt Normal file
View file

@ -0,0 +1,15 @@
# Set minimum version of CMake
cmake_minimum_required(VERSION 3.15)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/toolchain.cmake)
include(${CMAKE_TOOLCHAIN_FILE})
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
project(
dct
LANGUAGES C)
add_executable(dct main.c)

11
README.md Normal file
View file

@ -0,0 +1,11 @@
# DCT-II algorithm tailored for RISC-V satellites 🛰️
The goal of this project is to explore a hardware-software co-design approach to the DCT-II algorithm on a RISC-V processor.
There is currently a rise of RISC-V processors in the industry, especially in the space domain.
I thought it would be interesting to explore the DCT-II algorithm due to its applications in satellites, and its potential to be accelerated.
Therefore, this project places emphasis on energy efficient configurations in contrast to maximizing throughput.
## To build:
```bash
riscv64-unknown-elf-gcc -march=rv64gcv main.c -lm -o dct2d_riscv.out
```

90
main.c Normal file
View file

@ -0,0 +1,90 @@
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#define DCT_SIZE 8
#define TOTAL_DCT_BLOCKS 100
#define PI 3.14159265358979323846
#define element_t int16_t
#define real_t double
void dct_2d(element_t** matrix_in, element_t** matrix_out) {
real_t cu, cv, sum;
int u, v, i, j;
for (u = 0; u < DCT_SIZE; u++) {
for (v = 0; v < DCT_SIZE; v++) {
cu = u == 0 ? 1 / sqrt(DCT_SIZE) : sqrt(2) / sqrt(DCT_SIZE);
cv = v == 0 ? 1 / sqrt(DCT_SIZE) : sqrt(2) / sqrt(DCT_SIZE);
sum = 0;
for (i = 0; i < DCT_SIZE; i++) {
for (j = 0; j < DCT_SIZE; j++) {
sum += matrix_in[i][j] * cos((2 * i + 1) * u * PI / (2 * DCT_SIZE)) * cos((2 * j + 1) * v * PI / (2 * DCT_SIZE));
}
}
matrix_out[u][v] = cu * cv * sum;
}
}
}
void populate_mock_matrices(element_t*** mock_matrices) {
for (long i = 0; i < TOTAL_DCT_BLOCKS; i++) {
for (int j = 0; j < DCT_SIZE; j++) {
for (int k = 0; k < DCT_SIZE; k++) {
mock_matrices[i][j][k] = j + k;
}
}
}
}
element_t*** generate_mock_matrices() {
element_t ***mock_matrices = (element_t ***) malloc(TOTAL_DCT_BLOCKS * sizeof(element_t**));
for (int i = 0; i < TOTAL_DCT_BLOCKS; i++) {
mock_matrices[i] = (element_t **) malloc(DCT_SIZE * sizeof(element_t*));
for (int j = 0; j < DCT_SIZE; j++) {
mock_matrices[i][j] = (element_t *) malloc(DCT_SIZE * sizeof(element_t));
}
}
populate_mock_matrices(mock_matrices);
return mock_matrices;
}
void free_mock_matrices(element_t*** mock_matrices, element_t** matrix_out) {
for (int i = 0; i < TOTAL_DCT_BLOCKS; i++) {
for (int j = 0; j < DCT_SIZE; j++) {
free(mock_matrices[i][j]);
}
free(mock_matrices[i]);
}
free(mock_matrices);
}
int main() {
element_t ***mock_matrices = generate_mock_matrices();
element_t** matrix_out = (element_t **) malloc(DCT_SIZE * sizeof(element_t*));
for (int i = 0; i < DCT_SIZE; i++) {
matrix_out[i] = (element_t *) malloc(DCT_SIZE * sizeof(element_t));
}
for(long i = 0; i < TOTAL_DCT_BLOCKS; i++) {
dct_2d(mock_matrices[i], matrix_out);
}
free_mock_matrices(mock_matrices, matrix_out);
for (int i = 0; i < DCT_SIZE; i++) {
free(matrix_out[i]);
}
free(matrix_out);
return 0;
}

21
toolchain.cmake Normal file
View file

@ -0,0 +1,21 @@
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR riscv64)
set(CMAKE_SYSTEM_VERSION "0.0.0")
set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_OSX_ARCHITECTURES "")
set(CMAKE_C_COMPILER riscv64-unknown-elf-gcc)
set(CMAKE_LINKER riscv64-unknown-elf-ld)
set(CMAKE_C_FLAGS "-march=rv64gcv")
set(CMAKE_C_LINK_EXECUTABLE
"<CMAKE_C_COMPILER> <FLAGS> <CMAKE_C_LINK_FLAGS> <LINK_FLAGS> \"${CRTI_OBJ}\" \"${CRTBEGIN_OBJ}\" <OBJECTS> \"${CRTEND_OBJ}\" \"${CRTN_OBJ}\" -o <TARGET> <LINK_LIBRARIES>")
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)