Subject: | |
From: | |
Reply To: | |
Date: | Tue, 11 Feb 2014 15:19:13 -0500 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
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);
}
|
|
|