1
(*Here is an comparison*)
data = {};
For[n = 1, n < 10, n++,
t1 = Timing[ParallelSum[1/i^2, {i, 1, n*100000}]];
t2 = Timing[HarmonicNumber[n*100000, 2]];
AppendTo[data, {t1[[1]], t2[[1]]}]]
data

The result is as following which shows the built-in functions always win here.

{{0.432000, 0.192000}, {0.896000, 0.500000}, {1.476000, 
0.860000}, {2.228000, 1.236000}, {3.048000, 1.664000}, {3.956000, 
2.128000}, {4.920000, 2.540000}, {5.932000, 2.936000}, {7.140000, 
3.492000}}

The question is: If we rewrite a built-in function with parallelized functions, e.g. rewrite

HarmonicNumber[n*100000, 2]

as

ParallelSum[1/i^2, {i, 1, n*100000}]

Could we win (in more general case)? If not, then why? And what is the advantage of parallelization? Any help will be appreciated!

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Eden Harder
  • 1,145
  • 6
  • 22
  • 2
    If you read documentation of core language they have clearly specified that if some API exists to provide some functionality, they are so much so optimized that anything you write will be slower than it.Secondly, is it not possible that if required it can switch to parallel processing own its own. Parallel processing is not helpful in all scenarios, hence used only if problem is breakable."Built-in functions will usually run faster than any compiled Mathematica programs you can create."..reference is http://reference.wolfram.com/mathematica/tutorial/CompilingMathematicaExpressions.html – Pankaj Sejwal Aug 11 '13 at 12:41
  • Somewhat related: (1418), (17360) – Mr.Wizard Aug 11 '13 at 13:21
  • @Blackbird You should consider posting a excerpt from that tutorial as an answer. – Mr.Wizard Aug 11 '13 at 13:24
  • @Mr.Wizard:I agree but it will be repetition of information.I doubt I can write it better than them.Link shall help. – Pankaj Sejwal Aug 11 '13 at 13:36
  • 2
    @Blackbird "We" try to discourage answering in comments; there's nothing wrong with giving a documentation excerpt as an answer if it the most appropriate one available. Of course you can embellish it with additional resources if you find them. – Mr.Wizard Aug 11 '13 at 13:38
  • the builtin may well be using a more efficient algorithm than directly crunching the sum... – george2079 Aug 11 '13 at 13:53
  • @Blackbird Thanks! The built-in functions will win even we test them on super computer (500+ CPU)? If 'yes', then why we need parallelization or gridmathematica? We only need more build-in functions! – Eden Harder Aug 11 '13 at 14:32
  • @Mr.Wizard Thanks! Your answer is very helpful. Maybe some built-in functions already parallelized. – Eden Harder Aug 11 '13 at 14:34
  • @Eden You should not assume that the built-in functions do not already make use of parallelism, as a number of them do. In cases that they do not then yes, assuming sufficiently low overhead and a sufficiently large number of processors your own function may be faster. Also, not every built-in is well optimized; there are cases that even top-level code may be faster, as I believe has been the case with date and time functions at least in past versions. – Mr.Wizard Aug 11 '13 at 14:38
  • @Mr.Wizard Can you give some examples to show that our functions can win as an answer to end this question? Maybe you can help me to understand if some built-in functions already parallelized, why there is no 'lanuching kernels' when we use them? btw. – Eden Harder Aug 12 '13 at 00:59

1 Answers1

1

You are not comparing similar operations. If you do compare same functions, then ParallelSum is clearly faster:

data = {};
For[n = 1, n < 5, n++, 
 t1 = AbsoluteTiming[1/ParallelSum[1/i^2, {i, 1, n*100000}]];
 t2 = AbsoluteTiming[1/Sum[1/i^2, {i, 1, n*100000}]];
 AppendTo[data, {t1[[1]], t2[[1]]}]]
data

gives (for 4 kernels)

    {{0.554032, 2.482142}, {1.297074, 9.613550}, {2.040117, 
  21.586235}, {2.843163, 37.915397}}
Rolf Mertig
  • 17,172
  • 1
  • 45
  • 76
  • That's true. But the point is that if we have two ways for a computation on a multicore computer, to use parallelization or to use the built-in function, which one should we choose? Will the built-in functions always be better? – Eden Harder Aug 11 '13 at 12:06
  • i think your question would be more of a general question on when to use parallel computing. When performing the same operation (e.g., sum vs parallel sum), the parallel version should always win in performance unless the operation involved in each parallel step is too small - this is due to the fact that each parallel call has a small overhead (which is also affected by how your OS distributes the cores and processing power). – Jonie Aug 11 '13 at 12:53
  • @EdenHarder:Parallelized version of the built-in function will be better.But if you break it in steps than it will not be. – Pankaj Sejwal Aug 11 '13 at 12:55
  • Yeah, maybe I confused you all. I have updated the question. Thanks all. – Eden Harder Aug 11 '13 at 13:20