dilation_vertical_block2d.hpp 9.68 KB
Newer Older
Edwin Carlinet's avatar
Edwin Carlinet committed
1
2
3
4
5
6
7
8
9
10
11
#pragma once
#include <experimental/simd>
#include <cassert>
#include <range/v3/functional/concepts.hpp>

#include <mln/core/image/experimental/ndimage_fwd.hpp>
#include <mln/core/box.hpp>

namespace mln::morpho::experimental::details
{

Edwin Carlinet's avatar
Edwin Carlinet committed
12
13
14
15
16
17
18
19
20
21
  template <class T, class I, class J, class BinaryFunction>
  void running_max_2d(I& input, J& output, BinaryFunction sup, mln::experimental::box2d roi, int k, bool use_extension, bool vertical);


  /******************************************/
  /****          Implementation          ****/
  /******************************************/


  class TileLoaderBase
Edwin Carlinet's avatar
Edwin Carlinet committed
22
  {
Edwin Carlinet's avatar
Edwin Carlinet committed
23
24
25
    // Load tile from memory (roi is in the vertical layout coordinates system)
    virtual void load_tile(std::byte* out, std::ptrdiff_t byte_stride, mln::experimental::box2d roi) = 0;
  };
Edwin Carlinet's avatar
Edwin Carlinet committed
26

Edwin Carlinet's avatar
Edwin Carlinet committed
27
28
29
30
31
32
33
34
35
  class TileWriterBase
  {
    // Copy a line to output (coordinates and size are in the vertical layout coordinates system)
    virtual void write_tile(const std::byte* in,  std::ptrdiff_t byte_stride, mln::experimental::box2d roi) = 0;
  };

  class vertical_running_max_algo_base_t
  {
  private:
Edwin Carlinet's avatar
Edwin Carlinet committed
36
37
38
39
40
    // Accumulate the supremum column-wise (eq to the python A.cumsum(axis=0))
    virtual void partial_sum_block2d(const std::byte* __restrict in, std::byte* __restrict out, int width, int height,
                                     std::ptrdiff_t in_byte_stride, std::ptrdiff_t out_byte_stride) = 0;

    // Apply PW OUT[x] = SUP(A[x], B[x])
Edwin Carlinet's avatar
Edwin Carlinet committed
41
    virtual void apply_sup(std::byte* __restrict A, std::byte* __restrict B, std::byte* __restrict OUT, int width) = 0;
Edwin Carlinet's avatar
Edwin Carlinet committed
42
43
44
45

    virtual int         get_block_width() const = 0;
    virtual std::size_t get_sample_size() const = 0;

Edwin Carlinet's avatar
Edwin Carlinet committed
46
47
48
    TileLoaderBase* m_tile_loader = nullptr;
    TileWriterBase* m_tile_writer = nullptr;

Edwin Carlinet's avatar
Edwin Carlinet committed
49
50
51
  public:
    // Apply the running max algorithm over a block
    // Memory has already been allocated
Edwin Carlinet's avatar
Edwin Carlinet committed
52
53
54
    void running_max_block2d(std::byte* f, std::byte* g, std::byte* h, std::ptrdiff_t f_byte_stride,
                             std::ptrdiff_t g_byte_stride, std::ptrdiff_t h_byte_stride, mln::experimental::box2d roi,
                             int k, bool use_extension);
Edwin Carlinet's avatar
Edwin Carlinet committed
55

Edwin Carlinet's avatar
Edwin Carlinet committed
56
57
    // Apply the running max algorithm over a roi using tiling
    void execute(mln::experimental::box2d roi, int k, bool use_extension, bool vertical = true);
Edwin Carlinet's avatar
Edwin Carlinet committed
58

Edwin Carlinet's avatar
Edwin Carlinet committed
59
60
    void set_tile_reader(TileLoaderBase* r) { m_tile_loader = r; }
    void set_tile_writer(TileWriterBase* w) { m_tile_writer = w; }
Edwin Carlinet's avatar
Edwin Carlinet committed
61
62
63
64
65
66
67
68
69
70
71
72
73
  };


