Re: Do I need MathLink to run finite-difference fast enough for Manipulate?
- To: mathgroup at smc.vnet.net
- Subject: [mg115804] Re: Do I need MathLink to run finite-difference fast enough for Manipulate?
- From: Armand Tamzarian <mike.honeychurch at gmail.com>
- Date: Fri, 21 Jan 2011 04:31:41 -0500 (EST)
- References: <ih6e2q$36r$1@smc.vnet.net>
On Jan 19, 9:23 pm, "James" <icor... at hotmail.com> wrote:
> Hello guys,
>
> I would like to create a Manipulate for a finite-difference on a system
> of PDEs using a 100x100 array that is processed about 10,000 times. Is
> it possible to code this in native Mathematica, for example using
> Compile, fast enough to be reasonably used by Manipulate (under 3 or 4
> seconds)? The C++ code is below and with the Compiler set to optimize
> speed, it runs in approximately 1.5 seconds. However, if I attempt to
> just code it in Mathematica using arrays, it takes much too long to
> execute.
>
> Can someone help me decide if the only reasonable option is to use
> MathLink or can I even use MathLink in a Manipulate?
>
> Thanks Guys. Here's the C++ code. The variables u, v, and lap
> are all 100x100 arrays.
>
> startt=clock();
>
> for(p=1;p<=10000;p++){
>
> lap[0][0]=(u[0][1]-4*u[0][0]+u[0][M-1]+u[1][0]+u[N-1][0]);
>
> for(j=1;j<=M-2;j++)
> {
> lap[0][j]=(u[0][j+1]-4*u[0][j]+u[0][j-1]+u[1][j]+u[N-1]=
[j]);
> }
>
> lap[0][M-1]=(u[0][0]-4*u[0][M-1]+u[0][M-2]+u[1][M-1]+u[N-1][M-1=
]);
>
> for(i=1;i<=N-2;i++)
> {
> lap[i][0]=(u[i][1]-4*u[i][0]+u[i][M-1]+u[i+1][0]+u[i-1]=
[0]);
> }
>
> for(i=1;i<=N-2;i++)
> {
>
> lap[i][M-1]=(u[i][0]-4*u[i][M-1]+u[i][M-2]+u[i+1][M-1]+u[i-1][M-1]);
> }
>
> lap[N-1][0]=(u[N-1][1]-4*u[N-1][0]+u[N-1][M-1]+u[0][0]+u[N-2][0=
]);
>
> lap[N-1][M-1]=(u[N-1][0]-4*u[N-1][M-1]+u[N-1][M-2]+u[0][M-1]+u[N-2][M-1=
]);
>
> for(j=1;j<=M-2;j++)
> {
>
> lap[N-1][j]=(u[N-1][j+1]-4*u[N-1][j]+u[N-1][j-1]+u[0][j]+u[N-2][j]);
> }
>
> for(i=1;i<=N-2;i++)
> for(j=1;j<=M-2;j++)
> {
> lap[i][j]=(u[i][j+1]-4*u[i][j]+u[i][j-1]+u[i+1][j]+u[i-=
1][j]);
> }
>
> for(i=0;i<M;i++)
> for(j=0;j<N;j++){
>
> u[i][j] =
> =u[i][j]+dt*(D_u*lap[i][j]+(a-(b+1)*u[i][j]+v[i][j]*u[i][j]*u[i][j]));
>
> }
>
> lap[0][0]=(v[0][1]-4*v[0][0]+v[0][M-1]+v[1][0]+v[N-1][0]);
>
> for(j=1;j<=M-2;j++)
> {
> lap[0][j]=(v[0][j+1]-4*v[0][j]+v[0][j-1]+v[1][j]+v[N-1]=
[j]);
> }
>
> lap[0][M-1]=(v[0][0]-4*v[0][M-1]+v[0][M-2]+v[1][M-1]+v[N-1][M-1=
]);
>
> for(i=1;i<=N-2;i++)
> {
> lap[i][0]=(v[i][1]-4*v[i][0]+v[i][M-1]+v[i+1][0]+v[i-1]=
[0]);
> }
>
> for(i=1;i<=N-2;i++)
> {
>
> lap[i][M-1]=(v[i][0]-4*v[i][M-1]+v[i][M-2]+v[i+1][M-1]+v[i-1][M-1]);
> }
>
> lap[N-1][0]=(v[N-1][1]-4*v[N-1][0]+v[N-1][M-1]+v[0][0]+v[N-2][0=
]);
>
> lap[N-1][M-1]=(v[N-1][0]-4*v[N-1][M-1]+v[N-1][M-2]+v[0][M-1]+v[N-2][M-1=
]);
>
> for(j=1;j<=M-2;j++)
> {
>
> lap[N-1][j]=(v[N-1][j+1]-4*v[N-1][j]+v[N-1][j-1]+v[0][j]+v[N-2][j]);
> }
>
> for(i=2;i<=N-2;i++)
> for(j=2;j<=M-2;j++)
> {
> lap[i][j]=(v[i][j+1]-4*v[i][j]+v[i][j-1]+v[i+1][j]+v[i-=
1][j]);
> }
>
> for(i=0;i<M;i++)
> for(j=0;j<N;j++){
> v[i][j] =
> =v[i][j]+dt*(D_v*lap[i][j]+(b*u[i][j]-v[i][j]*u[i][j]*u[i][j]));
> }
>
> endt=clock();
>
> fprintf(stderr,"%d\n",endt-startt);
Can you give us an indication of how much time "much too long" is?
Apologies in advance if this is a dumb question but you aren't use For
loops in your Mathematica code are you?
Can you give us the actual PDE. I know I should be able to work it out
from your code but it gives me a headache.
Mike