// *
  public static void main(String[] args) throws Exception {
    Comm.init(args);
    Comm world = Comm.world();
    int rank = world.rank();
    int size = world.size();

    int dim = DEFAULT_D, kay = DEFAULT_K, datasize = Integer.MAX_VALUE;
    String infile = DEFAULT_IN;
    switch (args.length) {
      case 4:
        if (isInteger(args[3])) dim = Integer.valueOf(args[3]);
      case 3:
        if (isInteger(args[2])) kay = Integer.valueOf(args[2]);
      case 2:
        if (isInteger(args[1])) datasize = Integer.valueOf(args[1]);
      case 1:
        if (new File(args[0]).exists()) infile = args[0];
    }
    // the size argument to constructor is the max # of lines to read
    IrisKMeans me = new IrisKMeans(infile, datasize, kay, dim);
    me.prepareData();
    if (rank == 0) me.prepareClusters();

    // we want the actual number of elements read
    datasize = me.getSize();
    Range ir = new Range(0, datasize - 1);
    me.setStart(ir.lb());
    me.setEnd(ir.ub());

    while (!me.changed()) {
      // broadcast ciuster assignments
      double[][] assignments = new double[kay][dim];
      DoubleBuf dbuf = DoubleBuf.buffer(assignments);
      if (rank == 0) dbuf = DoubleBuf.buffer(me.clusterCenters());
      world.broadcast(0, dbuf);

      me.assignmentStep();
      if (rank == 0) {
        // gather total-count pairs
        me.updateStep();
      }
    }
  }
Example #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();
    }
  }