Package org.jamesii.model.carules

Examples of org.jamesii.model.carules.ICARulesModel


  @Override
  public void instrumentModel(IModel model,
      IComputationTaskConfiguration simConfig) {
    model.setMediator(new Mediator());
    ICARulesModel m = (ICARulesModel) model;
    ICARulesGrid g = m.getGrid();

    // try to extract a custom cell renderer from the simulation configuration
    Object userParameters = simConfig.getParameters().get("user.parameters");
    IGridCellRenderer renderer = null;
    if (userParameters instanceof IGridCellRenderer) {
View Full Code Here


    return time;
  }

  @Override
  protected void nextStep() {
    ICARulesModel m = this.getModel();

    // get the grid from the model
    ICARulesGrid grid = m.getGrid();

    // make a copy of the grid, and thus of each cell's state
    ICARulesGrid oldGrid = grid.cloneGrid();

    // now iterate over all cells in the copy of the grid
    for (ICACell<?> cell : oldGrid.getCellList()) {

      // get the current state
      int current = cell.getState();

      // get the neighbours of the current cell
      INeighborStates<Integer> neighbours =
          oldGrid.getNeighborStates(m.getBaseRules().getNeighborhood(current),
              false, cell.getPosition());

      // compute the new state based on the current one, and the states of the
      // neighbours
      int next = m.getBaseRules().getNextState(current, neighbours);

      // now set the new state (to the ORIGINAL grid, not the original one)
      grid.setState(next, cell.getPosition());

    }
    m.changed();

    // advance the time
    time += 1;
    changed();
View Full Code Here

    return time;
  }

  @Override
  protected void nextStep() {
    ICARulesModel m = this.getModel();

    // get the grid from the model
    ICARulesGrid grid = m.getGrid();

    Runtime r = Runtime.getRuntime();

    int procs = r.availableProcessors();

    // list for the cells of which we computed new states
    HashMap<int[], Integer> ccells = new HashMap<>();

    // System.out.println("Going to use " + procs + " processors");

    List<int[]> cells = new ArrayList<>(candidates.keySet());

    // naive approach
    int partition = cells.size() / procs;

    ComputeThread[] threads = new ComputeThread[procs + 1];

    // create a thread per partition
    int p = 0;
    for (int i = 0; i < procs; i++) {
      threads[i] = new ComputeThread(m, grid, cells, p, p + partition);
      p += partition;
      threads[i].start();
    }
    // create an extra thread for the rest of the division ("uncomplete"
    // partition), if there is such a rest
    if (p != cells.size()) {
      threads[procs] = new ComputeThread(m, grid, cells, p, cells.size());
      threads[procs].start();
    }

    // now wait until all threads have finished their job
    try {
      for (ComputeThread thread : threads) {
        if (thread != null) {
          thread.join();
          ccells.putAll(thread.ccells);
        }

      }
    } catch (InterruptedException e) {
      SimSystem.report(e);
    }

    // empty the old list of cells to be updated, and let's refill it
    this.candidates.clear();

    // for all modified cells do
    for (Map.Entry<int[], Integer> e : ccells.entrySet()) {
      int current = m.getGrid().getState(e.getKey());
      // update the cell's state
      m.getGrid().setState(e.getValue(), e.getKey());

      // remember the cell as to be updated in the next turn
      this.candidates.put(e.getKey(), null);

      // add all neighbour cells of the current cell to the list of cells to be
      // computed in the next step
      List<int[]> n =
          m.getGrid().getNeighbors(m.getBaseRules().getNeighborhood(current),
              m.getBaseRules().isTorus(current), e.getKey());
      for (int i = 0; i < n.size(); i++) {
        this.candidates.put(n.get(i), null);
      }
    }

    m.changed();

    // advance the time
    time += 1;
    changed();
View Full Code Here

  protected void nextStep() {
    // list for the cells of which we computed new states
    HashMap<int[], Integer> cells = new HashMap<>();

    // the cellular automaton
    ICARulesModel m = (ICARulesModel) this.getModel();

    // let's update each cell which could get a new state
    for (int[] cell : candidates.keySet()) {
      if (cell == null) {
        continue;
      }

      // get the current state of the cell
      int current = m.getGrid().getState(cell);

      // get the neighbours of the current cell
      INeighborStates<Integer> neighbours =
          m.getGrid().getNeighborStates(
              m.getBaseRules().getNeighborhood(current), false, cell);

      // compute the new state
      int next = m.getBaseRules().getNextState(current, neighbours);

      // if the state has been changed
      if (current != next) {
        cells.put(cell, next);
      }
    }

    // empty the old list of cells to be updated, and let's refill it
    this.candidates = new HashMap<>(); // .clear();

    // for all modified cells do
    for (Map.Entry<int[], Integer> e : cells.entrySet()) {
      int current = m.getGrid().getState(e.getKey());
      // update the cell's state
      m.getGrid().setState(e.getValue(), e.getKey());

      // remember the cell as to be updated in the next turn
      this.candidates.put(e.getKey(), null);

      // add all neighbour cells of the current cell to the list of cells to be
      // computed in the next step
      List<int[]> n =
          m.getGrid().getNeighbors(m.getBaseRules().getNeighborhood(current),
              false, e.getKey());
      for (int i = 0; i < n.size(); i++) {
        this.candidates.put(n.get(i), null);
      }
    }

    m.changed();

    // long curr = System.currentTimeMillis();
    // advance the time
    time += 1;
    // if (time % 1000 == 0) {
View Full Code Here

    return time;
  }

  @Override
  protected void nextStep() {
    ICARulesModel m = this.getModel();

    // get the grid from the model
    ICARulesGrid grid = m.getGrid();

    // make a copy of the grid, and thus of each cell's state
    ICARulesGrid oldGrid = grid.cloneGrid();

    Runtime r = Runtime.getRuntime();

    int procs = r.availableProcessors();

    // System.out.println("Going to use " + procs + " processors");
    int[] size = oldGrid.getSize();
    int cellCount = 1;
    for (int element : size) {
      cellCount *= element;
    }

    int partition = cellCount / procs;

    ComputeThread[] threads = new ComputeThread[procs + 1];

    // create a thread per partition
    int p = 0;
    for (int i = 0; i < threads.length; i++) {
      threads[i] =
          new ComputeThread(m, grid, oldGrid, p, Math.min(cellCount - 1, p
              + partition));
      p += partition;
      threads[i].start();
    }
    // create an extra thread for the rest of the division ("uncomplete"
    // partition), if there is such a rest
    if (p < cellCount - 1) {
      threads[partition] =
          new ComputeThread(m, grid, oldGrid, p, cellCount - 1);
      threads[partition].start();
    }

    // now wait until all threads have finished their job
    try {
      for (ComputeThread thread : threads) {
        if (thread != null) {
          thread.join();
        }
      }
    } catch (InterruptedException e) {
      SimSystem.report(e);
    }

    m.changed();

    // advance the time
    time += 1;
    changed();

View Full Code Here

    return time;
  }

  @Override
  protected void nextStep() {
    ICARulesModel m = this.getModel();

    // get the grid from the model
    ICARulesGrid grid = m.getGrid();

    // update number of processors occasionally
    procCountRefresh++;
    if (procCountRefresh == 1000) {
      Runtime r = Runtime.getRuntime();
      procCount = r.availableProcessors();
      // recompute the number of threads - if threadMult = 1.0 we'll have as
      // many threads as physical resources, > 1 means that's there is the
      // Probability to use more threads
      procCount = (int) (procCount * threadCountFactor);
      procCountRefresh = 0;
    }

    // list for the cells of which we computed new states
    HashMap<int[], Integer> ccells = new HashMap<>();

    // System.out.println("Going to use " + procs + " processors");

    List<int[]> cells = new ArrayList<>(candidates.keySet());

    // get the number of threads to be used right now, procsUsed = 0 is fine
    // here, because we'll have automatically an "additional one" for the rest
    int procsUsed = procCount;
    while ((procsUsed * minNumberOfCellsPerThread > cells.size())
        && (procsUsed > 1)) {
      procsUsed--;
    }

    int partition = cells.size() / procsUsed;

    ComputeThread[] threads = new ComputeThread[procsUsed + 1];

    // create a thread per partition
    int p = 0;
    for (int i = 0; i < procsUsed; i++) {
      threads[i] = new ComputeThread(m, grid, cells, p, p + partition);
      p += partition;
      threads[i].start();
    }
    // create an extra thread for the rest of the division ("uncomplete"
    // partition), if there is such a rest
    if (p != cells.size()) {
      threads[procsUsed] = new ComputeThread(m, grid, cells, p, cells.size());
      threads[procsUsed].start();
    }

    // now wait until all threads have finished their job
    try {
      for (ComputeThread thread : threads) {
        if (thread != null) {
          thread.join();
          ccells.putAll(thread.ccells);
        }

      }
    } catch (InterruptedException e) {
      SimSystem.report(e);
    }

    // empty the old list of cells to be updated, and let's refill it
    this.candidates.clear();

    // for all modified cells do
    for (Map.Entry<int[], Integer> e : ccells.entrySet()) {
      int current = m.getGrid().getState(e.getKey());
      // update the cell's state
      m.getGrid().setState(e.getValue(), e.getKey());

      // remember the cell as to be updated in the next turn
      this.candidates.put(e.getKey(), null);

      // add all neighbour cells of the current cell to the list of cells to be
      // computed in the next step
      List<int[]> n =
          m.getGrid().getNeighbors(m.getBaseRules().getNeighborhood(current),
              m.getBaseRules().isTorus(current), e.getKey());
      for (int i = 0; i < n.size(); i++) {
        this.candidates.put(n.get(i), null);
      }
    }

    m.changed();

    // advance the time
    time += 1;
    changed();

View Full Code Here

TOP

Related Classes of org.jamesii.model.carules.ICARulesModel

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.