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


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

Re: ParallelDo and C-compiled routines

From Oliver Ruebenkoenig <ruebenko@wolfram.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: ParallelDo and C-compiled routines
Date 2011-09-29 06:04 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <j611q4$ltv$1@smc.vnet.net> (permalink)
References <201109280646.CAA07799@smc.vnet.net>

Show all headers | View raw


On Wed, 28 Sep 2011, DmitryG wrote:

> Hi All,
>
> I am going to run several instances of a long calculation on different
> cores of my computer and then average the results. The program looks
> like this:
>
> SetSharedVariable[Res];
> ParallelDo[
> Res[[iKer]] = LongRoutine;
> , {iKer, 1, NKer}]
>
> LongRoutine is compiled. When compiled in C, it is two times faster
> than when compiled in Mathematica. In the case of a Do cycle, this
> speed difference can be seen, However, in the case of ParallelDo I
> have the speed of the Mathematica-compiled routine independently of
> the CompilationTarget in LongRoutine, even if I set NKer=1.
>
> What does it mean? Are routines compiled in C unable of parallel
> computing? Or there is a magic option to make them work? I tried
> Parallelization->True but there is no result, and it seems this option
> is for applying the routine to lists.
>
> Here is an example:
> ************************************************************
> NKer = 1;
>
> (* Subroutine compiled in Mathematica *)
> m = Compile[ {{x, _Real}, {n, _Integer}},
>   	Module[ {sum, inc}, sum = 1.0; inc = 1.0;
>    Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum]];
>
> (* Subroutine compiled in C *)
> c = Compile[ {{x, _Real}, {n, _Integer}},
>   	Module[ {sum, inc}, sum = 1.0; inc = 1.0;
>    Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum],
>   CompilationTarget -> "C"];
>
> (* There is a difference between Mathematica and C *)
> Do[
> Print[AbsoluteTiming[m[1.5, 10000000]][[1]]];
> Print[AbsoluteTiming[c[1.5, 10000000]][[1]]];
> , {iKer, 1, NKer}]
> Print[];
>
> (* With ParallelDo there is no difference *)
> ParallelDo[
> Print[AbsoluteTiming[m[1.5, 10000000]][[1]]];
> Print[AbsoluteTiming[c[1.5, 10000000]][[1]]];
> , {iKer, 1, NKer}]
> **************************************************************
>
> Any help?
>
> Best,
>
> Dmitry
>
>

Dmitry,

you have to make the definitions of the code available on the sub kernels. 
e.g.:

ParallelEvaluate[
  NKer = 1;

  (*Subroutine compiled in Mathematica*)

  m = Compile[{{x, _Real}, {n, _Integer}},
    Module[{sum, inc}, sum = 1.0; inc = 1.0;
     Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum]];

  (*Subroutine compiled in C*)

  c = Compile[{{x, _Real}, {n, _Integer}},
    Module[{sum, inc}, sum = 1.0; inc = 1.0;
     Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum],
    CompilationTarget -> "C"];
  ]
m =.;
c =.;

NKer = 1;
(*With ParallelDo there is no difference*)
ParallelDo[
  Print[AbsoluteTiming[m[1.5, 10000000]][[1]]];
  Print[AbsoluteTiming[c[1.5, 10000000]][[1]]];, {iKer, 1, NKer}]


Now, one might want to try to compile the code and then 
DistributeDefinitions. For very subtle reasons this is not a good way to 
proceed in this case (actually the same issue come up with
InterpolationFunctions and CompiledFunctions)

img = ExampleData[{"TestImage", "Elaine"}];
data = ImageData[img];
{nx, ny} = ImageDimensions[img];
ip = ListInterpolation[data];
DistributeDefinitions[data, nx, ny, ip];

AbsoluteTiming[
  Image@Table[ip[y, x], {y, 1, ny, 2}, {x, 1, nx, 2}];]

{0.980372, Null}

AbsoluteTiming[
  Image@ParallelTable[ip[y, x], {y, 1, ny, 2}, {x, 1, nx, 2}];]

{33.957764, Null}

The best way around this is to compute the if on each sub-kernel (which is 
not too dramatic, since it is done in parallel)

DistributeDefinitions[data];
ParallelEvaluate[ip = ListInterpolation[data]];

ip =.

AbsoluteTiming[
  Image@ParallelTable[ip[y, x], {y, 1, ny, 2}, {x, 1, nx, 2}];]


Another way could be to DumpSave the definition and reload on every sub 
kernel

ip = ListInterpolation[data];
DumpSave["test.mx", ip]

Quit

img = ExampleData[{"TestImage", "Elaine"}];
{nx, ny} = ImageDimensions[img];
DistributeDefinitions[nx, ny];

ParallelEvaluate[Get["test.mx"]]

AbsoluteTiming[
  Image@ParallelTable[ip[y, x], {y, 1, ny, 2}, {x, 1, nx, 2}];]


I do not know if that works with compiled functions though. Perhaps 
someone wants to try.


Hope this helps,
Oliver

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


Thread

Re: ParallelDo and C-compiled routines Oliver Ruebenkoenig <ruebenko@wolfram.com> - 2011-09-29 06:04 +0000

csiph-web