1
$\begingroup$

Is there a simple way to parallelize sum of list elements ? when I use:

n = 500;
T1[z_] = Table[RandomReal[]/(RandomReal[] + RandomReal[] z), {i, n}];
Timing@Parallelize@Total@T1[1.]

error

Parallelize::nopar1: "T1[1.] cannot be parallelized; proceeding with sequential evaluation."

It is only an examle. I my calculation I have a huge expression with elements p/(q+r z) which are a result of using Apart[]


Thanks for your help , but the problem is something else f[z_]- very big formula (rational function) which should be integrated

Af[z_]:=Apart@f[z]

after that the Af[z] = p1/(q1+r1 z)+p2/(q2+ r2 z) +.....

thus integral is simply defined:

F[z_]:=Af[z]/. p_/(q_ + r_ z) -> p/r Log[q + r z]

The problem is: F[z] has a lot of simple elements, and I should compute F[z] for many complex points as quick as possible

regards, Olaf

$\endgroup$
3
  • $\begingroup$ Why do you feel the need to use Parallelize here? Evaluating each element of the lists in turn inside Table is very slow, you should be able to do everything in one step. If you have the lists p, q, and r then simply define T1[z_]:=p/(q+r z). $\endgroup$ Oct 13, 2016 at 10:38
  • $\begingroup$ Also see the suggestions here: mathematica.stackexchange.com/q/128525/6588 $\endgroup$ Oct 13, 2016 at 10:38
  • $\begingroup$ Why not try directly parallelizing the Table evaluation with ParallelTable? I.e., T2[z_] := ParallelTable[RandomReal[]/(RandomReal[] + RandomReal[] z), {i, n}]; $\endgroup$
    – Marchi
    Dec 12, 2016 at 16:51

2 Answers 2

1
$\begingroup$

tl; dr Do not try to parallelize this. Use vector arithmetic (vectorization) instead.

Total cannot be parallelized because it already makes use of all your CPU cores in a way that is much more effective than Mathematica's parallel tools. The same applies to vector arithmetic. If a and b are two large arrays of equal size, then operations such as a^2, a*b, a.b, Total[a], etc. will run very efficiently and will make use of all your CPU cores. In fact these will often be faster than a naïve for loop one might write in C.

$\endgroup$
0
$\begingroup$
n = 10^7;
p = RandomReal[1, n];
q = RandomReal[1, n];
r = RandomReal[1, n];

T1[z_] := p/(q + z r)

AbsoluteTiming[
 Total[T1[1.]]
]

{0.143513, 6.92922*10^6}

$\endgroup$
2
  • $\begingroup$ But the problem is: $\endgroup$
    – Olaf
    Oct 13, 2016 at 12:51
  • $\begingroup$ Thanks for your help , but the problem is something else f[z_]- very big formula (rational function) which should be integrated Af[z_]:=Apart@f[z] after that the Af[z] = p1/(q1+r1 z)+p2/(q2+ r2 z) +..... thus integral is simply defined: F[z_]:=Af[z]/. p_/(q_ + r_ z) -> p/r Log[q + r z] The problem is: F[z] has a lot of simple elements, and I should compute F[z] for many complex points as quick as possible regards, Olaf $\endgroup$
    – Olaf
    Oct 13, 2016 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.