Пример #1
0
  /** Mandelbrot Set main program. */
  public static void main(String[] args) throws Exception {
    // Start timing.
    long t1 = System.currentTimeMillis();

    // Initialize middleware.
    Comm.init(args);
    world = Comm.world();
    size = world.size();
    rank = world.rank();

    // Validate command line arguments.
    if (args.length != 7) usage();
    width = Integer.parseInt(args[0]);
    height = Integer.parseInt(args[1]);
    xcenter = Double.parseDouble(args[2]);
    ycenter = Double.parseDouble(args[3]);
    resolution = Double.parseDouble(args[4]);
    maxiter = Integer.parseInt(args[5]);
    outfile = new File(args[6]);

    // Initial pixel offsets from center.
    xoffset = -(width - 1) / 2;
    yoffset = (height - 1) / 2;

    // Create histogram.
    histogram = new int[maxiter + 1];

    long t2 = System.currentTimeMillis();

    // Compute pixels in parallel.
    new WorkerTeam()
        .execute(
            new WorkerRegion() {
              public void run() throws Exception {
                execute(
                    0,
                    height - 1,
                    new WorkerIntegerForLoop() {
                      // Compute chunk of pixels, rows lb .. ub.
                      public void run(int lb, int ub) throws Exception {
                        // Count chunks.
                        ++chunkCount;

                        // Compute pixels.
                        for (int r = lb; r <= ub; ++r) {
                          double y = ycenter + (yoffset - r) / resolution;

                          for (int c = 0; c < width; ++c) {
                            double x = xcenter + (xoffset + c) / resolution;

                            // Iterate until convergence.
                            int i = 0;
                            double aold = 0.0;
                            double bold = 0.0;
                            double a = 0.0;
                            double b = 0.0;
                            double zmagsqr = 0.0;
                            while (i < maxiter && zmagsqr <= 4.0) {
                              ++i;
                              a = aold * aold - bold * bold + x;
                              b = 2.0 * aold * bold + y;
                              zmagsqr = a * a + b * b;
                              aold = a;
                              bold = b;
                            }

                            // Increment histogram counter for pixel value.
                            ++histogram[i];
                          }
                        }
                      }
                    });
              }
            });

    // Reduce histogram into process 0.
    world.reduce(0, HISTOGRAM_DATA_MSG, IntegerBuf.buffer(histogram), IntegerOp.SUM);

    long t3 = System.currentTimeMillis();

    // Process 0 prints histogram.
    if (rank == 0) {
      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outfile)));
      for (int i = 0; i <= maxiter; ++i) {
        out.print(i);
        out.print('\t');
        out.print(histogram[i]);
        out.println();
      }
      out.close();
    }

    // Stop timing.
    long t4 = System.currentTimeMillis();
    System.out.println(chunkCount + " chunks " + rank);
    System.out.println((t2 - t1) + " msec pre " + rank);
    System.out.println((t3 - t2) + " msec calc " + rank);
    System.out.println((t4 - t3) + " msec post " + rank);
    System.out.println((t4 - t1) + " msec total " + rank);
  }
Пример #2
0
  /**
   * main method
   *
   * @param args
   * @throws IOException
   */
  public static void main(String[] args) throws IOException {
    if (args.length != 4) { // usage check
      usage();
    }
    seed = Integer.parseInt(args[0]); // see for rand
    m = Integer.parseInt(args[1]);
    n = Integer.parseInt(args[2]); // number of rows/columns
    filename = args[3]; // output file name
    // matrices and vectors
    matrixA = new int[m][n];
    vectorX = new int[n];
    vectorY = new int[m];

    long t1 = System.currentTimeMillis();
    Comm.init(args);
    Comm world = Comm.world();
    int size = world.size();
    int rank = world.rank();
    blockrow = m / size;
    blockcol = n / size;

    // split the matrixA into rows equally for processors
    rangerow = new Range(0, m - 1).subranges(size);
    myrangerow = rangerow[rank];
    rowlb = myrangerow.lb();
    rowub = myrangerow.ub();

    // split the vectorX into cols equally for processors
    rangecol = new Range(0, n - 1).subranges(size);
    myrangecol = rangecol[rank];
    collb = myrangecol.lb();
    colub = myrangecol.ub();

    // row slicing in matrixA and slicing in vectorX
    slicesA = IntegerBuf.rowSliceBuffers(matrixA, rangerow);
    mysliceA = slicesA[rank];
    slicesX = IntegerBuf.sliceBuffers(vectorX, rangecol);
    mysliceX = slicesX[rank];
    slicesY = IntegerBuf.sliceBuffers(vectorY, rangerow);
    mysliceY = slicesY[rank];

    Random number = Random.getInstance(seed);

    number.skip(rowlb * n);
    // matrixA generated by idividual processors
    for (int i = rowlb; i <= rowub; i++) {
      for (int j = 0; j < n; j++) {
        matrixA[i][j] = number.nextInt(10);
      }
    }

    number = Random.getInstance(seed);
    number.skip(m * n + collb);
    // vectorX generated by individual processors
    for (int i = collb; i <= colub; i++) {
      vectorX[i] = number.nextInt(10);
    }

    // allGather in sliceX
    world.allGather(0, mysliceX, slicesX);

    // matrix-vector calculation performed in individual processor
    for (int i = rowlb; i <= rowub; i++) {
      for (int j = 0; j < n; j++) {
        vectorY[i] = vectorY[i] + matrixA[i][j] * vectorX[j];
      }
    }

    // gather everything to processor 0
    world.gather(0, mysliceY, slicesY);
    long t2 = System.currentTimeMillis();
    System.out.println((t2 - t1) + " msec");

    // display the result
    if (rank == 0) {
      FileWriter fstream = new FileWriter(filename);
      BufferedWriter out = new BufferedWriter(fstream);
      out.write("Matrix C : \n");
      for (int i = 0; i < m; i++) {
        out.write(vectorY[i] + " ");
      }
      out.close();
    }
  }
