Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matlab API for other languages #96

Closed
sihaeri opened this issue Mar 2, 2018 · 5 comments
Closed

Matlab API for other languages #96

sihaeri opened this issue Mar 2, 2018 · 5 comments

Comments

@sihaeri
Copy link

sihaeri commented Mar 2, 2018

A less known capability of matlab is c/c++ and fortran interoperability with mex functions to interface other languages. This can be added as a part of the course and I found this an extremely powerful tool, this can be a short introduction with a simple example as follow:
Following is a demonstration of how this would work,
Assuming the mex command is configured correctly (following documentation, you need a c or fortran compiler and need to use mex -setup); do the following

mex dist.c
Building with 'gcc'.
MEX completed successfully.
[[You should now have a dist.mexa64 in linux or dist.dll in windows]]

x=rand(3,5)
x =
0.1190 0.3404 0.7513 0.6991 0.5472
0.4984 0.5853 0.2551 0.8909 0.1386
0.9597 0.2238 0.5060 0.9593 0.1493
y=dist(x)
y =
0 0.3481 0.4431 0.3244 0.5328
0.3481 0 0.3411 0.4666 0.2427
0.4431 0.3411 0 0.3804 0.2257
0.3244 0.4666 0.3804 0 0.5714
0.5328 0.2427 0.2257 0.5714 0

the required c file (copy and paste in dist.c, this calculates a distance but the for loop can be simplified to something like just squaring elements or something similar), this can be provided for the students.

#include "mex.h"
#include "math.h"

void mexFunction(int nlhs,mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
	int i,j,k;
        double *dist,sum;
        /* Check for proper number of arguments. */
 	if(nrhs!=1) {
    	mexErrMsgTxt("One input required.");
  	} else if(nlhs>1) {
    	mexErrMsgTxt("Too many output arguments.");
  	}

	/*Find number of rows and columns in input data and get a pointer to the first input*/
        double* x = mxGetPr(prhs[0]);
	int rows = mxGetM(prhs[0]);
	int cols = mxGetN(prhs[0]);		
    
        plhs[0] = mxCreateDoubleMatrix(cols, cols, mxREAL);
        dist = (double *)mxGetData(plhs[0]);
    
        for(i=0;i<cols;i++)
             for(j=0;j<cols;j++){
                  sum=0.0;
                  for(k=0;k<rows;k++)sum+=fabs(x[i*rows+k]-x[j*rows+k]);
                  dist[i*cols+j]=sum/(double)rows;
              }

}
@shwina
Copy link

shwina commented Mar 2, 2018

While this is indeed an extremely neat feature of MATLAB, I think this is far out of the scope of this lesson. The audience for most SWC workshops have no real prior experience in programming, let alone in a low-level language such as C.

@sihaeri
Copy link
Author

sihaeri commented Mar 9, 2018

Hi shwina,

I agree that his is perhaps a more advanced feature of Matlab, but to my experience particularly for postgraduate students that need to process large data-sets this can be extremely beneficial. The c part of the code here is a bit complex it can be as simple as passing an argument and printing that to screen. The idea here is to make them aware of the this possibility and show them the mechanics of doing this, so that if they needed they can learn on their own.

@shwina
Copy link

shwina commented Mar 9, 2018

@sihaeri

I do agree that if you are teaching an audience that (1) will have an immediate need for writing or modifying MEX-files, and (2) has prior experience with programming in C, it may be worth introducing the idea. For such a workshop, I would suggest making a copy of the lesson materials and adding the relevant contents.

I don't believe that this will fit in with the core lesson though, which is intended really to teach general programming concepts, and not to introduce features very specific to MATLAB.

@rgaiacs
Copy link

rgaiacs commented Mar 9, 2018

While this is indeed an extremely neat feature of MATLAB, I think this is far out of the scope of this lesson.

I second this. In all my years working with MATLAB users, I hardly encounter anyone using MEX probably because they didn't have performance issues that make cost efficient to migrate their code to C/C++ and use MEX. Some friends used CUTEst: a Constrained and Unconstrained Testing Environment with safe threads for mathematical optimization and its MEX integration but had to handle the extra complexity of having a C/C++ compiler on their Windows machine.

Said that, I think that worth mention MEX at the end of the workshop for things that people could look. As a novice, you don't know the words you need to search for. But you should not teach MEX at a novice Software Carpentry workshop. Python and R lessons don't teach their C/C++ integration. If you think your audience need to know MEX, it is the case to organise a workshop only to cover MEX.

@gcapes
Copy link
Contributor

gcapes commented Oct 12, 2018

Thanks for your suggestion, but this is firmly out of scope for this lesson.

@gcapes gcapes closed this as completed Oct 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants