However my function x is much more complicated and computer intensive. A single evaluation of x[] is roughly 8*10^5 bytes (800 kB). For summing a 1000 of these the memory usage is 800 MB (!!). (in my real application it is 10s of GB).
I now replaced it with a Do loop and do the ‘intermediate’ summing myself.
Is there a method, or option such that Sum already starts summing, more like a fold-operation? Such that it doesn’t store all the results before adding them all up. The current implementation of Sum looks more like:
ClearAll
x := RandomInteger[10, {100, 1000}]
x // ByteCount
MaxMemoryUsed[total = ParallelSum[x, {i, 1000}]]
In my machine the memory usage is 25 MB 2 core Cpu.
both show the same behaviour, is Procedural the default for ‘small’ k? What does “compute the sum procedurally” actually mean? Whatever the method, it will be done with some procedure…
Sum uses the procedural method (adding successive values to obtain the sum) by default for your example,
Unfortunately, at present, the procedural method in Sum is not optimized for adding arrays, and using a Do loop for doing the intermediate summations (as already done by you) is a nice way to work around the limitation.
Thank you for drawing our attention to this issue, and sorry for the inconvenience.
Sum allows very general inputs (numeric, symbolic, etc.) in its first argument. If it detects that the input is numeric,
then a direct procedural method (with no storage) is used. At present, it does not check if the first argument is an array
and hence does not apply procedural summation without storage of intermediate results, which leads to large memory
usage in examples such as the one considered by you. Optimization in this situation would mean checking for arrays,
which may be somewhat expensive particularly because Sum is not specially designed for such inputs.
Thanks for your explanation! Either I will use the procedural method, or keep my Do loop, as it works fine now (speed and memory wise). Parallelisation of such a Do loop is however trickier than a Sum, as inside my do loop I overwrite variables. So I have to cut the do loop in to pieces manually… But no big deal.
Unfortunately I could but it would be a lot slower. Each matrix that I’m summing is (kinda) the result of a DistanceMatrix with a large number of points. To do those individually is very slow.
What if you write an “externalSum” function in C. It would take your arrays and sum them up in C. I’ve done this a few times when Mathematica was “difficult”.
(ie The last time I did this was to write a printf function in C so I could pass variables and a format string and get the exact printout that I wanted. )
Would that work for you? (While your knowledge of Mathematica is impressive, If you have not done C externals in Mathematica before I can send you an example)