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


Groups > comp.soft-sys.math.mathematica > #1591 > unrolled thread

How to Compile this code (multiple random walks)

Started by"mfific@gmail.com" <mfific@gmail.com>
First post2011-04-11 11:06 +0000
Last post2011-04-12 09:54 +0000
Articles 3 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  How to  Compile this code (multiple random walks) "mfific@gmail.com" <mfific@gmail.com> - 2011-04-11 11:06 +0000
    Re: How to  Compile this code (multiple random walks) Peter Pein <petsie@dordos.net> - 2011-04-12 09:54 +0000
    Re: How to Compile this code (multiple random walks) Derek Yates <yatesd@me.com> - 2011-04-12 09:54 +0000

#1591 — How to Compile this code (multiple random walks)

From"mfific@gmail.com" <mfific@gmail.com>
Date2011-04-11 11:06 +0000
SubjectHow to Compile this code (multiple random walks)
Message-ID<inuncc$2dl$1@smc.vnet.net>
Hi All,

I would appreciate help with Compile function. Below is the code that
I could not compiled. The output are the three random walk counters
with (-5, 5) bounds, along with a counter for a number of steps. The
compile does not work here, I assume because the output is a matrix
(it works if there is only one counter). But I need these three
counters running in parallel for later development. Any help would be
appreciated.

thanks,

Mario Fific

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 & ]

[toc] | [next] | [standalone]


#1612

FromPeter Pein <petsie@dordos.net>
Date2011-04-12 09:54 +0000
Message-ID<io17g0$i59$1@smc.vnet.net>
In reply to#1591
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.

[toc] | [prev] | [next] | [standalone]


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

FromDerek Yates <yatesd@me.com>
Date2011-04-12 09:54 +0000
SubjectRe: How to Compile this code (multiple random walks)
Message-ID<io17hc$i6l$1@smc.vnet.net>
In reply to#1591
To my knowledge NestWhileList is not compilable, so I am surprised
that you say it worked for a single counter. You may need to use
SetSystemOptions["CompileOptions"->"CompileReportExternal"->True] to
get a warning message about not being able to compile.

There is a work around for NestWhileList. FixedPointList is
compilable, and by using the option SameTest carefully, you can
replicate the NestWhileList behaviour. Here is some code that appears
to compile properly (using Mathematica 8) and does most of what you
want in compiled form. The final counter for the number of steps is
not compiled. The code below is also slightly more general than yours,
in that it uses the length of the input (start) vector to decide how
many random walk counters there are.

With[{compiledfunc=Compile[{{start,_Real,1}},
Most@FixedPointList[#+(If[#<0.5,-1,1]&)/
@RandomReal[{0,1},Length[start]]&,start,
SameTest-
>(Module[{b=False},Scan[If[Not[-5.<#<5.],Return[b=True]]&,#];b]&)]]},

f[start_]:=Transpose[{Range[0,Length[#]-1],#}]&@compiledfunc[start]]

I don't know of any way to include the path counter into the compile
as the result you generate is not a tensor. If you are planning to use
the code in Mathematica, then I doubt the performance hit of not
having that last step in the Compile will be very great. If you are
wanting to compile it to C code, then you probably need to think of a
different format to return the results.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web