Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #1643

Re: How to Compile this code (multiple random walks)

From Oliver Ruebenkoenig <ruebenko@wolfram.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: How to Compile this code (multiple random walks)
Date 2011-04-12 10:54 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <io1b0a$jgk$1@smc.vnet.net> (permalink)

Show all headers | View raw


On Tue, 12 Apr 2011, Peter Pein wrote:

> Am 11.04.2011 13:06, schrieb mfific@gmail.com:
>> Transpose[{Range[0, Length[#] - 1], #}]&@
>>   NestWhileList[(# + {If[Random[]>  .5, 1, -1],
>>        If[Random[]>  .5, 1, -1], If[Random[]>  .5, 1, -1]}
>>      )&, {0, 0, 0}, -5<  #[[1]]<  5&&  -5<  #[[2]]<  5&&  -5<  #[[3]]
>> <  5&  ]
>
> Hi Mario,
>
> you _can_ compile the part that will be given to Transpose but
> NestWhileList seems to be efficient enough.
>
> I tried your version and
>
> count3helper=Compile[{},
>   NestWhileList[# + 2 RandomInteger[1, {3}] - 1 &,
>     {0, 0, 0},
>     -5 < #[[1]] < 5 && -5 < #[[2]] < 5 && -5 < #[[3]] < 5 &],
>   {{_NestWhileList, _Integer, 2}},
>   CompilationTarget -> "C",
>   Parallelization -> True, RuntimeAttributes -> {Listable}
> ];
>
> count3[] := Transpose[{Range[0, Length[#] - 1], #} &@
>   count3helper[]
> ]
>
> using ParallelTable some times 10^6 times each and got timings for your
> version of ~25 seconds and ~32 seconds for the compiled version above.
>
> Sorry,
> Peter
>
> P.S.: I used
>
> RandomSeed[1];
> {tim, hst} = AbsoluteTiming[Histogram[ParallelTable[Length[count3[]],
>  {10^6}], PlotLabel -> "time here"]];
> hst /. "time here" -> ToString[NumberForm[tim, 4]]
>
> for testing.
>
> P.P.S.:
>
> changing the test in NestWhileList to
> -5 < Min[#] && Max[#] < 5 &
> results in timings ~24.3s for your and 27.6s for the compiled version.
>
>

Hi Peter,

good idea with the _NestWhileList. I forgot about that. However, if you 
look at

Needs["CompiledFunctionTools`"]
CompilePrint[count3helper]

there is still the call to MainEvaluate.

A small note, the options Parallelization -> True, RuntimeAttributes -> 
{Listable} would only make a difference if the function had, say, an

{in, _Integer, 0}

type of input. Then, you could call the function with a integer vector and 
the code would run in parallel.

Hth,

Oliver

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: How to  Compile this code (multiple random walks) Oliver Ruebenkoenig <ruebenko@wolfram.com> - 2011-04-12 10:54 +0000

csiph-web