/** Constructor to generate a random 3-CNF formula for solving. */
  public ThreeSatDecision(int numVars, int numClauses, long seed) {
    this.numVars = numVars;
    this.numClauses = numClauses;

    // Initialize the PRNG and the formula/variable data structures
    Random prng = Random.getInstance(seed);
    formula = new Literal[numClauses][3]; // 3 literals per clause
    variables = new boolean[numVars];

    // Randomly generate the formula the formula
    for (int i = 0; i < numClauses; i++) {
      for (int j = 0; j < 3; j++) {
        formula[i][j] = new Literal(prng.nextBoolean(), prng.nextInt(numVars));
      }
    }
  }
Exemplo n.º 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();
    }
  }