![](/img/trans.png)
[英]Can I speed up my MATLAB code using a MEX-file if the bottleneck is a big matrix multiplication?
[英]Matlab's Mex-file using a vector class definition
我正在嘗試從C ++代碼源文件創建一個Mex文件,以便與Matlab一起使用。 由於我不太了解的向量類定義處理,我編譯錯誤。 我想知道如何修改代碼才能工作。 下面我將展示相關代碼的各個部分,我將其分為四個部分以便進一步說明(計算代碼,MexFunction代碼,Vector類定義和編譯錯誤):
#include "mex.h"
#include "SAT_VecMat.h"
void AccelSolrad (const Vector& r, const Vector& r_Sun, double Area, double mass,
double CR, double P0, double AU,const Vector& Accel )
...
const Vector& r_sat(3); // dummy argument name for r
const Vector& r_sol(3); // dummy argument name for r_Sun
const Vector& outMatrix(3); // dummy argument name for Accel
...
r_sat = mxGetPr(prhs[0]);
r_sol = mxGetPr(prhs[1]);
plhs[0] = mxCreateDoubleMatrix(1,3,mxREAL);
outMatrix = mxGetPr(plhs[0]);
class Vector
{
public:
friend class Matrix;
// Constructors
Vector (); // Vector without elements
Vector (int Size); // Nullvector of specified size
Vector (const Vector& V); // Vector copy
Vector (const double* p, int N); // Array copy
Vector (double x, double y, double z); // 3dim-Vector
Vector (double x, double y, double z, // 6dim-Vector
double X, double Y, double Z);
// Destructor
~Vector();
// Size
int size() const { return n; };
Vector& resize(int Size);
// Assignment
Vector& operator=(const double value);
Vector& operator=(const Vector& V);
// Component access (Fortran notation)
double operator () (int i) const { return v[i]; };
double& operator () (int i) { return v[i]; };
Vector slice (int first, int last) const;
...
>> mex AccelSolrad.cpp
AccelSolrad.cpp
c:\program files\matlab\r2009b\extern\include\matrix.h(332) : error C2371: 'char16_t' : redefinition; different basic types
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\yvals.h(576) : see declaration of 'char16_t'
AccelSolrad.cpp(14) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Vector' (or there is no acceptable conversion)
d:\po\ejemplos\SAT_VecMat.h(69): could be 'Vector &Vector::operator =(const double)'
d:\po\ejemplos\SAT_VecMat.h(70): or 'Vector &Vector::operator =(const Vector &)'
while trying to match the argument list '(const Vector, Vector)'
AccelSolrad.cpp(18) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Vector' (or there is no acceptable conversion)
d:\po\ejemplos\SAT_VecMat.h(69): could be 'Vector &Vector::operator =(const double)'
d:\po\ejemplos\SAT_VecMat.h(70): or 'Vector &Vector::operator =(const Vector &)'
while trying to match the argument list '(const Vector, Vector)'
AccelSolrad.cpp(94) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Vector' (or there is no acceptable conversion)
d:\po\ejemplos\SAT_VecMat.h(69): could be 'Vector &Vector::operator =(const double)'
d:\po\ejemplos\SAT_VecMat.h(70): or 'Vector &Vector::operator =(const Vector &)'
while trying to match the argument list '(const Vector, double *)'
AccelSolrad.cpp(96) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Vector' (or there is no acceptable conversion)
d:\po\ejemplos\SAT_VecMat.h(69): could be 'Vector &Vector::operator =(const double)'
d:\po\ejemplos\SAT_VecMat.h(70): or 'Vector &Vector::operator =(const Vector &)'
while trying to match the argument list '(const Vector, double *)'
AccelSolrad.cpp(112) : error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const Vector' (or there is no acceptable conversion)
d:\po\ejemplos\SAT_VecMat.h(69): could be 'Vector &Vector::operator =(const double)'
d:\po\ejemplos\SAT_VecMat.h(70): or 'Vector &Vector::operator =(const Vector &)'
while trying to match the argument list '(const Vector, double *)'
C:\PROGRA~1\MATLAB\R2009B\BIN\MEX.PL: Error: Compile of 'AccelSolrad.cpp' failed.
至少有關char16_t
的錯誤消息可能是由於您使用Visual Studio 2010和MATLAB R2009b這一事實。 對於這個版本的MATLAB,該編譯器太新了。 請參閱R2009b的“ 支持的編譯器”列表 。
關於其他錯誤不太清楚,但是:
Vector& r_sat(3);
對我來說似乎很有問題C ++:因為它是對向量的引用,你正在做的是創建一個大小為3的臨時Vector,然后初始化引用以引用那個臨時的。 最好將r_sat
聲明為Vector
。
然后你有:
r_sat = mxGetPr(prhs[0]);
mxGetPr()
函數返回一個指向double數組的指針,但Vector類沒有operator=
以double*
作為參數。
也許是這樣的:
r_sat = Vector(mxGetPr(prhs[0]), 3); // use the Vector(double*, int N) constructor
Vector
類是C ++,而不是C. lcc
編譯器是C編譯器,它不了解C ++。 如果您要編譯C ++ MEX文件,則需要運行mex -setup
並選擇支持的C ++編譯器(通常是Visual Studio)。 mex
決定是否構建C或C ++ MEX文件(影響它是否調用C或C ++編譯器來進行實際編譯和鏈接)的方式基於文件擴展名。 所以將AccelSolrad.c
重命名為AccelSolrad.cpp
,你應該好好去。 我想我可能會有一些東西....雖然我以前從未使用過const obj&
。
您將主代碼中的向量類實例化為常量:
const Vector& r_sat(3);
然后你試圖將矢量r_span分配給一個數組。 由於r_span被聲明為常量,你不能。 您必須將其作為構造函數的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.