34
34
35
35
package net .imglib2 .algorithm .stats ;
36
36
37
- import java .util .Vector ;
38
- import java .util .concurrent .atomic .AtomicInteger ;
39
-
40
- import net .imglib2 .Cursor ;
41
37
import net .imglib2 .IterableInterval ;
42
38
import net .imglib2 .RandomAccessibleInterval ;
43
39
import net .imglib2 .algorithm .Algorithm ;
44
40
import net .imglib2 .algorithm .Benchmark ;
45
41
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 ;
48
44
import net .imglib2 .type .Type ;
49
45
import net .imglib2 .util .Util ;
46
+ import net .imglib2 .util .ValuePair ;
50
47
import net .imglib2 .view .Views ;
51
48
49
+ import java .util .List ;
50
+
52
51
/**
53
52
* TODO
54
53
*
@@ -63,7 +62,7 @@ public class ComputeMinMax< T extends Type< T > & Comparable< T >> implements Al
63
62
* @param min
64
63
* @param max
65
64
*/
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 )
67
66
{
68
67
final ComputeMinMax < T > c = new ComputeMinMax < T >( Views .iterable ( interval ), min , max );
69
68
c .process ();
@@ -72,20 +71,18 @@ final public static < T extends Comparable< T > & Type< T > > void computeMinMax
72
71
max .set ( c .getMax () );
73
72
}
74
73
75
- final IterableInterval < T > image ;
74
+ private final IterableInterval < T > image ;
76
75
77
- final T min , max ;
76
+ private final T min , max ;
78
77
79
- String errorMessage = "" ;
78
+ private String errorMessage = "" ;
80
79
81
- int numThreads ;
80
+ private int numThreads ;
82
81
83
- long processingTime ;
82
+ private long processingTime ;
84
83
85
84
public ComputeMinMax ( final IterableInterval < T > interval , final T min , final T max )
86
85
{
87
- setNumThreads ();
88
-
89
86
this .image = interval ;
90
87
91
88
this .min = min ;
@@ -107,87 +104,52 @@ public boolean process()
107
104
{
108
105
final long startTime = System .currentTimeMillis ();
109
106
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 () );
158
111
159
112
processingTime = System .currentTimeMillis () - startTime ;
160
113
161
114
return true ;
162
115
}
163
116
164
- protected void compute ( final long startPos , final long loopSize , final T min , final T max )
117
+ private void computeMinAndMax ( )
165
118
{
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
+ );
170
139
171
- min .set ( cursor .get () );
172
- max .set ( cursor .get () );
173
-
174
- cursor .reset ();
140
+ // compute overall min and max
141
+ computeGlobalMinAndMax ( listOfMinAndMaxValues );
142
+ }
175
143
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 () );
178
148
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 )
181
150
{
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 () ) );
191
153
}
192
154
}
193
155
@@ -216,7 +178,7 @@ public long getProcessingTime()
216
178
@ Override
217
179
public void setNumThreads ()
218
180
{
219
- this .numThreads = Runtime . getRuntime (). availableProcessors () ;
181
+ this .numThreads = 0 ;
220
182
}
221
183
222
184
@ Override
@@ -228,7 +190,7 @@ public void setNumThreads( final int numThreads )
228
190
@ Override
229
191
public int getNumThreads ()
230
192
{
231
- return numThreads ;
193
+ return numThreads == 0 ? Parallelization . getTaskExecutor (). getParallelism () : numThreads ;
232
194
}
233
195
234
196
@ Override
0 commit comments