From: "Sale, Mark " <>
Subject: NONMEM as a dll
Date: Fri, 4 Dec 1998 11:00:31 -0500

Dear users,
A good number of nonmem users seem to be increasingly interested in applying "numerically intensive" methods to these analysis (as if they weren't numerically intensive enough). These include bootstrap, jackknife, Monte Carlo simulation, sensitivity analysis and others. We have had a little experience with this at GlaxoWellcome, and I'd like to share some experiences, and hope for some help as well. We've found that the most effective way to do this (on a pc) is to compile NONMEM as a dll (dynamic link library) that can be called from another application. For example, for a bootstrap, on might use an application (SAS, Splus, Excel, Matlab) to sample the data, generate a data set, run NONMEM, and compile the results for 1000 runs. Users on a PC who do this with a system escape (e.g., shell() in Visual basic and Visual basic for applications and the bang (!) operator in Matlab) will find that a new process is created for each run. The problem is that this basically disables you computer, since that new process (a command/DOS window) will have the focus, so, as you are typing you latest report/grant in Word you suddently switch over to this command window. Very inconvenient. In some applications the child process will not inheirate the priority of the parent process, so even if you do set the priority of Excel to low the NONMEM run will be at normal priority anyway, markedly reducing the responsiveness of your computer. (To set the priorty to low on NT hit ctrl-alt-delete, click task master, select the processes tab, click on the CPU header, right mouse button over the process name (nonmem.exe), select Set priority and then Low.)

We have worked out a partial solution to this. This solution is specific to Matlab, but I'm hoping that someone more familiar than I with Windows API can make it more general.

For a NONMEM bootstrap analysis, we compile NONMEM as a dll, generate the data set and run NONMEM. We compile the output (THETA, OMEGA, SIGMA etc) with a call from INFN, writing the values to a file, then reading them into Matlab. To do this (compile NONMEM as a dll) some changes are required in the source code. There are two major issues and one minor issue.
1. There is a lot of output to the console/standard output in NONMEM (MONITERING OF SEARCH etc). There is not standard output for a dll, there is no DOS window to write to. This will cause an immediate crash of the calling application. (dlls are very unforgiving).
2. NONMEM terminates from CFILES with a STOP command. This will terminate not only NONMEM, but the calling application.

Minor issue.
The data file size will usually change between runs. NMTRAN writes the number of data records to FCON, which is read by NONMEM. Fortunately in version 5, this can be set to 0 (and in fact is by NMTRAN if the number of records is > 9999). Alternatively, you can just run NMTRAN (without compiling) for each run, takes only about a second if the data set is small.

You'll need to run NMTRAN if you aren't writing formatted data anyway.

Changes to NONMEM source code to compile as dll:

nonmem.for - Make NONMEM a subroutine rather than a program

BLKDAT.for - Make standard output/console output to 'nul'
line 26 change 'con' to 'nul' to read:


cfiles.for Make sure that FCON (unit 5) and OUTPUT files (unit 6) get closed.
line 28 add close (5)
line 29 uncomment close (6)

and finally the part specific to Matlab cfiles. Can't use STOP command.
line 64 change STOP to CALL mexErrMsgTxt('ending nonmem')

mexErrMsgTxt is a routine in Matlab that returns an error code, and returns control to Matlab.

My question is (for those who have made it this far) is:
Is there a Windows API call the is similar to mexErrMsgTxt, i.e., that returns control to the calling application from a dll. Note that the "usual" way to do this is to return to the "entry point" subroutine in the dll, and get to a RETURN and END. Looking at the code (and discussing with Stu) this is not practical in NONMEM as it is currently written, CFILES is called from dozens of places, often many calls deep from the entry subroutine (which is NONMEM.for). To do this would be a monumental rewrite. Any help is appreciated, and I'd be happy to supply code for the required gateway routine in Matlab to anyone interested.


Mark Sale M.D.
5 Moore Drive 17.2219
PO Box 13398
RTP NC 27709


From: Pascal Girard <>
Subject: Re: NONMEM as a dll
Date: Fri, 04 Dec 1998 17:50:41 +0100

Dear Mark,

Thank you for sharing with us your experience.

My personal experience (I'm not a specialist in computer) is that finding tricks to trick the MS Windows 95 and NT "gaz factory" gives me an headache. That why I do prefer to keep a computer running under UNIX and implement what you suggest using some very simple C-shell. The Linux system seems now to become a very good UNIX alternative to MS Windows and has the advantage of being free. (you even can use OCTAVE, a Matlab free equivalent that runs under Linux see as well as the R program, a free equivalent of Splus).

Another solution that has the advantage of keeping the MS Win OS is to use UNIX native freeware tools as grep, awk that runs in a DOS windows (see the Fri, 9 Jan 1998 09:49:29 +1300 Email From: Nick Holford about bootstrap). PERL should also be very helpful for such purpose.

Pascal Girard
Service Pharmacologie Clinique
BP 3041,162, avenue Lacassagne Tel : +33 (0)4 78 78 57 26
69394 LYON Cedex 03 FRANCE Fax : +33 (0)4 78 53 10 30


From: "Eltahtawy, Ahmed" <>
Subject: RE: NONMEM as a dll
Date: Fri, 4 Dec 1998 13:27:46 -0500

Dear Girard,

Thank you for a valuable advice; I have the same experience with UNIX; running it (NONMEM & Monte Carlo simulations) for years without major problems.

How to get the free Linux? and do you think I can still use my C-shell, AWK, GREP scripts of UNIX on the Linux without any changes.

Thanks again


From: Nick Holford <>
Subject: Re: RE: NONMEM as a dll
Date: Fri, 4 Dec 1998 10:57:04 +0000 (GMT)

> > From: Pascal Girard []
> > Sent: Friday, December 04, 1998 11:51 AM
> > To: Sale, Mark
> > Cc: ''
> > Subject: Re: NONMEM as a dll

> > My personal experience (I'm not a specialist in computer) is that
> > finding tricks to trick the MS Windows 95 and NT "gaz factory"
> > gives me an headache. That why I do prefer to keep a computer
> > running under UNIX and implement what you suggest using some
> > very simple C-shell. The Linux system seems now to become a very
> > good UNIX alternative to MS Windows and has the advantage of being free.

I agree with this advice. Some limited benchmarking of Linux suggests it may be a much as 3 times faster running NONMEM on the same machine compared with running in a DOS shell under Windoze.

Nick Holford, L226,Dept of Neurology,OHSU
3181 SW Sam Jackson Park Road,Portland,OR 97201-3098,(503)494-4778,fax 494-7242


From: Niclas Jonsson <niclas@c255t.ucsf.EDU>
Subject: RE: NONMEM as a dll
Date: Fri, 4 Dec 1998 11:16:53 -0800 (PST)

We are using Linux as the operating system on two of our (NONMEM) computing servers. The first is a single processor P2 333 MhZ with 96Mb of RAM and the second is a dual processor P2 333 MhZ machine with 164Mb RAM. In addition, I have Linux installed on my laptop (Pentium 233 MhZ, 96Mb).

In my experience, Linux is a *very* suitable system for running NONMEM on, both in terms of stability (it is certainly more stable than Win95 but also more stable than Win NT (personal experience)) and price (the Linux OS and fortran compiler is free and the hardware is the same as a regular Win95/NT PC). My impression is also that the same run is faster under Linux than under Win95/NT, although I haven't formally benchmarked this.

As for Ahmeds question about the compatibility of the classical unix programs (awk,sed and so on). In my experience the answer is yes! Except for path specifications, I can run my perl, sed, awk and csh scripts on both the Linux machines and our HP workstations.

Linux comes in a number of different distributions, which can generally be obtained without cost from the Internet. They can also be purchased (and you pay for the distribution media and documentation) fairly cheaply. The latter way of obtaining Linux is probably the best unless you have experience installing Linux and/or unix-like systems administration. Information about Linux in general can be obtained from the URL

Personally I use the RedHat 5.1 distribution, which comes with the GNU Fortran compiler. With this compiler, NONMEM V compiles "out of the box".

As Pascal points out, octave is a free "MathLab equivalent". But the real thing is also available (i.e. MathLab) for Linux, as is Mathematica, S-PLUS (version 5.0) and a lot of other technical programs.

Niclas Jonsson


From: "Steven L. Shafer" <Steven.Shafer@Stanford.Edu>
Subject: RE: NONMEM as a dll
Date: Fri, 4 Dec 1998 13:41:48 -0800

Dear Mark:

I've been VERY happy with a dual processor machine, and I would guess this will address the problem you refer to below (as long as you don't start two runs at once). My machine is a Pentium Pro 200 (dual 200 chips), which isn't particularly fast or expensive by today's PC standards. However, ANY pc with a single processor will find other tasks significantly slowed by NONMEM, regardless of processor speed, simply because most CPU cycles are consumed by NONMEM. By contrast, although the NONMEM run is slower on my PC than on a standard 400 MHz Pentium II, the delay is typically only a few minutes, and in the mean time my e-mail, word processing, and other tasks proceed as usual on the other processor, all of which is entirely transparent to the user.

Best regards,
Steve Shafer


From: Larry Bauer <>
Subject: Re: RE: NONMEM as a dll
Date: Fri, 4 Dec 1998 14:42:02 -0800 (Pacific Standard Time)

I agree with Nick, et al. I have been using the Slackware version of Linux for several years. The earilier versions used f77, which is a preprocessor for the gcc compiler. Now the g77 compiler is used, and it is pretty quick. A P133 with 32Mb of RAM is about the same speed for NONMEM as a SparcStation 10 with Sun's Fortran compiler.

The Slackware version of Linux is available at or I downloaded it to my hard drive, then made the installation disks on my local machine.



From: "Eltahtawy, Ahmed" <>
Subject: RE: NONMEM as a dll
Date: Mon, 7 Dec 1998 11:43:20 -0500

Thank you for sharing this valuable info. I have already visited different web sites for linux. I found ,as you mentioned, redhat site to be very useful for the first time users of linux. It is really very assuring good news to know there is an operating system that is faster, more stable, compatible with different platforms, runs a lot of technical programs, and is FREE.

thanks again


From: "Harnisch, Lutz, HMR/DE" <>
Subject: AW: NONMEM as a dll
Date: Mon, 7 Dec 1998 18:25:00 +0100

We also do using Linux as a plaform for running NONMEM, but unfortunately it's compared to the Alpha and/or SGI platform a bit to slow. Therefore I would like to ask for some advice in using the gnu fortran77 to optimize NONMEM.

Lutz Harnisc
Hoechst Marion Roussel | Product Realization | Biodynamics
Building H840 | Room 449 | D-65926-Frankfurt-Main | Germany
phone +49-69-305-16481 | fax +49-69-305-81990 |


From: Niclas Jonsson <niclas@c255t.ucsf.EDU>
Subject: NONMEM under Linux (was: NONMEM as a dll)
Date: Mon, 7 Dec 1998 09:31:19 -0800 (PST)

The g77 options we use to compile NONMEM V with is -O and -ff90.

Niclas Jonsson


From: "Gibiansky, Ekaterina" <>
Subject: RE: NONMEM as a dll
Date: Tue, 8 Dec 1998 09:28:04 -0500


check whether this helps:

>I've been thinking about the problem with the fortran library, so I went to
>comp.lang.fortran discussion group and looked up for "exception handling."I
>found that Sun supports setjmp and longjmp functions for its fortran
>compiler, and they work similar to the same functions in C. However, they
>are not a part of the standard for fortran. Another thing that I've found is
>something called "alternative return," but I've never programmed in fortran,
>so I couldn't get all the details.

As I understand, setjmp works as a label to which you return when you call longjmp. And presumably, it can be set right before end in nonmem.for and called in place of STOP in CFILES. If only, these functions (or similar) are supported by DIGITAL FORTRAN.

Katya Gibiansky
GloboMax LLC
7250 Parkway Drive, Suite 430
Hanover, MD 21076
Voice (410) 782-2234
FAX (410) 712-0737