Skip to content

Commit 6a624ed

Browse files
committed
feat benchmark: added mpi test
Signed-off-by: John Sanpe <[email protected]>
1 parent 85381a2 commit 6a624ed

File tree

6 files changed

+172
-14
lines changed

6 files changed

+172
-14
lines changed

examples/benchmark/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Copyright(c) 2023 ffashion <[email protected]>
44
#
55

6-
add_executable(benchmark main.c crc.c rbtree.c)
6+
add_executable(benchmark main.c mpi.c crc.c rbtree.c)
77
target_link_libraries(benchmark ${CMAKE_PROJECT_NAME})
88
target_link_libraries(benchmark bfdev)
99

examples/benchmark/crc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@
1010
#include <stdlib.h>
1111
#include <stdint.h>
1212
#include <errno.h>
13-
#include <bfdev/crc.h>
14-
#include <bfdev/prandom.h>
15-
#include <bfdev/log.h>
16-
#include <bfdev/size.h>
13+
#include <bfdev.h>
14+
15+
#include "main.h"
1716
#include "py32f0xx_hal.h"
1817

1918
#define TEST_SIZE BFDEV_SZ_1KiB
@@ -27,6 +26,7 @@ for (count = 0; count < TEST_LOOP; ++count) { \
2726
func(buff, size, 0); \
2827
loop++; \
2928
} while (start + 1000 > HAL_GetTick()); \
29+
iwdg_touch(); \
3030
bfdev_log_notice( \
3131
name " bandwidth %u: %uKiB/s\n", \
3232
count, loop \
@@ -42,7 +42,7 @@ int crc_benchmark(int argc, char const *argv[])
4242

4343
buff = malloc(TEST_SIZE);
4444
if (!buff)
45-
return 1;
45+
return -ENOMEM;
4646

4747
bfdev_prandom_seed(&pstate, HAL_GetTick());
4848
for (count = 0; count < TEST_SIZE; ++count)

examples/benchmark/main.c

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
#define MODULE_NAME "benchmark"
77
#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt
88

9-
#include <bfdev/log.h>
9+
#include <stdio.h>
10+
#include <errno.h>
11+
#include <bfdev.h>
12+
13+
#include "main.h"
1014
#include "py32f0xx_hal.h"
1115

1216
UART_HandleTypeDef huart1;
17+
IWDG_HandleTypeDef hiwgd;
18+
1319
extern int crc_benchmark(void);
1420
extern int rbtree_benchmark(void);
21+
extern int mpi_benchmark(void);
1522

1623
int __io_putchar(int ch)
1724
{
@@ -21,17 +28,27 @@ int __io_putchar(int ch)
2128
return ch;
2229
}
2330

31+
void _exit(int status)
32+
{
33+
printf("Benchmark panic: (%d)\n", errno);
34+
for (;;)
35+
iwdg_touch();
36+
}
37+
2438
int main(void)
2539
{
2640
RCC_OscInitTypeDef OscInitType = {};
2741
RCC_ClkInitTypeDef ClkInitType = {};
2842
GPIO_InitTypeDef GPIOInitType = {};
43+
const char *errinfo;
44+
int retval;
2945

3046
HAL_Init();
3147

32-
OscInitType.OscillatorType = RCC_OSCILLATORTYPE_HSI;
48+
OscInitType.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_LSI;
3349
OscInitType.HSICalibrationValue = RCC_HSICALIBRATION_24MHz;
3450
OscInitType.HSIState = RCC_HSI_ON;
51+
OscInitType.LSIState = RCC_LSI_ON;
3552
OscInitType.HSIDiv = RCC_HSI_DIV1;
3653
OscInitType.PLL.PLLState = RCC_PLL_ON;
3754
OscInitType.PLL.PLLSource = RCC_PLLSOURCE_HSI;
@@ -51,21 +68,64 @@ int main(void)
5168
__HAL_RCC_GPIOA_CLK_ENABLE();
5269
HAL_GPIO_Init(GPIOA, &GPIOInitType);
5370

71+
GPIOInitType.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_3;
72+
GPIOInitType.Mode = GPIO_MODE_OUTPUT_PP;
73+
__HAL_RCC_GPIOB_CLK_ENABLE();
74+
HAL_GPIO_Init(GPIOB, &GPIOInitType);
75+
5476
huart1.Instance = USART1;
5577
huart1.Init.BaudRate = 115200;
5678
huart1.Init.Mode = UART_MODE_TX_RX;
5779
__HAL_RCC_USART1_CLK_ENABLE();
5880
HAL_UART_Init(&huart1);
5981

82+
hiwgd.Instance = IWDG;
83+
hiwgd.Init.Prescaler = IWDG_PRESCALER_32;
84+
hiwgd.Init.Reload = 1500;
85+
HAL_IWDG_Init(&hiwgd);
86+
6087
bfdev_log_info("Benchmark for PY32F0xx.\n");
6188
bfdev_log_info("Bfdev version: %s\n", __bfdev_stringify(BFDEV_VERSION));
6289
bfdev_log_info("This may take a few minutes...\n");
63-
6490
puts(""); /* '\n' */
65-
crc_benchmark();
6691

67-
puts(""); /* '\n' */
68-
rbtree_benchmark();
92+
for (;;) {
93+
iwdg_touch();
94+
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
95+
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
96+
97+
retval = crc_benchmark();
98+
if (retval) {
99+
bfdev_errname(retval, &errinfo);
100+
printf("error %d: %s\n", retval, errinfo);
101+
abort();
102+
}
103+
puts(""); /* '\n' */
104+
105+
iwdg_touch();
106+
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);
107+
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
108+
109+
retval = rbtree_benchmark();
110+
if (retval) {
111+
bfdev_errname(retval, &errinfo);
112+
printf("error %d: %s\n", retval, errinfo);
113+
abort();
114+
}
115+
puts(""); /* '\n' */
116+
117+
iwdg_touch();
118+
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
119+
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_SET);
120+
121+
retval = mpi_benchmark();
122+
if (retval) {
123+
bfdev_errname(retval, &errinfo);
124+
printf("error %d: %s\n", retval, errinfo);
125+
abort();
126+
}
127+
puts(""); /* '\n' */
128+
}
69129

