Consider this code:
LaunchKernels[];
On["Packing"]
a = RandomReal[{0., 1.}, {64, 30000}];
ParallelMap[Fourier, a]; // AbsoluteTiming
Developer
FromPackedArray::unpack: Unpacking array in call to Language
ExtendedFullDefinition. Developer`FromPackedArray::unpack: Unpacking array in call to MemberQ. >>
(*{1.853567, Null}*)
Map[Fourier, a]; // AbsoluteTiming
Developer`FromPackedArray::punpackl1: Unpacking array with dimensions {64,30000} to level 1.
(*{0.289122, Null}*)
How to avoid the unpacking and get some speedup by parallelization?
Update
By avoiding the MemberQ
unpack (fix
function copied from here), we can get about 2X speedup, but still slower than the non-parallel version :
memberQ[list_, form_] := Or @@ (MatchQ[#, form] & /@ list)
ClearAll[fix]
SetAttributes[fix, HoldAll]
fix[expr_] := Block[{MemberQ = memberQ}, expr]
fix@ParallelMap[Fourier, a]; // AbsoluteTiming
Developer
FromPackedArray::unpack: Unpacking array in call to Language
ExtendedFullDefinition. >>
(*{0.564126, Null}*)
Update 2
Using the ParallelTable
can eliminate unpacking and can actually get speedup
first run
fix[
ParallelTable[
Fourier[a[[n]]], {n, 1, Length[a]}]]; // AbsoluteTiming
(*{0.215288, Null}*)
second run
fix[
ParallelTable[
Fourier[a[[n]]], {n, 1, Length[a]}]]; // AbsoluteTiming
(*{0.092006, Null}*)
Questions:
- What is this
Language
ExtendedFullDefinition` and why I always get this warning? How to avoid unpacking from it? I'm using version 9. - Can you give more evidence on "Fourier is so fast that you loose any time you gain in the overhead of parallelism"?
- If the slow is because of parallel over head, why
ParallelTable
is 5X faster thanParallelMap
? Thanks a lot!