dilation_vertical_block2d.hpp 9.58 KB
Newer Older
Edwin Carlinet's avatar
Edwin Carlinet committed
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#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
{

  class vertical_running_max_algo_base_t
  {

    // 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])
    virtual void apply_sup(std::byte* __restrict A, std::byte* __restrict B, std::byte* __restrict OUT, std::size_t n) = 0;

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

  public:
    // Apply the running max algorithm over a block
    // Memory has already been allocated
    void running_max_block2d(std::byte* f, std::byte* g, std::byte* h, int width, int height, std::ptrdiff_t f_byte_stride,
                             std::ptrdiff_t g_byte_stride, std::ptrdiff_t h_byte_stride, int k, bool use_extension);


    // Running the 2D dilation vertically using tiling
    template <class I, class T>
    void running_max_v2d(I& in, mln::experimental::image2d<T>& out, mln::experimental::box2d roi, int k, bool use_extension);

    // Running the 2D dilation vertically using tiling
    template <class I, class T>
    void running_max_h2d(I& in, mln::experimental::image2d<T>& out, mln::experimental::box2d roi, int k, bool use_extension);
  };


  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>);

    void apply_sup(std::byte* __restrict A, std::byte* __restrict B, std::byte* __restrict OUT, std::size_t n) final;
    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); }

    BinaryFunction m_sup;

  public:
    vertical_running_max_algo_t(BinaryFunction sup)
      : m_sup{std::move(sup)}
    {
    }
  };

  template <class T, class BinaryFunction>
  void vertical_running_max_algo_t<T, BinaryFunction>::apply_sup(std::byte* __restrict A, std::byte* __restrict B, std::byte* __restrict out, std::size_t n)
  {
    std::transform((T*)A, (T*)A + n, (T*)B, (T*)out, m_sup);
  }


  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)
  {
    const int x0     = roi.tl().x();
    const int y0     = roi.tl().y();

    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)
  {
    const int x0     = roi.tl().x();
    const int y0     = roi.tl().y();

    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)
  {
    const int x0     = input_roi.tl().x();
    const int y0     = input_roi.tl().y();


    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)
  {
    const int x0     = output_roi.tl().x();
    const int y0     = output_roi.tl().y();

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




  template <class I, class T>
  void vertical_running_max_algo_base_t::running_max_v2d(I& in, mln::experimental::image2d<T>& out, mln::experimental::box2d roi, int k, bool use_extension)
  {
    int            kBlockWidth    = this->get_block_width();
    auto           sz             = this->get_sample_size();
    std::ptrdiff_t kBlockByteSize = sz * kBlockWidth;

    const int x0     = roi.tl().x();
    const int y0     = roi.tl().y();
    const int y1     = roi.br().y();
    const int width  = roi.width();
    const int height = roi.height();

    std::byte* f = (std::byte*) std::malloc(sz * kBlockWidth * (height + 2 * k));
    std::byte* g = (std::byte*) std::malloc(sz * kBlockWidth * (height + 2 * k));
    std::byte* h = (std::byte*) std::malloc(sz * kBlockWidth * (height + 2 * k));

    for (int x = 0; x < width; x += kBlockWidth)
    {
      int w = std::min(kBlockWidth, width - x);


      // Copy the block
      {
        mln::experimental::box2d region = {{x + x0, y0 - k}, {x + x0 + w, y1 + k}};
        copy_block(in, region, (T*)f, kBlockWidth);
      }

      this->running_max_block2d(f + k * kBlockByteSize, //
                                g + k * kBlockByteSize, //
                                h + k * kBlockByteSize, //
                                w, height, kBlockByteSize, kBlockByteSize, kBlockByteSize, k, use_extension);

      // Copy back
      {
        mln::experimental::box2d region = {{x + x0, y0}, {x + x0 + w, y1}};
        copy_block((T*)f + kBlockWidth * k, kBlockWidth, region, out);
      }
    }

    std::free(f);
    std::free(g);
    std::free(h);
  }


  template <class I, class T>
  void vertical_running_max_algo_base_t::running_max_h2d(I& in, mln::experimental::image2d<T>& out, mln::experimental::box2d roi, int k, bool use_extension)
  {
    int            kBlockWidth    = this->get_block_width();
    auto           sz             = this->get_sample_size();
    std::ptrdiff_t kBlockByteSize = sz * kBlockWidth;

    const int x0     = roi.tl().x();
    const int y0     = roi.tl().y();
    const int x1     = roi.br().x();
    const int width  = roi.width();
    const int height = roi.height();

    std::byte* f = (std::byte*) std::malloc(sz * kBlockWidth * (width + 2 * k));
    std::byte* g = (std::byte*) std::malloc(sz * kBlockWidth * (width + 2 * k));
    std::byte* h = (std::byte*) std::malloc(sz * kBlockWidth * (width + 2 * k));

    for (int y = 0; y < height; y += kBlockWidth)
    {
      int H = std::min(kBlockWidth, height - y);

      // Copy the block
      {
        mln::experimental::box2d region = {{x0 - k, y0 + y}, {x1 + k, y0 + y + H}};
        transpose_block2d(in, region, (T*)f, kBlockWidth);
      }

      this->running_max_block2d(f + k * kBlockByteSize, //
                                g + k * kBlockByteSize, //
                                h + k * kBlockByteSize, //
                                H, width, kBlockByteSize, kBlockByteSize, kBlockByteSize, k, use_extension);

      // Copy back
      {
        mln::experimental::box2d region = {{x0, y0 + y}, {x1, y0 + y + H}};
        transpose_block2d((T*)f + kBlockWidth * k, kBlockWidth, region, out);
      }
    }

    std::free(f);
    std::free(g);
    std::free(h);
  }


} // namespace mln::morpho::details