Example #1
0
/*
 * check_nargin --- interface to IPT checknargin function for 
 * checking for the proper number of input arguments.
 */
void check_nargin(double low, 
                  double high, 
                  int numInputs, 
                  const char *function_name)
{
    mxArray *prhs[4];
    mxArray *plhs[1];
    int nlhs = 0;
    int nrhs = 4;

    plhs[0] = NULL;

    prhs[0] = mxCreateScalarDouble((double) low);
    prhs[1] = mxCreateScalarDouble((double) high);
    prhs[2] = mxCreateScalarDouble((double) numInputs);
    prhs[3] = mxCreateString(function_name);

    mexCallMATLAB(nlhs, plhs, nrhs, prhs, "checknargin");
    
    mxDestroyArray(prhs[0]);
    mxDestroyArray(prhs[1]);
    mxDestroyArray(prhs[2]);
    mxDestroyArray(prhs[3]);

    if (plhs[0] != NULL)
    {
        mxDestroyArray(plhs[0]);
    }
}
Example #2
0
/*
 * check_input --- interface to IPT checkarray function for
 * checking validity of input arguments.
 */
void check_input(const mxArray *A,
                 const char    *classes,
                 const char    *attributes,
                 const char    *function_name,
                 const char    *variable_name,
                 int           argument_position)
{
    mxArray *prhs[6];
    mxArray *plhs[1];
    int nlhs = 0;
    int nrhs = 6;
        
    prhs[0] = (mxArray *) A;
    prhs[1] = mxCreateString(classes);
    prhs[2] = mxCreateString(attributes);
    prhs[3] = mxCreateString(function_name);
    prhs[4] = mxCreateString(variable_name);
    prhs[5] = mxCreateScalarDouble((double) argument_position);

    plhs[0] = NULL;
    
    mexCallMATLAB(nlhs, plhs, nrhs, prhs, "checkinput");

    mxDestroyArray(prhs[1]);
    mxDestroyArray(prhs[2]);
    mxDestroyArray(prhs[3]);
    mxDestroyArray(prhs[4]);
    mxDestroyArray(prhs[5]);

    if (plhs[0] != NULL)
    {
        mxDestroyArray(plhs[0]);
    }
}
Example #3
0
/*
 * Returns pointer to GPUmat structure
 */
GPUmat * gmGetGPUmat() {
  // tmp
  mxArray *lhs[2];
  mexCallMATLAB(1, &lhs[0], 0, NULL, "GPUmodulesManager");
  GPUmat *gm = (GPUmat *) (UINTPTR mxGetScalar(lhs[0]));
  return gm;
}
int mxW_CVodeBBDgcom(long int Nlocal, realtype t, N_Vector y, void *user_data)
{
  cvmPbData fwdPb;
  mxArray *mx_in[4], *mx_out[2];
  int ret;

  /* Extract global interface data from user-data */
  fwdPb = (cvmPbData) user_data;

  /* Inputs to the Matlab function */
  mx_in[0] = mxCreateDoubleScalar(t);           /* current t */
  mx_in[1] = mxCreateDoubleMatrix(N,1,mxREAL);  /* current y */
  mx_in[2] = fwdPb->GCOMfct;                    /* matlab function handle */
  mx_in[3] = fwdPb->mtlb_data;                  /* matlab user data */
  
  /* Call matlab wrapper */
  GetData(y, mxGetPr(mx_in[1]), N);

  mexCallMATLAB(2,mx_out,4,mx_in,"cvm_gcom");

  ret = (int)*mxGetPr(mx_out[0]);

  if (!mxIsEmpty(mx_out[1])) {
    UpdateUserData(mx_out[1], fwdPb);
  }

  /* Free temporary space */
  mxDestroyArray(mx_in[0]);
  mxDestroyArray(mx_in[1]);
  mxDestroyArray(mx_out[0]);
  mxDestroyArray(mx_out[1]);

  return(ret);
}
void mexFunction( int nlhs, mxArray *plhs[], 
		  int nrhs, const mxArray*prhs[] )
     