  template <class T, class BinaryFunction>
  class vertical_running_max_algo_t : public vertical_running_max_algo_base_t
  {
    using simd_t                     = std::experimental::simd<T>;
    static constexpr int WARP_SIZE   = simd_t::size();
    static constexpr int BLOCK_WIDTH = WARP_SIZE * 4;

    static_assert(::ranges::regular_invocable<BinaryFunction, simd_t, simd_t>);
    static_assert(::ranges::regular_invocable<BinaryFunction, T, T>);

Edwin Carlinet's avatar
Edwin Carlinet committed
74
    void apply_sup(std::byte* __restrict A, std::byte* __restrict B, std::byte* __restrict OUT, int n) final;
Edwin Carlinet's avatar
Edwin Carlinet committed
75
76
77
78
79
80
81
82
83
84
85
86
    void partial_sum_block2d(const std::byte* __restrict in, std::byte* __restrict out, int width, int height,
                             std::ptrdiff_t in_byte_stride, std::ptrdiff_t out_byte_stride) final;

    int         get_block_width() const final { return BLOCK_WIDTH; }
    std::size_t get_sample_size() const final { return sizeof(T); }


  public:
    vertical_running_max_algo_t(BinaryFunction sup)
      : m_sup{std::move(sup)}
    {
    }
Edwin Carlinet's avatar
Edwin Carlinet committed
87
88
89

  protected:
    BinaryFunction m_sup;
Edwin Carlinet's avatar
Edwin Carlinet committed
90
91
  };

Edwin Carlinet's avatar
Edwin Carlinet committed
92
93
94
95




Edwin Carlinet's avatar
Edwin Carlinet committed
96
  template <class T, class BinaryFunction>
Edwin Carlinet's avatar
Edwin Carlinet committed
97
  void vertical_running_max_algo_t<T, BinaryFunction>::apply_sup(std::byte* __restrict A_, std::byte* __restrict B_, std::byte* __restrict out_, int width)
Edwin Carlinet's avatar
Edwin Carlinet committed
98
  {
Edwin Carlinet's avatar
Edwin Carlinet committed
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
    const T* A   = (T*)A_;
    const T* B   = (T*)B_;
    T*       out = (T*)out_;

    const int     K               = width / WARP_SIZE;
    const int     rem             = width % WARP_SIZE;

    assert(width <= BLOCK_WIDTH);
    for (int k = 0; k < K; k++)
    {
      simd_t a;
      simd_t b;
      simd_t c;
      a.copy_from(A + k * WARP_SIZE, std::experimental::element_aligned);
      b.copy_from(B + k * WARP_SIZE, std::experimental::element_aligned);
      c = m_sup(a, b);
      c.copy_to(out + k * WARP_SIZE, std::experimental::element_aligned);
    }

    if (rem > 0)
    {
      A += K * WARP_SIZE;
      B += K * WARP_SIZE;
      out += K * WARP_SIZE;
      std::transform(A, A + rem, B, out, m_sup);
    }
Edwin Carlinet's avatar
Edwin Carlinet committed
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
  }


