File: adaptor.rst

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 (232 lines) | stat: -rw-r--r-- 7,074 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
.. 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.

Adapting 1-D containers
=======================

*xtensor* can adapt one-dimensional containers in place, and provide them a tensor interface.
Only random access containers can be adapted.

Adapting std::vector
--------------------

The following example shows how to bring an ``std::vector`` into the expression system of
*xtensor*:

.. code::

    #include <cstddef>
    #include <vector>
    #include <xtensor/xarray.hpp>
    #include <xtensor/xadapt.hpp>

    std::vector<double> v = {1., 2., 3., 4., 5., 6. };
    std::vector<std::size_t> shape = { 2, 3 };
    auto a1 = xt::adapt(v, shape);

    xt::xarray<double> a2 = {{ 1., 2., 3.},
                             { 4., 5., 6.}};

    xt::xarray<double> res = a1 + a2;
    // res = {{ 2., 4., 6. }, { 8., 10., 12. }};

``v`` is not copied into ``a1``, so if you change a value in ``a1``, you're actually changing
the corresponding value in ``v``:

.. code::

    a1(0, 0) = 20.;
    // now v is { 20., 2., 3., 4., 5., 6. }

Adapting C-style arrays
-----------------------

*xtensor* provides two ways for adapting a C-style array; the first one does not take the
ownership of the array:

.. code::

    #include <cstddef>
    #include <xtensor/xadapt.hpp>

    void compute(double* data, std::size_t size)
    {
        std::vector<std::size_t> shape = { size };
        auto a = xt::adapt(data, size, xt::no_ownership(), shape);
        a = a + a; // does not modify the size
    }

    int main()
    {
        std::size_t size = 2;
        double* data = new double[size];
        for (int i = 0; i < size; i++)
            data[i] = i;
        std::cout << data << std::endl;
        // prints e.g. 0x557a363b7c20
        compute(data, size);
        std::cout << data << std::endl;
        // prints e.g. 0x557a363b7c20 (same pointer)
        for (int i = 0; i < size; i++)
            std::cout << data[i] << " ";
        std::cout << std::endl;
        // prints 0 2 (data is still available here)
    }

However if you replace :cpp:enumerator:`xt::no_ownership` with :cpp:enumerator:`xt::acquire_ownership`, the adaptor will take
the ownership of the array, meaning it will be deleted when the adaptor is destroyed:

.. code::

    #include <cstddef>
    #include <xtensor/xarray.hpp>
    #include <xtensor/xadapt.hpp>

    void compute(double*& data, std::size_t size)
    {
        // data pointer can be changed, hence double*&
        std::vector<std::size_t> shape = { size };
        auto a = xt::adapt(data, size, xt::acquire_ownership(), shape);
        xt::xarray<double> b {1., 2.};
        b.reshape({2, 1});
        a = a * b; // size has changed, shape is now { 2, 2 }
    }

    int main()
    {
        std::size_t size = 2;
        double* data = new double[size];
        for (int i = 0; i < size; i++)
            data[i] = i;
        std::cout << data << std::endl;
        // prints e.g. 0x557a363b7c20
        compute(data, size);
        std::cout << data << std::endl;
        // prints e.g. 0x557a363b8220 (pointer has changed)
        for (int i = 0; i < size * size; i++)
            std::cout << data[i] << " ";
        std::cout << std::endl;
        // prints e.g. 4.65504e-310 1 0 2 (data has been deleted and is now corrupted)
    }

To safely get the computed data out of the function, you could pass an additional output parameter
to ``compute`` in which you copy the result before exiting the function. Or you can create the
adaptor before calling ``compute`` and pass it to the function:

.. code::

    #include <cstddef>
    #include <xtensor/xarray.hpp>
    #include <xtensor/xadapt.hpp>

    template <class A>
    void compute(A& a)
    {
        xt::xarray<double> b {1., 2.};
        b.reshape({2, 1});
        a = a * b; // size has changed, shape is now { 2, 2 }
    }

    int main()
    {
        std::size_t size = 2;
        double* data = new double[size];
        for (int i = 0; i < size; i++)
            data[i] = i;
        std::vector<std::size_t> shape = { size };
        auto a = xt::adapt(data, size, xt::acquire_ownership(), shape);
        compute(a);
        for (int i = 0; i < size * size; i++)
            std::cout << data[i] << " ";
        std::cout << std::endl;
        // prints 0 1 0 2
    }

Adapting stack-allocated arrays
-------------------------------

Adapting C arrays allocated on the stack is as simple as adapting ``std::vector``:

.. code::

    #include <cstddef>
    #include <vector>
    #include <xtensor/xarray.hpp>
    #include <xtensor/xadapt.hpp>

    double v[6] = {1., 2., 3., 4., 5., 6. };
    std::vector<std::size_t> shape = { 2, 3 };
    auto a1 = xt::adapt(v, shape);

    xt::xarray<double> a2 = {{ 1., 2., 3.},
                             { 4., 5., 6.}};

    xt::xarray<double> res = a1 + a2;
    // res = {{ 2., 4., 6. }, { 8., 10., 12. }};

``v`` is not copied into ``a1``, so if you change a value in ``a1``, you're actually changing
the corresponding value in ``v``:

.. code::

    a1(0, 0) = 20.;
    // now v is { 20., 2., 3., 4., 5., 6. }

Adapting C++ smart pointers
---------------------------

If you want to manage your data with shared or unique pointers, you can use the
:cpp:func:`xt::adapt_smart_ptr` function of xtensor.
It will automatically increment the reference count of shared pointers upon creation, and decrement upon deletion.

.. code::

    #include <memory>
    #include <xtensor/xadapt.hpp>
    #include <xtensor/xio.hpp>

    std::shared_ptr<double> sptr(new double[8], std::default_delete<double[]>());
    sptr.get()[2] = 321.;
    auto xptr = xt::adapt_smart_ptr(sptr, {4, 2});
    xptr(1, 3) = 123.;
    std::cout << xptr;

Or if you operate on shared pointers that do not directly point to the underlying
buffer, you can pass the data pointer and the smart pointer (to manage the underlying
memory) as follows:

.. code::

    #include <memory>
    #include <xtensor/xadapt.hpp>
    #include <xtensor/xio.hpp>

    struct Buffer {
        Buffer(std::vector<double>& buf) : m_buf(buf) {}
        ~Buffer() { std::cout << "deleted" << std::endl; }
        std::vector<double> m_buf;
    };

    auto data = std::vector<double>{1,2,3,4,5,6,7,8};
    auto shared_buf = std::make_shared<Buffer>(data);
    auto unique_buf = std::make_unique<Buffer>(data);

    std::cout << shared_buf.use_count() << std::endl;
    {
        auto obj = xt::adapt_smart_ptr(shared_buf.get()->m_buf.data(),
                                       {2, 4}, shared_buf);
        // Use count increased to 2
        std::cout << shared_buf.use_count() << std::endl;
        std::cout << obj << std::endl;
    }
    // Use count reset to 1
    std::cout << shared_buf.use_count() << std::endl;

    {
        auto obj = xt::adapt_smart_ptr(unique_buf.get()->m_buf.data(),
                                       {2, 4}, std::move(unique_buf));
        std::cout << obj << std::endl;
    }