Пример #3
0
  /** AES partial key search main program. */
  public static void main(String[] args) throws Exception {
    // Start timing.
    long t1 = System.currentTimeMillis();

    Comm.init(args);
    Comm world = Comm.world();
    int size = world.size();
    int rank = world.rank();

    // Parse command line arguments.
    if (args.length != 4) usage();
    plaintext = Hex.toByteArray(args[0]);
    ciphertext = Hex.toByteArray(args[1]);
    partialkey = Hex.toByteArray(args[2]);
    n = Integer.parseInt(args[3]);

    // Make sure n is not too small or too large.
    if (n < 0) {
      System.err.println("n = " + n + " is too small");
      System.exit(1);
    }
    if (n > 63) {
      System.err.println("n = " + n + " is too large");
      System.exit(1);
    }

    // Set up variables for doing trial encryptions.
    keylsbs =
        ((partialkey[24] & 0xFFL) << 56)
            | ((partialkey[25] & 0xFFL) << 48)
            | ((partialkey[26] & 0xFFL) << 40)
            | ((partialkey[27] & 0xFFL) << 32)
            | ((partialkey[28] & 0xFFL) << 24)
            | ((partialkey[29] & 0xFFL) << 16)
            | ((partialkey[30] & 0xFFL) << 8)
            | ((partialkey[31] & 0xFFL));
    maxcounter = (1L << n) - 1L;
    ;
    foundkey = new byte[32];
    trialkey = new byte[32];
    System.arraycopy(partialkey, 0, trialkey, 0, 32);
    trialciphertext = new byte[16];
    cipher = new AES256Cipher(trialkey);

    // Determin which chunk of the search space this process will do.
    LongRange chunk = (new LongRange(0, maxcounter - 1)).subrange(size, rank);
    long lb = chunk.lb();
    long ub = chunk.ub();

    // Set up to receive notification when any process finds the key.
    CommRequest req = new CommRequest();
    world.floodReceive(IntegerBuf.emptyBuffer(), req);

    // Try every possible combination of low-order key bits.
    for (long counter = lb; counter <= ub; ++counter) {
      // Fill in low-order key bits.
      long lsbs = keylsbs | counter;
      trialkey[24] = (byte) (lsbs >>> 56);
      trialkey[25] = (byte) (lsbs >>> 48);
      trialkey[26] = (byte) (lsbs >>> 40);
      trialkey[27] = (byte) (lsbs >>> 32);
      trialkey[28] = (byte) (lsbs >>> 24);
      trialkey[29] = (byte) (lsbs >>> 16);
      trialkey[30] = (byte) (lsbs >>> 8);
      trialkey[31] = (byte) (lsbs);

      // Try the key.
      cipher.setKey(trialkey);
      cipher.encrypt(plaintext, trialciphertext);

      // If the result equals the ciphertext, we found the key.
      if (match(ciphertext, trialciphertext)) {
        System.arraycopy(trialkey, 0, foundkey, 0, 32);
        world.floodSend(IntegerBuf.emptyBuffer());
      }

      // If the key was found, exit the loop.
      if (req.isFinished()) break;
    }

    // Print the key we found.
    if (foundkey != null) {
      System.out.println(Hex.toString(foundkey));
    }

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