File: test_2306_cppyy_jit.py

package info (click to toggle)
python-awkward 2.8.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,932 kB
  • sloc: python: 178,875; cpp: 33,828; sh: 432; makefile: 21; javascript: 8
file content (117 lines) | stat: -rw-r--r-- 2,895 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
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
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward/blob/main/LICENSE

from __future__ import annotations

import packaging.version
import pytest

import awkward as ak
import awkward._connect.cling

cppyy = pytest.importorskip("cppyy")


def test_array_as_generated_dataset():
    array = ak.Array(
        [
            [{"x": 1, "y": [1.1]}, {"x": 2, "y": [2.2, 0.2]}],
            [],
            [{"x": 3, "y": [3.0, 0.3, 3.3]}],
        ]
    )

    generator = ak._connect.cling.togenerator(array.layout.form, flatlist_as_rvec=False)
    lookup = ak._lookup.Lookup(array.layout)

    source_code = f"""
    double go_fast(ssize_t length, ssize_t* ptrs) {{
        auto awkward_array = {generator.dataset()};
        double out = 0.0;

        for (auto list : awkward_array) {{
            for (auto record : list) {{
                for (auto item : record.y()) {{
                    out += item;
                }}
            }}
        }}

        return out;
    }}
    """

    generator.generate(cppyy.cppdef)
    cppyy.cppdef(source_code)
    out = cppyy.gbl.go_fast(len(array), lookup.arrayptrs)
    assert out == ak.sum(array["y"])


@pytest.mark.skipif(
    packaging.version.Version(cppyy.__version__) < packaging.version.Version("3.0.1"),
    reason="Awkward Array can only work with cppyy 3.0.1 or later.",
)
def test_array_as_type():
    array = ak.Array(
        [
            [{"x": 1, "y": [1.1]}, {"x": 2, "y": [2.2, 0.2]}],
            [],
            [{"x": 3, "y": [3.0, 0.3, 3.3]}],
        ]
    )

    source_code_cpp = f"""
    double go_fast_cpp({array.cpp_type} awkward_array) {{
        double out = 0.0;

        for (auto list : awkward_array) {{
            for (auto record : list) {{
                for (auto item : record.y()) {{
                    out += item;
                }}
            }}
        }}

        return out;
    }}
    """

    cppyy.cppdef(source_code_cpp)

    out = cppyy.gbl.go_fast_cpp(array)
    assert out == ak.sum(array["y"])


@pytest.mark.skipif(
    packaging.version.Version(cppyy.__version__) < packaging.version.Version("3.0.1"),
    reason="Awkward Array can only work with cppyy 3.0.1 or later.",
)
def test_array_as_templated_type():
    array = ak.Array(
        [
            [{"x": 1, "y": [1.1]}, {"x": 2, "y": [2.2, 0.2]}],
            [],
            [{"x": 3, "y": [3.0, 0.3, 3.3]}],
        ]
    )

    source_code_cpp = """
    template<typename T>
    double go_fast_cpp_2(T& awkward_array) {
        double out = 0.0;

        for (auto list : awkward_array) {
            for (auto record : list) {
                for (auto item : record.y()) {
                    out += item;
                }
            }
        }

        return out;
    }
    """

    cppyy.cppdef(source_code_cpp)

    out = cppyy.gbl.go_fast_cpp_2[array.cpp_type](array)
    assert out == ak.sum(array["y"])