From 34917b834143c180562fec66a53d1180251a4b7d Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Mon, 15 Apr 2019 18:22:14 +0200 Subject: Initial commit. --- membw.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 membw.c (limited to 'membw.c') diff --git a/membw.c b/membw.c new file mode 100644 index 0000000..0ae9d7b --- /dev/null +++ b/membw.c @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2019 Anton Khirnov + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include + +#include + +#define REPEATS_DEFAULT 10 + +void membw_mem_write_avx(uint8_t *dst, size_t dstlen); +void membw_mem_read_avx(uint8_t *src, size_t srclen); + +static inline int64_t gettime(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec; +} + +void print_usage(int argc, char **argv) +{ + fprintf(stderr, + "Usage: %s [repeats]\n" + "\tN: array size in kB\n", + argv[0]); +} + +int main(int argc, char **argv) +{ + int ret; + size_t N; + unsigned int repeats = REPEATS_DEFAULT; + + uint8_t *mem; + + int64_t start; + int64_t duration = 0; + + if (argc < 2) { + print_usage(argc, argv); + return 1; + } + + N = strtol(argv[1], NULL, 0); + if (N <= 0) { + fprintf(stderr, "Invalid N: %zu\n", N); + print_usage(argc, argv); + return 1; + } + N *= 1024; + + if (argc >= 3) { + repeats = strtol(argv[2], NULL, 0); + if (repeats <= 0) { + fprintf(stderr, "Invalid repeats: %u\n", repeats); + return 1; + } + } + + ret = posix_memalign((void**)&mem, 64, N); + if (ret != 0) { + fprintf(stderr, "Error allocating memory\n"); + return 2; + } + + start = gettime(); + + for (unsigned int i = 0; i < repeats; i++) + membw_mem_write_avx(mem, N); + + duration = gettime() - start; + + fprintf(stdout, "Written %u x %g MB in %g ms: write bandwidth: %g MB/s\n", + repeats, (double)N / (1024 * 1024), duration / 1e3, + (double)N * repeats / duration * 1e6 / (1024 * 1024)); + + start = gettime(); + + for (unsigned int i = 0; i < repeats; i++) + membw_mem_read_avx(mem, N); + + duration = gettime() - start; + + fprintf(stdout, "Read %u x %g MB in %g ms: read bandwidth: %g MB/s\n", + repeats, (double)N / (1024 * 1024), duration / 1e3, + (double)N * repeats / duration * 1e6 / (1024 * 1024)); + + free(mem); + + return 0; +} -- cgit v1.2.3