Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
parallel random sampling

Subject: parallel random sampling

From: audley james

Date: 23 Sep, 2011 08:23:08

Message: 1 of 8

Hello all,

I use a lot of random sampling functions to do my 'hypothesize and test' algorithms. Each 'hypothesis and test' is combined in a single loop and is based on one randomly generated hypothesis. I basically want to know if I can run two or more of these loops
at the same time in Matlab.

May be a stupid question,put let me know if any thing is possible to speedup random sampling is available .

cheers
audley

Subject: parallel random sampling

From: Peter Perkins

Date: 23 Sep, 2011 14:05:18

Message: 2 of 8

On 9/23/2011 4:23 AM, audley james wrote:
> I use a lot of random sampling functions to do my 'hypothesize and test'
> algorithms. Each 'hypothesis and test' is combined in a single loop and
> is based on one randomly generated hypothesis. I basically want to know
> if I can run two or more of these loops
> at the same time in Matlab.
>
> May be a stupid question,put let me know if any thing is possible to
> speedup random sampling is available .

Audley, it's hard to tell what you're asking.

Since R208b, MATLAB has had support for parallel random number
generation in the sense of multiple, independent random number streams.
  That's the kind of thing you'd use if you were running the same
simulation on multiple machines. See the documentation on RandStream
for details.

But it sounds like you're asking about vectorization, which is another
form of parallelism. Certainly you can generate "parallel simulations"
simply by using, say, RANDN to generate vectors or matrices. For
example, this

 >> x = randn(100,1000);
 >> y = log(sum(exp(x),1));
 >> [mean(y) var(y)]
ans =
        5.0937 0.016704

is 1000 trials of a Monte-Carlo simulation to find the first two moments
of the log of a sum of lognormal random variables. It relies on the
fact that RANDN returns independent random values, and so the columns of
x are "parallel simulations".

You may or may not be able to apply the same idea to what you are doing,
depending on what you're doing.

Subject: Run same .m file in parallel

From: audley james

Date: 24 Sep, 2011 01:01:30

Message: 3 of 8

Peter,

thanks for the response.

My concern is algorithm run-time(speed). In general, I was wondering if it is possible to run the same .m file twice( i.e. in parallel) on each processor or anything similar?

thanks
audley

Subject: Run same .m file in parallel

From: Peter Perkins

Date: 25 Sep, 2011 14:18:13

Message: 4 of 8

On 9/23/2011 9:01 PM, audley james wrote:
> My concern is algorithm run-time(speed). In general, I was wondering if
> it is possible to run the same .m file twice( i.e. in parallel) on each
> processor or anything similar?

You're not providing a lot to go on.

If you are trying to use multiple MATLAB sessions, or Parallel Computing
Toolbox workers in parallel, and have already read the MATLAB User Guide
section on random number generation, and have already looked at the
documentation for RandStream, then I'd recommend looking at these blog
posts to figure out what you need:

<http://blogs.mathworks.com/loren/2011/07/07/simpler-control-of-random-number-generation-in-matlab>
<http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i>
<http://blogs.mathworks.com/loren/2008/11/13/new-ways-with-random-numbers-part-ii>

Subject: Run same .m file in parallel

From: TideMan

Date: 25 Sep, 2011 20:05:03

Message: 5 of 8

On Sep 24, 2:01 pm, "audley james" <audle...@gmail.com> wrote:
> Peter,
>
> thanks for the response.
>
> My concern is algorithm run-time(speed). In general, I was wondering if it is possible to run the same .m file twice( i.e. in parallel) on each processor or anything similar?
>
> thanks
> audley

One way to do it (without the parallel processing toolbox) is to
simply open two (or more) Matlab sessions and run your .m file on each
of them. But you need to re-seed the random number generators before
running, eg.:
rand('state',sum(100*clock))
randn('state',sum(100*clock))
otherwise you could be generating the same random numbers in each
session.

Subject: Run same .m file in parallel

From: Peter Perkins

Date: 26 Sep, 2011 13:55:47

Message: 6 of 8

On 9/25/2011 4:05 PM, TideMan wrote:
> One way to do it (without the parallel processing toolbox) is to
> simply open two (or more) Matlab sessions and run your .m file on each
> of them. But you need to re-seed the random number generators before
> running, eg.:
> rand('state',sum(100*clock))
> randn('state',sum(100*clock))
> otherwise you could be generating the same random numbers in each
> session.

TideMan, your explanation of "why" is correct, but that specific code is
STRONGLY DISCOURAGED, unless you are using an old version of MATLAB.
There have been better ways of generating parallel streams of random
numbers in MATLAB since R2008b.

See for, example, this page in the current documentation for some of the
details:

<http://www.mathworks.com/help/techdoc/math/bsn94u0-1.html>

In addition to the MATLAB User Guide section on random numbers, these
blog posts:

<http://blogs.mathworks.com/loren/2011/07/07/simpler-control-of-random-number-generation-in-matlab>
<http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numbers-part-i>
<http://blogs.mathworks.com/loren/2008/11/13/new-ways-with-random-numbers-part-ii>

describe in detail the recommended ways of generating random numbers in
parallel.

Subject: Run same .m file in parallel

From: TideMan

Date: 26 Sep, 2011 19:58:25

Message: 7 of 8

On Sep 27, 2:55 am, Peter Perkins
<Peter.Remove.Perkins.T...@mathworks.com> wrote:
> On 9/25/2011 4:05 PM, TideMan wrote:
>
> > One way to do it (without the parallel processing toolbox) is to
> > simply open two (or more) Matlab sessions and run your .m file on each
> > of them.  But you need to re-seed the random number generators before
> > running, eg.:
> > rand('state',sum(100*clock))
> > randn('state',sum(100*clock))
> > otherwise you could be generating the same random numbers in each
> > session.
>
> TideMan, your explanation of "why" is correct, but that specific code is
> STRONGLY DISCOURAGED, unless you are using an old version of MATLAB.
> There have been better ways of generating parallel streams of random
> numbers in MATLAB since R2008b.
>
> See for, example, this page in the current documentation for some of the
> details:
>
> <http://www.mathworks.com/help/techdoc/math/bsn94u0-1.html>
>
> In addition to the MATLAB User Guide section on random numbers, these
> blog posts:
>
> <http://blogs.mathworks.com/loren/2011/07/07/simpler-control-of-random...>
> <http://blogs.mathworks.com/loren/2008/11/05/new-ways-with-random-numb...>
> <http://blogs.mathworks.com/loren/2008/11/13/new-ways-with-random-numb...>
>
> describe in detail the recommended ways of generating random numbers in
> parallel.

You're right, Peter, I'm running 2006a.

Subject: parallel random sampling

From: Bruno Luong

Date: 26 Sep, 2011 22:24:29

Message: 8 of 8

"audley james" <audleyer@gmail.com> wrote in message <j5hflb$idb$1@newscl01ah.mathworks.com>...
> Hello all,
>
> I use a lot of random sampling functions to do my 'hypothesize and test' algorithms. Each 'hypothesis and test' is combined in a single loop and is based on one randomly generated hypothesis. I basically want to know if I can run two or more of these loops
> at the same time in Matlab.
>
> May be a stupid question,put let me know if any thing is possible to speedup random sampling is available .

It seems like the below is what you are looking for:

http://www.mathworks.com/matlabcentral/fileexchange/33004-fasterparallel-random-number-generator-via-standard-c++

Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us