return value of large arrays from mex differs?

2 views (last 30 days)
Hello,
i wrote a mex-script which makes some calculation. After the calculation the data returns to matlab. so far so good. now i got the problem, when i check my returning data with my reference data, that the returning data differs *sometimes * to the reference data. Even i dont change the parameters and especially at large arrays. the values are all float-datatype.
My questions:
1) is it possible that i get incorrect data because my memory of my computer is not large enough even i use malloc? (as data goes from mex to matlab)
2) as i get float values back is it useful to check just the last 5 digits? as i know the following digits are garbage and can vary as it s float-type?

Answers (1)

James Tursa
James Tursa on 8 Jun 2013
1) Generally no. If you have a memory constraint that is being hit, it will not in and of itself change a floating point calculation result. You will either get a memory error (if you use mxMalloc and friends) that will return control back to the caller, or you might get unpredictable results (or a crash) if you use malloc (and friends) but do not check the return value to see if it is valid before using it.
2) If you use the same inputs on the same floating point calculation you generally should expect to get exactly the same output (but possibly not if there are multi-threaded calculations in the background using reduction techniques or similar, or processor floating point rounding etc flags have changed between calculations). The trailing bits may be "garbage" as far as being meaningful, but they should be the same from calculation to calculation if the inputs are the same. If they are not, then I would suspect a program bug.
We would have to see the code you are using to make more detailed comments about the differences you are seeing. Note that I would not generally expect a C mex calculation to match a MATLAB m-code calculation. Even if the source code looks the same to you, the MATLAB parser might do calculations in a different order than the C compiler did for the seemingly same source code.
  3 Comments
James Tursa
James Tursa on 19 Jun 2013
Just getting back to this ...
To clarify before I respond, is row the 1st dim index, counter the 2nd dim index, and frame the 3rd dim index? And are all the values of row, counter, and frame 1 based or 0 based? And what are the limits for each dimension (looks like maxRows is the limit of the 1st dimension and maxFrames is the limit of the 3rd dimension)?
mick strife
mick strife on 28 Jun 2013
thx for your effort james. in matlab i get an array like this (rows, cols, frames)
frame 1:
2.22 3.33 4.44
3.22 4.55 1.22
frame 2:
1.11 1.22 3.33
3.33 4.44 5.55
so "maxRows" indicates the number of rows in a frame. in the example above i would ve maxRows of 2. i have noticed that my code which i posted earlierer isnt clear. sorry my mistake. "row" is more like a column. so when it comes to the line "if(row == 2)" then it should be "if(cols==2)"
the reason behind this is that i want the values in a "line" like this: 1.11 1.22 3.33
here is the code of the loop
do {
if(row > 3){
row=1;
counter++;
}
if(row==1){
data[counter+(frame*maxRows*3)] = floatValue;
}else if(row==2){
data[counter+maxRows+(frame*maxRows*3)] = floatValue;
}else if(row==3){
data[counter+maxRows*2+(frame*maxRows*3)] = floatValue;
}
row++;
} while (...);
i hope its clear now..
2) i m not sure what you mean with 1 based or 0 based.
3) the limits are various depending of the input data. a small or very large array is possible. only the number of cols is always 3.

Sign in to comment.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!