On PackedArray, looking for tips to use them.

I haven't used PackedArray before, but just started looking at using them here to read their discussion here.

I have many large one-dimensional and two-dimensional large-sized matrices from all reals, and there are no symbolic ones (this is a PDE solver with finite difference), and therefore I thought that I should use the use of PackedArray.

I have an initialization function where I select all the necessary data / grids. So I went and used ToPackedArraythem. This seems a little faster, but I need to do more performance tests to better compare the speed before and after, and also compare the use of RAM.

But while I was looking at this, I noticed that some operations in M โ€‹โ€‹automatically return lists in PackedArray already, and some not.

For example, this does not return a packed array

a = Table[RandomReal[], {5}, {5}];
Developer`PackedArrayQ[a]

But it does

a = RandomReal[1, {5, 5}];
Developer`PackedArrayQ[a]

and this one does

a = Table[0, {5}, {5}];
b = ListConvolve[ {{0, 1, 0}, {1, 4, 1}, {0, 1, 1}}, a, 1];
Developer`PackedArrayQ[b]

as well as matrix multiplication makes the return result in a packed array

a = Table[0, {5}, {5}];
b = a.a;
Developer`PackedArrayQ[b]

But elemental multiplication is not

b = a*a;
Developer`PackedArrayQ[b]

My question is: is there a list somewhere, which documents that M-commands return PackedArray, is it not? (provided that the data complies with requirements such as Real, not mixed, not symbolic, etc.)

, - , , , / , ToPackedArray? , ToPackedArray , .

,

update (1)

, , PackedArray CDF, . . , . .

+5
3

. :

  • :
 
    In[66]:= a = RandomReal[1, {5, 5}];

    In[67]:= Developer`PackedArrayQ /@ {a, a.a, a*a}

    Out[67]= {True, True, True}
  • , (8.0.4) .

  • a Table , :

 
    In[71]:= Developer`PackedArrayQ[Table[RandomReal[], {24}, {10}]]

    Out[71]= False

    In[72]:= Developer`PackedArrayQ[Table[RandomReal[], {24}, {11}]]

    Out[72]= True

    In[73]:= Developer`PackedArrayQ[Table[RandomReal[], {25}, {10}]]

    Out[73]= True
  • On["Packing"] , , :
 
    In[77]:= On["Packing"]

    In[78]:= a = RandomReal[1, 10];

    In[79]:= Developer`PackedArrayQ[a]

    Out[79]= True

    In[80]:= a[[1]] = 0 (* force unpacking due to type mismatch *)

       Developer`FromPackedArray::punpack1: Unpacking array with dimensions {10}. >>

    Out[80]= 0
  • , , ,
    In[81]:= a = RandomReal[1, 10];

    In[82]:= Position[a, Max[a]]

       Developer`FromPackedArray::unpack: Unpacking array in call to Position. >>

    Out[82]= {{4}}
  • ToPackedArray , :
    In[90]:= a = RandomReal[1, 10^7];

    In[91]:= Timing[Do[Identity[a], {10^5}];]

    Out[91]= {0.028089, Null}

    In[92]:= Timing[Do[Developer`ToPackedArray[a], {10^5}];]

    Out[92]= {0.043788, Null}

  • , Dynamic Manipulate:
    In[97]:= Developer`PackedArrayQ[{1}]

    Out[97]= False

    In[98]:= Dynamic[Developer`PackedArrayQ[{1}]]

    Out[98]= True
  • , , . .
+9

:

SystemOptions["CompileOptions"]

, . , , Developer`ToPackedArray . .

SetSystemOptions["CompileOptions" -> {"TableCompileLength" -> 20}]

, , , , , , , :

f = # + 1 &;
g[x_] := x + 1;
data = RandomReal[1, 10^6];

On["Packing"]
Timing[Developer`PackedArrayQ[f /@ data]]
{0.131565, True}


Timing[Developer`PackedArrayQ[g /@ data]]
Developer`FromPackedArray::punpack1: Unpacking array with dimensions {1000000}.
{1.95083, False}
+7

: - , ToPackedArray , . :

http://library.wolfram.com/infocenter/Articles/3141/

, / .

, , On [ "" ], , , .

, , . . , , .

+4
source

All Articles