D
D Jensen
Hi,
A basic thread question that i can't seem to find an answer to in the
faqs:
The question is how to run a METHOD as a thread... situation is below:
I've written a MonteCarlo simulation program in Java (with minimal use
of object oriented principles -- i have to run through the simulation
about 5 million times, and the overhead that would be caused by
creating/modifying a few hundred objects per time would be
unacceptable).
It will only be used on Intel processors with HT, so i really want to
be able to use 2 threads, each doing the simulation 2.5 million times,
and then averaging the results. Right now, it's entirely a CLI program
(though a GUI would be trivial to add).
It's almost entirely sequentially oriented (as opposed to OO)
The basic outline of my program is as follows:
4 classes (for clarity purposes only -- none is ever instantiated as an
object):
1) ArrayFunc - contains optimized (for my purposes) copy methods for 1d
and 2d arrays, as well as methods for removing an element from an array
by index or value, "casting" an array of floats to ints, etc.
2) Interact - contains all the methods needed to get values from the
user, with error checking & exception handling
3) TestFor - contains about 65 methods operating on various sizes of
various dimension arrays that are needed for the simulation - often
calls methods in ArrayFunc
4) Simulator - the only class with a main() method, contains an
initialization method (as opposed to a constructor, as an object is
never created), as well as all the calls to the various methods; a
doSimulation() method, which runs through the simulation 5 million
times, each time incrementing a "winner" in an array before dividing
all elements of the winner-array by 5M, and printing out the output.
Fundamentally, then, what I want to do is run doSimulation twice (each
2.5M times), in separate threads, so that they run concurrently. After
a little experimentation, this seems (to me) to translate into putting
it into a separate class, making that class runnable, and putting it
all into the run() method of that new class. That seems a lot of
wierdness for just wanting to run one single method twice
concurrently... Any way to do this without creating actual objects? Any
help? (and yes, this is my first time trying to implement threads in a
non-trivial exercize). If someone might post a program that would
create a few random numbers and average them, twice in 2 threads, that
would probably solve my entire problem (i'd be able to work backwards,
i think)
Many thanks for any ideas!
if my explanation was too convoluted, i'll post the barebones of my
classes somewhere if that would help. THANKS!
David Jensen
EPFL, Switzerland
PS. on a 3.4GHz pentium4 running WinXP, the program as it currently is
takes around 110s to run, but task manager shows only 50% of the CPU is
being used (though the graphs in task manager show both being used
around 50%) -- if i could get it down to around 60 or 65 seconds, that
would be great, and is the main reason for me wanting to use 2 threads.
PPS. yes, the variance is so ridiculous that i DO need to run it 5M
times.
A basic thread question that i can't seem to find an answer to in the
faqs:
The question is how to run a METHOD as a thread... situation is below:
I've written a MonteCarlo simulation program in Java (with minimal use
of object oriented principles -- i have to run through the simulation
about 5 million times, and the overhead that would be caused by
creating/modifying a few hundred objects per time would be
unacceptable).
It will only be used on Intel processors with HT, so i really want to
be able to use 2 threads, each doing the simulation 2.5 million times,
and then averaging the results. Right now, it's entirely a CLI program
(though a GUI would be trivial to add).
It's almost entirely sequentially oriented (as opposed to OO)
The basic outline of my program is as follows:
4 classes (for clarity purposes only -- none is ever instantiated as an
object):
1) ArrayFunc - contains optimized (for my purposes) copy methods for 1d
and 2d arrays, as well as methods for removing an element from an array
by index or value, "casting" an array of floats to ints, etc.
2) Interact - contains all the methods needed to get values from the
user, with error checking & exception handling
3) TestFor - contains about 65 methods operating on various sizes of
various dimension arrays that are needed for the simulation - often
calls methods in ArrayFunc
4) Simulator - the only class with a main() method, contains an
initialization method (as opposed to a constructor, as an object is
never created), as well as all the calls to the various methods; a
doSimulation() method, which runs through the simulation 5 million
times, each time incrementing a "winner" in an array before dividing
all elements of the winner-array by 5M, and printing out the output.
Fundamentally, then, what I want to do is run doSimulation twice (each
2.5M times), in separate threads, so that they run concurrently. After
a little experimentation, this seems (to me) to translate into putting
it into a separate class, making that class runnable, and putting it
all into the run() method of that new class. That seems a lot of
wierdness for just wanting to run one single method twice
concurrently... Any way to do this without creating actual objects? Any
help? (and yes, this is my first time trying to implement threads in a
non-trivial exercize). If someone might post a program that would
create a few random numbers and average them, twice in 2 threads, that
would probably solve my entire problem (i'd be able to work backwards,
i think)
Many thanks for any ideas!
if my explanation was too convoluted, i'll post the barebones of my
classes somewhere if that would help. THANKS!
David Jensen
EPFL, Switzerland
PS. on a 3.4GHz pentium4 running WinXP, the program as it currently is
takes around 110s to run, but task manager shows only 50% of the CPU is
being used (though the graphs in task manager show both being used
around 50%) -- if i could get it down to around 60 or 65 seconds, that
would be great, and is the main reason for me wanting to use 2 threads.
PPS. yes, the variance is so ridiculous that i DO need to run it 5M
times.