Home > functions > internal > dsPrepareMEX.m

dsPrepareMEX

PURPOSE ^

PREPAREMEX - take an m-file path and compile it using the Matlab coder.

SYNOPSIS ^

function mexfileOutput = dsPrepareMEX(mfileInput, varargin)

DESCRIPTION ^

PREPAREMEX - take an m-file path and compile it using the Matlab coder.

 See also: dsGetSolveFile

 Author: Jason Sherfey, PhD <jssherfey@gmail.com>
 Copyright (C) 2016 Jason Sherfey, Boston University, USA

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function mexfileOutput = dsPrepareMEX(mfileInput, varargin)
0002 %PREPAREMEX - take an m-file path and compile it using the Matlab coder.
0003 %
0004 % See also: dsGetSolveFile
0005 %
0006 % Author: Jason Sherfey, PhD <jssherfey@gmail.com>
0007 % Copyright (C) 2016 Jason Sherfey, Boston University, USA
0008 
0009 % Input args
0010 args = varargin;
0011 
0012 % TODO: this block may be unnecesary since checkOptions can handle a struct
0013 if isstruct(args{1})
0014   % User specified an options structure
0015   keyvals = dsOptions2Keyval(args{1});
0016   % Convert it to keyvalues and prepend
0017   keyvals = horzcat(keyvals,args(2:end));
0018 else
0019   keyvals = args;
0020 end
0021 
0022 options=dsCheckOptions(keyvals,{...
0023   'verbose_flag',0,{0,1},... % set verbose to 1 by default
0024   'mex_dir_flag',1,{0,1},... % Flag to tell whether or not to search in mex_dir for pre-compiled solve files (solve*_mex*).
0025   'mex_dir',[],[],... % Directory to search for pre-compiled mex files. Can be relative to 'study_dir' or absolute path.
0026   'codegen_args',[],[],...
0027   'cluster_flag',0,{0,1},...
0028   },false);
0029 
0030 % % Example code for testing mex_dir options:
0031 % eqns={
0032 %   's=10; r=27; b=2.666'
0033 %   'dx/dt=s*(y-x)'
0034 %   'dy/dt=r*x-y-x*z'
0035 %   'dz/dt=-b*z+x*y'
0036 % };
0037 % data=dsSimulate(eqns, 'tspan',[0 100], 'ic',[1 2 .5],'verbose',1, 'solver','rk4', 'study_dir','demo_lorenz','compile_flag',1,'mex_dir_flag',0,'mex_dir',[]);
0038 % data=dsSimulate(eqns, 'tspan',[0 100], 'ic',[1 2 .5],'verbose',1, 'solver','rk4', 'study_dir','demo_lorenz','compile_flag',1,'mex_dir_flag',1,'mex_dir',[]);
0039 % data=dsSimulate(eqns, 'tspan',[0 100], 'ic',[1 2 .5],'verbose',1, 'solver','rk4', 'study_dir','demo_lorenz','compile_flag',1,'mex_dir_flag',1,'mex_dir','mexes_temp');
0040 if isempty(options.mex_dir)
0041     options.mex_dir = dsGetConfig('mex_path');
0042 end
0043 
0044 mex_dir = options.mex_dir;
0045 
0046 % make mex name from solve_file
0047 [fpath,fname] = fileparts(mfileInput);
0048 mexfileOutput = fullfile(fpath,[fname '_mex']);
0049 
0050 if ~exist(mexfileOutput,'file')
0051   if options.verbose_flag
0052     fprintf('Compiling file: %s\n',mfileInput);
0053     fprintf('            to: %s\n',mexfileOutput);
0054   end
0055   
0056   compile_start_time=tic;
0057   
0058   makeMex(mfileInput, options); % mex-file solver for solve file
0059   
0060   if options.verbose_flag
0061     fprintf('\tMEX generation complete!\n\tElapsed time: %g seconds.\n',toc(compile_start_time));
0062     %toc;
0063   end
0064   
0065   codemex_dir=fullfile(fileparts(mexfileOutput),'codemex');
0066   if exist(codemex_dir,'dir')
0067     if options.verbose_flag
0068       fprintf('\tRemoving temporary codemex directory: %s\n',codemex_dir);
0069     end
0070     rmdir(codemex_dir,'s');
0071   end
0072 else % mex file exists
0073   if options.verbose_flag
0074     fprintf('Using previous compiled file: %s\n',mexfileOutput);
0075   end
0076 end %if
0077 
0078 % If mex_dir is specified, back up the newly compiled mex files to this folder
0079 if ~isempty(mex_dir) && ~options.cluster_flag && options.mex_dir_flag
0080   [~,solvefile] = fileparts2(mfileInput);
0081   [~,mexfile] = fileparts2(mexfileOutput);
0082   
0083   if isempty(dir(fullfile(mex_dir,[solvefile '.m'])))
0084     if options.verbose_flag
0085       fprintf('Solve file %s does not yet exist in mex_dir %s. Copying... \n',solvefile,mex_dir);
0086     end
0087     if ~exist(mex_dir,'dir'); error('Cannot find %s! Make sure it exists and is specified as an *absolute* path',mex_dir); end
0088     copyfile(mfileInput,mex_dir);
0089   end
0090   
0091   if isempty(dir(fullfile(mex_dir,[mexfile '*'])))
0092     if options.verbose_flag
0093       fprintf('Mex file %s does not yet exist in mex_dir %s. Copying... \n',mexfile,mex_dir);
0094     end
0095     if ~exist(mex_dir,'dir'); error('Cannot find %s! Make sure it exists and is specified as an *absolute* path',mex_dir); end
0096     copyfile([mexfileOutput,'*'],mex_dir);
0097   end
0098 end
0099 
0100 end % main fn
0101 
0102 %% Subfunctions
0103 function makeMex(file, options)
0104 % Create a MEX configuration object
0105 cfg = coder.config('mex');
0106 
0107 % Turn on dynamic memory allocation
0108 cfg.DynamicMemoryAllocation = 'AllVariableSizeArrays';
0109 
0110 % Generate MEX function
0111 if isempty(options.codegen_args)
0112   eval(['codegen -d codemex -config cfg ',file]);
0113 else % codegen_args specified
0114   %eval(sprintf('codegen -args %s -d codemex -config cfg %s', 'options.codegen_args', file));
0115   eval(['codegen -args options.codegen_args -d codemex -config cfg ',file]);
0116 end
0117 % TODO: convert eval to feval or call to codegen
0118 
0119 end

Generated on Tue 12-Dec-2017 11:32:10 by m2html © 2005