Login  Register

Re: Arrays and thread safety

Posted by Michael Doube on May 07, 2009; 2:54pm
URL: http://imagej.273.s1.nabble.com/Arrays-and-thread-safety-tp3692620p3692622.html

Hi Thomas

> I've noticed that it works much faster
> when each thread works on its own separate piece of image.

Do you mean when each thread is given a 1D array from a 2D array (e.g.
workArray[z][]), or when each thread is given a separate range in a 1D
workArray[]?

Mike


> Thomas
>
>
> Michael Doube a écrit :
>> Hi all
>>
>> I recently wrote a multithreaded plugin
>> (http://doube.org/plugins.html#purify) to find the biggest particle in a
>> stack and delete all others.  It works by running a particle joining
>> method on separate chunks (sets of slices), where each chunk is
>> processed in a separate thread. The chunks are then stitched back
>> together; the particle relabelling method replaceLabels() is run
>> multithreaded.  However, it seems that sometimes stitching fails - I can
>> run with the same conditions on the same stack and get different
>> results, so I guess that the threads are clobbering each other.
>>
>> I have 2 questions:
>>
>> 1) is there any difference in thread safety to use 1D work arrays with
>> (x,y,z) indexing like
>>
>>  workArray[index] where index = z * width * height + y * width + x
>>
>> and each thread given a discrete range of z values to work on
>>
>> or to use 2D arrays with (x,y,z) indexing like
>>
>>  workArray[z][index] where index = y * width + x
>>
>> and each thread given a z to work on
>>
>> (The plugin uses 1D arrays at the moment)
>>
>>
>> 2) the multithreaded version of replaceLabels() runs within a nested for
>> loop that iterates over voxels.  A single thread checks label values and
>> each time a particle label needs to be replaced, nThreads instances of
>> replaceLabels() run, replacing labels in discrete slices indexed in a 1D
>> work array.  Is it possible that one thread, having finished its job of
>> replaceLabels(), iterates through the for loop while the other thread(s)
>> is(are) still running replaceLabels()?
>>
>> Any comments appreciated,
>>
>> Mike
>>
>>