{
    double cellSurfaceAreaToVolumeRatio = mxGetScalar( mxGetField(prhs[0], 0, "cellSurfaceAreaToVolumeRatio") );
    double cellMembraneCapacitance = mxGetScalar( mxGetField(prhs[0], 0, "cellMembraneCapacitance") );
    double leftConductivity = mxGetScalar( mxGetField(prhs[0], 0, "leftConductivity") );
    double pdeTimeStep = mxGetScalar( mxGetField(prhs[0], 0, "pdeTimeStep") );
    double spaceStep = mxGetScalar( mxGetField(prhs[0], 0, "spaceStep") );
    double rightConductivity = mxGetScalar( mxGetField(prhs[0], 0, "rightConductivity") );
    unsigned numberCells = mxGetScalar( mxGetField(prhs[0], 0, "numberCells") );
        
    double surfaceRatioTimesCapacitance = cellSurfaceAreaToVolumeRatio * cellMembraneCapacitance;
    double leftCondutivityTimesTimeStepOverSpaceStepSquared = leftConductivity * pdeTimeStep / (spaceStep*spaceStep);
    double rightCondutivityTimesTimeStepOverSpaceStepSquared = rightConductivity * pdeTimeStep / (spaceStep*spaceStep);        
    
    // Create a dense matrix
    mxArray* denseMatrix = mxCreateDoubleMatrix(numberCells, numberCells, mxREAL);
    
    // Get a C pointer to the dense matrix 
    double *systemMatrix = mxGetPr(denseMatrix);

    unsigned rowIndex;
    double condutivityTimesTimeStepOverSpaceStepSquared;
    
    // Fill in matrix row 0
    acces_2d(systemMatrix,0,0,numberCells) = surfaceRatioTimesCapacitance + 2*leftCondutivityTimesTimeStepOverSpaceStepSquared;
    acces_2d(systemMatrix,0,1,numberCells) = -2*leftCondutivityTimesTimeStepOverSpaceStepSquared;

    // Fill in rows 1 to numberCells-2
    for (rowIndex=1;rowIndex<numberCells-1;rowIndex++)
    {
        // Determine which conductivity to use (left half or right half)
        if (rowIndex < (numberCells)/2)
        {
            condutivityTimesTimeStepOverSpaceStepSquared = leftCondutivityTimesTimeStepOverSpaceStepSquared;
        }
        else
        {
            condutivityTimesTimeStepOverSpaceStepSquared = rightCondutivityTimesTimeStepOverSpaceStepSquared;
        }
    
        acces_2d(systemMatrix,rowIndex,rowIndex-1,numberCells) = -condutivityTimesTimeStepOverSpaceStepSquared;
        acces_2d(systemMatrix,rowIndex,rowIndex,numberCells) = surfaceRatioTimesCapacitance + 2*condutivityTimesTimeStepOverSpaceStepSquared;
        acces_2d(systemMatrix,rowIndex,rowIndex+1,numberCells) = -condutivityTimesTimeStepOverSpaceStepSquared;
    }

    // Fill in row numberCells-1
    acces_2d(systemMatrix,numberCells-1,numberCells-2,numberCells) = -2*rightCondutivityTimesTimeStepOverSpaceStepSquared;
    acces_2d(systemMatrix,numberCells-1,numberCells-1,numberCells) = surfaceRatioTimesCapacitance + 2*rightCondutivityTimesTimeStepOverSpaceStepSquared;    
    
    // Do the conversion from dense to sparse (the output goes into plhs, ready to be returned by the MEX function)
    mexCallMATLAB(1,&plhs[0],1,&denseMatrix,"sparse");
    
    // We don't need the dense matrix anymore
    mxDestroyArray(denseMatrix);
    
    return;
}
int mtlb_IdaDenseJacB(long int NeqB, realtype tt,
                      N_Vector yy, N_Vector yp,
                      N_Vector yyB, N_Vector ypB, N_Vector rrB,
                      realtype c_jB, void *jac_dataB,
                      DenseMat JacB,
                      N_Vector tmp1B, N_Vector tmp2B,
                      N_Vector tmp3B)
{
    double *JB_data;
    long int i;
    mxArray *mx_in[10], *mx_out[3];
    int ret;

    /* Inputs to the Matlab function */
    mx_in[0] = mxCreateScalarDouble(-1.0);        /* type=-1: backward ODE */
    mx_in[1] = mxCreateScalarDouble(tt);          /* current t */
    mx_in[2] = mxCreateDoubleMatrix(N,1,mxREAL);  /* current yy */
    mx_in[3] = mxCreateDoubleMatrix(N,1,mxREAL);  /* current yp */
    mx_in[4] = mxCreateDoubleMatrix(NB,1,mxREAL); /* current yyB */
    mx_in[5] = mxCreateDoubleMatrix(NB,1,mxREAL); /* current ypB */
    mx_in[6] = mxCreateDoubleMatrix(NB,1,mxREAL); /* current rrB */
    mx_in[7] = mxCreateScalarDouble(c_jB);        /* current c_jB */
    mx_in[8] = mx_JACfctB;                        /* matlab function handle */
    mx_in[9] = mx_data;                           /* matlab user data */

    /* Call matlab wrapper */
    GetData(yy, mxGetPr(mx_in[2]), N);
    GetData(yp, mxGetPr(mx_in[3]), N);
    GetData(yyB, mxGetPr(mx_in[4]), NB);
    GetData(ypB, mxGetPr(mx_in[5]), NB);
    GetData(rrB, mxGetPr(mx_in[6]), NB);

    mexCallMATLAB(3,mx_out,10,mx_in,"idm_djac");

    JB_data = mxGetPr(mx_out[0]);
    for (i=0; i<NB; i++)
        memcpy(DENSE_COL(JacB,i), JB_data + i*NB, NB*sizeof(double));

    ret = (int)*mxGetPr(mx_out[1]);

    if (!mxIsEmpty(mx_out[2])) {
        UpdateUserData(mx_out[2]);
    }

    /* Free temporary space */
    mxDestroyArray(mx_in[0]);
    mxDestroyArray(mx_in[1]);
    mxDestroyArray(mx_in[2]);
    mxDestroyArray(mx_in[3]);
    mxDestroyArray(mx_in[4]);
    mxDestroyArray(mx_in[5]);
    mxDestroyArray(mx_in[6]);
    mxDestroyArray(mx_in[7]);
    mxDestroyArray(mx_out[0]);
    mxDestroyArray(mx_out[1]);
    mxDestroyArray(mx_out[2]);

    return(ret);
}
Example #7
0
void libsvmwrite(const char *filename, const mxArray *label_vec, const mxArray *instance_mat)
{
	FILE *fp = fopen(filename,"w");
	int i, k, low, high, l;
	mwIndex *ir, *jc;
	int label_vector_row_num;
	double *samples, *labels;
	mxArray *instance_mat_col; // instance sparse matrix in column format

	if(fp ==NULL)
	{
		mexPrintf("can't open output file %s\n",filename);			
		return;
	}

	// transpose instance matrix
	{
		mxArray *prhs[1], *plhs[1];
		prhs[0] = mxDuplicateArray(instance_mat);
		if(mexCallMATLAB(1, plhs, 1, prhs, "transpose"))
		{
			mexPrintf("Error: cannot transpose instance matrix\n");
                        fclose(fp);
			return;
		}
		instance_mat_col = plhs[0];
		mxDestroyArray(prhs[0]);
	}

	// the number of instance
	l = (int) mxGetN(instance_mat_col);
	label_vector_row_num = (int) mxGetM(label_vec);

	if(label_vector_row_num!=l)
	{
		mexPrintf("Length of label vector does not match # of instances.\n");
		return;
	}

	// each column is one instance
	labels = mxGetPr(label_vec);
	samples = mxGetPr(instance_mat_col);
	ir = mxGetIr(instance_mat_col);
	jc = mxGetJc(instance_mat_col);

	for(i=0;i<l;i++)
	{
		fprintf(fp,"%g", labels[i]);

		low = (int) jc[i], high = (int) jc[i+1];
		for(k=low;k<high;k++)
			fprintf(fp," %ld:%g", ir[k]+1, samples[k]);		

		fprintf(fp,"\n");
	}

	fclose(fp);
	return;
}
Example #8
0
	static void callMatlab(string command){
		const int nlhs=0;
		const int nrhs=0;
		mxArray *plhs[1];//?
		mxArray *prhs[1];//?
		int res=mexCallMATLAB(nlhs,plhs,nrhs,prhs,command.c_str());
		assert(res==0);
	}
