Ejemplo n.º 1
0
  /**
   * Write the current cell array to the given row of the image file.
   *
   * @param r Row index.
   */
  private static void writeCurrentCell(int r) throws IOException {
    // Set image row's gray values based on current cell states.
    for (int i = 0; i < C; ++i) {
      imagerow.setPixel(i, currentCell[i].floatValue());
    }

    // Set row r of the pixel matrix.
    pixelmatrix[r] = pixelrow;

    // Write row-r slice of the image to the image file.
    writer.writeRowSlice(new Range(r, r));
  }
Ejemplo n.º 2
0
  /** Main program. */
  public static void main(String[] args) throws Exception {
    // Start timing.
    long t1 = System.currentTimeMillis();

    // Parse command line arguments.
    if (args.length != 5) usage();
    int argi = 0;
    C = Integer.parseInt(args[0]);
    S = Integer.parseInt(args[1]);
    A = new BigRational(args[2]).mul(ONE_THIRD);
    B = new BigRational(args[3]);
    imagefile = new File(args[4]);

    // Allocate storage for old and new cell arrays. Initialize all cells to
    // 0, except center cell to 1.
    currentCell = new BigRational[C];
    nextCell = new BigRational[C];
    for (int i = 0; i < C; ++i) {
      currentCell[i] = new BigRational();
      nextCell[i] = new BigRational();
    }
    currentCell[C / 2].assign(ONE);

    // Set up pixel matrix, image, and image writer.
    pixelmatrix = new byte[S + 1][];
    image = new PJGGrayImage(S + 1, C, pixelmatrix);
    writer = image.prepareToWrite(new BufferedOutputStream(new FileOutputStream(imagefile)));

    // Allocate storage for one pixel matrix row.
    pixelrow = new byte[C];
    imagerow = new GrayImageRow(pixelrow);
    imagerow.setInterpretation(PJGGrayImage.ZERO_IS_WHITE);

    // Parallel calculation section.
    new ParallelTeam()
        .execute(
            new ParallelRegion() {
              public void run() throws Exception {
                // Do S time steps. Sequential outer loop.
                for (int s = 0; s < S; ++s) {
                  final int step = s;

                  // Calculate next state of each cell. Parallel inner loop.
                  execute(
                      0,
                      C - 1,
                      new IntegerForLoop() {
                        public IntegerSchedule schedule() {
                          return IntegerSchedule.guided();
                        }

                        public void run(int first, int last) {
                          for (int i = first; i <= last; ++i) {
                            nextCell[i]
                                .assign(currentCell[i])
                                .add(currentCell[(i - 1 + C) % C])
                                .add(currentCell[(i + 1) % C])
                                .mul(A)
                                .add(B)
                                .normalize()
                                .fracPart();
                          }
                        }
                      },

                      // Synchronize threads before next outer loop iteration.
                      new BarrierAction() {
                        public void run() throws Exception {
                          // Write current CA state to image file.
                          writeCurrentCell(step);

                          // Advance one time step -- swap old and new cell
                          // arrays.
                          BigRational[] tmp = currentCell;
                          currentCell = nextCell;
                          nextCell = tmp;
                        }
                      });
                }
              }
            });

    // Write final CA state to image file.
    writeCurrentCell(S);
    writer.close();

    // Stop timing.
    long t2 = System.currentTimeMillis();
    System.out.println((t2 - t1) + " msec total");
  }