Replies: 9 comments 18 replies
-
@ssun30 ... thank you for starting the discussion! I am not using the MATLAB interface much, but will try to answer. I also apologize that some of my working knowledge dates back to MEX files as they stood in the early 2000's. (Which is, however, consistent with what is used in the current MATLAB implementation). I am responding to the first two questions here; the other questions are better served in separate threads here.
What you see is a legacy structure of MATLAB classes (they since have moved to a newer implementation, see here), which are organized as a folder/file structure where You are correct about
In a nutshell, the execution is anything but straight-forward. Let's pick function rho = density(tp)
% DENSITY Get the density.
% rho = density(tp)
% :param tp:
% Instance of class :mat:func:`ThermoPhase` (or another
% object that derives from ThermoPhase)
% :return:
% Mass density. Units: kg/m**3
%
rho = phase_get(tp.tp_id, 2); which calls into the private function i = phase_get(n, job, a, b)
if nargin == 2
i = ctmethods(30, n, job);
elseif nargin == 3
i = ctmethods(30, n, job, a);
else
i = ctmethods(30, n, job, a, b);
end As is immediately clear, it's pretty hard to know what's going on (everything is handled by a single interface: To look into details for this example: per In short: the current MATLAB interface has no idea that the Cantera core code uses objects at all. Interactions use magic numbers and integer object ID's, which is not intuitive and hard to expand on and/or to maintain. It would be obviously much easier to interact with C++ objects directly; it probably would also make sense to move the syntax to something that is clearly recognizable as an object-oriented approach, e.g. allowing for the The way forward has two main options:
|
Beta Was this translation helpful? Give feedback.
-
This depends. You are absolutely correct that loops have to be avoided at all cost (I believe the speedups are even larger than what you reported). For handling of multiple states, have a look at Python |
Beta Was this translation helpful? Give feedback.
-
I would assume yes, although I'm not familiar with MATLAB's C++ integration. For a general approach, look at |
Beta Was this translation helpful? Give feedback.
-
Just wanted to cross-link Ingmar's comment on another thread, which seems relevant: #97 (reply in thread) |
Beta Was this translation helpful? Give feedback.
-
I would say start with trying to wrap the classes |
Beta Was this translation helpful? Give feedback.
-
As this is going to be a backwards-compatibility-breaking API change for MATLAB, I'm wondering what we should do when different interfaces behave differently. Should we try to "unify" them to make them more similar? For example, setting mole fractions with and without normalization. MATLAB (currently) there is a single function This goes via the C API, where there is a single method But in Python, These correspond to (and call directly, via Cython) the two C++ methods So MATLAB and C have a single method with a "norm" parameter (in one case a string, in the other an integer), but Python and C++ have two different methods. If we're aiming for a code-generation approach to interfaces (the MATLAB and potentially Fortran) then presumably a one-to-one mapping of methods would be simplest. But mapping to the C interface or mapping to the C++ interface? (And if mapping to the C interface, should this example case use |
Beta Was this translation helpful? Give feedback.
-
Here is a link to the Cantera Lite proof of concept: https://github.com/comocheng/CanteraLite. |
Beta Was this translation helpful? Give feedback.
-
Here's the first proof-of-concept toolbox for testing: This version of the toolbox directly loads cantera_shared.dll using Matlab's calllib feature. All functions are called directly from the C library. A readme file is included in the 'Utility' folder. As for using MEX, it is possible to completely get rid of the magic numbers if instead of one MEX interface (ctmethods) encompassing all methods we just make a bunch of individual MEX functions that are private to each class in Matlab. It's just a convention in MEX programming to have a centralized interface routine calling computational routines but doesn't have to be that way. I've been struggling with compiling MEX code in Matlab (using the built-in mex command and MinGW64). It's frequently throwing the error that it couldn't find the directory of all the dependent header files. How is the current ctmethods MEX file compiled? |
Beta Was this translation helpful? Give feedback.
-
Fwiw, I noticed that there is now a GH matlab-action that should be useful for testing purposes. |
Beta Was this translation helpful? Give feedback.
-
I will be posting questions regarding implementing a new MATLAB interface based on direct C++ library integration introduced in MATLAB 2019a.
Questions I have as of 06/03/21:
What makes the ctmethods MEX file difficult to maintain?
Is there a self-contained module of cantera that I could use to test direct C++ integration? Currently I'm working with a very small experimental library I wrote myself (only two header files) which doesn't give me a good idea of the complexity and scale of integrating a large library.
Another way is to re-write each C++ function into MEX which obviously we are not going to do. But one major advantage of coding in MEX is that it could use MATLAB's powerful vectorization capability to drastically speed up vector and matrix operations (I'm seeing at least 10x and potentially more with GPU). On the other hand MATLAB is extremely slow with sequential operations (like FOR loops). Is there some part of cantera that could benefit from parallelization but could not be easily vectorized in C++? If so then I think at least these could benefit from having a native MEX code for MATLAB to call directly.
Beta Was this translation helpful? Give feedback.
All reactions