// call eval_fd from matlab
mxArray* call_eval_fd(mxArray* rng, const mxArray* fdobj, const mxArray* Lfdobj){
    mxArray *result[1];
    int nin;
    
    if (Lfdobj == NULL) {
        mxArray *input[2] = {rng, fdobj};
        nin = 2;
        mexCallMATLAB(1, result, nin, input, "eval_fd");
    }
    else {
        mxArray *input[3] = {rng, fdobj, Lfdobj};
        nin = 3;
        mexCallMATLAB(1, result, nin, input, "eval_fd");
    }
    
    return result[0];
}
Example #10
0
/**
 * Returns a pointer to the CUDA module
 */
CUmodule * gmGetModule(STRINGCONST char *modname) {
  mxArray *myfun;
  mxArray *tmplhs = mxCreateString(modname);
  mexCallMATLAB(1, &myfun, 1, &(tmplhs), "GPUgetUserModule");
  CUmodule *drvmod = (CUmodule*) (UINTPTR mxGetScalar(myfun));
  mxDestroyArray(myfun);
  return drvmod;
}
Example #11
0
// #############################################################################################################
double MatlabEval(const char *operation)
{
    /*wrapper for no input - single scalar output Matlab calls*/
    mxArray *out;
    mexCallMATLAB (1,&out,0,NULL,operation);
    return (double) mxGetScalar(out);
    /*Matlab clears the memory here by himself*/
}
Example #12
0
void wavepacket_to_matlab(const char *script, const int nrhs, mxArray *prhs[])
{
  if(!file_exist(script + Str(".m"))) return;
  
  std::cout << " Matlab script " << script << std::endl;

  insist(!mexCallMATLAB(0, NULL, nrhs, prhs, script));
}
Example #13
0
void wavepacket_to_matlab(const char *script)
{
  if(!file_exist(script + Str(".m"))) return;

  std::cout << " Matlab script " << script << std::endl;

  insist(!mexCallMATLAB(0, NULL, 0, NULL, script));
}
Example #14
0
// descriptor extraction
inline void BriskInterface::describe(int nlhs, mxArray *plhs[], 
            int nrhs, const mxArray *prhs[]){
    // in this case, the user is forced to pass two lhs args
    if(nlhs!=2) 
        mexErrMsgTxt("Two left-hand side arguments must be passed.");
    if(img.empty())
        mexErrMsgTxt("No image loaded.");

    // check the keypoints
    if(keypoints.size()==0)
        mexErrMsgTxt("Keypoints empty. Run detect.");

    // now we can extract the descriptors
    cv::Mat descriptors;
    assert(p_descriptor);
    p_descriptor->compute(img,keypoints,descriptors);

    // allocate the lhs descriptor matrix
    int dim[2];
    dim[0]=p_descriptor->descriptorSize();
    dim[1]=keypoints.size();
    mxArray* tmp1=mxCreateNumericArray(2,dim,mxUINT8_CLASS,mxREAL);
    uchar* data = (uchar*) mxGetData(tmp1); 
    // copy - kind of dumb, but necessary due to the matlab memory 
    // management
    memcpy(data,descriptors.data,dim[0]*dim[1]);
    // transpose for better readibility
    mexCallMATLAB(1, &plhs[1], 1, &tmp1, "transpose");

    // also write the keypoints
    const int keypoint_size=keypoints.size();
    mxArray* tmp;
    tmp=mxCreateDoubleMatrix(4,keypoint_size,mxREAL);
    double *ptr=mxGetPr(tmp);
    // fill it - attention: in Matlab, memory is transposed...
    for(int k=0; k<keypoint_size; k++){
        const int k4=4*k;
        ptr[k4]=keypoints[k].pt.x;
        ptr[k4+1]=keypoints[k].pt.y;
        ptr[k4+2]=keypoints[k].size;
        ptr[k4+3]=keypoints[k].angle;
    }
    
    // finally, re-transpose for better readibility:
    mexCallMATLAB(1, plhs, 1, &tmp, "transpose");
}
Example #15
0
void CALLCONV mogl_GLU_TESS_END_DATA(void* polygondata)
{
    mogl_tess_struct* mytess = (mogl_tess_struct*) polygondata;
    mxArray* prhs[1];
    //mexPrintf("ENDCB\n");
        
    if (mytess->userData) {
        // Assign user-provided polygondata as double:
		prhs[0] = mxCreateNumericMatrix(1, 1, mxDOUBLE_CLASS, mxREAL);
		*(double*) mxGetData(prhs[0]) = mytess->polygondata;
        mexCallMATLAB(0, NULL, 1, prhs, mytess->nGLU_TESS_END_DATA);
    }
    else {
        // Don't assign polygondata:
        mexCallMATLAB(0, NULL, 0, NULL, mytess->nGLU_TESS_END);
    }
}
Example #16
0
static double *R_fn(uint lbl)
{
    *label = lbl + 1;
    mxArray *prhs[] = { m_R_fn, m_label };
    mexCallMATLAB(1, &m_R_w[lbl], 2, prhs, "feval");
    ASSERT(mxIsDouble(m_R_w[lbl]));
    return mxGetPr(m_R_w[lbl]);
}
void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    
    /*N = Number of steps to take*/
    int N;
    int i; 
    double h;
    mxArray* t;
    mwSize numberVariables;
    double* times;
    mxArray* y;
    double* solution;
    mxArray* yTranspose;
    
    /*Read inputs*/
    double* timeRange = mxGetPr(prhs[0]);
    double* initialConditions = mxGetPr(prhs[1]);
    
    odeInit(prhs[2]);
    
    N=5000;
    h=(timeRange[1]-timeRange[0])/(double)N;
    numberVariables = mxGetDimensions(prhs[1])[1];
    
    
    /*Enter timepoints*/
    t = mxCreateDoubleMatrix(N,1,mxREAL); 
    times = mxGetPr(t);
    for(i=0; i<N; i++){
        times[i]=timeRange[0]+i*h;
    }

    /*Make y vector and initialise*/    
    y = mxCreateDoubleMatrix(numberVariables,N,mxREAL);
    solution = mxGetPr(y);
    for(i=0; i<numberVariables; i++){
        solution[i] = initialConditions[i]; 
    }
    
    
    /*Solve ODE using Runge-Kutta*/
    for(i=0;i<N-1;i++){
        odeStepper(numberVariables, 
                &odeFunction, 
                h, 
                times[i], 
                solution+i*numberVariables,
                solution+(i+1)*numberVariables);
    }
    
    yTranspose = mxCreateDoubleMatrix(N,numberVariables,mxREAL);
    mexCallMATLAB(1,&yTranspose,1,&y,"transpose");
    mxDestroyArray(y);
    
    plhs[0]=t;
    plhs[1]=yTranspose;
    
}
Example #18
0
void mexFunction(int nlhs, mxArray *plhs[],
		 int nrhs, const mxArray *prhs[])
{

  if (nlhs > 1 ) 
    mexErrMsgTxt( "Too many output arguments." );
  
  if (nrhs != 1)
    mexErrMsgTxt( "Wrong number of input arguments." );

  {
    mxArray *MN[2], *R;
    double *p, *q, *r, *s, sum=0, b, c=0; 
    const int *dims;
    int i, n, a, k=0;

    dims = mxGetDimensions(prhs[0]);
    n = dims[0]*dims[1];            /* number of weights */
    p = mxGetPr(prhs[0]);           /* pointer to unormalized weights */
    q = mxMalloc(n*sizeof(double)); /* pointer to normalized and scaled weights */

    /* 'n' uniform random numbers, 'MN' is needed only temporarily */
    MN[0]=mxCreateDoubleScalar(dims[0]);
    MN[1]=mxCreateDoubleScalar(dims[1]);
    mexCallMATLAB(1,&R,2,MN,"rand");
    mxDestroyArray(MN[0]);
    mxDestroyArray(MN[1]);
    r = mxGetPr(R);                 /* pointer to uniform random numbers */

    /* allocate matrix for return value */
    plhs[0]=mxCreateDoubleMatrix(dims[0],dims[1],mxREAL);
    s = mxGetPr(plhs[0]);     /* pointer to samples */

    for (i = 0; i < n; i++)
      sum+=p[i];              /* compute sum for normalization */
    for (i = 0; i < n; i++) {
      q[i]=p[i]/sum*n;        /* normalize and scale weight */
      c+=q[i];                /* cumulate weights */
      if (c>=1.0) {           /* if cumulative weight over 1 */
	a=(int)(c);           /* integer part of the cumulative weight */
	c-=a;                 /* subract integer part from the cum weight */
	k+=a;                 
	for (b=i+1.0;a>0;a--)	  
	  *s++=b;             /* fill vector 'a' times with sample 'b' */
      }
      if (k<n && c>=r[k]) {   /* if cumulative larger than a random number...*/
	*s++=i+1.0;           /* .. add sample... */
	c-=1.0;               /* ...and subract one from the cumulative */
	k+=1;                 
      }
    }

    mxDestroyArray(R);        /* no need for this anymore */

  }

  return;
}
int mtlb_IdaSpilsPsolB(realtype tt,
                       N_Vector yy, N_Vector yp,
                       N_Vector yyB, N_Vector ypB, N_Vector rrB,
                       N_Vector rvecB, N_Vector zvecB,
                       realtype c_jB, realtype deltaB,
                       void *prec_dataB, N_Vector tmpB)
{
    mxArray *mx_in[11], *mx_out[3];
    int ret;

    /* Inputs to the Matlab function */
    mx_in[0] = mxCreateScalarDouble(-1.0);        /* type=-1: backward ODE */
    mx_in[1] = mxCreateScalarDouble(tt);          /* current t */
    mx_in[2] = mxCreateDoubleMatrix(N,1,mxREAL);  /* current yy */
    mx_in[3] = mxCreateDoubleMatrix(N,1,mxREAL);  /* current yp */
    mx_in[4] = mxCreateDoubleMatrix(NB,1,mxREAL); /* current yyB */
    mx_in[5] = mxCreateDoubleMatrix(NB,1,mxREAL); /* current ypB */
    mx_in[6] = mxCreateDoubleMatrix(NB,1,mxREAL); /* current rrB */
    mx_in[7] = mxCreateDoubleMatrix(NB,1,mxREAL); /* right hand side rB */
    mx_in[8] = mxCreateScalarDouble(c_jB);        /* current c_jB */
    mx_in[9] = mx_PSOLfctB;                       /* matlab function handle */
    mx_in[10] = mx_data;                          /* matlab user data */

    /* Call matlab wrapper */
    GetData(yy, mxGetPr(mx_in[2]), N);
    GetData(yp, mxGetPr(mx_in[3]), N);
    GetData(yyB, mxGetPr(mx_in[4]), NB);
    GetData(ypB, mxGetPr(mx_in[5]), NB);
    GetData(rrB, mxGetPr(mx_in[6]), NB);
    GetData(rvecB, mxGetPr(mx_in[7]), NB);

    mexCallMATLAB(3,mx_out,11,mx_in,"idm_psol");

    PutData(zvecB, mxGetPr(mx_out[0]), NB);
    ret = (int)*mxGetPr(mx_out[1]);

    if (!mxIsEmpty(mx_out[2])) {
        UpdateUserData(mx_out[2]);
    }

    /* Free temporary space */
    mxDestroyArray(mx_in[0]);
    mxDestroyArray(mx_in[1]);
    mxDestroyArray(mx_in[2]);
    mxDestroyArray(mx_in[3]);
    mxDestroyArray(mx_in[4]);
    mxDestroyArray(mx_in[5]);
    mxDestroyArray(mx_in[6]);
    mxDestroyArray(mx_in[7]);
    mxDestroyArray(mx_in[8]);
    mxDestroyArray(mx_out[0]);
    mxDestroyArray(mx_out[1]);
    mxDestroyArray(mx_out[2]);

    return(ret);

}
Example #20
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

  GPUmatResult_t status = GPUmatSuccess;
  // tmp
  mxArray *lhs[2];

  if (nrhs != 1)
    mexErrMsgTxt("Wrong number of arguments");

  // the passed element should be a GPUsingle
  if (!(mxIsClass(prhs[0], "GPUdouble")))
    mexErrMsgTxt(ERROR_EXPECTED_GPUDOUBLE);

  if (init == 0) {
    // Initialize function
    mexLock();
    // load GPUmanager
    mexCallMATLAB(2, &lhs[0], 0, NULL, "GPUmanager");
    GPUman = (GPUmanager *) (UINTPTR mxGetScalar(lhs[0]));
    mxDestroyArray(lhs[0]);
    init = 1;
  }
  GPUtype *p = mxToGPUtype(prhs[0], GPUman);
  
  int numel = p->getNumel();
  int ndims = p->getNdims();
  int *size = p->getSize();
  int mysize = p->getMySize();
  gpuTYPE_t type = p->getType();

  // create dest array
  // dims re set to [1 numel]. A reshape is required outside
  // this function
  mwSize dims[2];

  // create destination
  if (type == gpuDOUBLE) {
    dims[0] = 1;
    dims[1] = numel;

    plhs[0] = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
  } else if (type == gpuCDOUBLE) {
    dims[0] = 1;
    dims[1] = 2 * numel;

    plhs[0] = mxCreateNumericArray(2, dims, mxDOUBLE_CLASS, mxREAL);
  }

  try {
      status = GPUopCudaMemcpy(mxGetPr(plhs[0]), p->getGPUptr(),
          mysize * numel, cudaMemcpyDeviceToHost, p->getGPUmanager());

  } catch (GPUexception ex) {
    mexErrMsgTxt(ex.getError());
  }

}
Example #21
0
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[])
{
  const mxArray *mxOpenArg[2];

  mxOpenArg[0] = prhs[0];
  mxOpenArg[1] = mxCreateString("SELECT sql FROM sqlite_master ORDER BY name;");
  mexCallMATLAB(1, plhs, 2, (mxArray **) mxOpenArg, "sql_stmt");
}
Example #22
0
void TT_CALLBACK_ERROR(SimStruct *S, const char *error_msg) {

  mxArray *rhs[1];
  rhs[0] = mxCreateString(error_msg);
  mexCallMATLAB(0, NULL, 1, rhs, "error");
  mexPrintf("??? %s\n\nIn block ==> %s\nSimulation aborted!\n", error_msg, ssGetBlockName(S));
  ssSetErrorStatus(S, "");

}
int mxW_CVodeSensRhs(int Nsens, realtype t,
                     N_Vector y, N_Vector yd,
                     N_Vector *yS, N_Vector *ySd,
                     void *user_data,
                     N_Vector tmp1, N_Vector tmp2)
{
  cvmPbData fwdPb;
  mxArray *mx_in[7], *mx_out[3];
  int is, ret;
  double *tmp;

  /* Extract global interface data from user-data */
  fwdPb = (cvmPbData) user_data;

  /* Inputs to the Matlab function */
  mx_in[0] = mxCreateDoubleScalar(t);             /* current t */
  mx_in[1] = mxCreateDoubleMatrix(N,1,mxREAL);    /* current y */
  mx_in[2] = mxCreateDoubleMatrix(N,1,mxREAL);    /* current yd */
  mx_in[3] = mxCreateDoubleScalar(Ns);            /* number of sensitivities */
  mx_in[4] = mxCreateDoubleMatrix(N*Ns,1,mxREAL); /* current yS */
  mx_in[5] = fwdPb->SRHSfct;                      /* matlab function handle */      
  mx_in[6] = fwdPb->mtlb_data;                    /* matlab user data */
  
  /* Call matlab wrapper */
  GetData(y, mxGetPr(mx_in[1]), N);
  GetData(yd, mxGetPr(mx_in[2]), N);
  tmp = mxGetPr(mx_in[4]);
  for (is=0; is<Ns; is++)
    GetData(yS[is], &tmp[is*N], N);

  mexCallMATLAB(3,mx_out,7,mx_in,"cvm_rhsS");
  
  tmp = mxGetPr(mx_out[0]);

  for(is=0;is<Ns;is++)
    PutData(ySd[is], &tmp[is*N], N);

  ret = (int)*mxGetPr(mx_out[1]);

  if (!mxIsEmpty(mx_out[2])) {
    UpdateUserData(mx_out[2], fwdPb);
  }

  /* Free temporary space */

  mxDestroyArray(mx_in[0]);
  mxDestroyArray(mx_in[1]);
  mxDestroyArray(mx_in[2]);
  mxDestroyArray(mx_in[3]);
  mxDestroyArray(mx_in[4]);
  mxDestroyArray(mx_out[0]);
  mxDestroyArray(mx_out[1]);
  mxDestroyArray(mx_out[2]);

  return(ret);
}
// call isempty from matlab
bool call_isempty(const mxArray* wtfd) {
    mxArray *result[1];
    bool *isempty;
    
    mexCallMATLAB(1, result, 1, &wtfd, "isempty");
    
    isempty = (bool*)mxGetPr(result[0]);
    
    return isempty[0];
}
Example #25
0
/**
* Prints a matrix M to the MatLab standard output.
*/
void printmatrix(double* M, int rows, int cols) {
	mxArray* matrix = mxCreateDoubleMatrix(rows, cols, mxREAL);	
	double* matrixcell = mxGetPr(matrix);
	int i;
	/* memcpy(matrixcells, M, rows*cols*sizeof(double)); */
	for (i = 0; i < rows*cols; i++) {
		*(matrixcell++) = *(M++);
	}
	mexCallMATLAB(0, NULL, 1, &matrix, "disp");
}
Example #26
0
	static void callMatlab(mxArray*&Y1,string command,mxArray*X1){
		const int nlhs=1;
		const int nrhs=1;
		mxArray *plhs[nlhs];
		mxArray *prhs[nrhs];
		prhs[0]=X1;
		int res=mexCallMATLAB(nlhs,plhs,nrhs,prhs,command.c_str());
		assert(res==0);
		Y1=plhs[0];
	}
