File: driver.cpp

package info (click to toggle)
gridtools 2.3.9-1
  • links: PTS, VCS
  • area: main
  • in suites: 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 (70 lines) | stat: -rw-r--r-- 2,546 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
/*
 * 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 "interpolate_stencil.hpp"

#include <iostream>

bool verify(double weight, inputs in, outputs out) {
    auto in1_v = in.in1->const_host_view();
    auto in2_v = in.in2->const_host_view();
    auto out_v = out.out->const_host_view();

    // check consistency
    assert(in1_v.lengths() == out_v.lengths());
    assert(in2_v.lengths() == out_v.lengths());

    auto &&len = out_v.lengths();
    for (int k = 0; k < len[2]; ++k)
        for (int i = 0; i < len[0]; ++i)
            for (int j = 0; j < len[1]; ++j)
                if (weight * in1_v(i, j, k) + (1.0 - weight) * in2_v(i, j, k) - out_v(i, j, k) > 1e-8) {
                    std::cerr << "error in " << i << ", " << j << ", " << k << ": "
                              << "expected = " << weight * in1_v(i, j, k) + (1.0 - weight) * in2_v(i, j, k)
                              << ", out = " << out_v(i, j, k) << ", diff = "
                              << weight * in1_v(i, j, k) + (1.0 - weight) * in2_v(i, j, k) - out_v(i, j, k)
                              << std::endl;
                    return false;
                }
    return true;
}
int main(int argc, char **argv) {
    unsigned int d1, d2, d3;
    const double weight = 0.4;

    if (argc != 4) {
        std::cerr << "Usage: " << argv[0] << " dimx dimy dimz\n";
        return 1;
    } else {
        d1 = atoi(argv[1]);
        d2 = atoi(argv[2]);
        d3 = atoi(argv[3]);
    }

    // Add dimensions to the storage builder
    const auto storage_builder = gridtools::storage::builder<storage_traits_t>.dimensions(d1, d2, d3).type<double>();

    // Now we describe the iteration space. In this simple example the iteration space is just described by the full
    // grid (no particular care has to be taken to describe halo points).
    auto grid = gridtools::stencil::make_grid(d1, d2, d3);

    // Create some data stores
    inputs in = {storage_builder.initializer([](int i, int j, int k) { return i + j + k; }).build(),
        storage_builder.initializer([](int i, int j, int k) { return 4 * i + 2 * j + k; }).build()};
    outputs out = {storage_builder.build()};

    // Use the wrapped computation
    make_interpolate_stencil(grid, weight)(in, out);

    if (!verify(weight, in, out)) {
        std::cerr << "Failure" << std::endl;
        return 1;
    }
    std::cout << "Success" << std::endl;
};