All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sim.app.hexabugs.ThreadedHexaDiffuser Maven / Gradle / Ivy

Go to download

MASON is a fast discrete-event multiagent simulation library core in Java, designed to be the foundation for large custom-purpose Java simulations, and also to provide more than enough functionality for many lightweight simulation needs. MASON contains both a model library and an optional suite of visualization tools in 2D and 3D.

The newest version!
/*
  Copyright 2006 by Sean Luke and George Mason University
  Licensed under the Academic Free License version 3.0
  See the file "LICENSE" for more information
*/

package sim.app.hexabugs;
import sim.engine.*;
import sim.field.grid.*;

/** A dual-threaded version of Diffuser for use on machines with two processors.
    Holds a ParallelSequence which in turn holds two dummy Steppables which each
    call diffuse(...) with different start and end values.  That way basically we
    split the array diffusion up among two processors, one taking the first half
    of the array and one taking up the second half.  Because Diffuser takes up
    nearly all our time, this results in a dramatic increase in speed on a
    dual-processor machine. */

public /*strictfp*/ class ThreadedHexaDiffuser implements Steppable
    {
    private static final long serialVersionUID = 1;

    public ParallelSequence diffusers;
        
    DoubleGrid2D updateGrid;
    DoubleGrid2D tempGrid;
    double evaporationRate;
    double diffusionRate;

    public ThreadedHexaDiffuser( final DoubleGrid2D updateGrid,
        final DoubleGrid2D tempGrid,
        final double evaporationRate,
        final double diffusionRate )
        {
        this.updateGrid = updateGrid;
        this.tempGrid = tempGrid;
        this.evaporationRate = evaporationRate;
        this.diffusionRate = diffusionRate;
        diffusers = new ParallelSequence(new Steppable[]
            {
            new Steppable ()
                { 
                public void step(SimState state) 
                    {
                    // diffuse top half of field
                    HexaBugs hexabugs = (HexaBugs)state;
                    int _gridWidth = hexabugs.valgrid.getWidth();  // read-only, so threadsafe with other one
                    diffuse(hexabugs, 0, _gridWidth/2);
                    }
                },
            new Steppable ()
                {
                public void step(SimState state) 
                    {
                    // diffuse bottom half of field
                    HexaBugs hexabugs = (HexaBugs)state;
                    int _gridWidth = hexabugs.valgrid.getWidth();  // read-only, so threadsafe with other one
                    diffuse(hexabugs, _gridWidth/2, _gridWidth);
                    }
                }
            });
        }
        
        
    public void step(SimState state)
        {
        diffusers.step(state);
                
        // copy HexaBugs.this.valgrid2 to HexaBugs.this.valgrid
        HexaBugs hexabugs = (HexaBugs)state;
        hexabugs.valgrid.setTo(hexabugs.valgrid2);
        }
        
    /** We'll have Hexabugs call this in its stop() method to give US a chance to
        call cleanup on our underlying ParallelSequence so we don't leak threads. */
    public void cleanup()
        {
        diffusers.cleanup();
        }
                
        
    /** Diffuse hexabugs.valgrid.field[start...end] not including end */
        
    // this code is utterly incomprehenxible.  See HexaDiffuser.java for other less confusing examples
    // and for an explanation for why the code looks the way it does.
        
    void diffuse(HexaBugs hexabugs, int start, int end)
        {
        // stolen from HeatBugs and modified for our own purposes
        // locals are faster than instance variables
        final DoubleGrid2D _valgrid = updateGrid;
        final double[][] _valgrid_field = updateGrid.field;
        final double[][] _valgrid2_field = tempGrid.field;
        final int _gridWidth = _valgrid.getWidth();
        final int _gridHeight = _valgrid.getHeight();
        final double _evaporationRate = evaporationRate;
        final double _diffusionRate = diffusionRate;

        double average;
        double[] _past = _valgrid_field[_valgrid.stx(start-1)];
        double[] _current = _valgrid_field[start];
        double[] _next;
        double[] _put;
        
        int yminus1;
        int yplus1;
        
        // for each x and y position
        for(int x=start;x




© 2015 - 2025 Weber Informatics LLC | Privacy Policy