Skip to content

Commit 5c81783

Browse files
committed
Update ComputeMinMax to use parallelization framework
1 parent 7fe1eaf commit 5c81783

File tree

1 file changed

+48
-86
lines changed

1 file changed

+48
-86
lines changed

src/main/java/net/imglib2/algorithm/stats/ComputeMinMax.java

Lines changed: 48 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,20 @@
3434

3535
package net.imglib2.algorithm.stats;
3636

37-
import java.util.Vector;
38-
import java.util.concurrent.atomic.AtomicInteger;
39-
40-
import net.imglib2.Cursor;
4137
import net.imglib2.IterableInterval;
4238
import net.imglib2.RandomAccessibleInterval;
4339
import net.imglib2.algorithm.Algorithm;
4440
import net.imglib2.algorithm.Benchmark;
4541
import net.imglib2.algorithm.MultiThreaded;
46-
import net.imglib2.multithreading.Chunk;
47-
import net.imglib2.multithreading.SimpleMultiThreading;
42+
import net.imglib2.loops.IterableLoopBuilder;
43+
import net.imglib2.parallel.Parallelization;
4844
import net.imglib2.type.Type;
4945
import net.imglib2.util.Util;
46+
import net.imglib2.util.ValuePair;
5047
import net.imglib2.view.Views;
5148

49+
import java.util.List;
50+
5251
/**
5352
* TODO
5453
*
@@ -63,7 +62,7 @@ public class ComputeMinMax< T extends Type< T > & Comparable< T >> implements Al
6362
* @param min
6463
* @param max
6564
*/
66-
final public static < T extends Comparable< T > & Type< T > > void computeMinMax( final RandomAccessibleInterval< T > interval, final T min, final T max )
65+
public static < T extends Comparable< T > & Type< T > > void computeMinMax( final RandomAccessibleInterval< T > interval, final T min, final T max )
6766
{
6867
final ComputeMinMax< T > c = new ComputeMinMax< T >( Views.iterable( interval ), min, max );
6968
c.process();
@@ -72,20 +71,18 @@ final public static < T extends Comparable< T > & Type< T > > void computeMinMax
7271
max.set( c.getMax() );
7372
}
7473

75-
final IterableInterval< T > image;
74+
private final IterableInterval< T > image;
7675

77-
final T min, max;
76+
private final T min, max;
7877

79-
String errorMessage = "";
78+
private String errorMessage = "";
8079

81-
int numThreads;
80+
private int numThreads;
8281

83-
long processingTime;
82+
private long processingTime;
8483