70130
return 0;
71131
}

examples/benchmark/main.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* Copyright(c) 2021-2022 John Sanpe <[email protected]>
4+
*/
5+
6+
#ifndef _MAIN_H_
7+
#define _MAIN_H_
8+
9+
#include "py32f0xx_hal.h"
10+
11+
extern UART_HandleTypeDef huart1;
12+
extern IWDG_HandleTypeDef hiwgd;
13+
14+
static inline void
15+
iwdg_touch(void)
16+
{
17+
HAL_IWDG_Refresh(&hiwgd);
18+
}
19+
20+
#endif /* */

examples/benchmark/mpi.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* Copyright(c) 2023 John Sanpe <[email protected]>
4+
*/
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <stdint.h>
9+
#include <errno.h>
10+
#include <bfdev.h>
11+
12+
#include "main.h"
13+
#include "py32f0xx_hal.h"
14+
15+
#define TEST_LEN 200
16+
#define TEST_SIZE (TEST_LEN / 4 + 1)
17+
#define TEST_LOOP (TEST_LEN / 1.39793 + 1)
18+
19+
int mpi_benchmark(void)
20+
{
21+
uint32_t start, time;
22+
bfdev_mpi_t *vw, *vs, *vv, *vq;
23+
unsigned int k;
24+
int retval;
25+
26+
if (!((vw = bfdev_mpi_create(NULL)) &&
27+
(vs = bfdev_mpi_create(NULL)) &&
28+
(vv = bfdev_mpi_create(NULL)) &&
29+
(vq = bfdev_mpi_create(NULL))))
30+
return -ENOMEM;
31+
32+
bfdev_log_notice("Generate bignum: %u\n", TEST_SIZE);
33+
if ((retval = bfdev_mpi_set(vw, 16 * 5)) ||
34+
(retval = bfdev_mpi_set(vv, 239 * 4)) ||
35+
(retval = bfdev_mpi_set(vq, 10000)))
36+
return retval;
37+
38+
for (k = 0; k < TEST_SIZE; ++k) {
39+
if ((retval = bfdev_mpi_mul(vw, vw, vq)) ||
40+
(retval = bfdev_mpi_mul(vv, vv, vq)))
41+
return retval;
42+
43+
iwdg_touch();
44+
}
45+
46+
bfdev_log_notice("Calculate PI %d:\n", TEST_LEN);
47+
start = HAL_GetTick();
48+
for (k = 1; k <= TEST_LOOP; ++k) {
49+
if ((retval = bfdev_mpi_divi(vw, vw, vw, 25)) ||
50+
(retval = bfdev_mpi_divi(vv, vv, vv, 239 * 239)) ||
51+
(retval = bfdev_mpi_sub(vq, vw, vv)) ||
52+
(retval = bfdev_mpi_divi(vq, vq, vq, 2 * k - 1)))
53+
return retval;
54+
55+
if (k & 1)
56+
retval = bfdev_mpi_add(vs, vs, vq);
57+
else
58+
retval = bfdev_mpi_sub(vs, vs, vq);
59+
60+
if (retval)
61+
return retval;
62+
63+
iwdg_touch();
64+
}
65+
66+
bfdev_mpi_destory(vw);
67+
bfdev_mpi_destory(vs);
68+
bfdev_mpi_destory(vv);
69+
bfdev_mpi_destory(vq);
70+
iwdg_touch();
71+
72+
time = HAL_GetTick() - start;
73+
bfdev_log_notice("Total time: %lu.%lus\n", time / 1000, time % 1000);
74+
75+
return 0;
76+
}

examples/benchmark/rbtree.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
#include <stdlib.h>
1111
#include <stdint.h>
1212
#include <errno.h>
13-
#include <bfdev/rbtree.h>
14-
#include <bfdev/log.h>
13+
#include <bfdev.h>
14+
15+
#include "main.h"
1516
#include "py32f0xx_hal.h"
1617

1718
#define TEST_LEN 50
@@ -59,6 +60,7 @@ int rbtree_benchmark(void)
5960
for (count = 0; count < TEST_LEN; ++count)
6061
bfdev_rb_insert(&bench_root, &node[count].node, demo_cmp, NULL);
6162
bench_root = BFDEV_RB_INIT;
63+
iwdg_touch();
6264
}
6365

6466
time = HAL_GetTick() - start;

0 commit comments

Comments
 (0)