Pass one mex file to another mex file

1 Ansicht (letzte 30 Tage)
Qun Wu
Qun Wu am 11 Aug. 2017
Kommentiert: Joss Knight am 12 Aug. 2017
I have a mex file which is calling a matlab function "myfile5.m".
function f3 = myfile5(in1,u)
x1 = in1(:,1);
x2 = in1(:,2);
f3 = [x2+u.*(x1.*5.0e-1+5.0e-1);x1-u.*(x2.*2.0-5.0e-1)];
And the mex file is "mexTest.c".
#include "mex.h"
void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
mexCallMATLAB(1, plhs, 2, prhs, "myfile5");
}
In command window, the mex file can be compiled and I can test a simple example.
mex mexTest.c
mexTest([1 2], 3)
It worked and the ans is [5; -9.5]. I am trying to pass the mex file as a subroutine to another mex file which is a cuda code file. The data to be operated is passing from matlab workspace to mex via the entrance
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, mxArray *prhs[])
and the operator is passed from another mex file such as "mexTest.c". I searched the website over and over again. I find a poster https://www.mathworks.com/matlabcentral/answers/24501-overhead-of-calling-mex-functions-from-another-mex-file
They said that using dll is a good choice. I can not find a detail answer about how to generate dll from a m/mex file and how to call the dll from the main mex file. For now, I can call a simple rountine from the main mex file like this:
#include <mex.h>
#include <cuda.h>
__device__ double timesthree(double x){
return x * 3.0;
}
__global__ void ParallelCalculations(int Ny, double *X, double *Y){
int idx;
idx = blockDim.x * blockIdx.x + threadIdx.x;
int const pw = 2;
//int xdm;
if (idx < Ny - 1)
{
Y[idx] = 0;
Y[idx] = Y[idx] + pow(X[idx], pw) + pow(X[Ny + idx], pw) + timesthree(X[idx]);
__syncthreads();
}
}
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, mxArray *prhs[])
{ double *X, *Y;
double *xd;
int memSize_X;
int memSize_Y;
mwSize Nxdm, Nx, Ny; // Nxdm, // mwSize is kind of unsinged type
// Nxdm = mxGetN(prhs[0]); // number of columns in the Array, if N dimension 15-5-4-6 then return 5*4*6
Nxdm = mxGetN(prhs[0]);
xd = mxGetPr(prhs[0]);
Nx = mxGetN(prhs[1]);
Ny = (int)(Nx/xd[0]);
// printf("%i;",Ny);
memSize_X = sizeof(double) * Nx;
memSize_Y = sizeof(double) * Ny;
cudaMalloc(&X, memSize_X);
cudaMalloc(&Y, memSize_Y);
cudaMemcpy(X, (double *) mxGetData(prhs[1]), memSize_X, cudaMemcpyHostToDevice);
plhs[0] = mxCreateDoubleMatrix(1, (mwSize)Ny, mxREAL);
int const threadsPerBlock = 512;
int blockPerGrid = (Ny + threadsPerBlock - 1)/threadsPerBlock;
ParallelCalculations<<< blockPerGrid, threadsPerBlock >>> (Ny, X, Y);
cudaMemcpy( (double*) mxGetData(plhs[0]), Y, memSize_Y, cudaMemcpyDeviceToHost );
// cudaFree(&xdm);
cudaFree(&X);
cudaFree(&Y);
}
Please help. Thank you in advance.
  1 Kommentar
Joss Knight
Joss Knight am 12 Aug. 2017
Why does one mex function have to call the other? Why can't they both share a source file with the implementation? You can build a mex function out of more than one file.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by