| 
 | 1 | +/***************************************************************************  | 
 | 2 | +* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht          *  | 
 | 3 | +* Copyright (c) QuantStack                                                 *  | 
 | 4 | +*                                                                          *  | 
 | 5 | +* Distributed under the terms of the BSD 3-Clause License.                 *  | 
 | 6 | +*                                                                          *  | 
 | 7 | +* The full license is in the file LICENSE, distributed with this software. *  | 
 | 8 | +****************************************************************************/  | 
 | 9 | + | 
 | 10 | +#ifndef XTENSOR_CHUNKED_VIEW_HPP  | 
 | 11 | +#define XTENSOR_CHUNKED_VIEW_HPP  | 
 | 12 | + | 
 | 13 | +#include "xstrided_view.hpp"  | 
 | 14 | +#include "xnoalias.hpp"  | 
 | 15 | + | 
 | 16 | +namespace xt  | 
 | 17 | +{  | 
 | 18 | + | 
 | 19 | +    template <class E>  | 
 | 20 | +    class xchunked_view;  | 
 | 21 | + | 
 | 22 | +    template <class E>  | 
 | 23 | +    class xchunk_iterator  | 
 | 24 | +    {  | 
 | 25 | +    public:  | 
 | 26 | +        xchunk_iterator(xchunked_view<E>& chunked_view, std::size_t chunk_idx);  | 
 | 27 | +        xchunk_iterator() = default;  | 
 | 28 | + | 
 | 29 | +        xchunk_iterator<E>& operator++();  | 
 | 30 | +        xchunk_iterator<E> operator++(int);  | 
 | 31 | +        bool operator==(const xchunk_iterator& other) const;  | 
 | 32 | +        bool operator!=(const xchunk_iterator& other) const;  | 
 | 33 | +        auto operator*();  | 
 | 34 | + | 
 | 35 | +    private:  | 
 | 36 | +        xchunked_view<E>* m_pcv;  | 
 | 37 | +        std::size_t m_ci;  | 
 | 38 | +    };  | 
 | 39 | + | 
 | 40 | +    template <class E>  | 
 | 41 | +    class xchunked_view  | 
 | 42 | +    {  | 
 | 43 | +    public:  | 
 | 44 | +        template <class OE>  | 
 | 45 | +        xchunked_view(OE&& e, std::vector<std::size_t>& chunk_shape);  | 
 | 46 | + | 
 | 47 | +        xchunk_iterator<E> begin();  | 
 | 48 | +        xchunk_iterator<E> end();  | 
 | 49 | + | 
 | 50 | +        template <class OE>  | 
 | 51 | +        xchunked_view<E>& operator=(const OE& e);  | 
 | 52 | + | 
 | 53 | +    private:  | 
 | 54 | +        E m_expression;  | 
 | 55 | +        std::vector<std::size_t> m_shape;  | 
 | 56 | +        std::vector<std::size_t> m_chunk_shape;  | 
 | 57 | +        std::vector<std::size_t> m_shape_of_chunks;  | 
 | 58 | +        std::vector<std::size_t> m_ic;  | 
 | 59 | +        std::size_t m_chunk_nb;  | 
 | 60 | +        xstrided_slice_vector m_sv;  | 
 | 61 | + | 
 | 62 | +        friend class xchunk_iterator<E>;  | 
 | 63 | +    };  | 
 | 64 | + | 
 | 65 | +    template <class E>  | 
 | 66 | +    inline xchunk_iterator<E>::xchunk_iterator(xchunked_view<E>& chunked_view, std::size_t chunk_idx)  | 
 | 67 | +        : m_pcv(&chunked_view)  | 
 | 68 | +        , m_ci(chunk_idx)  | 
 | 69 | +    {  | 
 | 70 | +    }  | 
 | 71 | + | 
 | 72 | +    template <class E>  | 
 | 73 | +    inline xchunk_iterator<E>& xchunk_iterator<E>::operator++()  | 
 | 74 | +    {  | 
 | 75 | +        if (m_ci != m_pcv->m_chunk_nb - 1)  | 
 | 76 | +        {  | 
 | 77 | +            std::size_t di = m_pcv->m_shape.size() - 1;  | 
 | 78 | +            while (true)  | 
 | 79 | +            {  | 
 | 80 | +                if (m_pcv->m_ic[di] + 1 == m_pcv->m_shape_of_chunks[di])  | 
 | 81 | +                {  | 
 | 82 | +                    m_pcv->m_ic[di] = 0;  | 
 | 83 | +                    m_pcv->m_sv[di] = range(0, m_pcv->m_chunk_shape[di]);  | 
 | 84 | +                    if (di == 0)  | 
 | 85 | +                    {  | 
 | 86 | +                        break;  | 
 | 87 | +                    }  | 
 | 88 | +                    else  | 
 | 89 | +                    {  | 
 | 90 | +                        di--;  | 
 | 91 | +                    }  | 
 | 92 | +                }  | 
 | 93 | +                else  | 
 | 94 | +                {  | 
 | 95 | +                    m_pcv->m_ic[di] += 1;  | 
 | 96 | +                    m_pcv->m_sv[di] = range(m_pcv->m_ic[di] * m_pcv->m_chunk_shape[di], (m_pcv->m_ic[di] + 1) * m_pcv->m_chunk_shape[di]);  | 
 | 97 | +                    break;  | 
 | 98 | +                }  | 
 | 99 | +            }  | 
 | 100 | +        }  | 
 | 101 | +        m_ci++;  | 
 | 102 | +        return *this;  | 
 | 103 | +    }  | 
 | 104 | + | 
 | 105 | +    template <class E>  | 
 | 106 | +    inline xchunk_iterator<E> xchunk_iterator<E>::operator++(int)  | 
 | 107 | +    {  | 
 | 108 | +        xchunk_iterator<E> it = *this;  | 
 | 109 | +        ++(*this);  | 
 | 110 | +        return it;  | 
 | 111 | +    }  | 
 | 112 | + | 
 | 113 | +    template <class E>  | 
 | 114 | +    inline bool xchunk_iterator<E>::operator==(const xchunk_iterator& other) const  | 
 | 115 | +    {  | 
 | 116 | +        return m_ci == other.m_ci;  | 
 | 117 | +    }  | 
 | 118 | + | 
 | 119 | +    template <class E>  | 
 | 120 | +    inline bool xchunk_iterator<E>::operator!=(const xchunk_iterator& other) const  | 
 | 121 | +    {  | 
 | 122 | +        return !(*this == other);  | 
 | 123 | +    }  | 
 | 124 | + | 
 | 125 | +    template <class E>  | 
 | 126 | +    inline auto xchunk_iterator<E>::operator*()  | 
 | 127 | +    {  | 
 | 128 | +        auto chunk = strided_view(m_pcv->m_expression, m_pcv->m_sv);  | 
 | 129 | +        return std::make_pair(chunk, m_pcv->m_sv);  | 
 | 130 | +    }  | 
 | 131 | + | 
 | 132 | +    template <class E>  | 
 | 133 | +    template <class OE>  | 
 | 134 | +    inline xchunked_view<E>::xchunked_view(OE&& e, std::vector<std::size_t>& chunk_shape)  | 
 | 135 | +        : m_expression(std::forward<OE>(e))  | 
 | 136 | +        , m_chunk_shape(chunk_shape)  | 
 | 137 | +    {  | 
 | 138 | +        m_shape.resize(e.dimension());  | 
 | 139 | +        const auto& s = e.shape();  | 
 | 140 | +        std::copy(s.cbegin(), s.cend(), m_shape.begin());  | 
 | 141 | +        // compute chunk number in each dimension  | 
 | 142 | +        m_shape_of_chunks.resize(m_shape.size());  | 
 | 143 | +        std::transform  | 
 | 144 | +        (  | 
 | 145 | +            m_shape.cbegin(), m_shape.cend(),  | 
 | 146 | +            m_chunk_shape.cbegin(),  | 
 | 147 | +            m_shape_of_chunks.begin(),  | 
 | 148 | +            [](auto s, auto cs)  | 
 | 149 | +            {  | 
 | 150 | +                std::size_t cn = s / cs;  | 
 | 151 | +                if (s % cs > 0)  | 
 | 152 | +                {  | 
 | 153 | +                    cn++; // edge_chunk  | 
 | 154 | +                }  | 
 | 155 | +                return cn;  | 
 | 156 | +            }  | 
 | 157 | +        );  | 
 | 158 | +        m_ic.resize(m_shape.size());  | 
 | 159 | +        m_chunk_nb = std::accumulate(std::begin(m_shape_of_chunks), std::end(m_shape_of_chunks), std::size_t(1), std::multiplies<>());  | 
 | 160 | +        m_sv.resize(m_shape.size());  | 
 | 161 | +    }  | 
 | 162 | + | 
 | 163 | +    template <class E>  | 
 | 164 | +    inline xchunk_iterator<E> xchunked_view<E>::begin()  | 
 | 165 | +    {  | 
 | 166 | +        auto it = xchunk_iterator<E>(*this, 0);  | 
 | 167 | +        std::transform(m_chunk_shape.begin(), m_chunk_shape.end(), m_sv.begin(),  | 
 | 168 | +                       [](auto size) { return range(0, size); });  | 
 | 169 | +        std::fill(m_ic.begin(), m_ic.end(), std::size_t(0));  | 
 | 170 | +        return it;  | 
 | 171 | +    }  | 
 | 172 | + | 
 | 173 | +    template <class E>  | 
 | 174 | +    inline xchunk_iterator<E> xchunked_view<E>::end()  | 
 | 175 | +    {  | 
 | 176 | +        auto it = xchunk_iterator<E>(*this, m_chunk_nb);  | 
 | 177 | +        return it;  | 
 | 178 | +    }  | 
 | 179 | + | 
 | 180 | +    template <class E>  | 
 | 181 | +    template <class OE>  | 
 | 182 | +    xchunked_view<E>& xchunked_view<E>::operator=(const OE& e)  | 
 | 183 | +    {  | 
 | 184 | +        for (auto it = begin(); it != end(); it++)  | 
 | 185 | +        {  | 
 | 186 | +            auto el = *it;  | 
 | 187 | +            noalias(el.first) = strided_view(e, el.second);  | 
 | 188 | +        }  | 
 | 189 | +        return *this;  | 
 | 190 | +    }  | 
 | 191 | + | 
 | 192 | +    template <class E>  | 
 | 193 | +    auto as_chunked(E&& e, std::vector<std::size_t>& chunk_shape)  | 
 | 194 | +    {  | 
 | 195 | +        return xchunked_view<E>(std::forward<E>(e), chunk_shape);  | 
 | 196 | +    }  | 
 | 197 | + | 
 | 198 | +}  | 
 | 199 | + | 
 | 200 | +#endif  | 
0 commit comments