File: test_tmp_storage_sid.cpp

package info (click to toggle)
gridtools 2.3.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,480 kB
  • sloc: cpp: 228,792; python: 17,561; javascript: 9,164; ansic: 4,101; sh: 850; makefile: 231; f90: 201
file content (166 lines) | stat: -rw-r--r-- 6,301 bytes parent folder | download | duplicates (2)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * GridTools
 *
 * Copyright (c) 2014-2023, ETH Zurich
 * All rights reserved.
 *
 * Please, refer to the LICENSE file in the root directory.
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <gridtools/stencil/cpu_ifirst/tmp_storage_sid.hpp>

#include <gtest/gtest.h>

#include <gridtools/sid/concept.hpp>
#include <gridtools/stencil/common/extent.hpp>
#include <gridtools/thread_pool/omp.hpp>

using namespace gridtools;
using namespace literals;
using namespace stencil;
using namespace cpu_ifirst_backend;

static constexpr std::size_t byte_alignment = 64;

TEST(tmp_storage_sid, allocator) {
    tmp_allocator allocator;

    std::size_t n = 100;
    auto ptr_holder = allocate(allocator, meta::lazy::id<double>(), n);

    double *ptr = ptr_holder();
    EXPECT_EQ(reinterpret_cast<std::uintptr_t>(ptr) % byte_alignment, 0);

    for (std::size_t i = 0; i < n; ++i) {
        ptr[i] = 0;
        EXPECT_EQ(ptr[i], 0);
    }
}

TEST(tmp_storage_sid, nonzero_k_extents) {
    using extent_t = extent<-1, 2, -2, 3, -1, 2>;
    pos3<std::size_t> block_size{12, 2, 8};

    tmp_allocator allocator;
    auto tmp = make_tmp_storage<double, extent_t, false, thread_pool::omp>(allocator, block_size);

    using tmp_t = decltype(tmp);

    static_assert(is_sid<tmp_t>());
    static_assert(std::is_same_v<sid::ptr_type<tmp_t>, double *>);

    auto f = [](int_t i, int_t j, int_t k, int_t t) { return i + j * 100 + k * 200 + t * 400; };

    // check write and read
#pragma omp parallel
    {
        const int_t thread = omp_get_thread_num();
        auto strides = sid::get_strides(tmp);

        double *ptr = sid::get_origin(tmp)();
        // shift to origin of thread
        sid::shift(ptr, sid::get_stride<dim::thread>(strides), thread);

        // shift to very first data point in temporary
        sid::shift(ptr, sid::get_stride<dim::i>(strides), extent_t::iminus::value);
        sid::shift(ptr, sid::get_stride<dim::j>(strides), extent_t::jminus::value);
        sid::shift(ptr, sid::get_stride<dim::k>(strides), extent_t::kminus::value);

        const int_t size_i = extent_t::extend(dim::i(), block_size.i);
        const int_t size_j = extent_t::extend(dim::j(), block_size.j);
        const int_t size_k = extent_t::extend(dim::k(), block_size.k);
        for (int_t j = 0; j < size_j; ++j) {
            for (int_t k = 0; k < size_k; ++k) {
                for (int_t i = 0; i < size_i; ++i) {
                    // check alignment of first data point inside domain
                    if (i == -extent_t::iminus::value) {
                        EXPECT_EQ(reinterpret_cast<std::uintptr_t>(ptr) % byte_alignment, 0);
                    }
                    *ptr = f(i, j, k, thread);
                    sid::shift(ptr, sid::get_stride<dim::i>(strides), 1_c);
                }
                sid::shift(ptr, sid::get_stride<dim::i>(strides), -size_i);
                sid::shift(ptr, sid::get_stride<dim::k>(strides), 1_c);
            }
            sid::shift(ptr, sid::get_stride<dim::k>(strides), -size_k);
            sid::shift(ptr, sid::get_stride<dim::j>(strides), 1_c);
        }
        sid::shift(ptr, sid::get_stride<dim::j>(strides), -size_j);

#pragma omp barrier

        for (int_t j = 0; j < size_j; ++j) {
            for (int_t k = 0; k < size_k; ++k) {
                for (int_t i = 0; i < size_i; ++i) {
                    // check that previously expected value was not overwritten
                    EXPECT_EQ(*ptr, f(i, j, k, thread));
                    sid::shift(ptr, sid::get_stride<dim::i>(strides), 1_c);
                }
                sid::shift(ptr, sid::get_stride<dim::i>(strides), -size_i);
                sid::shift(ptr, sid::get_stride<dim::k>(strides), 1_c);
            }
            sid::shift(ptr, sid::get_stride<dim::k>(strides), -size_k);
            sid::shift(ptr, sid::get_stride<dim::j>(strides), 1_c);
        }
    }
}

TEST(tmp_storage_sid, zero_k_extents) {
    using extent_t = extent<-1, 2, -2, 3, 0, 0>;
    pos3<std::size_t> block_size{12, 5, 1};

    tmp_allocator allocator;
    auto tmp = make_tmp_storage<double, extent_t, true, thread_pool::omp>(allocator, block_size);

    using tmp_t = decltype(tmp);

    static_assert(is_sid<tmp_t>());
    static_assert(std::is_same_v<sid::ptr_type<tmp_t>, double *>);

    auto f = [](int_t i, int_t j, int_t t) { return i + j * 100 + t * 200; };

    // check write and read
#pragma omp parallel
    {
        const int_t thread = omp_get_thread_num();
        auto strides = sid::get_strides(tmp);

        double *ptr = sid::get_origin(tmp)();
        // shift to origin of thread
        sid::shift(ptr, sid::get_stride<dim::thread>(strides), thread);

        // shift to very first data point in temporary
        sid::shift(ptr, sid::get_stride<dim::i>(strides), extent_t::iminus::value);
        sid::shift(ptr, sid::get_stride<dim::j>(strides), extent_t::jminus::value);
        sid::shift(ptr, sid::get_stride<dim::k>(strides), extent_t::kminus::value);

        const int_t size_i = extent_t::extend(dim::i(), block_size.i);
        const int_t size_j = extent_t::extend(dim::j(), block_size.j);
        for (int_t j = 0; j < size_j; ++j) {
            for (int_t i = 0; i < size_i; ++i) {
                // check alignment of first data point inside domain
                if (i == -extent_t::iminus::value) {
                    EXPECT_EQ(reinterpret_cast<std::uintptr_t>(ptr) % byte_alignment, 0);
                }
                *ptr = f(i, j, thread);
                sid::shift(ptr, sid::get_stride<dim::i>(strides), 1_c);
            }
            sid::shift(ptr, sid::get_stride<dim::i>(strides), -size_i);
            sid::shift(ptr, sid::get_stride<dim::j>(strides), 1_c);
        }
        sid::shift(ptr, sid::get_stride<dim::j>(strides), -size_j);

#pragma omp barrier

        for (int_t j = 0; j < size_j; ++j) {
            for (int_t i = 0; i < size_i; ++i) {
                // check that previously expected value was not overwritten
                EXPECT_EQ(*ptr, f(i, j, thread));
                sid::shift(ptr, sid::get_stride<dim::i>(strides), 1_c);
            }
            sid::shift(ptr, sid::get_stride<dim::i>(strides), -size_i);
            sid::shift(ptr, sid::get_stride<dim::j>(strides), 1_c);
        }
    }
}