diff --git a/examples/benchmark/CMakeLists.txt b/examples/benchmark/CMakeLists.txt index 9b9e492..7ce942c 100644 --- a/examples/benchmark/CMakeLists.txt +++ b/examples/benchmark/CMakeLists.txt @@ -3,7 +3,7 @@ # Copyright(c) 2023 ffashion # -add_executable(benchmark main.c crc.c rbtree.c) +add_executable(benchmark main.c mpi.c crc.c rbtree.c) target_link_libraries(benchmark ${CMAKE_PROJECT_NAME}) target_link_libraries(benchmark bfdev) diff --git a/examples/benchmark/crc.c b/examples/benchmark/crc.c index 7a74501..40cefe8 100644 --- a/examples/benchmark/crc.c +++ b/examples/benchmark/crc.c @@ -10,10 +10,9 @@ #include #include #include -#include -#include -#include -#include +#include + +#include "main.h" #include "py32f0xx_hal.h" #define TEST_SIZE BFDEV_SZ_1KiB @@ -27,6 +26,7 @@ for (count = 0; count < TEST_LOOP; ++count) { \ func(buff, size, 0); \ loop++; \ } while (start + 1000 > HAL_GetTick()); \ + iwdg_touch(); \ bfdev_log_notice( \ name " bandwidth %u: %uKiB/s\n", \ count, loop \ @@ -42,7 +42,7 @@ int crc_benchmark(int argc, char const *argv[]) buff = malloc(TEST_SIZE); if (!buff) - return 1; + return -ENOMEM; bfdev_prandom_seed(&pstate, HAL_GetTick()); for (count = 0; count < TEST_SIZE; ++count) diff --git a/examples/benchmark/main.c b/examples/benchmark/main.c index a07ab5f..508df1d 100644 --- a/examples/benchmark/main.c +++ b/examples/benchmark/main.c @@ -6,12 +6,18 @@ #define MODULE_NAME "benchmark" #define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt -#include +#include +#include + +#include "main.h" #include "py32f0xx_hal.h" UART_HandleTypeDef huart1; +IWDG_HandleTypeDef hiwgd; + extern int crc_benchmark(void); extern int rbtree_benchmark(void); +extern int mpi_benchmark(void); int __io_putchar(int ch) { @@ -26,12 +32,15 @@ int main(void) RCC_OscInitTypeDef OscInitType = {}; RCC_ClkInitTypeDef ClkInitType = {}; GPIO_InitTypeDef GPIOInitType = {}; + const char *errinfo; + int retval; HAL_Init(); - OscInitType.OscillatorType = RCC_OSCILLATORTYPE_HSI; + OscInitType.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI; OscInitType.HSICalibrationValue = RCC_HSICALIBRATION_24MHz; OscInitType.HSIState = RCC_HSI_ON; + OscInitType.LSIState = RCC_LSI_ON; OscInitType.HSIDiv = RCC_HSI_DIV1; OscInitType.PLL.PLLState = RCC_PLL_ON; OscInitType.PLL.PLLSource = RCC_PLLSOURCE_HSI; @@ -51,21 +60,66 @@ int main(void) __HAL_RCC_GPIOA_CLK_ENABLE(); HAL_GPIO_Init(GPIOA, &GPIOInitType); + GPIOInitType.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3; + GPIOInitType.Mode = GPIO_MODE_OUTPUT_PP; + __HAL_RCC_GPIOB_CLK_ENABLE(); + HAL_GPIO_Init(GPIOB, &GPIOInitType); + huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.Mode = UART_MODE_TX_RX; __HAL_RCC_USART1_CLK_ENABLE(); HAL_UART_Init(&huart1); + hiwgd.Instance = IWDG; + hiwgd.Init.Prescaler = IWDG_PRESCALER_32; + hiwgd.Init.Reload = 2000; + HAL_IWDG_Init(&hiwgd); + bfdev_log_info("Benchmark for PY32F0xx.\n"); bfdev_log_info("Bfdev version: %s\n", __bfdev_stringify(BFDEV_VERSION)); bfdev_log_info("This may take a few minutes...\n"); - puts(""); /* '\n' */ - crc_benchmark(); - puts(""); /* '\n' */ - rbtree_benchmark(); + for (;;) { + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET); + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET); + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET); + iwdg_touch(); + + retval = crc_benchmark(); + if (retval) { + bfdev_errname(retval, &errinfo); + printf("error %d: %s\n", retval, errinfo); + abort(); + } + + puts(""); /* '\n' */ + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET); + iwdg_touch(); + + retval = rbtree_benchmark(); + if (retval) { + bfdev_errname(retval, &errinfo); + printf("error %d: %s\n", retval, errinfo); + abort(); + } + + puts(""); /* '\n' */ + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); + iwdg_touch(); + + retval = mpi_benchmark(); + if (retval) { + bfdev_errname(retval, &errinfo); + printf("error %d: %s\n", retval, errinfo); + abort(); + } + + puts(""); /* '\n' */ + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET); + iwdg_touch(); + } return 0; } diff --git a/examples/benchmark/main.h b/examples/benchmark/main.h new file mode 100644 index 0000000..723fe54 --- /dev/null +++ b/examples/benchmark/main.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2021-2022 John Sanpe + */ + +#ifndef _MAIN_H_ +#define _MAIN_H_ + +#include "py32f0xx_hal.h" + +extern UART_HandleTypeDef huart1; +extern IWDG_HandleTypeDef hiwgd; + +static inline void +iwdg_touch(void) +{ + HAL_IWDG_Refresh(&hiwgd); +} + +#endif /* */ diff --git a/examples/benchmark/mpi.c b/examples/benchmark/mpi.c new file mode 100644 index 0000000..6d1638f --- /dev/null +++ b/examples/benchmark/mpi.c @@ -0,0 +1,70 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * Copyright(c) 2023 John Sanpe + */ + +#include +#include +#include +#include +#include + +#include "main.h" +#include "py32f0xx_hal.h" + +#define TEST_LEN 100 +#define TEST_SIZE (TEST_LEN / 4 + 1) +#define TEST_LOOP (TEST_LEN / 1.39793 + 1) + +int mpi_benchmark(void) +{ + uint32_t start, time; + bfdev_mpi_t *vw, *vs, *vv, *vq; + unsigned int k; + int retval; + + if (!((vw = bfdev_mpi_create(NULL)) && + (vs = bfdev_mpi_create(NULL)) && + (vv = bfdev_mpi_create(NULL)) && + (vq = bfdev_mpi_create(NULL)))) + return -ENOMEM; + + bfdev_log_notice("Generate bignum: %u\n", TEST_SIZE); + if ((retval = bfdev_mpi_set(vw, 16 * 5)) || + (retval = bfdev_mpi_set(vv, 239 * 4)) || + (retval = bfdev_mpi_set(vq, 10000))) + return retval; + + for (k = 0; k < TEST_SIZE; ++k) { + if ((retval = bfdev_mpi_mul(vw, vw, vq)) || + (retval = bfdev_mpi_mul(vv, vv, vq))) + return retval; + + iwdg_touch(); + } + + bfdev_log_notice("Calculate PI %d:\n", TEST_LEN); + start = HAL_GetTick(); + for (k = 1; k <= TEST_LOOP; ++k) { + if ((retval = bfdev_mpi_divi(vw, vw, vw, 25)) || + (retval = bfdev_mpi_divi(vv, vv, vv, 239 * 239)) || + (retval = bfdev_mpi_sub(vq, vw, vv)) || + (retval = bfdev_mpi_divi(vq, vq, vq, 2 * k - 1))) + return retval; + + if (k & 1) + retval = bfdev_mpi_add(vs, vs, vq); + else + retval = bfdev_mpi_sub(vs, vs, vq); + + if (retval) + return retval; + + iwdg_touch(); + } + + time = HAL_GetTick() - start; + bfdev_log_notice("Total time: %lu.%lus\n", time / 1000, time % 1000); + + return 0; +} diff --git a/examples/benchmark/rbtree.c b/examples/benchmark/rbtree.c index 2e6ed1e..611052f 100755 --- a/examples/benchmark/rbtree.c +++ b/examples/benchmark/rbtree.c @@ -10,8 +10,9 @@ #include #include #include -#include -#include +#include + +#include "main.h" #include "py32f0xx_hal.h" #define TEST_LEN 50 @@ -59,6 +60,7 @@ int rbtree_benchmark(void) for (count = 0; count < TEST_LEN; ++count) bfdev_rb_insert(&bench_root, &node[count].node, demo_cmp, NULL); bench_root = BFDEV_RB_INIT; + iwdg_touch(); } time = HAL_GetTick() - start;