Skip to main content

matrix - Fast element wise multiplication of many, many submatrices of a 2D array


There is an edited section at the bottom to clarify some questions which have come up in the comments.



I have some code which generates (according to two coordinates) two $m\times n$ arrays and then performs element wise multiplication of the two arrays to then get the average.


part[da_, db_, array_] := Mean@Flatten@(array[[span[db], span[da]]]* array[[span[-db], span[-da]]]);
span[x_] := Switch[Sign[x], -1, 1 ;; x - 1, 0, 1 ;; -1, 1, x + 1 ;; -1];

SeedRandom[1]
array = Developer`ToPackedArray[RandomChoice[{-1, 1}, {1000, 1000}]];
p = {2,3};
q = {-6,8};

diff=p-q;

d1=diff[[1]];
d2=diff[[2]];

test = part[d1,d2,array]// AbsoluteTiming

This gives me


{0.0647411, -(919/493520)}

I need to run this code for a lot of different points - in the order of 500 million. Since all I am doing in the function "part" is generating


a1=array[span[db], span[da]] ]


and


a2=array[span[-db], span[-da]] ]

and element wise multiplying them


a1*a2

I really feel that I could use the power of my graphics card (GTX 1070) and 4 CPU cores (i7 7700k) to really get a massive speed boost.


As a current bench mark (on my slower 2 core laptop), I get the following for 1000 different points


pointsList = Partition[RandomInteger[1000, {2000, 2}], 2];


compute = ParallelTable[
diff = pointsList [[i, 2]] - pointsList [[i, 1]];
N@part[diff[[1]], diff[[2]], array],
{i, Length[pointsList]}]; // AbsoluteTiming

This gives me


{18.8524, Null}

which would roughly make it ~ 110 days for all 500 million points. I honestly believe this could be done in a matter of hours using a CUDA implementation. Since I have never used CUDA in mathematica and will be teaching myself - I could in principal take some time and figure this out myself. However, due to the fact that I need the results of these computations for a paper I am writing, It would really be great to use a working code now, and figure it out later.



EDIT


To give better clarity to my question here is the full code with explanation of what is happening.


I have 14 sets of arrays. Each is 2048 x 2048, with entries which can be real values. For the sake of explanation, this is what I call


SeedRandom[1]
array = Developer`ToPackedArray[RandomChoice[{-1, 1}, {2048,2048}]];

Next I feed a list of points. This list also does not change. I generate my list according to the following:


