简体   繁体   English

运行Matlab mex-算法,而不是双精度单精度

[英]Run Matlab mex-Algorithm instead of Double- in Single-Precision

I have a working Matlab C code (mex files) which currently uses double precision. 我有一个工作的Matlab C代码(mex文件),当前使用双精度。 Thus I replaced 因此,我更换了

double *datOut = mxGetPr(mxOut) by float *datOut = (float*)mxGetData(mxOut); 通过float *datOut = (float*)mxGetData(mxOut); double *datOut = mxGetPr(mxOut) float *datOut = (float*)mxGetData(mxOut); ,

mxCreateDoubleMatrix by mxCreateNumericArray() mxCreateDoubleMatrix通过mxCreateNumericArray()

and

the datatypes of the variables double by float . 变量的数据类型double float The only other mex-Function which is being used is mxDuplicateArray() but nothing else. 唯一正在使用的其他mex-Function是mxDuplicateArray()但仅此而已。 I didn't change anything to this call... Now I have a Code running which never finishes. 我没有更改此调用的任何内容...现在,我正在运行永远无法完成的代码。 I stripped it down quite a bit so that I hope it's short enough that somebody could help me: 我将其精简了很多,所以我希望它足够短,以至于有人可以帮助我:

float myFunc(const mxArray *point, int index)
{
    float *dat = (float*)mxGetData(point);
    return dat[index]*dat[index]*dat[index];
}

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    float h, f0, df1, df2, diff;

    // Input Vars #1
    float diff  = (float)mxGetScalar(prhs[1]);
    float H = (float)mxGetScalar(prhs[2]);
    int index1 = (int)mxGetScalar(prhs[3]);
    int index2 = (int)mxGetScalar(prhs[4]);

    // Input Vars #2 -> Duplicate it
    mxArray *newPnt = mxDuplicateArray(prhs[0]);
    float *newPntDat = (float*)mxGetData(newPnt);

    // ...
    // PERHAPS SOME UNIMPORANT CODE HERE ...
    // ...
    h = H;
    f0 = myFunc(prhs[0], index1);

    newPntData[ index2 ] += h;
    df1 = (myFunc(newPnt, index1)-f0)/h;
    while(true)
    {
        h /= 2;

        newPntDat[ index2 ] -= h;
        df2 = (myFunc(newPnt, index1)-f0)/h;

        // If precision is okay
        if(abs(df2-df1) <= diff)
            break;

        // Save for next loop iteration
        df1 = df2;
    }

    // Return df2-Value to Matlab
}

somehow it's an infinite loop and I dunno why since the precision which is defined via diff should be easily reachable for the given function myFunc() . 无论如何,这是一个无限循环,我不知道为什么对于给定的函数myFunc()通过diff定义的精度应该很容易达到。 The identicall code runs fine when using double precision with the both functions double *datOut = mxGetPr(mxOut) and mxCreateDoubleMatrix . 当对两个函数double *datOut = mxGetPr(mxOut)mxCreateDoubleMatrix使用双精度时,相同的代码运行良好。 I also tried to call the mex-Function by explicitly pass the point via point = zeros(rows, 1, 'single'); 我还尝试通过point = zeros(rows, 1, 'single');明确传递该点来调用mex-Function point = zeros(rows, 1, 'single'); .

Thanks very much for pointing me to the right direction or giving me ANY hint about it. 非常感谢您为我指明了正确的方向或给我任何提示。 Thanks! 谢谢!

You need to replace abs() with fabs() . 您需要将abs()替换为fabs()

In general, in such cases, I would use mexPrintf() to print the values that affect the termination condition. 通常,在这种情况下,我将使用mexPrintf()打印影响终止条件的值。 Ie, if the above change does not help, try adding 即,如果上述更改无济于事,请尝试添加

mexPrintf("%g %g %g %g\n",df2,df1,diff, fabs(df2-df1));

Just to make sure the behavior is as you expected. 只是为了确保行为符合您的预期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM