Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在cmex文件中使用BLAS库_C_Matlab_Mex_Blas - Fatal编程技术网

在cmex文件中使用BLAS库

在cmex文件中使用BLAS库,c,matlab,mex,blas,C,Matlab,Mex,Blas,我正在尝试使用我的C-Mex代码的BLAS库来分析有BLAS和没有BLAS时的性能差异。然而,我不知道如何在没有编译错误/警告的情况下正确使用BLAS(最终导致Matlab崩溃) 下面是一个示例代码,我想使用Blas()的ddot()函数计算向量积: 这似乎有点像我将错误的变量类型(或指针类型?)传递给BLAS函数,但我就是不知道如何修复它。谁能看一下吗? 非常感谢 如果我的猜测是正确的,您只需定义代码中所涉及的整数的正确类型: #include <mex.h> #inclu

我正在尝试使用我的C-Mex代码的BLAS库来分析有BLAS和没有BLAS时的性能差异。然而,我不知道如何在没有编译错误/警告的情况下正确使用BLAS(最终导致Matlab崩溃)

下面是一个示例代码,我想使用Blas()的ddot()函数计算向量积:

这似乎有点像我将错误的变量类型(或指针类型?)传递给BLAS函数,但我就是不知道如何修复它。谁能看一下吗?
非常感谢

如果我的猜测是正确的,您只需定义代码中所涉及的整数的正确类型:

  #include <mex.h>
  #include <math.h>
  #include <blas.h>

  void TestBlas(double *L, double *R, mwSignedIndex n) //changed
  {

      mwSignedIndex one = 1; //changed
      double sum = ddot(&n,L,&one,L,&one);

      //Output Matrix R not defined, just for test purposes...

  } 

  void mexFunction( int nlhs, mxArray *plhs[],
                    int nrhs, const mxArray *prhs[])
  {
      double *inMatrix;               /* 1xN input matrix */
      size_t ncols;                   /* size of matrix */
      long *ncolsPr;                  /* output matrix */
      double *outMatrix;              /* output matrix */

      inMatrix = mxGetPr(prhs[0]);
      ncols = mxGetN(prhs[0]);

      /* create the output matrix */
      plhs[0] = mxCreateDoubleMatrix((mwSize)1,(mwSize)ncols,mxREAL); //changed
      outMatrix = mxGetPr(plhs[0]);

      TestBlas(inMatrix,outMatrix,(mwSignedIndex)ncols); //changed
  }
#包括
#包括
#包括
void TestBlas(double*L,double*R,mwSignedIndex n)//已更改
{
mwSignedIndex one=1;//已更改
双和=ddot(&n,L,&1,L,&1);
//输出矩阵R未定义,仅用于测试目的。。。
} 
void MEX函数(整数nlhs,mxArray*plhs[],
整数nrhs,常量mxArray*prhs[]
{
双*inMatrix;/*1xN输入矩阵*/
大小\u t ncols;/*矩阵大小*/
长*ncolsPr;/*输出矩阵*/
双*输出矩阵;/*输出矩阵*/
inMatrix=mxGetPr(prhs[0]);
ncols=mxGetN(prhs[0]);
/*创建输出矩阵*/
plhs[0]=mxCreateDoubleMatrix((mwSize)1,(mwSize)ncols,mxREAL);//已更改
outMatrix=mxGetPr(plhs[0]);
TestBlas(inMatrix,outMatrix,(mwSignedIndex)ncols);//已更改
}

根据我的经验,您必须特别小心函数调用中出现的文本常量,因此请注意将
1
转换为
(mwSize)
,这是
mxCreateDoubleMatrix
所期望的。

正如警告所说:
ddot
期望
整数n
,而您给它
mwSize n
。这是不可能的。我遇到了一些这样的问题:总是定义适当的局部变量,传递给BLAS/LAPACK例程,并对它们进行类型转换。尝试在
TestBlas
函数中使用
int n
,并在调用它时相应地将
size\u t
强制转换为
int
。也应该非常有用:“mwSignedIndex通常是MATLAB附带的库的BLAS和LAPACK函数的整数参数的推荐值……至少这是示例代码中使用的。我还没有扫描头文件以查看它是否总是减少到ptrdiff_t。”。这解释了您的其余警告,这些警告确实会导致问题(
int
vs
long
)。请检查它,因为我不确定它是否能解决您的问题。这是一个非常全面和有用的答案,再次感谢@用户3116232我很高兴能帮上忙。我想明确一点:这确实解决了你的问题吗?:)是的,因为我真的被BLAS想要的文件类型弄糊涂了。我仍然是,但现在我知道如何调整我的代码。我想,理解是一个过程;)
 >> mex -largeArrayDims TestBlas.c -lmwblas
  Building with 'Xcode with Clang'.
  /Users/jhess/Dropbox/Uni/E-Technik Master/Forschungspraxis Machine Learning/Matlab/TestBlas.c:9:23: warning: passing 'mwSize *' (aka 'unsigned long *') to parameter of type 'const ptrdiff_t *' (aka 'const long *') converts between pointers to integer types with different sign [-Wpointer-sign]
      double sum = ddot(&n,L,&one,L,&one);
                        ^~
  /Applications/MATLAB_R2015a.app/extern/include/blas.h:559:22: note: passing argument to parameter 'n' here
      const ptrdiff_t *n,
                       ^
  /Users/jhess/Dropbox/Uni/E-Technik Master/Forschungspraxis Machine Learning/Matlab/TestBlas.c:9:28: warning: incompatible pointer types passing 'int *' to parameter of type 'const ptrdiff_t *' (aka 'const long *') [-Wincompatible-pointer-types]
      double sum = ddot(&n,L,&one,L,&one);
                             ^~~~
  /Applications/MATLAB_R2015a.app/extern/include/blas.h:561:22: note: passing argument to parameter 'incx' here
      const ptrdiff_t *incx,
                       ^
  /Users/jhess/Dropbox/Uni/E-Technik Master/Forschungspraxis Machine Learning/Matlab/TestBlas.c:9:35: warning: incompatible pointer types passing 'int *' to parameter of type 'const ptrdiff_t *' (aka 'const long *') [-Wincompatible-pointer-types]
      double sum = ddot(&n,L,&one,L,&one);
                                    ^~~~
  /Applications/MATLAB_R2015a.app/extern/include/blas.h:563:22: note: passing argument to parameter 'incy' here
      const ptrdiff_t *incy
                       ^
  3 warnings generated.

  MEX completed successfully.
  #include <mex.h>
  #include <math.h>
  #include <blas.h>

  void TestBlas(double *L, double *R, mwSignedIndex n) //changed
  {

      mwSignedIndex one = 1; //changed
      double sum = ddot(&n,L,&one,L,&one);

      //Output Matrix R not defined, just for test purposes...

  } 

  void mexFunction( int nlhs, mxArray *plhs[],
                    int nrhs, const mxArray *prhs[])
  {
      double *inMatrix;               /* 1xN input matrix */
      size_t ncols;                   /* size of matrix */
      long *ncolsPr;                  /* output matrix */
      double *outMatrix;              /* output matrix */

      inMatrix = mxGetPr(prhs[0]);
      ncols = mxGetN(prhs[0]);

      /* create the output matrix */
      plhs[0] = mxCreateDoubleMatrix((mwSize)1,(mwSize)ncols,mxREAL); //changed
      outMatrix = mxGetPr(plhs[0]);

      TestBlas(inMatrix,outMatrix,(mwSignedIndex)ncols); //changed
  }