Numerical integration with Numba

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












I'm a bit new to working with Numba, but I got the gist of it. I wonder if there any more advanced tricks to make four nested for loops even faster that what I have now. In particular, I need to calculate the following integral:



$$
G_B(mathbf X, T) = Lambda int_Omega G(mathbf X, mathbf X', T) W(mathbf X', T) dmathbf X' \
G(mathbf X, mathbf X', T) = frac12pi S_0^2 expleft[-fracleft2[S_0(1+EB(mathbf X, T))]^2right]
$$



Where $B$ is a 2D array, and $S_0$ and $E$ are certain parameters. My code is the following:



import numpy as np
from numba import njit, double

def calc_gb_gauss_2d(b,s0,e,dx):
n,m=b.shape
norm = 1.0/(2*np.pi*s0**2)
gb = np.zeros((n,m))
for i in range(n):
for j in range(m):
sigma = 2.0*(s0*(1.0+e*b[i,j]))**2
for ii in range(n):
for jj in range(m):
gb[i,j]+=np.exp(-(((i-ii)*dx)**2+((j-jj)*dx)**2)/sigma)
gb[i,j]*=norm
return gb

calc_gb_gauss_2d_nb = njit(double[:, :](double[:, :],double,double,double))(calc_gb_gauss_2d)


For and input array of size 256×256 the calculation speed is:



In [4]: a=random.random((256,256))

In [5]: %timeit calc_gb_gauss_2d_nb(a,0.1,1.0,0.5)
The slowest run took 8.46 times longer than the fastest. This could mean that an intermediate result is being cached.
1 loop, best of 3: 1min 1s per loop


Comparison between pure Python and Numba calculation speed give me this picture:
Comparative performance plot



Is there any way to optimize my code for better performance?







share|improve this question

















  • 1




    Welcome to Code Review! I hope you get some great answers.
    – Phrancis
    May 9 at 15:53
















up vote
4
down vote

favorite












I'm a bit new to working with Numba, but I got the gist of it. I wonder if there any more advanced tricks to make four nested for loops even faster that what I have now. In particular, I need to calculate the following integral:



$$
G_B(mathbf X, T) = Lambda int_Omega G(mathbf X, mathbf X', T) W(mathbf X', T) dmathbf X' \
G(mathbf X, mathbf X', T) = frac12pi S_0^2 expleft[-fracleft2[S_0(1+EB(mathbf X, T))]^2right]
$$



Where $B$ is a 2D array, and $S_0$ and $E$ are certain parameters. My code is the following:



import numpy as np
from numba import njit, double

def calc_gb_gauss_2d(b,s0,e,dx):
n,m=b.shape
norm = 1.0/(2*np.pi*s0**2)
gb = np.zeros((n,m))
for i in range(n):
for j in range(m):
sigma = 2.0*(s0*(1.0+e*b[i,j]))**2
for ii in range(n):
for jj in range(m):
gb[i,j]+=np.exp(-(((i-ii)*dx)**2+((j-jj)*dx)**2)/sigma)
gb[i,j]*=norm
return gb

calc_gb_gauss_2d_nb = njit(double[:, :](double[:, :],double,double,double))(calc_gb_gauss_2d)


For and input array of size 256×256 the calculation speed is:



In [4]: a=random.random((256,256))

In [5]: %timeit calc_gb_gauss_2d_nb(a,0.1,1.0,0.5)
The slowest run took 8.46 times longer than the fastest. This could mean that an intermediate result is being cached.
1 loop, best of 3: 1min 1s per loop


Comparison between pure Python and Numba calculation speed give me this picture:
Comparative performance plot



Is there any way to optimize my code for better performance?







share|improve this question

















  • 1




    Welcome to Code Review! I hope you get some great answers.
    – Phrancis
    May 9 at 15:53












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I'm a bit new to working with Numba, but I got the gist of it. I wonder if there any more advanced tricks to make four nested for loops even faster that what I have now. In particular, I need to calculate the following integral:



