summaryrefslogtreecommitdiffstats
path: root/src/libixion/compute_engine_test.cpp
blob: 58a45a30e5515febc74d1cf6a6d3918a1142511b (plain)
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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include "test_global.hpp" // This must be the first header to be included.

#include <ixion/compute_engine.hpp>
#include <ixion/module.hpp>
#include <algorithm>
#include <chrono>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <functional>

using std::cout;
using std::endl;

namespace {

using test_func_t = std::function<void()>;

std::vector<std::pair<std::string, test_func_t>> tests;

#define REGISTER_TEST(x) tests.emplace_back(#x, x);

template<typename T>
void print_values(std::string_view msg, const T& values)
{
    cout << msg << ": ";

    if (values.size() <= 20)
        std::copy(values.begin(), values.end(), std::ostream_iterator<typename T::value_type>(cout, " "));
    else
    {
        // Print only the first 15 and last 5 values.
        auto it = values.begin();
        auto it_end = it + 15;

        std::copy(it, it_end, std::ostream_iterator<typename T::value_type>(cout, " "));

        cout << "... ";

        it_end = values.end();
        it = it_end - 5;

        std::copy(it, it_end, std::ostream_iterator<typename T::value_type>(cout, " "));
    }

    cout << endl;
}

void print_summary(const std::shared_ptr<ixion::draft::compute_engine>& engine)
{
    cout << "--" << endl;
    cout << "name: " << engine->get_name() << endl;

    std::vector<uint32_t> values(16384u);

    uint32_t n = 0;
    std::generate(values.begin(), values.end(), [&n] { return n++; });

    ixion::draft::array io{};
    io.uint32 = values.data();
    io.size = values.size();
    io.type = ixion::draft::array_type::uint32;

    print_values("fibonacci input", values);
    {
        std::ostringstream os;
        os << "fibonacci (n=" << values.size() << ")";
        ixion::test::stack_printer __stack_printer__(os.str());
        engine->compute_fibonacci(io);
    }
    print_values("fibonacci output", values);
}

}

void test_create_default()
{
    IXION_TEST_FUNC_SCOPE;

    std::shared_ptr<ixion::draft::compute_engine> p = ixion::draft::compute_engine::create();
    assert(p);
    assert(p->get_name() == "default");
    print_summary(p);
}

void test_create_vulkan()
{
    IXION_TEST_FUNC_SCOPE;

    std::shared_ptr<ixion::draft::compute_engine> p = ixion::draft::compute_engine::create("vulkan");
    assert(p);
    assert(p->get_name() == "vulkan");
    print_summary(p);
}

int main()
{
    ixion::draft::init_modules();

    REGISTER_TEST(test_create_default);
#ifdef BUILD_VULKAN
    REGISTER_TEST(test_create_vulkan);
#endif

    for (auto test : tests)
    {
        cout << "--------------------------" << endl;
        cout << "  " << test.first << endl;
        cout << "--------------------------" << endl;
        test.second();
    }

    return EXIT_SUCCESS;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */