Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problems using MathLink

64 views
Skip to first unread message

Jonathan Yik

unread,
Jan 12, 2009, 8:16:09 PM1/12/09
to
Hi,

I'm trying to write a subroutine in C++ that passes a rotation matrix to
Mathematica through MathLink. The code compiles fine, but when I try to run
the compiled program, I get a segfault at the point in the program where the
subroutine is called. As far as I can tell, the fault occurs whenever the
program calls MLPutRealArray or MLPutReal32Array.

Here is my code for the subroutine:

void rot(MLINK link, double theta)
{
char **heads, **HEAD;
heads = new char*[3];
heads[0] = "List";
heads[1] = "List";
heads[2] = "List";
double a[3][3], *b;
a[0][0] = cos(theta);
a[0][1] = sin(theta);
a[0][2] = 0;
a[1][0] = - sin(theta);
a[1][1] = cos(theta);
a[1][2] = 0;
a[2][0] = 0;
a[2][1] = 0;
a[2][2] = 1;
long *dims, *D, depth;
dims = new long[3];
dims[0] = 3;
dims[1] = 3;
dims[2] = 3;
MLPutRealArray(link, *a, dims, heads, 3);
}

Is there something in this code that I'm missing that causes the fault? Or
is this a but in the ml32i3.dll file?

Yours,

Johnathan Yik

ragfield

unread,
Jan 13, 2009, 7:02:40 AM1/13/09
to
On Jan 12, 7:16 pm, "Jonathan Yik" <jonathan....@gmail.com> wrote:
> Hi,
>
> I'm trying to write a subroutine in C++ that passes a rotation matrix to
> Mathematica through MathLink. The code compiles fine, but when I try t=
o run
> the compiled program, I get a segfault at the point in the program where =
the
> subroutine is called. As far as I can tell, the fault occurs whenever =
> Is there something in this code that I'm missing that causes the fault? =

Or
> is this a but in the ml32i3.dll file?

You have a 2-dimensional (3x3) array of real numbers, yet you're
telling MathLink that you have a 3-dimensional (3x3x3) array of real
numbers. MathLink attempts to read 27 real numbers when you only have
9 allocated and the program crashes.

-Rob

Jens-Peer Kuska

unread,
Jan 14, 2009, 5:32:19 AM1/14/09
to
Hi,

you are sure that your array has the depth 3 and not
the dimension 3 x 3, and what is the third dimension
in a[3][3] ..

BTW, you never delete[] heads,dims.
If you wish to fill your main memory you should
use larger pieces of memory
that you don't recycle, you are faster done than.

Regards
Jens

Jonathan Yik

unread,
Jan 15, 2009, 6:09:26 AM1/15/09
to

Hi,

Since I posted to MathGroup I have discovered that the compiled program runs
properly if I set the fourth argument in MLPutRealArray to NULL rather than
using the values contained within the heads array. Presumably I have
misunderstood how this argument works.

If I understand your message, I should set the last argument of
MLPutRealArray to 2 and declare dims to have length 2 rather than 3, with
dims[0] and dims[1] both being 3.

However, when I modified the code as stated above, the program was able to
compile and run, but MLPutArray was not able to successfully pass the value,
returning a value of 0. Calling MLErrorMessage only resulted in an "Unknown
Mathlink problem encountered" message.

Is there any reason why this is so?

Yours,

Johnathan Yik


On Tue, Jan 13, 2009 at 8:02 PM, ragfield <ragf...@gmail.com> wrote:

> On Jan 12, 7:16 pm, "Jonathan Yik" <jonathan....@gmail.com> wrote:

> > Hi,
> >
> > I'm trying to write a subroutine in C++ that passes a rotation matrix to

> > Mathematica through MathLink. The code compiles fine, but when I try t=


> o run
> > the compiled program, I get a segfault at the point in the program where

> =
> the
> > subroutine is called. As far as I can tell, the fault occurs whenever =

