File: benchmark_view_adapt.cpp

package info (click to toggle)
xtensor 0.25.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,476 kB
  • sloc: cpp: 65,302; makefile: 202; python: 171; javascript: 8
file content (79 lines) | stat: -rw-r--r-- 2,549 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/***************************************************************************
 * Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht    *
 *                                                                          *
 * Distributed under the terms of the BSD 3-Clause License.                 *
 *                                                                          *
 * The full license is in the file LICENSE, distributed with this software. *
 ****************************************************************************/

#ifndef BENCHMARK_VIEW_ADAPT_HPP
#define BENCHMARK_VIEW_ADAPT_HPP

#include <benchmark/benchmark.h>

#include "xtensor/xadapt.hpp"
#include "xtensor/xfixed.hpp"
#include "xtensor/xnoalias.hpp"
#include "xtensor/xrandom.hpp"
#include "xtensor/xtensor.hpp"
#include "xtensor/xview.hpp"

namespace xt
{
    namespace benchmark_view_adapt
    {
        using T2 = xt::xtensor_fixed<double, xt::xshape<2, 2>>;

        T2 foo(const T2& A)
        {
            return 2. * A;
        }

        void random_view(benchmark::State& state)
        {
            xt::xtensor<double, 4> A = xt::random::randn<double>({2000, 8, 2, 2});
            xt::xtensor<double, 4> B = xt::empty<double>(A.shape());

            for (auto _ : state)
            {
                for (size_t i = 0; i < A.shape()[0]; ++i)
                {
                    for (size_t j = 0; j < A.shape()[1]; ++j)
                    {
                        auto a = xt::view(A, i, j);
                        auto b = xt::view(B, i, j);

                        xt::noalias(b) = foo(a);
                    }
                }
                benchmark::DoNotOptimize(B.data());
            }
        }

        void random_adapt(benchmark::State& state)
        {
            xt::xtensor<double, 4> A = xt::random::randn<double>({2000, 8, 2, 2});
            xt::xtensor<double, 4> B = xt::empty<double>(A.shape());

            for (auto _ : state)
            {
                for (size_t i = 0; i < A.shape()[0]; ++i)
                {
                    for (size_t j = 0; j < A.shape()[1]; ++j)
                    {
                        auto a = xt::adapt(&A(i, j, 0, 0), xt::xshape<2, 2>());
                        auto b = xt::adapt(&B(i, j, 0, 0), xt::xshape<2, 2>());

                        xt::noalias(b) = foo(a);
                    }
                }
                benchmark::DoNotOptimize(B.data());
            }
        }

        BENCHMARK(random_view);
        BENCHMARK(random_adapt);
    }
}

#endif