Will wrote:
Here are the relevant alien definitions.

;; These are C functions called from the Lisp system of equations
function.
(def-alien-routine get_sysvar_n int)
(def-alien-routine get_sysvar_val double  (i int))
(def-alien-routine get_sysvar_dotval double  (i int))
(def-alien-routine set_sysvar_val void  (i int)  (x double))
(def-alien-routine set_sysvar_dotval void  (i int)  (x double))

Alien routines should be declared inline to reduce/eliminate consing, according to the manual.
;; this builds a part of the equations that calls C from
;; Lisp: get_sysvar_val(i), is a simple function that lets
;; Lisp access an array stored in C. (defun cvode-assign-sys-vars ()
  (let ((syslst))
    (dotimes (i (length *system-variables*) syslst)
(push `(setf (aref *system-variables* ,i) (get_sysvar_val ,i))
            syslst))))

;; here's the declaration of get_sysvar_val from above
double get_sysvar_val(int i) {return sys_var.vals[i];}

You could, if desired, declare the alien structure in Lisp and access them directly. But the alien interface should work fine too.
;; i call this function once for each of n variables for each ;; explicit time step passed to the solver. so, for one full ;; simulation, this function could be called hundreds of ;; times.
Which function are you talking about? get_sysvar_val? If so, declaring inline should help.
(DEF-CALLBACK MS_MODEL
                  (VOID
                   (TIME
                     DOUBLE))
                  (DECLARE (OPTIMIZE (SPEED 3) (SAFETY 0)))
                  (SETF (AREF *SYSTEM-VARIABLES* 1) (GET_SYSVAR_VAL 1))
                  (SETF (AREF *SYSTEM-VARIABLES* 0) (GET_SYSVAR_VAL 0))
Is *system-variables* declared to be an array of double-floats? That will reduce consing.
                  (SET_SYSVAR_DOTVAL 1
                                     (EQUATION-AGGREGATOR
EQUATION-AGGREGATOR will also cons. That seems unavoidable unless you make it a local function , inline it, or use block compilation.

It's not quite clear to me, but is ms_model generated once and solved? Then a new ms_model is generated and solved again for a different problem?

To reduce consing, I think the defcallback needs to be compiled too.

That's all I can think of right now, after looking at your code for a few minutes,

Ray


Reply via email to