> > Is there something in this code that I'm missing that causes the fault? =


> Or
> > is this a but in the ml32i3.dll file?
>

Steve Wilson

unread,
Jan 15, 2009, 6:10:10 AM1/15/09
to
Here are the modifications that this function needs in order to work
correctly:

void rot(MLINK link, double theta)
{

/*
Because the original function was only sending Heads of type List,
We can simply pass MLPutRealArray a NULL pointer for the heads
argument and MathLink will send the head List for each level of the
array.
*/

double a[3][3];


a[0][0] = cos(theta);
a[0][1] = sin(theta);
a[0][2] = 0;
a[1][0] = - sin(theta);
a[1][1] = cos(theta);
a[1][2] = 0;
a[2][0] = 0;
a[2][1] = 0;
a[2][2] = 1;

long *dims, depth;

/*
The array is only depth 2. The original version told MLPutRealArray
that
the data array 'a' had a depth of 3. MLPutRealArray tried to send
the extra dimension
which didn't actually exist which lead to the crash.
*/

dims = new long[2];


dims[0] = 3;
dims[1] = 3;

MLPutRealArray(link, *a, dims, (char **)0, 2);
}


Steve Wilson
Senior Network/System Protocol Developer
Wolfram Research, Inc.


On Jan 14, 2009, at 3:31 AM, Jens-Peer Kuska wrote:

> Hi,
>
> you are sure that your array has the depth 3 and not
> the dimension 3 x 3, and what is the third dimension
> in a[3][3] ..
>
> BTW, you never delete[] heads,dims.
> If you wish to fill your main memory you should
> use larger pieces of memory
> that you don't recycle, you are faster done than.
>
> Regards
> Jens
>
> Jonathan Yik wrote:

Jens-Peer Kuska

unread,
Jan 15, 2009, 6:10:55 AM1/15/09
to
Hi,

you also not free the memory !

void rot(MLINK link, double theta)
{

/*


Because the original function was only sending Heads of type List,
We can simply pass MLPutRealArray a NULL pointer for the heads
argument and MathLink will send the head List for each level of the array.
*/

double a[3][3];


a[0][0] = cos(theta); a[0][1] = sin(theta); a[0][2] = 0;
a[1][0] = - sin(theta); a[1][1] = cos(theta); a[1][2] = 0;
a[2][0] = 0; a[2][1] = 0; a[2][2] = 1;

long *dims, depth;

/*
The array is only depth 2. The original version told MLPutRealArray that
the data array 'a' had a depth of 3. MLPutRealArray tried to send the
extra dimension
which didn't actually exist which lead to the crash.
*/

dims = new long[2];

dims[0] = 3; dims[1] = 3;

MLPutRealArray(link, *a, dims, (char **)0, 2);

/*************************************************
because otherwise, every call waste 32 byte of heap memory
************************************************/
delete[] dims;
}

Regards
Jens

Steve Wilson wrote:
> Here are the modifications that this function needs in order to work
> correctly:
>

> void rot(MLINK link, double theta)
> {
>

> /*
> Because the original function was only sending Heads of type List,
> We can simply pass MLPutRealArray a NULL pointer for the heads
> argument and MathLink will send the head List for each level of the
> array.
> */
>
> double a[3][3];

> a[0][0] = cos(theta);
> a[0][1] = sin(theta);
> a[0][2] = 0;
> a[1][0] = - sin(theta);
> a[1][1] = cos(theta);
> a[1][2] = 0;
> a[2][0] = 0;
> a[2][1] = 0;
> a[2][2] = 1;
>

> long *dims, depth;
>
> /*
> The array is only depth 2. The original version told MLPutRealArray that
> the data array 'a' had a depth of 3. MLPutRealArray tried to send
> the extra dimension
> which didn't actually exist which lead to the crash.
> */
>
> dims = new long[2];

> dims[0] = 3;
> dims[1] = 3;

> MLPutRealArray(link, *a, dims, (char **)0, 2);
> }
>
>
> Steve Wilson
> Senior Network/System Protocol Developer
> Wolfram Research, Inc.
>
>
> On Jan 14, 2009, at 3:31 AM, Jens-Peer Kuska wrote:
>
>> Hi,
>>
>> you are sure that your array has the depth 3 and not
>> the dimension 3 x 3, and what is the third dimension
>> in a[3][3] ..
>>
>> BTW, you never delete[] heads,dims.
>> If you wish to fill your main memory you should
>> use larger pieces of memory
>> that you don't recycle, you are faster done than.
>>
>> Regards
>> Jens
>>
>> Jonathan Yik wrote:

Steve Wilson

unread,
Jan 15, 2009, 6:11:29 AM1/15/09
to
Yes, you need to call delete[] for the dims array.


On Jan 14, 2009, at 2:08 PM, Jens-Peer Kuska wrote:

> Hi,
>


> you also not free the memory !
>

> void rot(MLINK link, double theta)
> {
>

> /*
> Because the original function was only sending Heads of type List,
> We can simply pass MLPutRealArray a NULL pointer for the heads
> argument and MathLink will send the head List for each level of the
> array.
> */
>
> double a[3][3];

> a[0][0] = cos(theta); a[0][1] = sin(theta); a[0][2] = 0;
> a[1][0] = - sin(theta); a[1][1] = cos(theta); a[1][2] = 0;
> a[2][0] = 0; a[2][1] = 0; a[2][2] = 1;
>

> long *dims, depth;
>
> /*
> The array is only depth 2. The original version told MLPutRealArray
> that
> the data array 'a' had a depth of 3. MLPutRealArray tried to send
> the extra dimension
> which didn't actually exist which lead to the crash.
> */
>
> dims = new long[2];

> dims[0] = 3; dims[1] = 3;

> MLPutRealArray(link, *a, dims, (char **)0, 2);
>
> /*************************************************
> because otherwise, every call waste 32 byte of heap memory
> ************************************************/
> delete[] dims;
> }
>
> Regards
> Jens
>
> Steve Wilson wrote:
>> Here are the modifications that this function needs in order to
>> work correctly:
>>

>> void rot(MLINK link, double theta)
>> {
>>

>> /*
>> Because the original function was only sending Heads of type List,
>> We can simply pass MLPutRealArray a NULL pointer for the heads
>> argument and MathLink will send the head List for each level of the
>> array.
>> */
>>
>> double a[3][3];

>> a[0][0] = cos(theta);
>> a[0][1] = sin(theta);
>> a[0][2] = 0;
>> a[1][0] = - sin(theta);
>> a[1][1] = cos(theta);
>> a[1][2] = 0;
>> a[2][0] = 0;
>> a[2][1] = 0;
>> a[2][2] = 1;
>>

>> long *dims, depth;
>>
>> /*
>> The array is only depth 2. The original version told
>> MLPutRealArray that
>> the data array 'a' had a depth of 3. MLPutRealArray tried to send
>> the extra dimension
>> which didn't actually exist which lead to the crash.
>> */
>>
>> dims = new long[2];

>> dims[0] = 3;
>> dims[1] = 3;

>> MLPutRealArray(link, *a, dims, (char **)0, 2);
>> }
>>
>>
>> Steve Wilson
>> Senior Network/System Protocol Developer
>> Wolfram Research, Inc.
>>
>>
>> On Jan 14, 2009, at 3:31 AM, Jens-Peer Kuska wrote:
>>
>>> Hi,
>>>
>>> you are sure that your array has the depth 3 and not
>>> the dimension 3 x 3, and what is the third dimension
>>> in a[3][3] ..
>>>
>>> BTW, you never delete[] heads,dims.
>>> If you wish to fill your main memory you should
>>> use larger pieces of memory
>>> that you don't recycle, you are faster done than.
>>>
>>> Regards
>>> Jens
>>>
>>> Jonathan Yik wrote:

