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


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

Re: Problem with compile fct. and Thread

From kris <kristophs.post@web.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Problem with compile fct. and Thread
Date 2012-01-26 08:32 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jfr332$nji$1@smc.vnet.net> (permalink)
References <jfm01p$js0$1@smc.vnet.net> <jforfd$62f$1@smc.vnet.net>

Show all headers | View raw


On 25 Jan., 13:10, "Oleksandr Rasputinov"
<oleksandr_rasputi...@hmamail.com> wrote:
> On Tue, 24 Jan 2012 10:10:01 -0000, kristoph <kristophs.p...@web.de> wrote:
> > Hi
>
> > I get an error when trying to compute the following "toy" code:
>
> > (*the compile function*)
> > test=Compile[{{bw,_Real},{regData,_Real,1},{leg,_Integer}},
> > Module[{h,tab2,xMat},
> > h=1;
> > tab2=Table[(regData[[i]]-regData[[j]]),{i,1,leg},{j,1,leg}];
> > xMat=Thread[{1.,tab2[[h]]}]
> > ]
> > ,CompilationOptions->{"InlineExternalDefinitions"->True}];
>
> > (*generating random sample data*)
> > leg=100;
> > data=RandomReal[{0,1},leg];
> > bw=.1;
>
> > (*using the compile function with the above generated data, produces
> > the error*)
> > test[bw, data, leg]
>
> > The function "test" works fine if I construct the matrix "xMat" using
> > the function Table. However, I prefer Thread because it is twice as
> > fast.
>
> > I just do not see where my mistake is. I do appreciate any
> > suggestions. Thanks in advance.
> > Cheers,
> > Kris
>
> The problem again is with the types of various expressions, but this time
> it is caused by a call out of the compiled code in evaluating Thread[...].
> In such cases one must explicitly specify the return type one expects for
> expressions matching a given pattern using the third argument of Compile,
> which in this case should be {{_Thread, _Real, 2}}:
>
> test = Compile[
>   {{bw, _Real}, {regData, _Real, 1}, {leg, _Integer}},
>   Module[{h = 1, tab2 = {{0., 0.}}, xMat = {{0., 0.}}},
>    tab2 = Table[
>     regData[[i]] - regData[[j]],
>     {i, 1, leg}, {j, 1, leg}
>    ];
>    xMat = Thread[{1., tab2[[h]]}]
>   ], {{_Thread, _Real, 2}},
>   CompilationOptions -> {"InlineExternalDefinitions" -> True}
> ];
>
> A better way to write it is to avoid the call out of compiled code
> altogether. There also isn't any reason to use xMat here and it introduces
> a needless copy operation on the final result, so one may as well remove
> this too, ending up with the following:
>
> test2 = Compile[
>   {{bw, _Real}, {regData, _Real, 1}, {leg, _Integer}},
>   Module[{h = 1, tab2 = {{0., 0.}}},
>    tab2 = Table[
>     regData[[i]] - regData[[j]],
>     {i, 1, leg}, {j, 1, leg}
>    ];
>    Transpose[{
>     ConstantArray[1., Length@tab2[[h]]],
>     tab2[[h]]
>    }]
>   ], CompilationOptions -> {"InlineExternalDefinitions" -> True}
> ];
>
> As is probably clear by now, the use of compiled code is not without its
> difficulties. (Also, incidentally, "InlineExternalDefinitions" -> True
> doesn't serve any purpose for this example, but I left it in anyway.)

Thanks a lot. This is very helpful!

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


Thread

Problem with compile fct. and Thread kristoph <kristophs.post@web.de> - 2012-01-24 10:10 +0000
  Re: Problem with compile fct. and Thread "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2012-01-25 12:10 +0000
    Re: Problem with compile fct. and Thread kris <kristophs.post@web.de> - 2012-01-26 08:32 +0000

csiph-web