[ / / / / / / / / / / / / / ] [ dir / acme / agatha2 / animu / arepa / randamu / tacos / vg / wooo ][Options][ watchlist ]

/tech/ - Technology

You can now write text to your AI-generated image at https://aiproto.com It is currently free to use for Proto members.
Email
Comment *
File
Select/drop/paste files here
Password (Randomized for file and post deletion; you may also set your own.)
* = required field[▶ Show post options & limits]
Confused? See the FAQ.
Expand all images

File (hide): 8b8d0a8935e888e⋯.jpg (58.77 KB, 498x500, 249:250, fragzeichenmadchen.jpg) (h) (u)

[–]

 No.984132>>984137 [Watch Thread][Show All Posts]

If I have 4 threads operating on the same array of 16 elements, which would be a better way for each thread to iterate through the array's indices?

[1 2 3 4 1 2 3 4 1 2 3 4]

or

[1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4]

Of course the actual array will be tens of thousands of items long, or else I wouldn't even be worrying about threads. The bit that I know about memory access suggests that the latter option will yield better performance, but if the array length is unable to be divided evenly by the number of threads, it can be more of a pain to handle. What would you do?

 No.984137

>>984132 (OP)

Do whatever maximises contiguous memory IO and requires fewer locks/unlocks and switching. So the second one I think.


 No.984143>>984144

Obviously the second, and like every programmer born under the sun, you think a bit to find the simple formula to distribute things as well as possible (so the last thread doesn't have more than a NB_THREAD/2 difference with the others).


 No.984144

>>984143

Obviously meant ceil(ARRAY_LEN/NB_THREAD) / 2.


 No.984156

Usually you want the same thread using nearby memory due to caching. So accessing 0, 1, 2, 3 is faster than 0,2,1,3 (assuming that isn't big enough to be in the same page).

>if the array length is unable to be divided evenly by the number of threads, it can be more of a pain to handle.

You can just use smaller chunks. Not too small or the overhead will kill any gains. There are ways to be clever about it of course but you probably don't need to.




[Return][Go to top][Catalog][Screencap][Nerve Center][Cancer][Update] ( Scroll to new posts) ( Auto) 4
4 replies | 0 images | Page ???
[Post a Reply]
[ / / / / / / / / / / / / / ] [ dir / acme / agatha2 / animu / arepa / randamu / tacos / vg / wooo ][ watchlist ]