Jonathan Yik

unread,
Jan 16, 2009, 6:06:28 AM1/16/09
to
I've looked at the code for the file that calls the subroutine, and it
appears that I have been attempting to pass the array to a loopback link,
though I am not sure if this would indeed cause the problem or not.
Changing the link to a direct link to the Mathematica kernel appears to
raise another problem, as the program appears to just sit there without
doing anything, until I am forced to cancel it. This problem does not
appear specific to my program, as my attempts to run the quotient program
from the examples also resulted in the program sitting dead without taking
further action.

Here is the code for the program that calls the subroutine. I have been
compiling the code in cygwin using g++:

#include<stdio.h>
#include<iostream>
#include<cmath>
#include "mathlink.h"
#include "fields.h"

using namespace std;

int main(int argc, char **argv)
{
int error, n, k;
const char *a, *b;
MLENV ep;
MLINK lp, func;
ep = MLInitialize(0);
lp = MLOpenArgcArgv(ep, argc, argv, &error);
rot(lp, 1.0);
MLEndPacket(lp);
MLPutFunction(lp, "Exit", 0);
MLClose(lp);
MLDeinitialize(ep);
cout << MLError(func) << "\n" << MLErrorMessage(func) << endl;
return 0;
}

Thanks for your help.

On Thu, Jan 15, 2009 at 1:55 AM, Rob Raguet-Schofield <ragf...@gmail.com>wrote:

> Very strange. Nothing else stands out to me as being wrong. I don't know
> what to tell you.
>
> If you want to send the full code I can try to look at it when I get the
> chance.
>
>
> On Jan 14, 2009, at 11:41 AM, Jonathan Yik wrote:
>
> Yes, MLPutInteger works, but MLPutRealArray doesn't. MLPutIntegerArray
> also works, but for some reason, I can't get MLPutRealArray to work right.
>
> Yours,
>
> Johnathan Yik
>
> On Thu, Jan 15, 2009 at 12:28 AM, Rob Raguet-Schofield <ragf...@gmail.com
> > wrote:


>
>> On Jan 14, 2009, at 8:14 AM, Jonathan Yik wrote:
>>
>>> Since I posted to MathGroup I have discovered that the compiled program
>>> runs properly if I set the fourth argument in MLPutRealArray to NULL rather
>>> than using the values contained within the heads array. Presumably I have
>>> misunderstood how this argument works.
>>>
>>

>> This argument is optional. If the heads are all List then you can safely
>> pass NULL. I don't see any reason why the old code wouldn't have worked
>> though.


>>
>> If I understand your message, I should set the last argument of
>>> MLPutRealArray to 2 and declare dims to have length 2 rather than 3, with
>>> dims[0] and dims[1] both being 3.
>>>
>>

>> Correct.


>>
>> However, when I modified the code as stated above, the program was able
>>> to compile and run, but MLPutArray was not able to successfully pass the
>>> value, returning a value of 0. Calling MLErrorMessage only resulted in an
>>> "Unknown Mathlink problem encountered" message.
>>>
>>> Is there any reason why this is so?
>>>
>>
>>

>> I think that should work. Is the link in the proper state to receive the
>> array? If you replace the MLPutRealArray(...) call with MLPutInteger(link,
>> 0) does that succeed?
>>
>
>
> Rob Raguet-Schofield
> (rob ra gA skO fEld)
>
>

Scot Martin

unread,
Jan 16, 2009, 6:08:38 AM1/16/09
to
I'm interested in what people know on the "scoop" for comments and opinions
on webMathematica. Also, does anyone know if there is a user group?

The basis of my question is as follows. I have been using the product for
about 6 months now, and I'm getting a lot of high mileage out of and give it
a 5-star rating. Nevertheless, it's based on Mathematica V5.2 (i.e., the
webMathematica product from Wolfram does not support a higher version), and
the "new" webMathematica that should support higher versions now seems
several years delayed.

