diff --git a/render-ws-java-client/src/main/java/org/janelia/render/client/newsolver/AffineDistributedSolver.java b/render-ws-java-client/src/main/java/org/janelia/render/client/newsolver/AffineDistributedSolver.java index b213764e5..a3fb8cc45 100644 --- a/render-ws-java-client/src/main/java/org/janelia/render/client/newsolver/AffineDistributedSolver.java +++ b/render-ws-java-client/src/main/java/org/janelia/render/client/newsolver/AffineDistributedSolver.java @@ -3,9 +3,13 @@ import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.Future; import java.util.function.Function; import org.janelia.render.client.newsolver.blockfactories.ZBlockFactory; @@ -14,11 +18,17 @@ import org.janelia.render.client.newsolver.setup.RenderSetup; import org.janelia.render.client.newsolver.solvers.Worker; import org.janelia.render.client.newsolver.solvers.affine.AffineAlignBlockWorker; +import org.janelia.render.client.solver.DistributedSolveDeSerialize; +import org.janelia.render.client.solver.DistributedSolveWorker; +import org.janelia.render.client.solver.SolveItemData; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import mpicbg.models.Affine2D; import mpicbg.models.AffineModel2D; import mpicbg.models.Model; import mpicbg.models.NoninvertibleModelException; +import mpicbg.spim.io.IOFunctions; public class AffineDistributedSolver { @@ -86,15 +96,72 @@ public static void main( final String[] args ) throws IOException final BlockCollection< ?, AffineModel2D, ?, ZBlockFactory > blockCollection = solverSetup.setupSolve( cmdLineSetup.blockModel(), cmdLineSetup.stitchingModel() ); - final ExecutorService taskExecutor = Executors.newFixedThreadPool( cmdLineSetup.threadsGlobal ); + LOG.info( "Multithreading with thread num=" + cmdLineSetup.threadsGlobal ); + + final ArrayList< Callable< List< BlockData > > > workers = new ArrayList<>(); + + for ( final BlockData< ?, AffineModel2D, ?, ZBlockFactory > blockData : blockCollection.allBlocks() ) + { + workers.add( new Callable>>() + { + @Override + public List< BlockData > call() throws Exception + { + BlockData block1 = blockData; + + final Worker worker = block1.createWorker( + solverSetup.col.maxId() + 1, + cmdLineSetup.threadsWorker ); + + // final Worker + /*block1.solveTypeParameters().createWorker( + block1, + solverSetup.col.maxId() + 1, + cmdLineSetup.threadsWorker );*/ + + //worker.run(); + + return null;//w.getSolveItemDataList(); + } + }); + } + + final ArrayList< BlockData > allItems = new ArrayList<>(); + try + { + final ExecutorService taskExecutor = Executors.newFixedThreadPool( cmdLineSetup.threadsGlobal ); + + // invokeAll() returns when all tasks are complete + final List< Future< List< BlockData > > > futures = taskExecutor.invokeAll( workers ); + + taskExecutor.shutdown(); + + for ( final Future< List< BlockData > > future : futures ) + allItems.addAll( future.get() ); + } + catch ( final Exception e ) + { + LOG.error( "Failed to compute alignments: " + e ); + e.printStackTrace(); + return; + } + + final ExecutorService taskExecutor = Executors.newFixedThreadPool( cmdLineSetup.threadsGlobal ); taskExecutor.submit( () -> blockCollection.allBlocks().parallelStream().forEach( block -> { try { - Worker worker = block.solveTypeParameters().createWorker( block, solverSetup.col.maxId() + 1, cmdLineSetup.threadsWorker ); + BlockData block1 = block; + + final Worker worker = block1.createWorker( + solverSetup.col.maxId() + 1, + cmdLineSetup.threadsWorker ); + worker.run(); + ArrayList l = worker.getBlockDataList(); + Object l1 = l.get( 0 ); } catch (IOException | ExecutionException | InterruptedException | NoninvertibleModelException e) { @@ -103,7 +170,6 @@ public static void main( final String[] args ) throws IOException } })); - taskExecutor.shutdown(); /* for ( final Worker worker : workers ) { @@ -212,4 +278,6 @@ protected < M extends Model< M > & Affine2D< M >, S extends Model< S > & Affine2 return workers; } + + private static final Logger LOG = LoggerFactory.getLogger(AffineDistributedSolver.class); }