Search Unity

Tricks to improve performance of burst compiled addition and subtraction of 2d arrays

Discussion in 'Entity Component System' started by threedots1, Jun 16, 2021.

  1. threedots1

    threedots1

    Joined:
    Oct 9, 2014
    Posts:
    88
    What would be the best way to copy 1 smaller 2d array to a larger 2d array?

    upload_2021-6-17_6-30-21.png

    Just say I had a large 2d array( red) (index = width * y + x), and a smaller 2d array (blue) contained inside. I want to either add or subtract the blue array from the red array. If I know the width of the blue array I can calculate the index once for each row, and add/subtract the entire row at once to the red array. What would be the fastest way to do this?

    I guess I could loop with a for starting on each row and x += 4 for each iteration to force SIMD operations? Any other ideas?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    Try writing the naive loop, but put the iterating through a given row as the inner loop with the ExpectVectorized intrinsic. Burst will then complain if something goes wrong. Otherwise, it will probably do what you want at a speed most people would struggle to manually replicate.
     
    MintTree117 likes this.
  3. threedots1

    threedots1

    Joined:
    Oct 9, 2014
    Posts:
    88
    My current naive implementation loops through the entire smaller array from 0 to length and calculates the position in the larger array every time before doing the maths and storing. Not very good as it would completely screw with vectorisation.

    Yeah I'll give that a go, did not know about that intrinsic, it will be very useful.

    For everyone else there's some optimisation guidelines here: Optimization Guidelines | Burst | 1.6.0-pre.2 (unity3d.com)