Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I've been working on a problem where I need to integrate a function numerically. The function is quite complex and it takes almost an hour for Mathematica to make a plot. To speed the process up I thought that I could implement a parallel NIntegrate function using ParallelTable[]. I believe I successfully accomplished that for single numerical values for the limits of the integration but there's something wrong since when I try to make a plot I got a bunch of errors. Here's my code, where instead of my horrible integrand I used the function $f(x)=x$

ParalellNIntegrate[imin_?NumericQ, imax_?NumericQ] :=
  (step = (imax - imin)/4;
   Total[
    ParallelTable[
     NIntegrate[u, {u, i, i+step}], {i, imin, imax - step, step}]
    ]);
Plot[ParalellNIntegrate[0, x], {x, -1, 1}]

(kernel 2) NIntegrate::nlim: u = 0.25 x is not a valid limit of integration.

(kernel 1) NIntegrate::nlim: u = 0.75 x is not a valid limit of integration.

(kernel 2) NIntegrate::nlim: u = 0.5 x is not a valid limit of integration.

(kernel 1) NIntegrate::nlim: u = x is not a valid limit of integration.

(kernel 2) NIntegrate::nlim: u = i+0.25 x is not a valid limit of integration.

(kernel 1) NIntegrate::nlim: u = i+0.25 x is not a valid limit of integration.

(kernel 1) General::stop: Further output of NIntegrate::nlim will be suppressed during this calculation.

(kernel 2) General::stop: Further output of NIntegrate::nlim will be suppressed during this calculation.

Can somebody help me?

PS: I also have this doubt about the step and I think I could use this post to clear it (I do realize that I shouldn't post more than one question per post but it's somehow related and it seems silly to open a post just for this). I divided the interval of integration in 4 regions since my cpu only works with 4 threads could somebody tell me if I'm thinking correctly?

share|improve this question
    
Before trying to parallelize NIntegrate, check if it is already making use of all your CPU cores internally. If it is, trying to parallelize manually will just slow things down. I noticed that when using the default "GlobalAdaptive" strategy, NIntegrate tends to already make use of all cores (depending on the integrand). –  Szabolcs Feb 24 at 21:02
1  
@Szabolcs No, in my case it only uses one core. I had previously checked that when the integral doesn't have analytical solution my implementation is indeed faster, although weirdly it oscillates between 4% and 100% faster depending on the limits of the integration... –  PML Feb 24 at 22:08
1  
It seems to me that plotting in that way is going to make a lot of redundant calculations. If you want the points at x = 0.5 and x = 1, you have to integrate from 0 to 0.5, and then integrate again from 0 to 1. But really to get that second point, we only need to add on the integral from 0.5 to 1. I'd be tempted to build up some points manually and then use a ListPlot. Also, your code works fine for me (apart from the extraneous comma). –  wxffles Feb 24 at 22:15
    
@wxffles Ups, just deleted the comma. Now that's weird, for me mathematica just throws those errors. –  PML Feb 24 at 22:38
    
@PML To deal with the issue wxffles mentions (i.e. unnecessary multiple integrations over the same intervals), you could try to recast the problem as an ODE and use NDSolve, which will give you a plottable InterpolatingFunction. It might speed up things sufficiently that parallelizing won't be necessary any more. –  Szabolcs Feb 24 at 22:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.