C++ engPutVariable() memory limitation
    10 次查看(过去 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
另请参阅
类别
				在 Help Center 和 File Exchange 中查找有关 Call MATLAB from C 的更多信息
			
	产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


