Guest User

Untitled

a guest
Mar 20th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. testwm = Compile[ {{x, _Real}, {n, _Integer}},
  2. Module[ {sum, inc}, sum = 1.0; inc = 1.0;
  3. Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum],
  4. CompilationTarget -> "WVM"];
  5.  
  6. Map[(testwm[1.5, 2000000] + $KernelID) &,
  7. Range[2 $ProcessorCount]] // AbsoluteTiming
  8. (*
  9. ==> {1.4290817, {4.48169, 4.48169, 4.48169, 4.48169, 4.48169,
  10. 4.48169, 4.48169, 4.48169, 4.48169, 4.48169, 4.48169, 4.48169}}
  11. *)
  12. ParallelMap[(testwm[1.5, 2000000] + $KernelID) &,
  13. Range[2 $ProcessorCount]] // AbsoluteTiming
  14. (*
  15. ==> {1.4210813, {10.4817, 10.4817, 9.48169, 9.48169, 8.48169,
  16. 8.48169, 7.48169, 7.48169, 6.48169, 6.48169, 5.48169, 5.48169}}
  17. *)
  18. ParallelEvaluate[testwm = Compile[ {{x, _Real}, {n, _Integer}},
  19. Module[ {sum, inc}, sum = 1.0; inc = 1.0;
  20. Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum],
  21. CompilationTarget -> "WVM"];];
  22. ParallelMap[(testwm[1.5, 2000000] + $KernelID) &,
  23. Range[2 $ProcessorCount], DistributedContexts -> None] // AbsoluteTiming
  24. (*
  25. ==> {0.2760158, {10.4817, 10.4817, 9.48169, 9.48169, 8.48169,
  26. 8.48169, 7.48169, 7.48169, 6.48169, 6.48169, 5.48169, 5.48169}}
  27. *)
  28. ParallelEvaluate[ClearAll[testwm]];
  29.  
  30. testnc[x_, n_] :=
  31. Module[ {sum, inc}, sum = 1.0; inc = 1.0;
  32. Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum];
  33. Map[(testnc[1.5, 10000] + $KernelID) &,
  34. Range[2 $ProcessorCount]] // AbsoluteTiming
  35. (*
  36. ==> {0.5660324, {4.48169, 4.48169, 4.48169, 4.48169, 4.48169,
  37. 4.48169, 4.48169, 4.48169, 4.48169, 4.48169, 4.48169, 4.48169}}
  38. *)
  39. ParallelMap[(testnc[1.5, 10000] + $KernelID) &,
  40. Range[2 $ProcessorCount]] // AbsoluteTiming
  41. (*
  42. ==> {0.1210069, {10.4817, 10.4817, 9.48169, 9.48169, 8.48169,
  43. 8.48169, 7.48169, 7.48169, 6.48169, 6.48169, 5.48169, 5.48169}}
  44. *)
  45.  
  46. ClearAll[CompiledFunctionNames];
  47. CompiledFunctionNames[pattern_] :=
  48. Select[Names["Global`*"],
  49. MatchQ[Evaluate[Symbol@#], _CompiledFunction] &];
  50.  
  51. ClearAll[DistributeCompiledFunction]
  52. DistributeCompiledFunction[name_] :=
  53. With[{copy = Symbol[name]},
  54. ParallelEvaluate[
  55. If[ MatchQ[Evaluate[Symbol@name], _Symbol],(*Print["Setting ",
  56. name];*)Evaluate[Symbol[name]] = copy]]];
  57.  
  58. ClearAll[DistributeCompiledFunctions];
  59. DistributeCompiledFunctions[pattern_] :=
  60. DistributeCompiledFunction /@ CompiledFunctionNames[pattern];
  61.  
  62. testwm = Compile[{{x, _Real}, {n, _Integer}},
  63. Module[{sum, inc}, sum = 1.0; inc = 1.0;
  64. Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum],
  65. CompilationTarget -> "WVM"];
  66. testwm1 =
  67. Compile[{{x, _Real}, {n, _Integer}},
  68. Module[{sum, inc}, sum = 1.0; inc = 1.0;
  69. Do[inc = inc*x/i; sum = sum + inc, {i, n}]; sum],
  70. CompilationTarget -> "WVM"];
  71. a = 5; (*a non compiled symbol -- will not get destributed*)
  72. (*see which compiled functions are in the current context*)
  73. CompiledFunctionNames["Global`*"]
  74. DistributeCompiledFunctions["Global`*"];
  75. (*distributing twice does not override previous definition*)
  76. DistributeCompiledFunctions["Global`*"];
  77. (*check if it worked*)
  78. DeleteDuplicates@ParallelEvaluate@HoldForm[testwm // Evaluate]
  79. DeleteDuplicates@ParallelEvaluate@HoldForm[testwm1 // Evaluate]
  80. DeleteDuplicates@ParallelEvaluate@HoldForm[a // Evaluate]
  81. ParallelMap[(testwm[1.5, 2000000] + $KernelID) &,
  82. Range[2 $ProcessorCount],
  83. DistributedContexts -> None] // AbsoluteTiming
  84. ParallelEvaluate[ClearAll[testwm, testwm1]];
Add Comment
Please, Sign In to add comment