1 2Introduction 3============ 4 5A set of interface functions that allow FLAME computational routines written in C 6to be called from Matlab is described here. Matlab provides an external interface 7API to call routines coded in the C language, which is used as the basis for the 8FLAME interface functions. This involves writing a C file, named FLA_foo.c, for 9each FLAME routine, FLA_foo(...), that needs to be interfaced. An example is shown 10below to elucidate the structure of this C file. The FLAME function for which the 11interface is written in this example is FLA_Axpyt_external(...). 12 13 14An example 15========== 16 17#include "mex.h" 18#include "FLA_Matlab2C.h" 19 20extern double FLA_Clock(); 21 22#define NRHS 4 23#define NINT 1 24 25#define NOBJ (NRHS-NINT) 26 27 28void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { 29 int attr[NINT]; 30 FLA_Obj obj[NOBJ]; 31 double *dtime; 32 33 FLA_Init(); 34 35 /* Check if the number of arguments supplied is correct */ 36 FLA_M2C_CheckNumArgs(NRHS, nrhs); 37 38 /* Convert Matlab arguments into the appropriate FLAME C arguments */ 39 FLA_M2C_ConvertArgs(NRHS, prhs, NINT, attr, obj); 40 41 /* If an extra argument is supplied, collect timing informaion in it. */ 42 if (nrhs == NRHS+1) 43 dtime = FLA_M2C_ConvertDoublePtr(prhs[NRHS]); 44 45 /* Now call the C FLAME function, timing it if the extra argument is given. */ 46 47 if (nrhs == NRHS+1) 48 *dtime = FLA_Clock(); 49 50 FLA_Axpyt_external(attr[0], obj[0], obj[1], obj[2]); 51 52 if (nrhs == NRHS+1) 53 *dtime = FLA_Clock() - *dtime; 54 55 FLA_Finalize(); 56 57} 58 59 60Matlab Interface Requirement 61============================ 62 63These interface files are called MEX files in Matlab lingo. Matlab requires the 64inclusion of the header mex.h, and the definition of the function mexFunction(...). 65The function mexFunction(...) is called when the interfaced C function (FLA_Axpy_t, 66in this example) is invoked from Matlab. The arguments provided to the function 67when invoked from Matlab are passed to mexFunction. 68 69nlhs: number of LHS arguments. 70plhs: pointer to an array of LHS arguments, which are Matlab arrays (mxArray). 71nrhs: number of RHS arguments. 72prhs: pointer to an array of RHS arguments, which are Matlab arrays (mxArray). 73 74The task of the interface writer is to write the body of mexFunction such that it 75does the following things. 761. Make sure that the function is invoked with the correct number of arguments. 772. Allocate memory for the output arguments. 783. If an argument is used for both input and output, and it is desired that the 79 input be preserved, then make a copy of it. 804. Convert the arguments from Matlab format (mxArray) to the format expected by 81 the C function. 825. Call the C function. 83 84 85Matlab To C FLAME Functions 86=========================== 87 88To make this task easier for the specific case of interfacing FLAME computational 89routines, the following functions are used in the above example. 90 91void FLA_M2C_CheckNumArgs(int rrhs, int srhs); 92 93rrhs: required number of RHS arguments. 94srhs: supplied number of RHS arguments. 95 96Checks if the correct number of arguments are being passed. The supplied number of 97arguments must be either rrhs or rrhs+1. In the latter case, the last argument is 98used to collect and return timing information of the function call. If the conditions 99are not met, the program is aborted with an error message. It performs task (1) 100in the above list. 101 102Tasks (2) and (3) in the above list are ignored because we don't make use of output 103or LHS arguments, and FLAME functions return results by overwriting one or more 104RHS arguments. 105 106 107void FLA_M2C_ConvertArgs(int nrhs, const mxArray *prhs[], int nint, 108 int attr[], FLA_Obj obj[]); 109 110nrhs, prhs: same meaning as in the definition of mexFunction. 111nint: number of FLAME attribute arguments in the C function being interfaced. 112attr: pointer to an int array containing the converted FLAME attributes on return. 113obj: pointer to a FLA_Obj array containing the converted numeric data on return. 114 115This function converts the FLAME attributes and returns them in the attr[] array and 116converts the numeric data and returns them in the obj[] array. The types of arguments 117are checked for validity before performing the conversions. It performs task (4) 118listed above. This function is applicable only if the FLAME C 119function follows the conventions listed below. 120 1. The arguments to the C FLAME routine are either type int, representing FLAME 121 attributes, or type FLA_Obj, representing FLAME objects used in computation. 122 2. The int arguments, if any, precede all the FLA_Obj arguments in the function 123 interface. 124 3. Return type of the function is void. 125 126 127double *FLA_M2C_ConvertDoublePtr(const mxArray *mobj); 128 129mobj: a scalar double mxArray. 130 131The FLAME function, when invoked from Matlab can be supplied an additional argument at 132the end, which if present is used to return timing information about the function. 133This function returns a double pointer to this argument (also task (4)). 134 135 136Calling the C Function 137====================== 138 139Once arguments have been converted to the format required by the C function, it 140can be called as shown in the example by passing in the attribute values and the 141FLAME objects (task (5)). 142 143FLA_foo(attr[0], ..., attr[NINT-1], obj[0], ..., obj[NOBJ-1]); 144 145 146Template for FLAME MEX file 147=========================== 148 149The following preprocessor symbols are defined. 150NRHS: Number of RHS arguments expected. 151NINT: Number of integer (attribute) arguments expected. 152 153For constructing a new MEX file that interfaces a new FLAME C function to Matlab, 154assuming that the function obeys the conventions listed above, use the example code 155as a template and change the values of NRHS and NINT to the appropriate values 156and call the new FLAME C function, say FLA_foo, instead of FLA_Axpy_t as follows. 157FLA_foo(attr[0], ..., attr[NINT-1], obj[0], ..., obj[NOBJ-1]); 158 159 160Automatic Generation of FLAME MEX files 161======================================= 162 163The template for the FLAME MEX file has been coded into a perl script, interface.pl, 164that can generate the MEX file for a function, given its name, nrhs and nint. 165The directions for use of interface.pl can be found at the top of the file. 166 167 168Compiling the MEX file 169====================== 170 171The MEX file is compiled into a dynamically linked subroutine and outputs to a file 172with the .mexglx extension, say FLA_foo.mexglx. When FLA_foo is invoked from a Matlab 173program, the Matlab interpreter loads and executes FLA_foo.mexglx. The MEX C file is 174compiled using the mex utility included with the Matlab software. See the Makefile 175in this directory for the full command line and other libraries that need to be linked 176in for compiling a FLAME MEX file. To use the Makefile, edit it to specify the paths 177for FLAME, ATLAS and Matlab. Also list the MEX files that need to be compiled in the 178variable MEXFILES. 179 180 181Other Matlab To C Functions 182=========================== 183 184If the above conventions for the use of FLA_M2C_ConvertArgs are not obeyed, then 185the following two functions can be used that convert each Matlab array argument 186into the appropriate data type to be passed to the C function. 187 188int FLA_M2C_ConvertAttribute(const mxArray *mobj); 189 190mobj: pointer to an mxArray of characters (in other words, a Matlab string) that 191 specifies a FLAME attribute such as FLA_TRANSPOSE or FLA_UNIT_DIAG. 192 193In the C API for FLAME, attributes are represented by symbols that are mapped by 194the preprocessor to integers. This function returns the integer corresponding to 195the attribute specified by mobj. 196 197 198 199FLA_Obj FLA_M2C_ConvertMxArray(const mxArray *mobj); 200 201mobj: pointer to an mxArray that contains numeric data, which could be a scalar, 202 vector or matrix. 203 204In the C API for FLAME, numeric data is represented by the FLA_Obj data type. 205This function converts the data from the mxArray type to the FLA_Obj type and 206returns the latter. 207 208 209