Matlab 将节与OpenMP和MEX一起使用
我试图计算3x3矩阵的逆,并将其乘以其他3x3矩阵。 这些计算的代码用作mex函数。 我的问题始于我尝试使用OpenMP库时。 我试图使矩阵逆的两部分并行,但我得到了许多编译错误。 注释pragmas时,代码编译是干净的。 非常感谢你的帮助Matlab 将节与OpenMP和MEX一起使用,matlab,openmp,mex,Matlab,Openmp,Mex,我试图计算3x3矩阵的逆,并将其乘以其他3x3矩阵。 这些计算的代码用作mex函数。 我的问题始于我尝试使用OpenMP库时。 我试图使矩阵逆的两部分并行,但我得到了许多编译错误。 注释pragmas时,代码编译是干净的。 非常感谢你的帮助 /*========================================================== * inv_and_mul_3by3.c * inverse 3x3 matrix and multiply it by anoth
/*==========================================================
* inv_and_mul_3by3.c
* inverse 3x3 matrix and multiply it by another 3x3 matrix
* The calling syntax is: outMatrix = (mat_to_inv,mat_to_mul)
*========================================================*/
#include "mex.h"
#include <omp.h>
/* The computational routine */
void inv_and_mul_3by3_omp(double *mat_to_mul, double *mat_to_inv, double *out)
{
// Description : out = inv(mat_to_inv)*mat_to_mul
double det;
double det_2by2;
double det_2by2B;
double inversed[9];
// Calculating 2x2 determinant that is being calculated for the 3x3 determinant
// and also for matrix inversion
det_2by2 = mat_to_inv[4]*mat_to_inv[8] - mat_to_inv[7]*mat_to_inv[5];
det_2by2B = mat_to_inv[1]*mat_to_inv[5] - mat_to_inv[4]*mat_to_inv[2];
/* Calculate the matrix deteminant */
det = mat_to_inv[0]*(det_2by2) - mat_to_inv[3]*(mat_to_inv[1]*mat_to_inv[8]-mat_to_inv[7]*mat_to_inv[2])+mat_to_inv[6]*(det_2by2B);
#pragma omp parallel sections
{
#pragma omp section {
// Calcualte the inversed matrix
inversed[0] = (det_2by2)/det;
inversed[3] = (mat_to_inv[6]*mat_to_inv[5] - mat_to_inv[3]*mat_to_inv[8])/det;
inversed[6] = (mat_to_inv[3]*mat_to_inv[7] - mat_to_inv[6]*mat_to_inv[4])/det;
inversed[1] = (mat_to_inv[7]*mat_to_inv[2] - mat_to_inv[1]*mat_to_inv[8])/det;
}
#pragma omp section {
inversed[4] = (mat_to_inv[0]*mat_to_inv[8] - mat_to_inv[6]*mat_to_inv[2])/det;
inversed[7] = (mat_to_inv[6]*mat_to_inv[1] - mat_to_inv[0]*mat_to_inv[7])/det;
inversed[2] = (det_2by2B)/det;
inversed[5] = (mat_to_inv[3]*mat_to_inv[2] - mat_to_inv[0]*mat_to_inv[5])/det;
inversed[8] = (mat_to_inv[0]*mat_to_inv[4] - mat_to_inv[3]*mat_to_inv[1])/det;
}
} /*-- End of sections block --*/
// multiply the matrix by the the matrix that was inversed
out[0] = mat_to_mul[0]*inversed[0] + mat_to_mul[3]*inversed[1] + mat_to_mul[6]*inversed[2];
out[1] = mat_to_mul[1]*inversed[0] + mat_to_mul[4]*inversed[1] + mat_to_mul[7]*inversed[2];
out[2] = mat_to_mul[2]*inversed[0] + mat_to_mul[5]*inversed[1] + mat_to_mul[8]*inversed[2];
out[3] = mat_to_mul[0]*inversed[3] + mat_to_mul[3]*inversed[4] + mat_to_mul[6]*inversed[5];
out[4] = mat_to_mul[1]*inversed[3] + mat_to_mul[4]*inversed[4] + mat_to_mul[7]*inversed[5];
out[5] = mat_to_mul[2]*inversed[3] + mat_to_mul[5]*inversed[4] + mat_to_mul[8]*inversed[5];
out[6] = mat_to_mul[0]*inversed[6] + mat_to_mul[3]*inversed[7] + mat_to_mul[6]*inversed[8];
out[7] = mat_to_mul[1]*inversed[6] + mat_to_mul[4]*inversed[7] + mat_to_mul[7]*inversed[8];
out[8] = mat_to_mul[2]*inversed[6] + mat_to_mul[5]*inversed[7] + mat_to_mul[8]*inversed[8];
} //end function
/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *inMatToInv; /* 3x3 input matrix that is inversed */
double *inMatToMul; /* 3x3 input matrix that multiply the inversed matrix*/
double *outMatrix; /* 3x3 output matrix */
/* create pointers to the real data in the input matrix */
inMatToInv = mxGetPr(prhs[0]);
inMatToMul = mxGetPr(prhs[1]);
/* create the output matrix */
plhs[0] = mxCreateDoubleMatrix(3,3,mxREAL);
/* get a pointer to the real data in the output matrix */
outMatrix = mxGetPr(plhs[0]);
/* call the computational routine */
inv_and_mul_3by3_omp(inMatToInv,inMatToMul,outMatrix);
}
/*==========================================================
*inv_和_mul_3by3.c
*求3x3矩阵的逆,并将其与另一个3x3矩阵相乘
*调用语法为:outMatrix=(mat_to_inv,mat_to_mul)
*========================================================*/
#包括“mex.h”
#包括
/*计算程序*/
通过3个omp作废库存和库存(双*物料到库存,双*物料到库存,双*出库)
{
//描述:输出=库存(物料到库存)*物料到多个
双检测器;
双Det2by2;
双数据单元2by2B;
双倒[9];
//计算为3x3行列式计算的2x2行列式
//还有矩阵求逆
det_2by2=物料至存货[4]*物料至存货[8]-物料至存货[7]*物料至存货[5];
det_2by2B=物料至存货[1]*物料至存货[5]-物料至存货[4]*物料至存货[2];
/*计算矩阵决定式*/
det=mat_to_inv[0]*(det_2by2)-mat_to_inv[3]*(mat_to_inv[1]*mat_to_inv[8]-mat_to_inv[7]*mat_to_inv[2])+mat to_inv[6]*(det_2by2by2b);
#pragma-omp平行截面
{
#pragma-omp段{
//求逆矩阵
逆[0]=(det2by2)/det;
逆[3]=(物料至存货[6]*物料至存货[5]-物料至存货[3]*物料至存货[8])/det;
逆[6]=(物料至存货[3]*物料至存货[7]-物料至存货[6]*物料至存货[4])/det;
逆[1]=(物料至存货[7]*物料至存货[2]-物料至存货[1]*物料至存货[8])/det;
}
#pragma-omp段{
逆[4]=(物料到物料库存[0]*物料到物料库存[8]-物料到物料库存[6]*物料到物料库存[2])/det;
逆[7]=(物料至存货[6]*物料至存货[1]-物料至存货[0]*物料至存货[7])/det;
逆[2]=(det2by2b)/det;
逆[5]=(物料到物料库存[3]*物料到物料库存[2]-物料到物料库存[0]*物料到物料库存[5])/det;
逆[8]=(物料到物料库存[0]*物料到物料库存[4]-物料到物料库存[3]*物料到物料库存[1])/det;
}
}/*--段末块--*/
//将矩阵乘以逆矩阵
out[0]=mat_to_mul[0]*反向[0]+mat_to_mul[3]*反向[1]+mat_to_mul[6]*反向[2];
out[1]=mat_to_mul[1]*反向[0]+mat_to_mul[4]*反向[1]+mat_to_mul[7]*反向[2];
out[2]=mat_to_mul[2]*反向[0]+mat_to_mul[5]*反向[1]+mat_to_mul[8]*反向[2];
out[3]=mat_to_mul[0]*反向[3]+mat_to_mul[3]*反向[4]+mat_to_mul[6]*反向[5];
out[4]=mat_to_mul[1]*反向[3]+mat_to_mul[4]*反向[4]+mat_to_mul[7]*反向[5];
out[5]=mat_to_mul[2]*反向[3]+mat_to_mul[5]*反向[4]+mat_to_mul[8]*反向[5];
out[6]=mat_to_mul[0]*反向[6]+mat_to_mul[3]*反向[7]+mat_to_mul[6]*反向[8];
out[7]=mat_to_mul[1]*反向[6]+mat_to_mul[4]*反向[7]+mat_to_mul[7]*反向[8];
out[8]=mat_to_mul[2]*反向[6]+mat_to_mul[5]*反向[7]+mat_to_mul[8]*反向[8];
}//结束函数
/*网关功能*/
void MEX函数(整数nlhs,mxArray*plhs[],
整数nrhs,常量mxArray*prhs[]
{
双*inMatToInv;/*3x3输入矩阵,逆矩阵*/
双*inMatToMul;/*3x3输入矩阵,与逆矩阵相乘*/
双*输出矩阵;/*3x3输出矩阵*/
/*创建指向输入矩阵中真实数据的指针*/
inMatToInv=mxGetPr(prhs[0]);
inMatToMul=mxGetPr(prhs[1]);
/*创建输出矩阵*/
plhs[0]=mxCreateDoubleMatrix(3,3,mxREAL);
/*获取指向输出矩阵中实际数据的指针*/
outMatrix=mxGetPr(plhs[0]);
/*调用计算例程*/
inv_和INU mul_3by3_omp(inMatToInv、inMatToMul、outMatrix);
}
这是我在Matlab中使用的编译命令mex inv_和_mul_3by3_omp.c COMPFLAGS=“/openmp$COMPFLAGS” 以下是编译错误: 使用mex时出错 inv_和_mul_3BY 3公司c (32):错误C3005:“{”:在OpenMP上遇到意外令牌 “部分”指令
(35):错误C3047:OpenMP“部分”中的结构化块 区域前面必须加“#pragma omp section”
(36):错误C3047:OpenMP“部分”中的结构化块 区域前面必须加“#pragma omp section”
(37):错误C3047:OpenMP“部分”中的结构化块 区域前面必须加“#pragma omp section”
(40):错误C3005:“{”:在OpenMP上遇到意外令牌 “部分”指令
(40):错误C3044:“节”:仅允许直接嵌套 在OpenMP“部分”指令下
(47):错误C2059:语法错误:'}'
(53):错误C2065:“mat_to_mul”:未声明的标识符
(53):错误C2109:下标需要数组或指针类型
(53):错误C2065:“反向”:未声明的标识符
(54):错误C2369:“out”:重新定义;不同的下标
(53):参见“退出”声明
(54):错误C2065:“mat_to_mul”:未声明的标识符
(54):错误C2109:下标需要数组或指针类型
(54):错误C2065:“反向”:未声明的标识符
(55):错误C2369:“out”:重新定义;不同的下标
(53):参见“退出”声明
(55):错误C2065:“mat_to_mul”:未声明的标识符
(55):错误C2109:下标需要数组或指针类型
(55):错误C2065:“反向”:未声明的标识符
(56):错误C2369:“out”:重新定义;不同的下标
(53):参见“退出”声明
(56):错误C2065:“mat_to_mul”:未声明的标识符
(56):错误C2109:下标需要数组或指针类型
(56):错误C2065:'在
#pragma omp parallel
{
#pragma omp parallel sections num_threads(2)
#pragma omp parallel sections
#pragma omp section {
#pragma omp section
{