Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- testwm = 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 -> "WVM"];
- Map[(testwm[1.5, 2000000] + $KernelID) &,
- Range[2 $ProcessorCount]] // AbsoluteTiming
- (*
- ==> {1.4290817, {4.48169, 4.48169, 4.48169, 4.48169, 4.48169,
- 4.48169, 4.48169, 4.48169, 4.48169, 4.48169, 4.48169, 4.48169}}
- *)
- ParallelMap[(testwm[1.5, 2000000] + $KernelID) &,
- Range[2 $ProcessorCount]] // AbsoluteTiming
- (*
- ==> {1.4210813, {10.4817, 10.4817, 9.48169, 9.48169, 8.48169,
- 8.48169, 7.48169, 7.48169, 6.48169, 6.48169, 5.48169, 5.48169}}
- *)
- ParallelEvaluate[testwm = 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 -> "WVM"];];
- ParallelMap[(testwm[1.5, 2000000] + $KernelID) &,
- Range[2 $ProcessorCount], DistributedContexts -> None] // AbsoluteTiming
- (*
- ==> {0.2760158, {10.4817, 10.4817, 9.48169, 9.48169, 8.48169,
- 8.48169, 7.48169, 7.48169, 6.48169, 6.48169, 5.48169, 5.48169}}
- *)
- ParallelEvaluate[ClearAll[testwm]];
- testnc[x_, n_] :=
- Module[ {sum, inc}, sum = 1.0; inc = 1.0;
- Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum];
- Map[(testnc[1.5, 10000] + $KernelID) &,
- Range[2 $ProcessorCount]] // AbsoluteTiming
- (*
- ==> {0.5660324, {4.48169, 4.48169, 4.48169, 4.48169, 4.48169,
- 4.48169, 4.48169, 4.48169, 4.48169, 4.48169, 4.48169, 4.48169}}
- *)
- ParallelMap[(testnc[1.5, 10000] + $KernelID) &,
- Range[2 $ProcessorCount]] // AbsoluteTiming
- (*
- ==> {0.1210069, {10.4817, 10.4817, 9.48169, 9.48169, 8.48169,
- 8.48169, 7.48169, 7.48169, 6.48169, 6.48169, 5.48169, 5.48169}}
- *)
- ClearAll[CompiledFunctionNames];
- CompiledFunctionNames[pattern_] :=
- Select[Names["Global`*"],
- MatchQ[Evaluate[Symbol@#], _CompiledFunction] &];
- ClearAll[DistributeCompiledFunction]
- DistributeCompiledFunction[name_] :=
- With[{copy = Symbol[name]},
- ParallelEvaluate[
- If[ MatchQ[Evaluate[Symbol@name], _Symbol],(*Print["Setting ",
- name];*)Evaluate[Symbol[name]] = copy]]];
- ClearAll[DistributeCompiledFunctions];
- DistributeCompiledFunctions[pattern_] :=
- DistributeCompiledFunction /@ CompiledFunctionNames[pattern];
- testwm = 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 -> "WVM"];
- testwm1 =
- 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 -> "WVM"];
- a = 5; (*a non compiled symbol -- will not get destributed*)
- (*see which compiled functions are in the current context*)
- CompiledFunctionNames["Global`*"]
- DistributeCompiledFunctions["Global`*"];
- (*distributing twice does not override previous definition*)
- DistributeCompiledFunctions["Global`*"];
- (*check if it worked*)
- DeleteDuplicates@ParallelEvaluate@HoldForm[testwm // Evaluate]
- DeleteDuplicates@ParallelEvaluate@HoldForm[testwm1 // Evaluate]
- DeleteDuplicates@ParallelEvaluate@HoldForm[a // Evaluate]
- ParallelMap[(testwm[1.5, 2000000] + $KernelID) &,
- Range[2 $ProcessorCount],
- DistributedContexts -> None] // AbsoluteTiming
- ParallelEvaluate[ClearAll[testwm, testwm1]];
Add Comment
Please, Sign In to add comment