- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个 mex 文件,在计算 mex 时,每次运行 MATLAB 都会崩溃。这是我编写的 C++ 代码的一部分:
void mexFunction(mwSize nlhs, mxArray *plhs[],
mwSize nrhs, const mxArray *prhs[]){
vector<int> *NNLt;
double *NNLtout;
Vector *V;
Vector *Fb;
mwSize *sn;
mwSize nsn;
mwSize nf;
double hs;
double bw;
double mw;
mwSize ncols;
mwSize i;
double *NNLtoutt;
/* check for proper number of arguments */
if(nrhs!=9) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","Nine inputs required.");
}
if(nlhs!=1) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs","One output required.");
}
/* get the value of the scalar input */
nsn = mxGetScalar(prhs[4]);
nf = mxGetScalar(prhs[5]);
hs = mxGetScalar(prhs[6]);
bw = mxGetScalar(prhs[7]);
mw = mxGetScalar(prhs[8]);
/* create a pointer to the real data in the input matrix */
NNLt = (vector<int> *)mxGetData(prhs[0]);
V = (Vector *)mxGetData(prhs[1]);
Fb =(Vector *)mxGetData(prhs[2]);
sn = (mwSize *)mxGetData(prhs[3]);
/* call the computational routine */
createNNLtriangle(NNLt, V, Fb, sn, nsn, nf, hs, bw, mw);
/* create the output matrix */
plhs[0] = mxCreateCellMatrix(nsn,50);
for(i=0;i<nsn;i++){
mxArray* tmp = mxCreateDoubleMatrix(1, NNLt[i].size(), mxREAL);
copy(NNLt[i].begin(), NNLt[i].end(), mxGetPr(tmp));
mxSetCell(plhs[0], i, tmp);
mxFree(tmp);
}}
所以,我收到以下错误:
------------------------------------------------------------------------
Segmentation violation detected at Thu Aug 2 14:41:25 2018
------------------------------------------------------------------------
...
Stack Trace (from fault):
[ 0] 0x00007f0b0c635426 /home/user/dir/createNNLtriangle.mexa64+00005158
[ 1] 0x00007f0b0c635893 /home/user/dir/createNNLtriangle.mexa64+00006291 mexFunction+00000240
[ 2] 0x00007f0c562cac4a /home/user/bin/glnxa64/libmex.so+00142410 mexRunMexFile+00000090
...
对于我们造成的困惑,我们深表歉意,这里是我同事的澄清和其他问题。
您可以了解我们如何定义“NNLt”。这里的“tmp”是什么意思?
std::copy(mxGetPr(tmp), mxGetPr(tmp)+N, NNLt.begin());
下面是 Mexfile 中位于 MexFunction 之前的 cpp 函数:
#include "mex.h"
#include "matrix.h"
#include <omp.h>
#include "vema.h"s
#include "eig3.h"
#include <stdlib.h>
#include <iostream>
#include <cmath>
#include <vector>
#include <fstream>
#include <iomanip>
#include <sys/types.h>
#include <sys/stat.h>
using namespace std;
Vector closestPointTriangle(Vector&, Vector&, Vector&, Vector&, double&, double&, double&);
// Generates pomwSize-triangle proximity lists using the linked cell algorithm
void createNNLtriangle(vector<int>* NNLt, Vector* Ut, Vector* faces, int* SN, mwSize nsn, mwSize nf, double hs, double bw, double mw) {
int mx = max(1, (int)(bw/mw)); // ** = 40 cells bw=3.2, mw=0.08
vector<int> head(mx*mx*mx, -1);
vector<int> list(nf);
// std::vector<int> head(mx*mx*mx, -1); //****** mx*mx*mx cells nomber, size mx*mx*mx vector with all values are -1, 40*40*40 = 64000
// std::vector<int> list(nf); // **** nf = 101882
int xa, ya, za, xi, yi, zi;
double ub, vb, wb;
int pt, tri;
Vector cog;
for (int i = 0; i < nf; i++) { // Divide triangle faces mwSizeo cells, i index of face
//Vector cog = (Ut[faces[i].n1] + Ut[faces[i].n2] + Ut[faces[i].n3])/3.0;
cog = (Ut[(int)faces[i].x] + Ut[(int)faces[i].y] + Ut[(int)faces[i].z])/3.0;
int xa = (int)((cog.x + 0.5*bw)/bw*mx);
int ya = (int)((cog.y + 0.5*bw)/bw*mx);
int za = (int)((cog.z + 0.5*bw)/bw*mx);
int tmp = mx*mx*za + mx*ya + xa; // *** 1641838 > 64000
list[i]=head[mx*mx*za + mx*ya + xa];
head[mx*mx*za + mx*ya + xa] = i;
}
#pragma omp parallel for
for (int i = 0; i < nsn; i++) { // Search cells around each pomwSize and build proximity list
int pt = SN[i];
NNLt[i].clear();
int xa = (int)((Ut[pt].x + 0.5*bw)/bw*mx);
int ya = (int)((Ut[pt].y + 0.5*bw)/bw*mx);
int za = (int)((Ut[pt].z + 0.5*bw)/bw*mx);
for (int xi = max(0, xa-1); xi <= min(mx-1, xa+1); xi++)// *** Browse head list
for (int yi = max(0, ya-1); yi <= min(mx-1, ya+1); yi++)
for (int zi = max(0, za-1); zi <= min(mx-1, za+1); zi++) {
int tri = head[mx*mx*zi + mx*yi + xi];
while (tri != -1) {
if ( pt != (int)faces[tri].x && pt != (int)faces[tri].y && pt != (int)faces[tri].z ) {
if ( (closestPointTriangle(Ut[pt], Ut[(int)faces[tri].x], Ut[(int)faces[tri].y], Ut[(int)faces[tri].z], ub, vb, wb) - Ut[pt]).length() < hs) {
NNLt[i].push_back(tri);
}
}
tri = list[tri];
}
}
}
}
// Returns the closest pomwSize of triangle abc to pomwSize p ***** a or b or c, if not, pt projection through the barycenter inside the triangle
Vector closestPointTriangle(Vector& p, Vector& a, Vector& b, Vector& c, double& u, double& v, double& w) {
Vector ab = b - a;
Vector ac = c - a;
Vector ap = p - a;
double d1 = ab.dot(ap);
double d2 = ac.dot(ap);
if (d1 <= 0.0 && d2 <= 0.0) {
u = 1.0;
v = 0.0;
w = 0.0;
return a;
}
Vector bp = p - b;
double d3 = ab.dot(bp);
double d4 = ac.dot(bp);
if (d3 >= 0.0 && d4 <= d3) {
u = 0.0;
v = 1.0;
w = 0.0;
return b;
}
double vc = d1*d4 - d3*d2;
if (vc <= 0.0 && d1 >= 0.0 && d3 <= 0.0) {
v = d1 / (d1 - d3);
u = 1.0 - v;
w = 0.0;
return a + ab * v;
}
Vector cp = p - c;
double d5 = ab.dot(cp);
double d6 = ac.dot(cp);
if (d6 >= 0.0 && d5 <= d6) {
u = 0.0;
v = 0.0;
w = 1.0;
return c;
}
double vb = d5*d2 - d1*d6;
if (vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0) {
w = d2 / (d2 - d6);
u = 1.0 - w;
v = 0.0;
return a + ac * w;
}
double va = d3*d6 - d5*d4;
if (va <= 0.0 && (d4 - d3) >= 0.0 && (d5 - d6) >= 0.0) {
w = (d4 - d3) / ((d4 - d3) + (d5 - d6));
u = 0.0;
v = 1.0 - w;
return b + (c - b) * w;
}
double denom = 1.0 / (va + vb + vc);
v = vb * denom;
w = vc * denom;
u = 1.0 - v - w;
return a + ab * v + ac * w;
}
下面是在Matlab中调用Mexfile的部分:
NNLt = cell(1,nsn);
V = A(1:n,1:3); //A: Matrix
Fb = A(n:2*n,1:3);
nf = size(Fb,1);
sn = B(1,:); //B: Matrix
parfor i = 1:nsn
maxDist = max(maxDist, length(V(sn(i),:) - Vtold(i,:)));
end
if maxDist > 0.5*(hs-hc)
[NNLt] = createNNLtriangle(NNLt, V, Fb, sn, nsn, nf, hs, bw, mw);
for i = 1:nsn
Vtold(i,:) = V(sn(i),:);
end
end
最后,这是用于定义 V 和 Fb 的“Vector*”类:
class Vector{
public:
double x, y, z;
Vector(): x(0.0), y(0.0), z(0.0) {};
Vector(double ax, double ay, double az): x(ax), y(ay), z(az) {};
double length(){
return sqrt(x*x + y*y + z*z);
}
double dot(const Vector& b){
return x*b.x + y*b.y + z*b.z;
}
Vector cross(const Vector& b){
return Vector(y*b.z - z*b.y, z*b.x - x*b.z, x*b.y - y*b.x);
}
void normalize(){
double temp = 1.0/length();
x *= temp;
y *= temp;
z *= temp;
}
void clear(){
x = y = z = 0.0;
}
Vector& operator+= (const Vector& b){
x += b.x;
y += b.y;
z += b.z;
return *this;
}
Vector& operator-= (const Vector& b){
x -= b.x;
y -= b.y;
z -= b.z;
return *this;
}
Vector& operator*= (const double& c){
x *= c;
y *= c;
z *= c;
return *this;
}
Vector& operator/= (const double& c){
x /= c;
y /= c;
z /= c;
return *this;
}
Vector operator+ (const Vector& b){
Vector r = *this;
return r += b;
}
Vector operator- (const Vector& b){
Vector r = *this;
return r -= b;
}
Vector operator* (const double& c){
Vector r = *this;
return r *= c;
}
Vector operator/ (const double& c){
Vector r = *this;
return r /= c;
}
};
再次抱歉!谢谢,干杯
最佳答案
作为rahnema1 said in a comment , 你不能转换 mxGetData
的输出到您需要的任何指针类型。此输出指向 mxArray
的数据,你需要这样阅读它。
例如,
NNLt = (vector<int> *)mxGetData(prhs[0]);
A vector<int>
是特定的 C++ 数据结构,您正在将 MATLAB 数组中的数据重新解释为 C++ 数据结构。此数据结构的元素之一是指向数据的指针,因此您正在将(大概) double 浮点值重新解释为指针,然后访问该指针,这显然会导致崩溃。
对于 Vector*
的转换也是如此,虽然我不知道这个类是什么。
相反,首先确保您的输入类型正确,然后复制数据:
if(!mxIsDouble(prhs[0])) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","First input must be a double array.");
}
std::size_t N = mxGetNumberOfElements(prhs[0]);
std::vector<int> NNLt(N);
std::copy(mxGetPr(prhs[0]), mxGetPr(prhs[0])+N, NNLt.begin());
但是,从您的其余代码来看,它似乎是 NNLt
应该是 vector 的 vector ,而不是单个 vector 。是 createNNLtriangle
函数取自 here ?这看起来像是 C 和 C++ 的可怕破坏......
也许你需要在这里做这样的事情:
if(!mxIsDouble(prhs[0])) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs","First input must be a double array.");
}
std::size_t M = mxGetM(prhs[0]); // number of rows
std::size_t N = mxGetN(prhs[0]); // number of columns
std::vector<std::vector<int>> NNLt(N, std::vector<int>(M));
double* ptr = mxGetPr(prhs[0]);
for(std::size_t ii = 0; ii < N; ++ii) {
std::copy(ptr, ptr+M, NNLt[ii].begin());
ptr += M;
}
// mwSize nsn = static_cast<mwSize>(mxGetScalar(prhs[4])); // Ignore this value!
// ... read the other values here in the same way
createNNLtriangle(NNLt.data(), V, Fb, sn, N, nf, hs, bw, mw);
// ^^^^^^^^^^^ ^--- NOTE!
还要注意第三个参数,Fb
, 是一个 vector<Face>
, 不是 Vector*
.你必须弄清楚如何转换 Vector*
和 vector<Face>
,它将涉及相当多的代码,而不仅仅是转换指针!
第 4 个参数是 int*
, 不是 mwSize*
:
sn = (mwSize *)mxGetData(prhs[3]);
您需要确保 prhs[3]
实际上包含一个 int32
矩阵,那么你可以将它的指针指向一个 int*
.否则,您必须像我们上面那样复制数据。
还要仔细检查所有其他参数,以确保您传递的类型正确。设置你的编译器来警告你所有的隐式转换,然后注意所有这些警告。这些警告中的每一个都是导致程序在运行时崩溃的潜在原因。全部修复!
关于c++ - 运行 MEX 文件时 MATLAB 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51655201/
我试图通过用 C++ 编写一些函数并使用 mex 接口(interface)来集成它们来加速我的 Matlab 程序。我在 C++ 的向量中得到了我的结果。我想将它传输到 MATLAB 中的数组。我知
我用 C++ 编写了带有服务器和客户端的 TCPIP 套接字连接,它在 VisualStudio 中工作得很好。现在我想通过 MEX 文件在 MATLAB/Simulink 中使用 C++ - 客户端
我有返回 C++ 主机端数组的 cuda/C++ 代码。我想在 MATLAB 中操作这些数组,所以我用 mex 格式重写了我的代码并用 mex 编译。 我通过将预分配的数组从 MATLAB 传递到 m
我正在尝试将一段代码从 Matlab 转换为 python。我正在运行 Ubuntu 16.04LTS,需要依靠 Octave 来运行引用代码(Matlab 安装时出现深奥错误)。无论如何,以前从未使
我使用 VS2010 C-编译器在 Windows8 上开发了一个 Matlab mex 文件。很长一段时间,一切都很顺利…… 但是现在,mex 文件会阻止代码中的所有更改。无论我更改哪一行代码,重建
我的 C 代码应该将 Matlab 稀疏格式转换为 TAUCS format 也是列的主要格式。 当然,我是在 Matlab 本身生成 Matlab 稀疏格式,然后将其传输到 mex 文件。 代码编译
这是我编写的用于在制表符分隔文件中读取的 mex 代码。 mex 文件已创建,但它导致我的 MATLAB 突然结束并给出以下错误。谁能帮助我哪里出错了?如果需要任何进一步的信息,请告诉我 异常终止:分
我有一个 Matlab mex 函数,它重复调用名为 calculate(). 的 C 函数我制作了两个版本的函数: 版本A:每次mex()来电 calculate() ,它只传递输入参数,以及cal
我正在尝试在 MATLAB 中构建一个 mex 函数。该函数依赖于 C++ 库。但是,无论我做什么,我都会在 MATLAB 中得到 Unresolved external 问题。我创建了三个简单的文件
我正在尝试从 C++ 源代码编译一些 MATLAB MEX 文件。我正在尝试编译的文件 can be found here ;我在 32 位系统 MATLAB 2012a 上使用 Windows XP
我使用 mxCreateSparse 在 MEX 中创建了一个稀疏矩阵. mxArray *W; W=mxCreateSparse(n*n,n*n,xsize,mxREAL); double *wpo
我正在尝试在 C 语言的 MEX 文件中实现一些基本的线性代数例程以进行练习,但我被点积困住了。这是我到目前为止所拥有的: #define char16_t UINT16_T //shenanigan
我是编写 MEX 函数的新手,我有内存问题。 MEXf 逍遥法外的套路如下: void mexFunction (int nlhs, mxArray *plhs[], int nrhs,const m
我这里有一个可以正确执行的 C mex 文件,但是在执行完成后 MATLAB 因段错误而崩溃。由于它在程序完成执行后崩溃,这让我认为 MATLAB 自动释放分配的内存导致了这个问题。但是,我释放了我自
我有一个简单的 mex 函数,它从库中调用另一个 C++ 函数。我用编译源代码 mex -cxx mymexfunction.cpp -I/some/include -L/some/lib -lmyl
我正在寻找一种算法来找到 mex但除了这个 wiki 链接,找不到任何有用的东西。 看完后我拉出这段代码: nList = [int(x) for x in input().split()] nLis
我希望仅在通过 Matlab 中的 mex 命令编译我的代码时包含某个头文件。如果它是直接用 Visual Studio 编译的,我不希望包含它。 是否有一个宏可以帮助解决这个问题? 我想做这样的事情
如何在用 C 编写的 MEX 文件中创建二维稀疏矩阵。创建矩阵后如何像在 C 中一样单独访问元素,比如 mat[i][j]? 我厌倦了使用 mxCreateNumericArray函数,但我无法访问元
我正在尝试编译 a matlab wrapper for libdc1394这是一个用于火线相机的库。我收到一个奇怪的错误,涉及头文件中的一些内联函数。我正在使用 gcc-4.6 开发 ubuntu
我需要尽快将大量数据写入磁盘。在 MATLAB 中,我可以使用 fwrite 来做到这一点: function writeBinaryFileMatlab(data) fid = fopen(
我是一名优秀的程序员,十分优秀!