$$
G_B(mathbf X, T) = Lambda int_Omega G(mathbf X, mathbf X', T) W(mathbf X', T) dmathbf X' \
G(mathbf X, mathbf X', T) = frac12pi S_0^2 expleft[-fracleft2[S_0(1+EB(mathbf X, T))]^2right]
$$



Where $B$ is a 2D array, and $S_0$ and $E$ are certain parameters. My code is the following:



import numpy as np
from numba import njit, double

def calc_gb_gauss_2d(b,s0,e,dx):
n,m=b.shape
norm = 1.0/(2*np.pi*s0**2)
gb = np.zeros((n,m))
for i in range(n):
for j in range(m):
sigma = 2.0*(s0*(1.0+e*b[i,j]))**2
for ii in range(n):
for jj in range(m):
gb[i,j]+=np.exp(-(((i-ii)*dx)**2+((j-jj)*dx)**2)/sigma)
gb[i,j]*=norm
return gb

calc_gb_gauss_2d_nb = njit(double[:, :](double[:, :],double,double,double))(calc_gb_gauss_2d)


For and input array of size 256×256 the calculation speed is:



In [4]: a=random.random((256,256))

In [5]: %timeit calc_gb_gauss_2d_nb(a,0.1,1.0,0.5)
The slowest run took 8.46 times longer than the fastest. This could mean that an intermediate result is being cached.
1 loop, best of 3: 1min 1s per loop


Comparison between pure Python and Numba calculation speed give me this picture:
Comparative performance plot



Is there any way to optimize my code for better performance?







share|improve this question













I'm a bit new to working with Numba, but I got the gist of it. I wonder if there any more advanced tricks to make four nested for loops even faster that what I have now. In particular, I need to calculate the following integral:



$$
G_B(mathbf X, T) = Lambda int_Omega G(mathbf X, mathbf X', T) W(mathbf X', T) dmathbf X' \
G(mathbf X, mathbf X', T) = frac12pi S_0^2 expleft[-fracleft2[S_0(1+EB(mathbf X, T))]^2right]
$$



Where $B$ is a 2D array, and $S_0$ and $E$ are certain parameters. My code is the following:



import numpy as np
from numba import njit, double

def calc_gb_gauss_2d(b,s0,e,dx):
n,m=b.shape
norm = 1.0/(2*np.pi*s0**2)
gb = np.zeros((n,m))
for i in range(n):
for j in range(m):
sigma = 2.0*(s0*(1.0+e*b[i,j]))**2
for ii in range(n):
for jj in range(m):
gb[i,j]+=np.exp(-(((i-ii)*dx)**2+((j-jj)*dx)**2)/sigma)
gb[i,j]*=norm
return gb

calc_gb_gauss_2d_nb = njit(double[:, :](double[:, :],double,double,double))(calc_gb_gauss_2d)


For and input array of size 256×256 the calculation speed is:



In [4]: a=random.random((256,256))

In [5]: %timeit calc_gb_gauss_2d_nb(a,0.1,1.0,0.5)
The slowest run took 8.46 times longer than the fastest. This could mean that an intermediate result is being cached.
1 loop, best of 3: 1min 1s per loop


Comparison between pure Python and Numba calculation speed give me this picture:
Comparative performance plot



Is there any way to optimize my code for better performance?









share|improve this question












share|improve this question




share|improve this question








edited May 9 at 17:47









200_success

123k14142399




123k14142399









asked May 9 at 15:34









Ohm

1262




1262







  • 1




    Welcome to Code Review! I hope you get some great answers.
    – Phrancis
    May 9 at 15:53












  • 1




    Welcome to Code Review! I hope you get some great answers.
    – Phrancis
    May 9 at 15:53







1




1




Welcome to Code Review! I hope you get some great answers.
– Phrancis
May 9 at 15:53




Welcome to Code Review! I hope you get some great answers.
– Phrancis
May 9 at 15:53















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194023%2fnumerical-integration-with-numba%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194023%2fnumerical-integration-with-numba%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Chat program with C++ and SFML

Function to Return a JSON Like Objects Using VBA Collections and Arrays

Will my employers contract hold up in court?