C++ engPutVariable() memory limitation
Show older comments
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 Comments
Ken Atwell
on 14 Jan 2015
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?
Zohar
on 14 Jan 2015
Answers (2)
Titus Edelhofer
on 14 Jan 2015
0 votes
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 Comments
Zohar
on 14 Jan 2015
Titus Edelhofer
on 14 Jan 2015
O.k., then this is not the problem ;-)
Zohar
on 16 Jan 2015
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!