From a28860ae396cdc16348487c111a062b9bcc85969 Mon Sep 17 00:00:00 2001 From: Ben Edgington Date: Sun, 28 Feb 2021 17:00:42 +0000 Subject: [PATCH] Benchmarks for recover and zero_poly --- src/Makefile | 2 +- src/fft_fr_bench.c | 1 + src/fft_g1_bench.c | 1 + src/recover_bench.c | 107 ++++++++++++++++++++++++++++++++++++++++++ src/zero_poly.c | 2 +- src/zero_poly_bench.c | 87 ++++++++++++++++++++++++++++++++++ 6 files changed, 198 insertions(+), 2 deletions(-) create mode 100644 src/recover_bench.c create mode 100644 src/zero_poly_bench.c diff --git a/src/Makefile b/src/Makefile index 7205d19..9b422c5 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,6 +1,6 @@ TESTS = bls12_381_test das_extension_test c_kzg_util_test fft_common_test fft_fr_test fft_g1_test \ fk20_proofs_test kzg_proofs_test poly_test recover_test utility_test zero_poly_test -BENCH = fft_fr_bench fft_g1_bench +BENCH = fft_fr_bench fft_g1_bench recover_bench zero_poly_bench LIB_SRC = bls12_381.c c_kzg_util.c das_extension.c fft_common.c fft_fr.c fft_g1.c fk20_proofs.c kzg_proofs.c poly.c recover.c utility.c zero_poly.c LIB_OBJ = $(LIB_SRC:.c=.o) diff --git a/src/fft_fr_bench.c b/src/fft_fr_bench.c index 7d5d234..d39122b 100644 --- a/src/fft_fr_bench.c +++ b/src/fft_fr_bench.c @@ -50,6 +50,7 @@ long run_bench(int scale, int max_seconds) { free(out); free(data); + free_fft_settings(&fs); return total_time / nits; } diff --git a/src/fft_g1_bench.c b/src/fft_g1_bench.c index 94b6f0a..c367da4 100644 --- a/src/fft_g1_bench.c +++ b/src/fft_g1_bench.c @@ -50,6 +50,7 @@ long run_bench(int scale, int max_seconds) { free(out); free(data); + free_fft_settings(&fs); return total_time / nits; } diff --git a/src/recover_bench.c b/src/recover_bench.c new file mode 100644 index 0000000..166fb94 --- /dev/null +++ b/src/recover_bench.c @@ -0,0 +1,107 @@ +/* + * Copyright 2021 Benjamin Edgington + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include // malloc(), free(), atoi() +#include // printf() +#include // assert() +#include // EXIT_SUCCESS/FAILURE +#include "bench_util.h" +#include "test_util.h" +#include "fft_fr.h" +#include "recover.h" + +// Run the benchmark for `max_seconds` and return the time per iteration in nanoseconds. +long run_bench(int scale, int max_seconds) { + timespec_t t0, t1; + unsigned long total_time = 0, nits = 0; + FFTSettings fs; + + assert(C_KZG_OK == new_fft_settings(&fs, scale)); + + // Allocate on the heap to avoid stack overflow for large sizes + fr_t *poly = malloc(fs.max_width * sizeof(fr_t)); + for (int i = 0; i < fs.max_width / 2; i++) { + fr_from_uint64(&poly[i], i); + } + for (int i = fs.max_width / 2; i < fs.max_width; i++) { + poly[i] = fr_zero; + } + + fr_t *data = malloc(fs.max_width * sizeof(fr_t)); + assert(C_KZG_OK == fft_fr(data, poly, false, fs.max_width, &fs)); + + fr_t *samples = malloc(fs.max_width * sizeof(fr_t)); + for (int i = 0; i < fs.max_width; i++) { + samples[i] = data[i]; + } + + // randomly zero out half of the points + for (int i = 0; i < fs.max_width / 2; i++) { + int j = rand() % fs.max_width; + while (fr_is_null(&samples[j])) j = rand() % fs.max_width; + samples[j] = fr_null; + } + + fr_t *recovered = malloc(fs.max_width * sizeof(fr_t)); + while (total_time < max_seconds * NANO) { + clock_gettime(CLOCK_REALTIME, &t0); + assert(C_KZG_OK == recover_poly_from_samples(recovered, samples, fs.max_width, &fs)); + clock_gettime(CLOCK_REALTIME, &t1); + + // Verify the result is correct + for (int i = 0; i < fs.max_width; i++) { + assert(fr_equal(&data[i], &recovered[i])); + } + + nits++; + total_time += tdiff(t0, t1); + } + + free(recovered); + free(samples); + free(data); + free(poly); + free_fft_settings(&fs); + + return total_time / nits; +} + +int main(int argc, char *argv[]) { + int nsec = 0; + + switch (argc) { + case 1: + nsec = NSEC; + break; + case 2: + nsec = atoi(argv[1]); + break; + default: + break; + }; + + if (nsec == 0) { + printf("Usage: %s [test time in seconds > 0]\n", argv[0]); + exit(EXIT_FAILURE); + } + + printf("*** Benchmarking Recover From Samples, %d second%s per test.\n", nsec, nsec == 1 ? "" : "s"); + for (int scale = 5; scale <= 15; scale++) { + printf("recover/scale_%d %lu ns/op\n", scale, run_bench(scale, nsec)); + } + + return EXIT_SUCCESS; +} diff --git a/src/zero_poly.c b/src/zero_poly.c index 1edf38b..0eb5cda 100644 --- a/src/zero_poly.c +++ b/src/zero_poly.c @@ -156,7 +156,7 @@ C_KZG_RET reduce_leaves(fr_t *dst, uint64_t len_dst, fr_t *scratch, uint64_t len * @retval C_CZK_ERROR An internal error occurred * @retval C_CZK_MALLOC Memory allocation failed * - * @todo What is the performance impact of tuning `per_leaf_poly`? + * @todo What is the performance impact of tuning `per_leaf_poly` and `reduction factor`? */ C_KZG_RET zero_polynomial_via_multiplication(fr_t *zero_eval, fr_t *zero_poly, uint64_t *zero_poly_len, uint64_t length, const uint64_t *missing_indices, uint64_t len_missing, diff --git a/src/zero_poly_bench.c b/src/zero_poly_bench.c new file mode 100644 index 0000000..27ad711 --- /dev/null +++ b/src/zero_poly_bench.c @@ -0,0 +1,87 @@ +/* + * Copyright 2021 Benjamin Edgington + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include // malloc(), free(), atoi() +#include // printf() +#include // assert() +#include // EXIT_SUCCESS/FAILURE +#include "bench_util.h" +#include "test_util.h" +#include "fft_fr.h" +#include "zero_poly.h" + +// Run the benchmark for `max_seconds` and return the time per iteration in nanoseconds. +long run_bench(int scale, int max_seconds) { + timespec_t t0, t1; + unsigned long total_time = 0, nits = 0; + FFTSettings fs; + + assert(C_KZG_OK == new_fft_settings(&fs, scale)); + + // Allocate on the heap to avoid stack overflow for large sizes + uint64_t *missing = malloc(fs.max_width * sizeof(uint64_t)); + for (int i = 0; i < fs.max_width; i++) { + missing[i] = i; + } + shuffle(missing, fs.max_width); + + fr_t *zero_eval = malloc(fs.max_width * sizeof(fr_t)); + fr_t *zero_poly = malloc(fs.max_width * sizeof(fr_t)); + uint64_t zero_poly_len; + while (total_time < max_seconds * NANO) { + clock_gettime(CLOCK_REALTIME, &t0); + // Half missing leaves enough FFT computation space + assert(C_KZG_OK == zero_polynomial_via_multiplication(zero_eval, zero_poly, &zero_poly_len, fs.max_width, + missing, fs.max_width / 2, &fs)); + clock_gettime(CLOCK_REALTIME, &t1); + nits++; + total_time += tdiff(t0, t1); + } + + free(zero_poly); + free(zero_eval); + free(missing); + free_fft_settings(&fs); + + return total_time / nits; +} + +int main(int argc, char *argv[]) { + int nsec = 0; + + switch (argc) { + case 1: + nsec = NSEC; + break; + case 2: + nsec = atoi(argv[1]); + break; + default: + break; + }; + + if (nsec == 0) { + printf("Usage: %s [test time in seconds > 0]\n", argv[0]); + exit(EXIT_FAILURE); + } + + printf("*** Benchmarking Zero Polynomial, %d second%s per test.\n", nsec, nsec == 1 ? "" : "s"); + for (int scale = 5; scale <= 15; scale++) { + printf("zero_poly/scale_%d %lu ns/op\n", scale, run_bench(scale, nsec)); + } + + return EXIT_SUCCESS; +}