|
| 1 | +/* |
| 2 | + * |
| 3 | + * File: FileAll.java Author: Fujitsu |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +import java.nio.IntBuffer; |
| 8 | + |
| 9 | +import mpi.File; |
| 10 | +import mpi.MPI; |
| 11 | +import mpi.MPIException; |
| 12 | +import mpi.Request; |
| 13 | + |
| 14 | +public class FileAll |
| 15 | +{ |
| 16 | + private static final int ITEMS = 50; |
| 17 | + private static final int SIZEOF_INT = 4; |
| 18 | + private final static boolean DEBUG = false; |
| 19 | + |
| 20 | + public static void main (String args[]) throws MPIException |
| 21 | + { |
| 22 | + int myrank, size; |
| 23 | + Request req1, req2; |
| 24 | + File file; |
| 25 | + |
| 26 | + MPI.Init(args); |
| 27 | + myrank = MPI.COMM_WORLD.getRank(); |
| 28 | + size = MPI.COMM_WORLD.getSize(); |
| 29 | + |
| 30 | + /* Do this on every node, because in a testing environment, we can't |
| 31 | + * assume a common filesystem |
| 32 | + */ |
| 33 | + String filename = "ompi_testfile_all"; |
| 34 | + |
| 35 | + /* iWriteAtAll */ |
| 36 | + file = new File(MPI.COMM_WORLD, filename, |
| 37 | + MPI.MODE_WRONLY | MPI.MODE_CREATE); |
| 38 | + |
| 39 | + IntBuffer buffer = MPI.newIntBuffer(ITEMS); |
| 40 | + for (int i = 0; i < ITEMS; ++i) { |
| 41 | + buffer.put(i, i*100*(myrank+1)); // myrank=0: 0,100,200,300..., myrank=1: 0,200,400,600..., myrank=2: 0,300,600,900... |
| 42 | + } |
| 43 | + file.setView(myrank * ITEMS * SIZEOF_INT, MPI.INT, MPI.INT, "native"); |
| 44 | + req1 = file.iWriteAll(buffer, ITEMS, MPI.INT); |
| 45 | + req1.waitFor(); |
| 46 | + |
| 47 | + if ((myrank == 0) && DEBUG) { |
| 48 | + System.out.println("myrank " + myrank + " of " + size + |
| 49 | + " file.iWriteAll() end"); |
| 50 | + } |
| 51 | + |
| 52 | + file.close(); |
| 53 | + |
| 54 | + /* iReadAtAll */ |
| 55 | + file = new File(MPI.COMM_WORLD, filename, MPI.MODE_RDONLY); |
| 56 | + file.setView(myrank * ITEMS * SIZEOF_INT, MPI.INT, MPI.INT, "native"); |
| 57 | + req2 = file.iReadAll(buffer, ITEMS, MPI.INT); |
| 58 | + req2.waitFor(); |
| 59 | + |
| 60 | + /* printing read data */ |
| 61 | + for (int i = 0; i < ITEMS; ++i) { |
| 62 | + if (buffer.get(i) != i*100*(myrank+1)){ |
| 63 | + OmpitestError.ompitestError(OmpitestError.getFileName(), |
| 64 | + OmpitestError.getLineNumber(), |
| 65 | + "myrank " + myrank + " of " + size + |
| 66 | + " data incorrect, read data=" + buffer.get(i) + |
| 67 | + ", expected data=" + i*100*(myrank+1)); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + if ((myrank == 0) && DEBUG) { |
| 72 | + System.out.println("myrank " + myrank + " of " + size + |
| 73 | + " file.iReadAll() end"); |
| 74 | + } |
| 75 | + |
| 76 | + file.close(); |
| 77 | + |
| 78 | + /* Delete the testfile. |
| 79 | + * original file: unlink(filename); |
| 80 | + * The second parameter is an info object. |
| 81 | + */ |
| 82 | + MPI.COMM_WORLD.barrier(); |
| 83 | + if(myrank == 0) { |
| 84 | + File.delete(filename); |
| 85 | + } |
| 86 | + MPI.Finalize(); |
| 87 | + |
| 88 | + if ((myrank == 0) && DEBUG) { |
| 89 | + System.out.println("myrank " + myrank + " of " + size + |
| 90 | + " MPI.Finalize() end"); |
| 91 | + } |
| 92 | + |
| 93 | + } |
| 94 | +} |
0 commit comments