Skip to content

improve performance of binary morphology for fully nonzero structuring elements - #4059

Merged
mergify[bot] merged 1 commit into
cupy:masterfrom
grlee77:ndimage_binary_morphology_nnz
Oct 13, 2020
Merged

mergify[bot] merged 1 commit into
cupy:masterfrom
grlee77:ndimage_binary_morphology_nnz

Conversation

@grlee77

@grlee77 grlee77 commented Oct 2, 2020

Copy link
Copy Markdown
Member

This PR adds a parameter that skips the check for non-zero elements when possible in the binary erosion kernel. Surprisingly, this leads to a fairly large increase in performance (It does result in an ~50 microsecond increase in CPU time, but GPU time is reduced by a factor of 2-3 for large images).

A similar change could be made for the convolution filters and rank filters as well, but I did not measure any change in performance when I tried it for those, so I have left it out of this PR.

Benchmark Script

import cupy
import cupyx.scipy.ndimage as ndi
from cupyx.time import repeat

for shape in [(256, 256), (4096, 4096)]:
    density = 0.7
    connectivity = 2
    xp = cupy

    rstate = cupy.random.RandomState(5)
    x = rstate.randn(*shape) > density
    structure = ndi.generate_binary_structure(x.ndim, connectivity)

    print(f"**shape {shape}**")
    for func in (ndi.binary_erosion, ndi.binary_dilation):
        perf = repeat(func, (x, structure), n_warmup=100, n_repeat=100)
        print(perf)

Benchmark Result

master

**shape (256, 256)**
binary_erosion      :    CPU:   45.320 us   +/- 3.375 (min:   42.463 / max:   61.476) us     GPU-0:   51.205 us   +/- 3.371 (min:   48.096 / max:   67.520) us
binary_dilation     :    CPU:   68.328 us   +/- 3.717 (min:   64.392 / max:   84.751) us     GPU-0:   84.201 us   +/- 4.077 (min:   79.840 / max:  101.344) us
**shape (4096, 4096)**
binary_erosion      :    CPU:   46.791 us   +/- 4.223 (min:   43.281 / max:   64.498) us     GPU-0: 1075.381 us   +/-46.183 (min: 1006.592 / max: 1124.352) us
binary_dilation     :    CPU:   70.238 us   +/- 5.299 (min:   65.694 / max:   96.081) us     GPU-0: 3021.352 us   +/-11.456 (min: 3007.488 / max: 3049.472) us

This PR

**shape (256, 256)**
binary_erosion      :    CPU:  109.395 us   +/-17.944 (min:   98.399 / max:  157.815) us     GPU-0:  115.403 us   +/-19.009 (min:  103.680 / max:  168.320) us
binary_dilation     :    CPU:   96.015 us   +/- 4.039 (min:   92.903 / max:  115.009) us     GPU-0:  100.732 us   +/- 4.194 (min:   97.120 / max:  119.168) us
**shape (4096, 4096)**
binary_erosion      :    CPU:   81.708 us   +/-10.644 (min:   74.575 / max:  180.727) us     GPU-0:  575.700 us   +/- 3.880 (min:  569.344 / max:  592.192) us
binary_dilation     :    CPU:  108.657 us   +/- 9.958 (min:   96.774 / max:  165.093) us     GPU-0: 1174.641 us   +/- 9.765 (min: 1163.200 / max: 1228.800) us

@kmaehashi kmaehashi added cat:enhancement Improvements to existing features prio:medium labels Oct 5, 2020
@emcastillo

Copy link
Copy Markdown
Member

Jenkins, test this please

@emcastillo emcastillo added this to the v9.0.0a1 milestone Oct 13, 2020
@emcastillo emcastillo added the st:test-and-merge (deprecated) Ready to merge after test pass. label Oct 13, 2020
@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 0a1deab, target branch master) failed with status FAILURE.

@emcastillo

Copy link
Copy Markdown
Member

Jenkins, test this please

@chainer-ci

Copy link
Copy Markdown
Member

Jenkins CI test (for commit 0a1deab, target branch master) succeeded!

@mergify
mergify Bot merged commit a776607 into cupy:master Oct 13, 2020
@grlee77
grlee77 deleted the ndimage_binary_morphology_nnz branch December 18, 2020 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:enhancement Improvements to existing features prio:medium st:test-and-merge (deprecated) Ready to merge after test pass.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants