# Parallelization of function evaluations

## Problem

I have an expression that calculates the coefficients of a 2x2 matrix which depend on other three independent expressions, i.e,

$K_{l l^\prime} = ( k_{l l^\prime}^{(1)} + k_{l l^\prime}^{(2)} + k_{l l^\prime}^{(3)} )$

Each $k$ depends on parameters $x_0$ and $\beta$. In Mathematica I have defined the following functions:

k1[x0_, beta_]:= NIntegrate[(*huge expression*)]
k2[x0_, beta_]:= NIntegrate[(*huge expression*)]
k3[x0_, beta_]:= x*NIntegrate[(*huge expression*)]


Here $x$ is a symbolic value, so essentially $k_1$ and $k_2$ are purely numeric and $k_3$ is symbolic.

To evaluate the result I have defined

K[x0_beta_]:= k1[x0,beta]+k2[x0,beta]+k3[x0,beta]


I would like to paralelize the evaluation of $K$, that is, calculate each $k$ in a different kernel (processor) to speed up the calculation, then join and sum the terms.

My research in the Wolfram documentation pages led me to believe that I should use the

ParallelSubmit[]


function but I am unsure on how to exactly initialize and distribute the task to the kernels, then join the computations.

If it helps, I am using Mathematica 11.0.

## Contextualizing

To give a bit more of context, I'm trying to reproduce the graphs (more specifically figure 5) in this paper https://arxiv.org/pdf/1304.0582.pdf

For that I must compute equations (77)-(79) with the help of equations (42) and (44)-(47). The functions $f(y)$ and $g(y)$ are given in equations (14) and (15).

In my Mathematica program, $g(y)$ is a numeric solution to (15) and is represented by a interpolating function.

• That's hard to say given this abstract setting. How large is the matrix? What exactly are the functions k1, k2, k3? Do they involve symbolic computation? If not, are they compilable? Submitting the tasks to different kernels in the way you discribed it need not be a good idea at all since summing up results from different kernels involves communication that may spoil all the speedup. There are at least two other parallelization techniques accessible from within Mathematica: vectorization and the one that Compile provides. – Henrik Schumacher Apr 7 '18 at 19:20
• I have the feeling that the latter two might be more appropriate. – Henrik Schumacher Apr 7 '18 at 19:21
• Total@Parallelize@{k1[x0,beta], k2[x0,beta], k3[x0,beta]}, but as Henrik points out, whether it will be efficient depends on what k1 etc. do. There's also ParallelSum[k[x0, beta], {k, {k0, k1, k2}}]. – Michael E2 Apr 7 '18 at 19:40
• @HenrikSchumacher I've added some clarifications about the types of the k functions. Basically k1 and k2 are compilable and the matrix is 2x2. – Lsheep Apr 7 '18 at 21:28
• @Lsheep Then I would try to figure out a suitable quadrature rule for the integral, in particular if the integrants have certain properties in common? Is it one- or multidimensional integration? Do the integrants have any singularities or discontinuities? If not, Gauss quadrature rules should yield an enormous speedup. Otherwise, one has to fiddle a bit more. It would be really helpful if you would show the integrants and provide some more context. – Henrik Schumacher Apr 7 '18 at 21:43

Well, you could try to index the k functions in such a way that you can use ParallelTable:

k[1, x0_, beta_] := ...
k[2, x0_, beta_] := ...
k[3, x0_, beta_] := ...


Now you can parallelize the sum as follows:

Total @ ParallelTable[k[i, x0, beta], {i, 1, 3}]


(BTW, don't use K or any other capital letter as a function because they have build-in definitions)