|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * ImageJ software for multidimensional image processing and analysis. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2014 - 2018 ImageJ developers. |
| 6 | + * %% |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * |
| 10 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer. |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 20 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 23 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 25 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 26 | + * POSSIBILITY OF SUCH DAMAGE. |
| 27 | + * #L% |
| 28 | + */ |
| 29 | + |
| 30 | +package net.imagej.ops.thread.chunker; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.concurrent.ExecutionException; |
| 34 | +import java.util.concurrent.ExecutorService; |
| 35 | +import java.util.concurrent.Future; |
| 36 | + |
| 37 | +import org.scijava.ops.core.Op; |
| 38 | +import org.scijava.ops.core.inplace.Inplace3First; |
| 39 | +import org.scijava.param.Parameter; |
| 40 | +import org.scijava.plugin.Plugin; |
| 41 | +import org.scijava.struct.ItemIO; |
| 42 | + |
| 43 | +/** |
| 44 | + * Simple default implementation of a {@link ChunkerOp}. The list of |
| 45 | + * elements is chunked into equally sized (besides the last one), disjoint |
| 46 | + * chunks, which are processed in parallel. The stepSize is set to one, i.e. |
| 47 | + * each chunk consists of consecutive elements. |
| 48 | + * |
| 49 | + * @author Christian Dietz (University of Konstanz) |
| 50 | + */ |
| 51 | +@Plugin(type = Op.class, name = "thread.chunker") |
| 52 | +@Parameter(key = "chunk", type = ItemIO.BOTH) |
| 53 | +@Parameter(key = "numberOfElements") |
| 54 | +@Parameter(key = "executorService") |
| 55 | +public class DefaultChunker implements Inplace3First<Chunk, Long, ExecutorService> { |
| 56 | + |
| 57 | + private final int STEP_SIZE = 1; |
| 58 | + |
| 59 | + @Override |
| 60 | + public void mutate(final Chunk chunk, final Long numberOfElements, final ExecutorService es) { |
| 61 | + |
| 62 | + // TODO: is there a better way to determine the optimal chunk size? |
| 63 | + |
| 64 | + final long numSteps = Math.max(1, |
| 65 | + (long) (numberOfElements / Runtime.getRuntime().availableProcessors())) ; |
| 66 | + |
| 67 | + final int numChunks = (int) (numberOfElements / numSteps); |
| 68 | + |
| 69 | + final ArrayList<Future<?>> futures = new ArrayList<>(numChunks); |
| 70 | + |
| 71 | + for (int i = 0; i < numChunks - 1; i++) { |
| 72 | + final long j = i; |
| 73 | + |
| 74 | + futures.add(es.submit(new Runnable() { |
| 75 | + |
| 76 | + @Override |
| 77 | + public void run() { |
| 78 | + chunk.execute(j * numSteps, STEP_SIZE, numSteps); |
| 79 | + } |
| 80 | + })); |
| 81 | + } |
| 82 | + |
| 83 | + // last chunk additionally add the rest of elements |
| 84 | + futures.add(es.submit(new Runnable() { |
| 85 | + |
| 86 | + @Override |
| 87 | + public void run() { |
| 88 | + chunk.execute((numChunks - 1) * numSteps, STEP_SIZE, |
| 89 | + (int) (numSteps + (numberOfElements % numSteps))); |
| 90 | + } |
| 91 | + })); |
| 92 | + |
| 93 | + for (final Future<?> future : futures) { |
| 94 | + try { |
| 95 | + future.get(); |
| 96 | + } |
| 97 | + catch (final InterruptedException exc) { |
| 98 | + throw new RuntimeException(exc); |
| 99 | + } |
| 100 | + catch (final ExecutionException exc) { |
| 101 | + throw new RuntimeException(exc); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments