MASON-INTEREST-L Archives

February 2014

MASON-INTEREST-L@LISTSERV.GMU.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Sean Luke <[log in to unmask]>
Reply To:
MASON Multiagent Simulation Toolkit <[log in to unmask]>
Date:
Tue, 11 Feb 2014 15:19:13 -0500
Content-Type:
text/plain
Parts/Attachments:
text/plain (59 lines)
On Feb 11, 2014, at 11:27 AM, Lucas Branisso <[log in to unmask]> wrote:

> What I did was to write a function that create a pool of threads (using Java's ExecutorService) and submit each simulation as job for the thread pool.

You could also hijack ParallelSequence.  Consider the following standard "simple" MASON loop:

public static void main(String[] args)
	{
	int jobs = 100;  // let’s do 100 runs
	long maxsteps = 5000;
	int numCores = 4;  // how many cores do I want to use at one time
	long seed = System.currentTimeMillis();

       SimState state = new MyModel(seed);
       for(int job = 0; job < jobs; job++)
		{
		state.setJob(job);
		state.start();
		do
			if (!state.schedule.step(state)) break;
		while(state.schedule.getSteps() < maxsteps);
		state.finish();
		}
       System.exit(0);
	}

You could do it this way to make it multithreaded (I just whipped this up, dunno if it even compiles):

public static void main(String[] args)
	{
	int jobs = 100;  // let’s do 100 runs
	long maxsteps = 5000;
	int numCores = 4;  // how many cores do I want to use at one time
	final long seed = System.currentTimeMillis();

	final Steppable[] steps = new Steppable[jobs];
	for(int i = 0; i < jobs; i++)
		{
		final int job = i;
		steps[job] = new Steppable()
			{
			public void step(SimState s)  // s will be null, ignore
				{
				MyModel state = new MyModel(seed + job);
				state.setJob(job);
				state.start();
				do
					if (!state.schedule.step(state)) break;
				while(state.schedule.getSteps() < maxsteps);
				state.finish();
				}
			};
		}
	ParallelSequence p = new ParallelSequence(steps, jobs);
	p.step(null);
	p.cleanup();
	System.exit(0);
	}

ATOM RSS1 RSS2