8584
public ComputeMinMax( final IterableInterval< T > interval, final T min, final T max )
8685
{
87-
setNumThreads();
88-
8986
this.image = interval;
9087

9188
this.min = min;
@@ -107,87 +104,52 @@ public boolean process()
107104
{
108105
final long startTime = System.currentTimeMillis();
109106

110-
final long imageSize = image.size();
111-
112-
final AtomicInteger ai = new AtomicInteger( 0 );
113-
final Thread[] threads = SimpleMultiThreading.newThreads( getNumThreads() );
114-
115-
final Vector< Chunk > threadChunks = SimpleMultiThreading.divideIntoChunks( imageSize, numThreads );
116-
final Vector< T > minValues = new Vector< T >();
117-
final Vector< T > maxValues = new Vector< T >();
118-
119-
for ( int ithread = 0; ithread < threads.length; ++ithread )
120-
{
121-
minValues.add( image.firstElement().createVariable() );
122-
maxValues.add( image.firstElement().createVariable() );
123-
124-
threads[ ithread ] = new Thread( new Runnable()
125-
{
126-
@Override
127-
public void run()
128-
{
129-
// Thread ID
130-
final int myNumber = ai.getAndIncrement();
131-
132-
// get chunk of pixels to process
133-
final Chunk myChunk = threadChunks.get( myNumber );
134-
135-
// compute min and max
136-
compute( myChunk.getStartPosition(), myChunk.getLoopSize(), minValues.get( myNumber ), maxValues.get( myNumber ) );
137-
138-
}
139-
} );
140-
}
141-
142-
SimpleMultiThreading.startAndJoin( threads );
143-
144-
// compute overall min and max
145-
min.set( minValues.get( 0 ) );
146-
max.set( maxValues.get( 0 ) );
147-
148-
for ( int i = 0; i < threads.length; ++i )
149-
{
150-
T value = minValues.get( i );
151-
if ( Util.min( min, value ) == value )
152-
min.set( value );
153-
154-
value = maxValues.get( i );
155-
if ( Util.max( max, value ) == value )
156-
max.set( value );
157-
}
107+
if ( numThreads == 0 )
108+
computeMinAndMax();
109+
else
110+
Parallelization.runWithNumThreads( numThreads, () -> computeMinAndMax() );
158111

159112
processingTime = System.currentTimeMillis() - startTime;
160113

161114
return true;
162115
}
163116

164-
protected void compute( final long startPos, final long loopSize, final T min, final T max )
117+
private void computeMinAndMax()
165118
{
166-
final Cursor< T > cursor = image.cursor();
167-
168-
// init min and max
169-
cursor.fwd();
119+
T firstElement = image.firstElement();
120+
List< ValuePair< T, T > > listOfMinAndMaxValues = IterableLoopBuilder
121+
.setImages( image )
122+
.multithreaded()
123+
.forEachChunk(
124+
chunk -> {
125+
T min = firstElement.createVariable();
126+
T max = firstElement.createVariable();
127+
min.set( firstElement );
128+
max.set( firstElement );
129+
chunk.forEachPixel( value -> {
130+
if ( Util.min( min, value ) == value )
131+
min.set( value );
132+
133+
if ( Util.max( max, value ) == value )
134+
max.set( value );
135+
} );
136+
return new ValuePair<>( min, max );
137+
}
138+
);
170139

171-
min.set( cursor.get() );
172-
max.set( cursor.get() );
173-
174-
cursor.reset();
140+
// compute overall min and max
141+
computeGlobalMinAndMax( listOfMinAndMaxValues );
142+
}
175143

176-
// move to the starting position of the current thread
177-
cursor.jumpFwd( startPos );
144+
public void computeGlobalMinAndMax( List< ValuePair< T, T > > listOfMinAndMaxValues )
145+
{
146+
min.set( listOfMinAndMaxValues.get( 0 ).getA() );
147+
max.set( listOfMinAndMaxValues.get( 0 ).getB() );
178148

179-
// do as many pixels as wanted by this thread
180-
for ( long j = 0; j < loopSize; ++j )
149+
for ( ValuePair< T, T > minAndMax : listOfMinAndMaxValues )
181150
{
182-
cursor.fwd();
183-
184-
final T value = cursor.get();
185-
186-
if ( Util.min( min, value ) == value )
187-
min.set( value );
188-
189-
if ( Util.max( max, value ) == value )
190-
max.set( value );
151+
min.set( Util.min( min, minAndMax.getA() ) );
152+
max.set( Util.max( max, minAndMax.getB() ) );
191153
}
192154
}
193155

@@ -216,7 +178,7 @@ public long getProcessingTime()
216178
@Override
217179
public void setNumThreads()
218180
{
219-
this.numThreads = Runtime.getRuntime().availableProcessors();
181+
this.numThreads = 0;
220182
}
221183

222184
@Override
@@ -228,7 +190,7 @@ public void setNumThreads( final int numThreads )
228190
@Override
229191
public int getNumThreads()
230192
{
231-
return numThreads;
193+
return numThreads == 0 ? Parallelization.getTaskExecutor().getParallelism() : numThreads;
232194
}
233195

234196
@Override

0 commit comments

Comments
 (0)