Preface: This article mainly through a simple example procedure to the C-mex to carry on a preliminary explanation. Pre-construction of the environment (including the installation of MATLAB and the GCC compiler) is not here to repeat.
First, why use C-mex? It is the efficiency of the program. Recently, I was doing a parallel project, the original program was written in Matlab, running time of more than 6 hours, after I changed to MATLAB and C (or C + +) mixed, the running time reduced to 2 minutes. Days! More than 180 times the speedup, dare not imagine! Of course, not every program changed to C can get so much speed, the key depends on the program itself and the frequency of the number of visits.
So, what is a Mex file? The Mex file is actually an interface between other languages and Matlab. With this interface, we can use programs written in other languages in the same way that we use MATLAB functions (that is,. m files). For example, the following is an example of the simplest matrix addition.
Matlab main function (MAIN.M):
1%compiling C files2 Mex AddVector.cpp;3 4%Initialize Parameters5A = [1 2 3;4 5 6];6B = [7 8 9;2 3 4];7 8%invoking a Mex file9C = Addvector (A, b);
The MATLAB program is very simple, first compile the file addVector.cpp, and then declare a variable A and B. Finally, A and B are used as parameters of the function addvector, and the function is called and the result is saved as C. Here, you may notice that AddVector.cpp is a C + + file, and there is no difference, and the compiler will automatically help you identify the C and C + + programs that distinguish them. However, if you have obsessive-compulsive disorder, you can also write a. c file, as long as there is no such thing as class and object in your file (c This single dog can have objects, hahaha ...). )。
Gee, that uses the statement "Mex AddVector.cpp" to compile our C file, what is the compiled file? Come and see below:
Look at that blue guy. "Addvector.mexw64" is the executable that we compiled with the MEX command. In use, we just need to shout her name "Addvector", and then put the parameters in, she will be honest to listen to your words! Hehe hey, isn't it wonderful! So, what is the suffix "mexw64"? In fact, the suffix "mexw64" in the "Mex" is to tell everyone that this file is a MEX file! Then, "W64" is that this file can only be used under the 64-bit Windows system! There's nothing.
I said she would be honest, don't you believe it? Of course, I didn't lie to you, but you must be good to her first. So, what's good for her? Next, let me introduce to you the main "AddVector.cpp" of Our Lord. Why don't we have a light on the polo shirt and savor it? See:
1#include"mex.h" 2#include <stdio.h>3 4 //NLHS is the number of output parameters, NRHS is the number of input parameters5 //PLHS is the output parameter pointer, PRHS is the input parameter pointer6 voidMexfunction (intNLHS, Mxarray *plhs[],intNRHS, Mxarray *prhs[])7 {8 //Get input data9 intI=0;Ten Double* Inputmatric_a = (Double*) Mxgetdata (prhs[i++]);//the parameter passed by MATLAB defaults to double type . One Double* Inputmatric_b = (Double*) Mxgetdata (prhs[i++]); A - //gets the number of rows and columns of the first argument - intNUMROWSA = (int) Mxgetm (prhs[0]); the intNumcolsa = (int) Mxgetn (prhs[0]); -printf"NUMROWSA =%d\n", NUMROWSA); -printf"Numcolsa =%d\n", Numcolsa); - + //generate output output -plhs[0] = Mxcreatedoublematrix (numrowsa,numcolsa,mxreal);//three parameters are number of rows, number of columns and data type, Mxreal is real + Double* Outputmatric_c = (Double*) Mxgetdata (plhs[0]);//create pointer to output data A at //calculate and put the results into the output data area - intDataSize = NUMROWSA * NUMCOLSA;//total number of matrix A elements - for(i=0; i<datasize; i++) - { -Outputmatric_c[i] = Inputmatric_a[i] +Inputmatric_b[i]; - } in}
Hee Hee The code is a bit long, a whole 29 lines! First look at the header file, calculate the top # include those two lines! "#include" mex.h "is to include the function declarations of the functions you want to use first, and you look at the functions called below, all you've ever seen! "Include <stdio.h>" is just the C IO header file. Now, nothing, don't tell me you will not C, teleport back to learn to finish again.
High energy ahead, see the 6th line of code, yes, that's the function "void mexfunction (int nlhs, Mxarray *plhs[], int nrhs, Mxarray *prhs[])". What the hell are you asking me? I don't have a TM. I only know that it is her heart that must exist. Did you write the C program and do you want a main function as an entry? Then you write the MEX program also need an entrance Ah, this entrance is "mexfunction"! Entrance, entrance, understand no! I know you know, hey! Gee, how can this function have so many parameters! I remember that the main function has only two parameters, what is called "argc" and "argv". Little fool, someone else is C-mex, and C can be the same, although we have no object. Since you want to know, I will tell you clearly. See no, four parameters: "NLHS", "PLHS", "NRHS", "PRHS". Wow, it looks like! N begins with the number of arguments, "NLHS" and "NRHS" are the number of output parameters and the number of input parameters, like our example above is nlhs=1,nrhs=3. P begins with a pointer to the input data, and "PLHS" and "PRHS" are pointers to the output data and input data, respectively. You see, people pass the parameter is a pointer, how good, do not have to copy the data again. Matlab That guy declared two variables A and B, put them in the warehouse (memory), and then put the location of the warehouse (memory address, that is, the pointer) told me, I (C) to use when you can go directly to the place to fetch! Hehe, I take the raw materials in the warehouse (A and B) to make cakes (outputmatric_c), and then put in my small warehouse (Plhs[0]), and then Matlab can come to eat! Original, without wasting extra space.
In order to carefully consider the specific content. 9~11 Line, we take the parameter one by one. Remember Oh! We got all the pointers! Matlab pass parameters are generally double type, so your "inputmatric_a" and so on are double type. If the parameter you passed from MATLAB is float type, it will be changed to float type.
Now that we have the input data, we have to make a small cake warehouse (output data) Ah! So, how big is this warehouse (memory)? You like it! 14~21 line is doing this thing, we get parameter prhs[0] The line number and column number is to be used to specify the size of the warehouse of the NA. NUMROWSA x Numcolsa, so big Oh!
Although we have allocated space for the output parameter plhs[0], we must not be able to manipulate the pointer directly! We use another pointer "Outputmatric_c" to point to this memory space, and then save the results in "Outputmatric_c" on it! After all, plhs[0] and "Outputmatric_c" are essentially a thing, except that they are of a different type, one is a mxarray type and the other is a double type.
Suddenly found himself a bit more nonsense ah! Let's just summarize it, don't talk, look at the picture:
First write the C or C + + source files, write finished compiling the executable file! After the fix, you can call in the MATLAB program, input parameters and parameters of the output parameters you decide.
Well, this article is written here! I'm going to eat! Flash man!!!
2016-04-24 11:32:02
Matlab && C-mex Round 1