Ejemplo n.º 1
0
  public static void main(String[] args) throws MPIException {
    args = MPI.Init(args);
    Intracomm worldProcComm = MPI.COMM_WORLD;
    int worldProcRank = worldProcComm.getRank();
    int worldProcsCount = worldProcComm.getSize();

    int size = Integer.parseInt(args[0]);
    Range[] ranges = RangePartitioner.partition(size, worldProcsCount);
    Range myRange = ranges[worldProcRank];

    String fname = "/dev/shm/mmap.tmp";
    try (FileChannel fc =
        FileChannel.open(
            Paths.get(fname),
            StandardOpenOption.CREATE,
            StandardOpenOption.WRITE,
            StandardOpenOption.READ)) {
      int extent = size * Double.BYTES;
      bytes = ByteBufferBytes.wrap(fc.map(FileChannel.MapMode.READ_WRITE, 0L, extent));
      byteBuffer = bytes.sliceAsByteBuffer(byteBuffer);
      byteSlice =
          bytes.slice(myRange.getStartIndex() * Double.BYTES, myRange.getLength() * Double.BYTES);
      byteBufferSlice = byteSlice.sliceAsByteBuffer(byteBufferSlice);

      Win win = new Win(byteBuffer, extent, Double.BYTES, MPI.INFO_NULL, worldProcComm);

      for (int i = 0; i < myRange.getLength(); ++i) {
        byteSlice.writeDouble(i * Double.BYTES, worldProcRank);
      }
      win.fence(0);
      if (worldProcRank != 0) {
        win.put(
            byteBufferSlice,
            myRange.getLength(),
            MPI.DOUBLE,
            0,
            myRange.getStartIndex(),
            myRange.getLength(),
            MPI.DOUBLE);
      }
      win.fence(0);

      worldProcComm.barrier();
      if (worldProcRank == 0) {
        for (int i = 0; i < size; ++i) {
          System.out.println(bytes.readDouble(i * Double.BYTES));
        }
      }
      worldProcComm.barrier();
    } catch (IOException e) {
      e.printStackTrace();
    }

    MPI.Finalize();
  }