solns[r_] := DeleteCases[{ToRules[Reduce[(x)^2 + (y)^2 == r, {x, y}, Integers]]}[[All, All, 2]], n_?(#[[2]] < 0 &)] /. {n_ /; n < 0, 0} ->Sequence[];
makedata[{x_, y_}] := ({x^2 + y^2, x, y})


makeRdata[m_] := Module[{allR, allSolns},
allR = DeleteCases[Table[If[SquaresR[2, i] != 0, i, 0], {i, m}], 0];
allSolns = Flatten[Table[DeleteCases[solns[allR[[i]]], n_?(#[[1]] < 0 &&#[[2]] == 0 &)], {i, Length[allR]}], 1];
Table[makedata[allSolns[[i]]], {i, Length[allSolns]}]
];

max = 2048^2+2048^2;
pntsList = makeRdata[max];

Once I have now created all the points I will be using (if you plan on test running this code, I suggest using max = 100^2+100^2), I feed them into part, to create a nested list called t1.



t1 = ParallelTable[{N@Sqrt@dat[[j, 1]], N@part[dat[[j, 2]], dat[[j, 3]],array]}, {j, Length[dat]}] // AbsoluteTiming

Once the nested list t1 has been generated, I will write it to a file, and then re run the same code, but this time for a new array - still exactly the same size. For this new array, pntsList does not change, and so it will only have to be generated once.


In summary,I create all the points to be fed into part before hand. Once I start feeding them into part the array does not change. It is all the same array with the same dimensions. Only after all the points have been proccessed and the result written to a file. Do I rerun the code for a new array - still the same size- and I feed into it the exact same points. I repeat this for 14 different arrays.



Answer



I would use ListCorrelate and rely on Mathematica's internal parallel implementation. Here is your code:


part[da_, db_, array_] := Mean @ Flatten @ (array[[span[db],span[da]]]*array[[span[-db],span[-da]]]);
span[x_]:=Switch[Sign[x],-1,1;;x-1,0,1;;-1,1,x+1;;-1];

Let's use a smaller array example:



array = Developer`ToPackedArray @ RandomChoice[{-1, 1}, {100, 100}];

Here is a table of all possible indices:


r1 = Table[part[da, db, array], {da, -99, 99}, {db, -99, 99}]; //AbsoluteTiming


{0.768523, Null}



And, here is an equivalent ListCorrelate implementation:


count[dim_] := Outer[

Times,
Join[Range[dim], Reverse[Range[dim-1]]],
Join[Range[dim], Reverse[Range[dim-1]]]
]

r2 = ListCorrelate[array, array, {-1, 1}, 0] / count[100] //Transpose; //AbsoluteTiming


{0.025539, Null}




They are equal:


r1 === r2


True



Now, let's consider a larger array:


array = Developer`ToPackedArray @ RandomChoice[{-1, 1}, {2048, 2048}];

I won't bother timing the OP code, but here is the timing for ListCorrelate:



ListCorrelate[array, array, {-1, 1}, 0] / count[2048] //Transpose; //AbsoluteTiming


{11.6008, Null}



This is slow because rational numbers can't be packed. If an answer using reals is acceptable, you could use:


ListCorrelate[array, array, {-1, 1}, 0] / N[count[2048]] //Transpose; //AbsoluteTiming


{1.15444, Null}




So, you should be able to do all 14 array objects in less than 170 seconds using rationals, or 15 seconds using reals.


Comments

Popular posts from this blog

front end - keyboard shortcut to invoke Insert new matrix

I frequently need to type in some matrices, and the menu command Insert > Table/Matrix > New... allows matrices with lines drawn between columns and rows, which is very helpful. I would like to make a keyboard shortcut for it, but cannot find the relevant frontend token command (4209405) for it. Since the FullForm[] and InputForm[] of matrices with lines drawn between rows and columns is the same as those without lines, it's hard to do this via 3rd party system-wide text expanders (e.g. autohotkey or atext on mac). How does one assign a keyboard shortcut for the menu item Insert > Table/Matrix > New... , preferably using only mathematica? Thanks! Answer In the MenuSetup.tr (for linux located in the $InstallationDirectory/SystemFiles/FrontEnd/TextResources/X/ directory), I changed the line MenuItem["&New...", "CreateGridBoxDialog"] to read MenuItem["&New...", "CreateGridBoxDialog", MenuKey["m", Modifiers-...

How to thread a list

I have data in format data = {{a1, a2}, {b1, b2}, {c1, c2}, {d1, d2}} Tableform: I want to thread it to : tdata = {{{a1, b1}, {a2, b2}}, {{a1, c1}, {a2, c2}}, {{a1, d1}, {a2, d2}}} Tableform: And I would like to do better then pseudofunction[n_] := Transpose[{data2[[1]], data2[[n]]}]; SetAttributes[pseudofunction, Listable]; Range[2, 4] // pseudofunction Here is my benchmark data, where data3 is normal sample of real data. data3 = Drop[ExcelWorkBook[[Column1 ;; Column4]], None, 1]; data2 = {a #, b #, c #, d #} & /@ Range[1, 10^5]; data = RandomReal[{0, 1}, {10^6, 4}]; Here is my benchmark code kptnw[list_] := Transpose[{Table[First@#, {Length@# - 1}], Rest@#}, {3, 1, 2}] &@list kptnw2[list_] := Transpose[{ConstantArray[First@#, Length@# - 1], Rest@#}, {3, 1, 2}] &@list OleksandrR[list_] := Flatten[Outer[List, List@First[list], Rest[list], 1], {{2}, {1, 4}}] paradox2[list_] := Partition[Riffle[list[[1]], #], 2] & /@ Drop[list, 1] RM[list_] := FoldList[Transpose[{First@li...

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...