6

I'm trying to solve a rather large linear programming problem in Mathematica, but for some reason the bottleneck is setting up the array of linear constraints.

My code for initializing the matrix looks like this:

AbsoluteTiming[S = SparseArray[{{i_, 1} -> iaa[[i]],
    {i_, j_} /; Divisible[a[[j]], aa[[i]]] -> 1.}, {2*n, n}]]

Here n is 4455, iaa is a list of reals, and a, aa are lists of integers. The output I get for this line is

{2652.014773,SparseArray[<111742>,{8910,4455}]}

In other words, it takes 45 minutes to initialize this matrix, even though it only has 111,742 nonzero entries. For comparison, actually solving the linear program only takes 17 seconds. What gives?

Edit: Also, can anyone explain why this takes up so much memory as it is running? Because in user time, this calculation takes less than ten minutes... most of the computation time is spent paging through memory.

Is Mathematica for some reason storing this matrix as a non-sparse matrix while it is building it? Because that would be really really dumb.

user1015507
  • 163
  • 5
  • 2
    But the /; is being evaluated (2 n^2) times – Dr. belisarius Oct 26 '11 at 22:55
  • @belisarius: You posted your comment whilst Mma had chewed up all of my computer's memory and brought my system to its knees! – Simon Oct 26 '11 at 23:09
  • I am not near a Mathematica implementation to check this, but what would be wrong with creating the SparseArray using `SparseArray[Outer[Boole[Divisible[#1,#2]]&,a,aa]]` and then changing the first column? That still calls `Divisible` the same number of times, but the pattern matcher isn't involved. – Verbeia Oct 26 '11 at 23:10
  • @Verbeia: That's the type of thing I'm playing with at the moment. Still kind of slow though. The `Outer` construction is almost twice as fast as the `Table` code I posted below. It's probably as fast as it gets though... – Simon Oct 26 '11 at 23:16
  • @Simon - that's worth knowing, thanks. Worth me posting it as an answer? – Verbeia Oct 26 '11 at 23:20
  • @Verbeia: Actually, I'm ready to edit my answer to include such a construction, it's about 2.5 times faster than the original `SparseArray` - but if you want to post, then I can hold off! – Simon Oct 26 '11 at 23:25
  • You may as well then - I have a meeting starting in a couple of minutes. – Verbeia Oct 26 '11 at 23:30
  • I'm ok with an O(n^2) running time, the problem is that it's taking up way too much memory. If I plug in n around 5000, the program runs for days and finally crashes saying it's out of memory... making the runtime feel more like O(1/(5000-n))... – user1015507 Oct 27 '11 at 00:58
  • @user: Yeah... the memory aspect is interesting. I'm not sure why it should behave like that... – Simon Oct 27 '11 at 01:10

2 Answers2

10

You can surely do a lot better. Here is a code based on the low-level sparse array API posted here, which I will reproduce to make the code self - contained:

ClearAll[spart, getIC, getJR, getSparseData, getDefaultElement, makeSparseArray];
HoldPattern[spart[SparseArray[s___], p_]] := {s}[[p]];
getIC[s_SparseArray] := spart[s, 4][[2, 1]];
getJR[s_SparseArray] := Flatten@spart[s, 4][[2, 2]];
getSparseData[s_SparseArray] := spart[s, 4][[3]];
getDefaultElement[s_SparseArray] := spart[s, 3];
makeSparseArray[dims : {_, _}, jc : {__Integer}, ir : {__Integer}, data_List, defElem_: 0] := 
    SparseArray @@ {Automatic, dims,  defElem, {1, {jc, List /@ ir}, data}};



