Skip to content

Commit

Permalink
feat benchmark: added mpi test
Browse files Browse the repository at this point in the history
Signed-off-by: John Sanpe <[email protected]>
  • Loading branch information
sanpeqf committed Feb 13, 2024
1 parent 85381a2 commit ed5558a
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright(c) 2023 ffashion <[email protected]>
#

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)

Expand Down
10 changes: 5 additions & 5 deletions examples/benchmark/crc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <bfdev/crc.h>
#include <bfdev/prandom.h>
#include <bfdev/log.h>
#include <bfdev/size.h>
#include <bfdev.h>

#include "main.h"
#include "py32f0xx_hal.h"

#define TEST_SIZE BFDEV_SZ_1KiB
Expand All @@ -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 \
Expand All @@ -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)
Expand Down
66 changes: 60 additions & 6 deletions examples/benchmark/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@
#define MODULE_NAME "benchmark"
#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt

#include <bfdev/log.h>
#include <stdio.h>
#include <bfdev.h>

#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)
{
Expand All @@ -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;
Expand All @@ -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;
}
20 changes: 20 additions & 0 deletions examples/benchmark/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright(c) 2021-2022 John Sanpe <[email protected]>
*/

#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 /* */
70 changes: 70 additions & 0 deletions examples/benchmark/mpi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright(c) 2023 John Sanpe <[email protected]>
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <bfdev.h>

#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;
}
6 changes: 4 additions & 2 deletions examples/benchmark/rbtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
#include <stdlib.h>
#include <stdint.h>
#include <errno.h>
#include <bfdev/rbtree.h>
#include <bfdev/log.h>
#include <bfdev.h>

#include "main.h"
#include "py32f0xx_hal.h"

#define TEST_LEN 50
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ed5558a

Please sign in to comment.