LMMP
4.3.0
Lamina High-Precision Arithmetic Library
Toggle main menu visibility
载入中...
搜索中...
未找到
perf.h
浏览该文件的文档.
1
/**
2
* Copyright (C) 2026 HJimmyK (Jericho Knox)
3
*
4
* This file is part of LMMP.
5
*
6
* LMMP is free software: you can redistribute it and/or modify it
7
* under the terms of the GNU Lesser General Public License as
8
* published by the Free Software Foundation; either version 3 of
9
* the License, or (at your option) any later version.
10
*
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
15
*
16
* You should have received a copy of the GNU Lesser General Public
17
* License along with this library. If not, see
18
* <https://www.gnu.org/licenses/>.
19
*
20
* SPDX-License-Identifier: LGPL-3.0-or-later
21
*/
22
23
#ifndef LMMP_PERF_H
24
#define LMMP_PERF_H
25
26
#include <stdbool.h>
27
#include <stdio.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <time.h>
31
32
#include "
lmmp/lmmp.h
"
33
#include "
lmmp/mprand.h
"
34
#include "
lmmp/numth.h
"
35
36
#if defined(LMMP_WINDOWS)
37
#include <windows.h>
38
#endif
39
40
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
41
42
typedef
enum
{
BENCH_MIN
,
BENCH_MEDIAN
,
BENCH_MEAN
}
bench_aggregation
;
43
44
typedef
struct
{
45
bench_aggregation
agg
;
46
unsigned
max_samples
;
47
double
min_block_sec
;
48
double
slow_block_sec
;
49
mp_size_t
max_iters
;
50
}
bench_options
;
51
52
static
const
bench_options
bench_default
= {
BENCH_MIN
, 5, 0.02, 0.05, 1ull << 26};
53
static
volatile
mp_limb_t
bench_sink
;
54
55
static
inline
double
bench_clock
(
void
) {
56
#if defined(LMMP_WINDOWS)
57
static
double
ticks_to_seconds;
58
LARGE_INTEGER ticks;
59
if
(ticks_to_seconds == 0) {
60
QueryPerformanceFrequency(&ticks);
61
ticks_to_seconds = 1.0 / (double)ticks.QuadPart;
62
}
63
QueryPerformanceCounter(&ticks);
64
return
(
double
)ticks.QuadPart * ticks_to_seconds;
65
#else
66
struct
timespec now;
67
clock_gettime(CLOCK_MONOTONIC, &now);
68
return
(
double
)now.tv_sec + 1e-9 * (double)now.tv_nsec;
69
#endif
70
}
71
72
static
inline
int
bench_compare_times
(
const
void
*lhs,
const
void
*rhs) {
73
double
a = *(
const
double
*)lhs, b = *(
const
double
*)rhs;
74
return
(a > b) - (a < b);
75
}
76
77
static
inline
double
bench_aggregate
(
double
*samples,
unsigned
n
,
bench_aggregation
agg) {
78
if
(agg ==
BENCH_MEDIAN
) {
79
qsort(samples,
n
,
sizeof
(*samples),
bench_compare_times
);
80
return
(samples[(
n
- 1) / 2] + samples[
n
/ 2]) * 0.5;
81
}
82
double
result = samples[0];
83
for
(
unsigned
i = 1; i <
n
; ++i) {
84
if
(agg ==
BENCH_MEAN
)
85
result += samples[i];
86
else
if
(samples[i] < result)
87
result = samples[i];
88
}
89
return
agg ==
BENCH_MEAN
? result /
n
: result;
90
}
91
92
// 调用表达式在计时循环内直接展开。
93
#define BENCH_MEASURE(result, options, ...) \
94
do { \
95
const bench_options *bench_opts_ = (options); \
96
{ __VA_ARGS__ } \
97
mp_size_t bench_iters_ = 1; \
98
double bench_elapsed_; \
99
for (;;) { \
100
double bench_start_ = bench_clock(); \
101
for (mp_size_t bench_i_ = 0; bench_i_ < bench_iters_; ++bench_i_) { __VA_ARGS__ } \
102
bench_elapsed_ = bench_clock() - bench_start_; \
103
if (bench_elapsed_ >= bench_opts_->min_block_sec || bench_iters_ >= bench_opts_->max_iters) \
104
break; \
105
bench_iters_ = LMMP_MIN(2 * bench_iters_, bench_opts_->max_iters); \
106
} \
107
unsigned bench_count_ = bench_opts_->max_samples ? bench_opts_->max_samples : 1; \
108
if (bench_elapsed_ >= bench_opts_->slow_block_sec && bench_count_ > 2) \
109
bench_count_ = 2; \
110
double bench_samples_[bench_count_]; \
111
for (unsigned bench_s_ = 0; bench_s_ < bench_count_; ++bench_s_) { \
112
double bench_start_ = bench_clock(); \
113
for (mp_size_t bench_i_ = 0; bench_i_ < bench_iters_; ++bench_i_) { __VA_ARGS__ } \
114
bench_samples_[bench_s_] = (bench_clock() - bench_start_) * 1e6 / bench_iters_; \
115
} \
116
(result) = bench_aggregate(bench_samples_, bench_count_, bench_opts_->agg); \
117
} while (0)
118
119
static
inline
mp_ptr
alloc_limbs
(
mp_size_t
n
) {
120
return
lmmp_alloc
(
n
*
sizeof
(
mp_limb_t
));
121
}
122
123
static
inline
void
fill_random
(
mp_ptr
p,
mp_size_t
n
,
mp_limb_t
seed) {
124
lmmp_seed_random_
(p,
n
, seed, 1);
125
if
(
n
!= 0)
126
p[
n
- 1] |= (
mp_limb_t
)1 << 63;
127
}
128
129
static
inline
void
report
(
const
char
*name,
double
us,
double
bytes) {
130
double
ops = us > 0 ? 1e6 / us : 0;
131
printf(
" %-40s %14.4f us/op %14.2f ops/s"
, name, us, ops);
132
if
(bytes > 0)
133
printf(
" %10.2f MiB/s"
, bytes * ops / (1024.0 * 1024.0));
134
putchar(
'\n'
);
135
}
136
137
static
inline
void
report_n
(
const
char
*name,
mp_size_t
n
,
double
us,
double
bytes) {
138
char
label[96];
139
snprintf(label,
sizeof
(label),
"%s(%lluL)"
, name, (
unsigned
long
long
)
n
);
140
report
(label, us, bytes);
141
}
142
143
static
inline
void
report_ab
(
const
char
*name,
mp_size_t
a,
mp_size_t
b,
double
us,
double
bytes) {
144
char
label[96];
145
snprintf(label,
sizeof
(label),
"%s(%llux%llu)"
, name, (
unsigned
long
long
)a, (
unsigned
long
long
)b);
146
report
(label, us, bytes);
147
}
148
149
static
inline
void
report_bits
(
const
char
*name,
uint
bits,
double
us) {
150
char
label[96];
151
snprintf(label,
sizeof
(label),
"%s(%ub)"
, name, bits);
152
report
(label, us, 0);
153
}
154
155
#endif
lmmp.h
mp_ptr
mp_limb_t * mp_ptr
定义
lmmp.h:96
mp_size_t
uint64_t mp_size_t
定义
lmmp.h:93
lmmp_alloc
void * lmmp_alloc(size_t size)
内存分配函数(调用lmmp_heap_alloc_fn)
定义
memory.c:382
mp_limb_t
uint64_t mp_limb_t
定义
lmmp.h:92
mprand.h
lmmp_seed_random_
mp_size_t lmmp_seed_random_(mp_ptr dst, mp_size_t n, mp_limb_t seed, unsigned seed_type)
生成随机大整数(0 - B^n-1 均匀分布)
n
#define n
numth.h
uint
uint32_t uint
定义
numth.h:38
bench_options::max_samples
unsigned max_samples
定义
perf.h:46
report_ab
static void report_ab(const char *name, mp_size_t a, mp_size_t b, double us, double bytes)
定义
perf.h:143
bench_options::agg
bench_aggregation agg
定义
perf.h:45
alloc_limbs
static mp_ptr alloc_limbs(mp_size_t n)
定义
perf.h:119
bench_clock
static double bench_clock(void)
定义
perf.h:55
report
static void report(const char *name, double us, double bytes)
定义
perf.h:129
report_n
static void report_n(const char *name, mp_size_t n, double us, double bytes)
定义
perf.h:137
bench_options::min_block_sec
double min_block_sec
定义
perf.h:47
bench_options::slow_block_sec
double slow_block_sec
定义
perf.h:48
bench_options::max_iters
mp_size_t max_iters
定义
perf.h:49
bench_aggregation
bench_aggregation
定义
perf.h:42
BENCH_MEAN
@ BENCH_MEAN
定义
perf.h:42
BENCH_MIN
@ BENCH_MIN
定义
perf.h:42
BENCH_MEDIAN
@ BENCH_MEDIAN
定义
perf.h:42
report_bits
static void report_bits(const char *name, uint bits, double us)
定义
perf.h:149
bench_compare_times
static int bench_compare_times(const void *lhs, const void *rhs)
定义
perf.h:72
bench_sink
static volatile mp_limb_t bench_sink
定义
perf.h:53
bench_aggregate
static double bench_aggregate(double *samples, unsigned n, bench_aggregation agg)
定义
perf.h:77
fill_random
static void fill_random(mp_ptr p, mp_size_t n, mp_limb_t seed)
定义
perf.h:123
bench_default
static const bench_options bench_default
定义
perf.h:52
bench_options
定义
perf.h:44
src
lmmp
support
perf.h
生成于
, 为 LMMP使用
1.18.0