C++ engPutVariable() memory limitation
조회 수: 1 (최근 30일)
이전 댓글 표시
It seems that engPutVariable has memory limitation and can't send big matrices. Withing matlab I have no problem allocating:
x = rand(259778664,3);
but the code below crashes:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "engine.h"
#include <iostream>
#include <cmath>
#include <cfloat>
#include <fstream>
#include <conio.h>
using namespace std;
int main()
{
Engine *ep;
if (!(ep = engOpen(""))) {
fprintf(stderr, "\nCan't start MATLAB engine\n");
return EXIT_FAILURE;
}
unsigned *rowind;
unsigned *colind;
double *vals;
size_t n;
if ( 0 ) {
} else {
n = 259778664; //43296444;
rowind = new unsigned[n];
colind = new unsigned[n];
vals = new double[n];
}
cout << "n=" << n << endl;
mxArray *M = mxCreateDoubleMatrix(n, 3, mxREAL);
double *pM = mxGetPr(M);
for (size_t i = 0; i < n; ++i)
{
pM[0*n+i] = double(rowind[i]+1);
pM[1*n+i] = double(colind[i]+1);
pM[2*n+i] = double( vals[i] );
}
delete[] rowind;
delete[] colind;
delete[] vals;
for ( int i = 0 ; i < 4 ; i++ ) {
char s[100];
sprintf(s, "M%d", i);
cout << s << endl;
cout << "Press a key to load.." << endl;
getch();
int res = engPutVariable(ep, s, M);
}
mxDestroyArray(M);
engClose(ep);
return EXIT_SUCCESS;
}
댓글 수: 2
Ken Atwell
2015년 1월 14일
Does it crash on the first loop iteration, a later loop iteration, or somewhere else? In other words, how much output to you get before the crash?
And, at the risk of asking the obvious, you're using 64-bit MATLAB and a 64-bit compiler, right? What platform are you on?
답변 (2개)
Titus Edelhofer
2015년 1월 14일
Hi,
I'm not sure but I guess the engPutVariable needs to copy the matrix from your C application to MATLAB. The two applications can't share the variable. Therefore the question is, if your machine is able to have two matrices of about 6 GB in memory...?
Titus
댓글 수: 2
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!