1 | import java.util.Vector; |
---|
2 | |
---|
3 | import loci.formats.ImageReader; |
---|
4 | import loci.tests.testng.TestTools; |
---|
5 | |
---|
6 | public class ParallelTileTest { |
---|
7 | |
---|
8 | private Vector<String> hashes = new Vector<String>(); |
---|
9 | |
---|
10 | public void run(String id) throws Exception { |
---|
11 | long t0 = System.currentTimeMillis(); |
---|
12 | getHashes(id); |
---|
13 | long t1 = System.currentTimeMillis(); |
---|
14 | checkParallelHashes(id); |
---|
15 | long t2 = System.currentTimeMillis(); |
---|
16 | System.out.println("sequential read took " + (t1 - t0) + " ms"); |
---|
17 | System.out.println("parallel read took " + (t2 - t1) + " ms"); |
---|
18 | } |
---|
19 | |
---|
20 | private void getHashes(String id) throws Exception { |
---|
21 | ImageReader reader = new ImageReader(); |
---|
22 | reader.setId(id); |
---|
23 | int optimalWidth = reader.getOptimalTileWidth(); |
---|
24 | int optimalHeight = reader.getOptimalTileHeight(); |
---|
25 | |
---|
26 | for (int y=0; y<reader.getSizeY(); y+=optimalHeight) { |
---|
27 | for (int x=0; x<reader.getSizeX(); x+=optimalWidth) { |
---|
28 | int width = (int) Math.min(optimalWidth, reader.getSizeX() - x); |
---|
29 | int height = (int) Math.min(optimalHeight, reader.getSizeY() - y); |
---|
30 | |
---|
31 | byte[] buf = reader.openBytes(0, x, y, width, height); |
---|
32 | hashes.add(TestTools.md5(buf)); |
---|
33 | } |
---|
34 | } |
---|
35 | reader.close(); |
---|
36 | } |
---|
37 | |
---|
38 | private void checkParallelHashes(String id) throws Exception { |
---|
39 | int nTiles = hashes.size(); |
---|
40 | TileRunner[] runners = new TileRunner[nTiles]; |
---|
41 | for (int i=0; i<nTiles; i++) { |
---|
42 | runners[i] = new TileRunner(id, i, hashes.get(i)); |
---|
43 | runners[i].start(); |
---|
44 | } |
---|
45 | for (int i=0; i<nTiles; i++) { |
---|
46 | runners[i].join(); |
---|
47 | if (!runners[i].valid()) { |
---|
48 | System.out.println("*** failed on tile #" + i); |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | public static void main(String[] args) throws Exception { |
---|
54 | ParallelTileTest test = new ParallelTileTest(); |
---|
55 | test.run(args[0]); |
---|
56 | } |
---|
57 | |
---|
58 | class TileRunner extends Thread { |
---|
59 | private ImageReader r; |
---|
60 | private int x = -1, y = -1, width, height; |
---|
61 | private String hash, validHash; |
---|
62 | |
---|
63 | public TileRunner(String id, int tile, String hash) { |
---|
64 | try { |
---|
65 | r = new ImageReader(); |
---|
66 | r.setId(id); |
---|
67 | int optimalWidth = r.getOptimalTileWidth(); |
---|
68 | int optimalHeight = r.getOptimalTileHeight(); |
---|
69 | int nextTile = 0; |
---|
70 | for (int yy=0; yy<r.getSizeY(); yy+=optimalHeight) { |
---|
71 | for (int xx=0; xx<r.getSizeX(); xx+=optimalWidth) { |
---|
72 | if (nextTile == tile) { |
---|
73 | x = xx; |
---|
74 | y = yy; |
---|
75 | width = (int) Math.min(optimalWidth, r.getSizeX() - xx); |
---|
76 | height = (int) Math.min(optimalHeight, r.getSizeY() - yy); |
---|
77 | |
---|
78 | break; |
---|
79 | } |
---|
80 | else nextTile++; |
---|
81 | } |
---|
82 | if (nextTile == tile && x >=0 && y >=0) { |
---|
83 | break; |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | catch (Exception e) { } |
---|
88 | this.validHash = hash; |
---|
89 | } |
---|
90 | |
---|
91 | public void run() { |
---|
92 | try { |
---|
93 | byte[] buf = r.openBytes(0, x, y, width, height); |
---|
94 | hash = TestTools.md5(buf); |
---|
95 | r.close(); |
---|
96 | } |
---|
97 | catch (Exception e) { } |
---|
98 | } |
---|
99 | |
---|
100 | public boolean valid() { |
---|
101 | return validHash.equals(hash); |
---|
102 | } |
---|
103 | |
---|
104 | } |
---|
105 | |
---|
106 | } |
---|