int mxW_CVodeQUADfctBS(realtype t, N_Vector y,  N_Vector *yS,
                       N_Vector yB, N_Vector yQBd, void *user_dataB)
{
  cvmPbData fwdPb, bckPb;
  mxArray *mx_in[8], *mx_out[3];
  int is, ret;
  double *tmp;

  /* Extract global interface data from user-data */
  bckPb = (cvmPbData) user_dataB;
  fwdPb = bckPb->fwd;

  /* Inputs to the Matlab function */
  mx_in[0] = mxCreateDoubleScalar(1.0);           /* type=1: dependent on yS */
  mx_in[1] = mxCreateDoubleScalar(t);             /* current t */
  mx_in[2] = mxCreateDoubleMatrix(N,1,mxREAL);    /* current y */
  mx_in[3] = mxCreateDoubleScalar(Ns);            /* number of sensitivities */
  mx_in[4] = mxCreateDoubleMatrix(N*Ns,1,mxREAL); /* current yS */
  mx_in[5] = mxCreateDoubleMatrix(NB,1,mxREAL);   /* current yB */
  mx_in[6] = bckPb->QUADfct;                      /* matlab function handle */ 
  mx_in[7] = bckPb->mtlb_data;                    /* matlab user data */

  /* Call matlab wrapper */

  GetData(y, mxGetPr(mx_in[2]), N);

  tmp = mxGetPr(mx_in[4]);
  for (is=0; is<Ns; is++)
    GetData(yS[is], &tmp[is*N], N);

  GetData(yB, mxGetPr(mx_in[5]), NB);

  mexCallMATLAB(3,mx_out,8,mx_in,"cvm_rhsQB");

  PutData(yQBd, mxGetPr(mx_out[0]), NqB);

  ret = (int)*mxGetPr(mx_out[1]);

  if (!mxIsEmpty(mx_out[2])) {
    UpdateUserData(mx_out[2], bckPb);
  }

  /* Free temporary space */
  mxDestroyArray(mx_in[0]);
  mxDestroyArray(mx_in[1]);
  mxDestroyArray(mx_in[2]);
  mxDestroyArray(mx_in[3]);
  mxDestroyArray(mx_in[4]);
  mxDestroyArray(mx_in[5]);
  mxDestroyArray(mx_out[0]);
  mxDestroyArray(mx_out[1]);
  mxDestroyArray(mx_out[2]);

  return(ret);
}
int mtlb_IdaBandJac(long int Neq, long int mupper,
                    long int mlower, realtype tt,
                    N_Vector yy, N_Vector yp, N_Vector rr,
                    realtype c_j, void *jac_data, BandMat Jac,
                    N_Vector tmp1, N_Vector tmp2,
                    N_Vector tmp3)
{
    double *J_data;
    long int eband, i;
    int ret;
    mxArray *mx_in[8], *mx_out[3];

    /* Inputs to the Matlab function */
    mx_in[0] = mxCreateScalarDouble(1.0);         /* type=1: forward ODE */
    mx_in[1] = mxCreateScalarDouble(tt);          /* current t */
    mx_in[2] = mxCreateDoubleMatrix(N,1,mxREAL);  /* current yy */
    mx_in[3] = mxCreateDoubleMatrix(N,1,mxREAL);  /* current yp */
    mx_in[4] = mxCreateDoubleMatrix(N,1,mxREAL);  /* current rr */
    mx_in[5] = mxCreateScalarDouble(c_j);         /* current c_j */
    mx_in[6] = mx_JACfct;                         /* matlab function handle */
    mx_in[7] = mx_data;                           /* matlab user data */

    /* Call matlab wrapper */
    GetData(yy, mxGetPr(mx_in[2]), N);
    GetData(yp, mxGetPr(mx_in[3]), N);
    GetData(rr, mxGetPr(mx_in[4]), N);

    mexCallMATLAB(3,mx_out,8,mx_in,"idm_bjac");

    /* Extract data */
    eband =  mupper + mlower + 1;
    J_data = mxGetPr(mx_out[0]);
    for (i=0; i<N; i++)
        memcpy(BAND_COL(Jac,i) - mupper, J_data + i*eband, eband*sizeof(double));

    ret = (int)*mxGetPr(mx_out[1]);

    if (!mxIsEmpty(mx_out[2])) {
        UpdateUserData(mx_out[2]);
    }

    /* Free temporary space */
    mxDestroyArray(mx_in[0]);
    mxDestroyArray(mx_in[1]);
    mxDestroyArray(mx_in[2]);
    mxDestroyArray(mx_in[3]);
    mxDestroyArray(mx_in[4]);
    mxDestroyArray(mx_in[5]);
    mxDestroyArray(mx_out[0]);
    mxDestroyArray(mx_out[1]);
    mxDestroyArray(mx_out[2]);

    return(ret);
}
Example #29
0
/* NO INDEX,
 * VALUE = uint8 */
static void setUINT8(int command, uint8_T value) {
   enum { COMMAND_ARG = 0, VALUE_ARG, NUMARGS };
   mxArray *rhs[NUMARGS];
   int i;
   rhs[COMMAND_ARG] = mxCreateScalarDouble(command);
   setUINT8arg(rhs, VALUE_ARG, value);
   mexCallMATLAB(0, NULL, NUMARGS, rhs, MEX_FILE);
   for (i=0; i<NUMARGS; i++) {
      mxDestroyArray(rhs[i]);
   }
}
Example #30
0
double matlab_rand(int row,int col)
{
/* Calling Matlab's random number*/
    double *rand;
    mxArray *lhs[1],*rhs[2];
    rhs[0]=mxCreateDoubleScalar(row);
    rhs[1]=mxCreateDoubleScalar(col);
    mexCallMATLAB(1, lhs, 2,rhs , "rand");
    rand=mxGetPr(lhs[0]);
    return rand[0];
}