Skip to content

Commit 4fe979b

Browse files
committed
TEMP: Add thread ops
1 parent 2beb9d2 commit 4fe979b

4 files changed

Lines changed: 301 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 net.imagej.ops.Parallel;
33+
34+
/**
35+
* A {@link Chunk} of code which can be executed by a {@link ChunkerOp}.
36+
* <p>
37+
* A {@link Chunk} processes a subset of a bigger problem and can be executed in
38+
* parallel with other {@link Chunk}s. The elements of the subproblem are
39+
* identified by enumerating the original problem.
40+
* </p>
41+
*
42+
* @author Christian Dietz (University of Konstanz)
43+
* @see ChunkerOp
44+
* @see Parallel
45+
*/
46+
public interface Chunk {
47+
48+
/**
49+
* Solve the subproblem for the element at startIndex, increase the index by
50+
* the given stepSize and repeat numSteps.
51+
*
52+
* @param startIndex zero based index that identifies the first element of
53+
* this subproblem (w.r.t. the global problem enumeration)
54+
* @param stepSize the step-size between two consecutive elements
55+
* @param numSteps how many steps shall be taken
56+
*/
57+
void execute(long startIndex, long stepSize, long numSteps);
58+
59+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.ExecutorService;
34+
import java.util.concurrent.Future;
35+
36+
import org.scijava.Priority;
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+
* Implementation of a {@link ChunkerOp} that interleaves the chunks. In a
45+
* element enumeration from 1..n with <b>k</b> {@link Chunk}s the
46+
* first one will process the elements 1, k+1, 2k+1, ... the second chunk
47+
* executable 2, k+2, 2k+2 and so on.
48+
*
49+
* @author Michael Zinsmaier (University of Konstanz)
50+
*/
51+
@Plugin(type = Op.class, name = "thread.chunker", priority = Priority.VERY_LOW)
52+
@Parameter(key = "chunk", type = ItemIO.BOTH)
53+
@Parameter(key = "numberOfElements")
54+
@Parameter(key = "executorService")
55+
public class ChunkerInterleaved implements Inplace3First<Chunk, Long, ExecutorService>{
56+
57+
private String cancellationMsg;
58+
59+
@Override
60+
public void mutate(final Chunk chunk, final Long numberOfElements, final ExecutorService es) {
61+
62+
final int numThreads = Runtime.getRuntime().availableProcessors();
63+
final int numStepsFloor = (int) (numberOfElements / numThreads);
64+
final int remainder = numberOfElements.intValue() - (numStepsFloor * numThreads);
65+
66+
final ArrayList<Future<?>> futures = new ArrayList<>(numThreads);
67+
68+
for (int i = 0; i < numThreads; i++) {
69+
final int j = i;
70+
71+
futures.add(es.submit(new Runnable() {
72+
73+
@Override
74+
public void run() {
75+
if (j < remainder) {
76+
chunk.execute(j, numThreads, (numStepsFloor + 1));
77+
}
78+
else {
79+
chunk.execute(j, numThreads, numStepsFloor);
80+
}
81+
}
82+
}));
83+
}
84+
85+
for (final Future<?> future : futures) {
86+
try {
87+
future.get();
88+
}
89+
catch (final Exception e) {
90+
cancellationMsg = e.getMessage();
91+
break;
92+
}
93+
}
94+
}
95+
96+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
package net.imagej.ops.thread.chunker;
30+
31+
import net.imglib2.Cursor;
32+
33+
public abstract class CursorBasedChunk implements Chunk {
34+
35+
public static void setToStart(final Cursor<?> c, long startIndex) {
36+
c.reset();
37+
c.jumpFwd(startIndex + 1);
38+
}
39+
40+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)