  template <class T, class BinaryFunction>
  void vertical_running_max_algo_t<T, BinaryFunction>::partial_sum_block2d(const std::byte* __restrict in, std::byte* __restrict out, int width, int height, std::ptrdiff_t in_byte_stride, std::ptrdiff_t out_byte_stride)
  {
    using simd_t = std::experimental::simd<T>;


    constexpr int MAX_WARP_COUNT  = BLOCK_WIDTH / WARP_SIZE;
    const int K   = width / WARP_SIZE;
    const int rem = width % WARP_SIZE;

    //fmt::print("BLOCK_WIDTH={} width={}\n", BLOCK_WIDTH, width);
    //fmt::print("WARP_SIZE={}\n", WARP_SIZE);

    assert(width <= BLOCK_WIDTH);
    std::memcpy(out, in, sizeof(T) * width);

    // By block
    if (K > 0)
    {

      simd_t xsum[MAX_WARP_COUNT];

      for (int k = 0; k < K; k++)
        xsum[k].copy_from((T*)in + k * WARP_SIZE, std::experimental::element_aligned);

      // Next lines
      for (int y = 1; y < height; ++y)
      {
        const T* in_lineptr  = (const T*)(in + y * in_byte_stride);
        T*       out_lineptr = (T*)(out + y * out_byte_stride);

        for (int k = 0; k < K; k++)
        {
          simd_t v;
          v.copy_from(in_lineptr + k * WARP_SIZE, std::experimental::element_aligned);
          xsum[k] = m_sup(xsum[k], v);
          xsum[k].copy_to(out_lineptr + k * WARP_SIZE, std::experimental::element_aligned);
        }
      }
    }

    if (rem > 0)
    {
      in += K * WARP_SIZE * sizeof(T);
      out += K * WARP_SIZE * sizeof(T);

      T  xsum[WARP_SIZE];
      std::memcpy(xsum, in, sizeof(T) * rem);
      for (int y = 1; y < height; ++y)
      {
        const T* in_lineptr  = (const T*)(in + y * in_byte_stride);
        T*       out_lineptr = (T*)(out + y * out_byte_stride);

        for (int c = 0; c < rem; ++c)
        {
          xsum[c] = m_sup(xsum[c], in_lineptr[c]);
          out_lineptr[c] = xsum[c];
        }
      }
    }
  }

  template <class I, class T>
  [[gnu::noinline]] void copy_block(I& in, mln::experimental::box2d roi, T* __restrict out, std::ptrdiff_t out_stride)
  {
Edwin Carlinet's avatar
Edwin Carlinet committed
193
194
    const int x0     = roi.x();
    const int y0     = roi.y();
Edwin Carlinet's avatar
Edwin Carlinet committed
195
196
197
198
199
200
201
202
203
204
205
206

    for (int y = 0; y < roi.height(); ++y)
    {
      T* lineptr = out + y * out_stride;
      for (int x = 0; x < roi.width(); ++x)
        lineptr[x] = in.at({x0 + x, y0 + y});
    }
  }

  template <class I, class T>
  [[gnu::noinline]] void copy_block(T* __restrict in, std::ptrdiff_t istride, mln::experimental::box2d roi, I& out)
  {
Edwin Carlinet's avatar
Edwin Carlinet committed
207
208
    const int x0     = roi.x();
    const int y0     = roi.y();
Edwin Carlinet's avatar
Edwin Carlinet committed
209
210
211
212
213
214
215
216
217
218
219
220

    for (int y = 0; y < roi.height(); ++y)
    {
      const T* lineptr = in + y * istride;
      for (int x = 0; x < roi.width(); ++x)
        out.at({x0 + x, y0 + y}) = lineptr[x];
    }
  }

  template <class I, class T>
  [[gnu::noinline]] void transpose_block2d(I& in, mln::experimental::box2d input_roi, T* __restrict out, std::ptrdiff_t out_stride)
  {
Edwin Carlinet's avatar
Edwin Carlinet committed
221
222
    const int x0     = input_roi.x();
    const int y0     = input_roi.y();
Edwin Carlinet's avatar
Edwin Carlinet committed
223
224
225
226
227
228
229
230
231
232


    for (int y = 0; y < input_roi.height(); ++y)
      for (int x = 0; x < input_roi.width(); ++x)
        *(out + x * out_stride + y) = in.at({x0 + x, y0 + y});
  }

  template <class I, class T>
  [[gnu::noinline]] void transpose_block2d(T* __restrict in, std::ptrdiff_t istride, mln::experimental::box2d output_roi, I& out)
  {
Edwin Carlinet's avatar
Edwin Carlinet committed
233
234
    const int x0     = output_roi.x();
    const int y0     = output_roi.y();
Edwin Carlinet's avatar
Edwin Carlinet committed
235
236
237

    for (int y = 0; y < output_roi.height(); ++y)
      for (int x = 0; x < output_roi.width(); ++x)
Edwin Carlinet's avatar
Edwin Carlinet committed
238
        out.at({x0 + x, y0 + y}) =  *(in + x * istride + y);
Edwin Carlinet's avatar
Edwin Carlinet committed
239
240
241
242
243
  }



  template <class I, class T>
Edwin Carlinet's avatar
Edwin Carlinet committed
244
  class TileLoader : public TileLoaderBase
Edwin Carlinet's avatar
Edwin Carlinet committed
245
  {
Edwin Carlinet's avatar
Edwin Carlinet committed
246
247
248
  public:
    // Load tile from memory (roi is in the vertical layout coordinates system)
    void load_tile(std::byte* out, std::ptrdiff_t byte_stride, mln::experimental::box2d roi) override
Edwin Carlinet's avatar
Edwin Carlinet committed
249
    {
Edwin Carlinet's avatar
Edwin Carlinet committed
250
251
252
      if (m_vertical)
        copy_block(*m_input, roi, (T*)out, byte_stride / sizeof(T));
      else
Edwin Carlinet's avatar
Edwin Carlinet committed
253
      {
Edwin Carlinet's avatar
Edwin Carlinet committed
254
255
        mln::experimental::box2d region(roi.y(), roi.x(), roi.height(), roi.width());
        transpose_block2d(*m_input, region, (T*)out, byte_stride / sizeof(T));
Edwin Carlinet's avatar
Edwin Carlinet committed
256
257
258
      }
    }

Edwin Carlinet's avatar
Edwin Carlinet committed
259
    TileLoader(I& input, bool vertical) : m_input(&input), m_vertical{vertical} {}
Edwin Carlinet's avatar
Edwin Carlinet committed
260

Edwin Carlinet's avatar
Edwin Carlinet committed
261
262
263
264
  private:
    I*   m_input;
    bool m_vertical;
  };
Edwin Carlinet's avatar
Edwin Carlinet committed
265
266

  template <class I, class T>
Edwin Carlinet's avatar
Edwin Carlinet committed
267
  class TileWriter : public TileWriterBase
Edwin Carlinet's avatar
Edwin Carlinet committed
268
  {
Edwin Carlinet's avatar
Edwin Carlinet committed
269
270
271
  public:
    // Copy a line to output (coordinates and size are in the vertical layout coordinates system)
    void write_tile(const std::byte* in, std::ptrdiff_t byte_stride, mln::experimental::box2d roi) override
Edwin Carlinet's avatar
Edwin Carlinet committed
272
    {
Edwin Carlinet's avatar
Edwin Carlinet committed
273
274
275
      if (m_vertical)
        copy_block((const T*)in, byte_stride / sizeof(T), roi, *m_output);
      else
Edwin Carlinet's avatar
Edwin Carlinet committed
276
      {
Edwin Carlinet's avatar
Edwin Carlinet committed
277
278
        mln::experimental::box2d region(roi.y(), roi.x(), roi.height(), roi.width());
        transpose_block2d((const T*)in, byte_stride / sizeof(T), region, *m_output);
Edwin Carlinet's avatar
Edwin Carlinet committed
279
      }
Edwin Carlinet's avatar
Edwin Carlinet committed
280
    }
Edwin Carlinet's avatar
Edwin Carlinet committed
281

Edwin Carlinet's avatar
Edwin Carlinet committed
282
283
284
285
286
287
    TileWriter(I& output, bool vertical) : m_output(&output), m_vertical{vertical} {}

  private:
    I*   m_output;
    bool m_vertical;
  };
Edwin Carlinet's avatar
Edwin Carlinet committed
288
289


Edwin Carlinet's avatar
Edwin Carlinet committed
290
291
292
293
294
295
296
297
298
299
  template <class T, class I, class J, class BinaryFunction>
  void running_max_2d(I& input, J& output, BinaryFunction sup, mln::experimental::box2d roi, int k, bool use_extension, bool vertical)
  {
    TileLoader<I, T> r(input, vertical);
    TileWriter<J, T> w(output, vertical);

    vertical_running_max_algo_t<T, BinaryFunction> alg(sup);
    alg.set_tile_reader(&r);
    alg.set_tile_writer(&w);
    alg.execute(roi, k, use_extension, vertical);
Edwin Carlinet's avatar
Edwin Carlinet committed
300
301
302
  }


Edwin Carlinet's avatar
Edwin Carlinet committed
303

Edwin Carlinet's avatar
Edwin Carlinet committed
304
} // namespace mln::morpho::details