Pass one mex file to another mex file

1 visualizzazione (ultimi 30 giorni)
Qun Wu
Qun Wu il 11 Ago 2017
Commentato: Joss Knight il 12 Ago 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 Commento
Joss Knight
Joss Knight il 12 Ago 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.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by