LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
mat22_sqr.c
浏览该文件的文档.
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#include "mparam.h"
24#include "numth/mat22_mul.h"
25#include "support/perf.h"
26
27typedef void (*sqr_fn)(lmmp_mat22_t*, const lmmp_mat22_t*, mp_ptr);
28
29static lmmp_mat22_t matrix_view(mp_ptr storage, mp_size_t stride) {
30 return (lmmp_mat22_t){.p = {{storage, storage + stride},
31 {storage + 2 * stride, storage + 3 * stride}}};
32}
33
34static bool matrix_equal(const lmmp_mat22_t* a, const lmmp_mat22_t* b) {
35 for (size_t i = 0; i < 2; ++i) {
36 for (size_t j = 0; j < 2; ++j) {
37 mp_size_t n = a->n[i][j];
38 if (n != b->n[i][j] || lmmp_cmp_(a->p[i][j], b->p[i][j], n) != 0)
39 return false;
40 }
41 }
42 return true;
43}
44
45static double measure_sqr(sqr_fn fn, lmmp_mat22_t* dst, const lmmp_mat22_t* a, mp_ptr scratch) {
46 const bench_options options = {BENCH_MIN, 1, 0.001, 0.05, 1ull << 22};
47 double us;
48 BENCH_MEASURE(us, &options, fn(dst, a, scratch););
49 return us;
50}
51
52int main(void) {
53 enum { MIN_N = 8, MAX_N = 80, SAMPLES = 5, OUT_CAP = 2 * MAX_N + 4 };
55 mp_ptr storage = alloc_limbs(4 * MAX_N + 8 * OUT_CAP + 15 * MAX_N + 14);
56 lmmp_mat22_t a = matrix_view(storage, MAX_N);
57 lmmp_mat22_t basecase = matrix_view(storage + 4 * MAX_N, OUT_CAP);
58 lmmp_mat22_t strassen = matrix_view(storage + 4 * MAX_N + 4 * OUT_CAP, OUT_CAP);
59 mp_ptr scratch = storage + 4 * MAX_N + 8 * OUT_CAP;
60
61 printf("Current MAT22_SQR_STRASSEN_THRESHOLD = %u\n", (unsigned)MAT22_SQR_STRASSEN_THRESHOLD);
62 puts("limbs basecase us Strassen us");
63 unsigned streak = 0, crossover = 0;
64 int result = EXIT_SUCCESS;
65
66 for (unsigned n = MIN_N; n <= MAX_N; ++n) {
67 for (size_t i = 0; i < 2; ++i) {
68 for (size_t j = 0; j < 2; ++j) {
69 a.n[i][j] = n;
70 fill_random(a.p[i][j], n, 0x2733ba7a11665d4full + 4 * n + 2 * i + j);
71 }
72 }
73
74 lmmp_internal_numth_mat22_sqr_basecase(&basecase, &a, scratch);
75 lmmp_internal_numth_mat22_sqr_strassen(&strassen, &a, scratch);
76 if (!matrix_equal(&basecase, &strassen)) {
77 fprintf(stderr, "Matrix square results differ at %u limbs\n", n);
78 result = EXIT_FAILURE;
79 break;
80 }
81
82 double times_base[SAMPLES], times_strassen[SAMPLES];
83 for (unsigned i = 0; i < SAMPLES; ++i) {
84 if (i % 2 == 0) {
85 times_base[i] = measure_sqr(lmmp_internal_numth_mat22_sqr_basecase, &basecase, &a, scratch);
86 times_strassen[i] = measure_sqr(lmmp_internal_numth_mat22_sqr_strassen, &strassen, &a, scratch);
87 } else {
88 times_strassen[i] = measure_sqr(lmmp_internal_numth_mat22_sqr_strassen, &strassen, &a, scratch);
89 times_base[i] = measure_sqr(lmmp_internal_numth_mat22_sqr_basecase, &basecase, &a, scratch);
90 }
91 }
92 double base_us = bench_aggregate(times_base, SAMPLES, BENCH_MEDIAN);
93 double strassen_us = bench_aggregate(times_strassen, SAMPLES, BENCH_MEDIAN);
94 printf("%5u %15.4f %15.4f\n", n, base_us, strassen_us);
95 streak = strassen_us < base_us ? streak + 1 : 0;
96 if (streak == 3 && crossover == 0)
97 crossover = n - 2;
98 }
99
100 if (result == EXIT_SUCCESS) {
101 if (crossover)
102 printf("First three consecutive Strassen wins begin at %u limbs\n", crossover);
103 else
104 puts("No three consecutive Strassen wins in the sampled range");
105 }
106 lmmp_free(storage);
108 return result;
109}
int main(void)
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义 main.c:26
mp_limb_t * mp_ptr
定义 lmmp.h:96
uint64_t mp_size_t
定义 lmmp.h:93
void lmmp_free(void *ptr)
内存释放函数(调用lmmp_heap_free_fn)
void lmmp_global_init(void)
全局初始化函数(线程局部的)
void lmmp_global_deinit(void)
全局释放函数(线程局部的)
int lmmp_cmp_(mp_srcptr numa, mp_srcptr numb, mp_size_t n)
比较函数
定义 cmp.c:26
mp_ptr p[2][2]
mp_size_t n[2][2]
Copyright (C) 2026 HJimmyK (Jericho Knox)
static bool matrix_equal(const lmmp_mat22_t *a, const lmmp_mat22_t *b)
void(*) sqr_fn(lmmp_mat22_t *, const lmmp_mat22_t *, mp_ptr)
Copyright (C) 2026 HJimmyK (Jericho Knox)
static double measure_sqr(sqr_fn fn, lmmp_mat22_t *dst, const lmmp_mat22_t *a, mp_ptr scratch)
static lmmp_mat22_t matrix_view(mp_ptr storage, mp_size_t stride)
#define n
#define scratch
#define MAT22_SQR_STRASSEN_THRESHOLD
#define BENCH_MEASURE(result, options,...)
定义 perf.h:93
@ BENCH_MIN
定义 perf.h:42
@ BENCH_MEDIAN
定义 perf.h:42
const size_t MAX_N
Copyright (C) 2026 HJimmyK (Jericho Knox)