Do I make the correct inference that webMathematica did not turn out to have
as wide an appeal as Wolfram had hoped for and that it is almost in the
category of discontinued product?

Or on the flip side is there a vibrant community out there that I can join?
Google hits seem to turn up only the Wolfram site, again leading to my
inference that the product did not turn out in the "success" category for
Wolfram. Does anyone know why?

Any more comments, opinions, or knowledge on the history and context of
webMathematica?

-Scot Martin

t...@wolfram.com

unread,
Jan 17, 2009, 5:36:51 AM1/17/09
to

Scott


We have been working on an update for webMathematica, one
that uses a higher version of Mathematica and also that has
a significant number of new features.


It has been in a stable and nearly ready state for some
time. Unfortunately, it was pushed back by the release of
Mathematica 7. The release of the base Mathematica always
has to take priority. Now that Mathematica 7 is released,
we hope to move forward with webMathematica.


In the interim there is a beta version of the update that is
available and you could sign up to try this. We would also
like to hear about what you have done with your existing
webMathematica.


We certainly do consider webMathematica to be an important
and relevant way to deliver Mathematica, and fully intend
to support and to develop it.


Tom Wickham-Jones
Wolfram Research


On Jan 16, 11:08 am, "Scot Martin" <smar...@seas.harvard.edu> wrote:
> I'm interested in what people know on the "scoop" for comments and opinio=


ns
> on webMathematica. Also, does anyone know if there is a user group?
>
> The basis of my question is as follows. I have been using the product for

> about 6 months now, and I'm getting a lot of high mileage out of and give=


it
> a 5-star rating. Nevertheless, it's based on Mathematica V5.2 (i.e., the

> webMathematica product from Wolfram does not support a higher version), a=


nd
> the "new" webMathematica that should support higher versions now seems
> several years delayed.
>

> Do I make the correct inference that webMathematica did not turn out to h=


ave
> as wide an appeal as Wolfram had hoped for and that it is almost in the
> category of discontinued product?
>

> Or on the flip side is there a vibrant community out there that I can joi=

Rob Raguet-Schofield

unread,
Jan 17, 2009, 5:39:53 AM1/17/09
to
This works (though doesn't do anything productive).

#include<stdio.h>
#include<iostream>
#include<cmath>
#include "mathlink.h"

//#include "fields.h"

using namespace std;

void rot(MLINK link, double theta)
{

char* heads[2] = {"List", "List"};
double a[3][3];


a[0][0] = cos(theta);
a[0][1] = sin(theta);
a[0][2] = 0;
a[1][0] = - sin(theta);
a[1][1] = cos(theta);
a[1][2] = 0;
a[2][0] = 0;
a[2][1] = 0;
a[2][2] = 1;

long dims[2] = {3, 3}, depth = 2;
MLPutRealArray(link, *a, dims, heads, depth);
cout << "err = " << MLError(link) << endl;
}

int main(int argc, char **argv)
{

int error, n, k, pkt;


const char *a, *b;
MLENV ep;
MLINK lp, func;
ep = MLInitialize(0);
lp = MLOpenArgcArgv(ep, argc, argv, &error);

MLPutFunction(lp, "EvaluatePacket", 1);
rot(lp, 1.0);
MLEndPacket(lp);

// drain the link
while((pkt = MLNextPacket(lp)) != 0 && pkt != RETURNPKT)
MLNewPacket(lp);

// Don't call Exit[], just close the link
//MLPutFunction(lp, "Exit", 0);
MLClose(lp);
MLDeinitialize(ep);
// func is uninitialized, and the environment gone anyway
//cout << MLError(func) << "\n" << MLErrorMessage(func) << endl;
return 0;
}


run with:

$ ./a.out -linkmode launch -linkname "'MathKernel' -mathlink"

0 new messages