Clear[formSparseDivisible];
formSparseDivisible[a_, aa_, iaa_, chunkSize_: 100] :=
  Module[{getDataChunkCode, i, start, ic, jr, sparseData, dims,  dataChunk, res},
    getDataChunkCode :=
      If[# === {}, {}, SparseArray[1 - Unitize@(Mod[#, aa] & /@ #)]] &@
        If[i*chunkSize >= Length[a],
           {},
           Take[a, {i*chunkSize + 1, Min[(i + 1)*chunkSize, Length[a]]}]];  
    i = 0;
    start = getDataChunkCode;
    i++;
    ic = getIC[start];
    jr = getJR[start];
    sparseData = getSparseData[start];
    dims = Dimensions[start];        
    While[True,
      dataChunk = getDataChunkCode;
      i++;
      If[dataChunk === {}, Break[]];
      ic = Join[ic, Rest@getIC[dataChunk] + Last@ic];
      jr = Join[jr, getJR[dataChunk]];
      sparseData = Join[sparseData, getSparseData[dataChunk]];
      dims[[1]] += First[Dimensions[dataChunk]];
    ];
    res = Transpose[makeSparseArray[dims, ic, jr, sparseData]];
    res[[All, 1]] = N@iaa;
    res]

Now, here are the timings:

In[249]:= 
n = 1500;
iaa = aa = Range[2 n];
a = Range[n];
AbsoluteTiming[res = formSparseDivisible[a, aa, iaa, 100];]

Out[252]= {0.2656250, Null}

In[253]:= AbsoluteTiming[
  res1 = SparseArray[{{i_, 1} :> 
  iaa[[i]], {i_, j_} /; Divisible[a[[j]], aa[[i]]] -> 1.}, {2*n, n}];]

Out[253]= {29.1562500, Null}

So, we've got 100 - fold speedup, for this size of the array. And of course, the results are the same:

In[254]:= Normal@res1 == Normal@res
Out[254]= True

The main idea of the solution is to vectorize the problem (Mod), and build the resulting sparse array incrementally, in chunks, using the low-level API above.

EDIT

The code assumes that the lists are of the right length - in particular, a should have a length n, while aa and iaa - 2n. So, to compare to other answers, the test code has to be slightly modified (for a only):

n = 500;
iaa = RandomReal[{0, 1}, 2 n];
a = Range[ n]; aa = RandomInteger[{1, 4 n}, 2 n];


In[300]:= 
AbsoluteTiming[U=SparseArray[ReplacePart[Outer[Boole[Divisible[#1,#2]]&,
a[[1;;n]],aa],1->iaa]]\[Transpose]]
AbsoluteTiming[res = formSparseDivisible[a,aa,iaa,100]]

Out[300]= {0.8281250,SparseArray[<2838>,{1000,500}]}
Out[301]= {0.0156250,SparseArray[<2838>,{1000,500}]}

In[302]:= Normal@U==Normal@res
Out[302]= True

EDIT 2

Your desired matrix size is done in about 3 seconds on my not very fast laptop (M8), and with a fairly decent memory usage as well:

In[323]:= 
n=5000;
iaa=RandomReal[{0,1},2 n];
a=Range[ n];aa=RandomInteger[{1,4 n},2 n];
AbsoluteTiming[res = formSparseDivisible[a,aa,iaa,200]]

Out[326]= {3.0781250,SparseArray[<36484>,{10000,5000}]}
Community
  • 1
  • 1
Leonid Shifrin
  • 22,449
  • 4
  • 68
  • 100
  • 1
    Leonid, you really need to try your hand at Project Euler, assuming you are not there already. – Mr.Wizard Oct 27 '11 at 01:13
  • @Mr.Wizard Thanks, this crossed my mind, but no time for it currently. – Leonid Shifrin Oct 27 '11 at 01:16
  • Leonid, do you have any idea why the naive use of the `SparseArray` construction is so memory inefficient (as the OP asked in the edit)? – Simon Oct 27 '11 at 01:27
  • 1
    @Simon my understanding is that the matrix is created at once, not gradually (like I do). I do not even exclude the (surely dumb) possibility that the matrix is first constructed normally and then converted to the `SparseArray`, because otherwise it is still not clear why such memory consumption. In any case, this is an improper behavior which seems to indicate some problems with (parts of) the implementation of `SparseArray`. – Leonid Shifrin Oct 27 '11 at 01:33
  • @user101 you'll think that again as you read more of Leonid's answers. He's the resident *Mathematica* professor. When I have time I am going to read all 200+ of his answers back to back, if my mind can handle it. – Mr.Wizard Oct 29 '11 at 00:02
3

Your construction calls Divisible 2*n^2 ~= 40 000 000 times, so it doesn't scale too well! Depending on the size of the integers, this will account for about one third to one half of the time. To check this, you just need to run

AbsoluteTiming[Table[Divisible[a[[j]], aa[[i]]], {i, 2*n}, {j, n}]]

Combine that with the pattern matching construction that needs to call the evaluator for every element of the sparse array and you can see why it might be a bit slow.


Edit: Here's some test code that compares the timings (on my machine) for various constructions:

In[1]:= n = 500;
        iaa = RandomReal[{0, 1}, 2 n];
        a = Range[2 n]; aa = RandomInteger[{1, 4 n}, 2 n]; 

In[4]:= AbsoluteTiming[S = SparseArray[{{i_, 1} :> iaa[[i]], 
                     {i_, j_} /; Divisible[a[[j]], aa[[i]]] -> 1.}, {2*n, n}]]
Out[4]= {3.423123, SparseArray[<2499>, {1000, 500}]}

In[5]:= AbsoluteTiming[T = SparseArray[ReplacePart[Table[
                     Boole[Divisible[a[[i]], aa[[j]]]], {i, 1, n}, {j, 1, 2 n}],
                     1 -> iaa]]\[Transpose]]
Out[5]= {1.524575, SparseArray[<2499>, {1000, 500}]}

In[6]:= AbsoluteTiming[U = SparseArray[ReplacePart[Outer[
                     Boole[Divisible[#1, #2]]&, a[[1 ;; n]], aa], 
                     1 -> iaa]]\[Transpose]]
Out[6]= {0.916801, SparseArray[<2499>, {1000, 500}]}

In[7]:= S == T == U
Out[7]= True

Unless you have some other information about the integers in a and aa, then the 2*n^2 tests using Divisible will effectively limit how fast the code can run.

Simon
  • 14,631
  • 4
  • 41
  • 101
  • Interesting! Hmm... but if the time scales quadratically, then it should only take 6 minutes to run the code with n = 5000, even with the first method... – user1015507 Oct 27 '11 at 00:49
  • 1
    Also the following construct is useful for determining how much `Divisible[]` costs `AbsoluteTiming[ S = SparseArray[{{i_, 1} -> iaa[[i]], {i_, j_} /; False -> 1.}, {2*n, n}]]` – Dr. belisarius Oct 27 '11 at 00:53
  • 2
    It may be worth pointing out that `1 - Unitize[Mod[#1, aa] & /@ a[[1 ;; n]]]` is a much faster alternative to `Outer[Boole@Divisible[#1, #2] &, a[[1 ;; n]], aa]`, and further, attempt to use `Outer` there also as `1 - Unitize@Outer[Mod, a[[1 ;; n]], aa]` (instead of `Map`) will only slow the code down, since `Mod[#,aa]&` utilizes the Listability of `Mod`, which is ignored (not used) by `Outer`. – Leonid Shifrin Oct 27 '11 at 02:04