Skip to content
Snippets Groups Projects
Commit 3738e8be authored by Tobias Pietzsch's avatar Tobias Pietzsch
Browse files

pass re-usable ExecutorService to AccumulateProjector, and fix min/max bug

in calcuation of numTasks.
parent 483405a0
Branches
Tags
No related merge requests found
package bdv.viewer.render; package bdv.viewer.render;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import net.imglib2.Cursor; import net.imglib2.Cursor;
...@@ -43,6 +43,8 @@ public abstract class AccumulateProjector< A, B > implements VolatileProjector ...@@ -43,6 +43,8 @@ public abstract class AccumulateProjector< A, B > implements VolatileProjector
*/ */
protected final int numThreads; protected final int numThreads;
protected final ExecutorService executorService;
/** /**
* Time needed for rendering the last frame, in nano-seconds. * Time needed for rendering the last frame, in nano-seconds.
*/ */
...@@ -57,7 +59,8 @@ public abstract class AccumulateProjector< A, B > implements VolatileProjector ...@@ -57,7 +59,8 @@ public abstract class AccumulateProjector< A, B > implements VolatileProjector
final ArrayList< ? extends RandomAccessible< A > > sources, final ArrayList< ? extends RandomAccessible< A > > sources,
final Converter< ? super A, B > converter, final Converter< ? super A, B > converter,
final RandomAccessibleInterval< B > target, final RandomAccessibleInterval< B > target,
final int numThreads ) final int numThreads,
final ExecutorService executorService )
{ {
this.sourceProjectors = sourceProjectors; this.sourceProjectors = sourceProjectors;
this.sources = new ArrayList< IterableInterval< A > >(); this.sources = new ArrayList< IterableInterval< A > >();
...@@ -67,6 +70,7 @@ public abstract class AccumulateProjector< A, B > implements VolatileProjector ...@@ -67,6 +70,7 @@ public abstract class AccumulateProjector< A, B > implements VolatileProjector
this.target = target; this.target = target;
this.iterableTarget = Views.flatIterable( target ); this.iterableTarget = Views.flatIterable( target );
this.numThreads = numThreads; this.numThreads = numThreads;
this.executorService = executorService;
lastFrameRenderNanoTime = -1; lastFrameRenderNanoTime = -1;
} }
...@@ -96,23 +100,25 @@ public abstract class AccumulateProjector< A, B > implements VolatileProjector ...@@ -96,23 +100,25 @@ public abstract class AccumulateProjector< A, B > implements VolatileProjector
final int height = ( int ) target.dimension( 1 ); final int height = ( int ) target.dimension( 1 );
final int length = width * height; final int length = width * height;
final ExecutorService ex = Executors.newFixedThreadPool( numThreads ); final boolean createExecutor = ( executorService == null );
final int numTasks = Math.max( numThreads * 10, height ); final ExecutorService ex = createExecutor ? Executors.newFixedThreadPool( numThreads ) : executorService;
final int numTasks = Math.min( numThreads * 10, height );
final double taskLength = ( double ) length / numTasks; final double taskLength = ( double ) length / numTasks;
final int numSources = sources.size(); final int numSources = sources.size();
final ArrayList< Callable< Void > > tasks = new ArrayList< Callable< Void > >( numTasks );
for ( int taskNum = 0; taskNum < numTasks; ++taskNum ) for ( int taskNum = 0; taskNum < numTasks; ++taskNum )
{ {
final int myOffset = ( int ) ( taskNum * taskLength ); final int myOffset = ( int ) ( taskNum * taskLength );
final int myLength = ( (taskNum == numTasks - 1 ) ? length : ( int ) ( ( taskNum + 1 ) * taskLength ) ) - myOffset; final int myLength = ( (taskNum == numTasks - 1 ) ? length : ( int ) ( ( taskNum + 1 ) * taskLength ) ) - myOffset;
final Runnable r = new Runnable() final Callable< Void > r = new Callable< Void >()
{ {
@SuppressWarnings( "unchecked" ) @SuppressWarnings( "unchecked" )
@Override @Override
public void run() public Void call()
{ {
if ( interrupted.get() ) if ( interrupted.get() )
return; return null;
final Cursor< A >[] sourceCursors = new Cursor[ numSources ]; final Cursor< A >[] sourceCursors = new Cursor[ numSources ];
for ( int s = 0; s < numSources; ++s ) for ( int s = 0; s < numSources; ++s )
...@@ -130,19 +136,21 @@ public abstract class AccumulateProjector< A, B > implements VolatileProjector ...@@ -130,19 +136,21 @@ public abstract class AccumulateProjector< A, B > implements VolatileProjector
sourceCursors[ s ].fwd(); sourceCursors[ s ].fwd();
accumulate( sourceCursors, targetCursor.next() ); accumulate( sourceCursors, targetCursor.next() );
} }
return null;
} }
}; };
ex.execute( r ); tasks.add( r );
} }
ex.shutdown();
try try
{ {
ex.awaitTermination( 1, TimeUnit.HOURS ); ex.invokeAll( tasks );
} }
catch ( final InterruptedException e ) catch ( final InterruptedException e )
{ {
e.printStackTrace(); e.printStackTrace();
} }
if ( createExecutor )
ex.shutdown();
lastFrameRenderNanoTime = stopWatch.nanoTime(); lastFrameRenderNanoTime = stopWatch.nanoTime();
......
package bdv.viewer.render; package bdv.viewer.render;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import net.imglib2.Cursor; import net.imglib2.Cursor;
import net.imglib2.RandomAccessible; import net.imglib2.RandomAccessible;
...@@ -14,9 +15,10 @@ public class AccumulateProjectorARGB extends AccumulateProjector< ARGBType, ARGB ...@@ -14,9 +15,10 @@ public class AccumulateProjectorARGB extends AccumulateProjector< ARGBType, ARGB
final ArrayList< VolatileProjector > sourceProjectors, final ArrayList< VolatileProjector > sourceProjectors,
final ArrayList< ? extends RandomAccessible< ARGBType > > sources, final ArrayList< ? extends RandomAccessible< ARGBType > > sources,
final RandomAccessibleInterval< ARGBType > target, final RandomAccessibleInterval< ARGBType > target,
final int numThreads ) final int numThreads,
final ExecutorService executorService )
{ {
super( sourceProjectors, sources, null, target, numThreads ); super( sourceProjectors, sources, null, target, numThreads, executorService );
} }
@Override @Override
......
...@@ -571,7 +571,7 @@ public class MultiResolutionRenderer ...@@ -571,7 +571,7 @@ public class MultiResolutionRenderer
sourceProjectors.add( p ); sourceProjectors.add( p );
sourceImages.add( renderImage ); sourceImages.add( renderImage );
} }
projector = new AccumulateProjectorARGB( sourceProjectors, sourceImages, screenImage, numRenderingThreads ); projector = new AccumulateProjectorARGB( sourceProjectors, sourceImages, screenImage, numRenderingThreads, renderingExecutorService );
} }
previousTimepoint = viewerState.getCurrentTimepoint(); previousTimepoint = viewerState.getCurrentTimepoint();
cache.initIoTimeBudget( iobudget ); cache.initIoTimeBudget( iobudget );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment