Hi, I'm trying to get a toy program making use of nmmin to run successfully. I've gotten to the point of compiling. However, when I attempt to run my executable, I guess a bus error. I see that someone else has asked about using nmmin before <http://tolstoy.newcastle.edu.au/R/help/06/03/23944.html>, but I haven't come across any replies. Is there some documentation on how to call nmmin apart the optim help page and the page in the R extensions manual giving the declaration? What do I need to change in the below to get it to work? (I also get a bus error with trace set to 0.) The source code and an example session: nmminDemo.c: #include <R_ext/Applic.h> #include <stdio.h> double parabola(int n, double *par, void *ex) { double xm = par[0] - 1; return xm * xm + 13; } int main() { double initial[1] = {1.5}; double result[1]; double value; int convergenceCode; const double abstol = 1e-16; const double reltol = 1e-8; const double alpha = 1.0; /* reflection factor */ const double beta = 0.5; /* contraction factor */ const double gamm = 2.0; /* expansion factor */ const int trace = 4; /* tracing on */ int fncount; const int maxit = 10000; nmmin(1, initial, result, &value, parabola, &convergenceCode, abstol, reltol, NULL, alpha, beta, gamm, trace, &fncount, maxit); printf("fncount: %d\n", fncount); printf("convergence code: %d\n", convergenceCode); printf("min of %f at x = %f\n", value, result[0]); return 0; } ------ $ gcc nmminDemo.c -g -I/Library/Frameworks/R.framework/Versions/2.1.1/Resources/include -L/Library/Frameworks/R.framework/Versions/2.1.1/Resources/lib -lR $ ./a.out Bus error $ gdb a.out GNU gdb 5.3-20030128 (Apple version gdb-330.1) (Fri Jul 16 21:42:28 GMT 2004) ... (gdb) run Starting program: /Users/dfaden/Desktop/R/a.out Reading symbols for shared libraries +......... done Program received signal EXC_BAD_ACCESS, Could not access memory. 0x00000000 in ?? () (gdb) bt #0 0x00000000 in ?? () Cannot access memory at address 0x0 Cannot access memory at address 0x0 #1 0x0054dd5c in REvprintf (format=0x5f0dc8 "%s", arg=0xbfffb5fc "") at ../../../../R-2.1.1/src/main/printutils.c:541 #2 0x0054dab0 in REprintf (format=0xbfff9570 "Error: invalid connection\n") at ../../../../R-2.1.1/src/main/printutils.c:458 #3 0x004de8c4 in verrorcall_dflt (call=0x0, format=0x5f0dc8 "%s", ap=0xbfffda30 "???P") at ../../../../R-2.1.1/src/main/errors.c:470 #4 0x004dea1c in Rf_errorcall (call=0x5f0dc8, format=0xbfffb5fc "") at ../../../../R-2.1.1/src/main/errors.c:514 #5 0x004deb38 in Rf_error (format=0xbfff9570 "Error: invalid connection\n") at ../../../../R-2.1.1/src/main/errors.c:538 #6 0x004acc18 in getConnection (n=-1073769104) at ../../../../R-2.1.1/src/main/connections.c:95 #7 0x0054dc04 in Rvprintf (format=0x5fe004 " Nelder-Mead direct search function minimizer\n", arg=0xbffffb7c "???P???X???`") at ../../../../R-2.1.1/src/main/printutils.c:494 #8 0x0054da70 in Rprintf (format=0xbfff9570 "Error: invalid connection\n") at ../../../../R-2.1.1/src/main/printutils.c:445 #9 0x0051bcec in nmmin (n=6575856, Bvec=0x5f0dc8, X=0xbffffd58, Fmin=0xbffffd60, fminfn=0x2b8c <parabola>, fail=0xbffffd68, abstol=9.9999999999999998e-17, intol=1e-08, ex=0x0, alpha=1, bet=0.5, gamm=2, trace=4, fncount=0xbffffd9c, maxit=1000) at ../../../../R-2.1.1/src/main/optim.c:667 #10 0x00002ce0 in main () at nmminDemo.c:30 ----- I'm working under Mac OS X 10.3.9 with gcc 3.3 and R platform powerpc-apple-darwin7.9.0 arch powerpc os darwin7.9.0 system powerpc, darwin7.9.0 status major 2 minor 1.1 year 2005 month 06 day 20 language R Thank you. David
bus error on calling nmmin
3 messages · Brian Ripley, David Faden
You appear to be trying to call nmmin from a standalone program. You linked against -lR, but failed to initialize R, hence the segfault. nmmin is not a part of R that is made available except to a running R process: it is documented in `Writing R Extensions' for use in R packages.
On Thu, 13 Apr 2006, David Faden wrote:
Hi, I'm trying to get a toy program making use of nmmin to run successfully. I've gotten to the point of compiling. However, when I attempt to run my executable, I guess a bus error. I see that someone else has asked about using nmmin before <http://tolstoy.newcastle.edu.au/R/help/06/03/23944.html>, but I haven't come across any replies.
In both his case and yours, the problem is that what you are trying to do is well-documented in Writing R Extensions, and people are reluctant both to write manuals out here.
Is there some documentation on how to call nmmin apart the optim help
page and the page in the R extensions manual giving the declaration?
What do I need to change in the below to get it to work? (I also get a
bus error with trace set to 0.)
The source code and an example session:
nmminDemo.c:
#include <R_ext/Applic.h>
#include <stdio.h>
double parabola(int n, double *par, void *ex) {
double xm = par[0] - 1;
return xm * xm + 13;
}
int main()
{
double initial[1] = {1.5};
double result[1];
double value;
int convergenceCode;
const double abstol = 1e-16;
const double reltol = 1e-8;
const double alpha = 1.0; /* reflection factor */
const double beta = 0.5; /* contraction factor */
const double gamm = 2.0; /* expansion factor */
const int trace = 4; /* tracing on */
int fncount;
const int maxit = 10000;
nmmin(1, initial, result, &value,
parabola, &convergenceCode,
abstol, reltol,
NULL,
alpha, beta, gamm,
trace, &fncount, maxit);
printf("fncount: %d\n", fncount);
printf("convergence code: %d\n", convergenceCode);
printf("min of %f at x = %f\n", value, result[0]);
return 0;
}
------
$ gcc nmminDemo.c -g
-I/Library/Frameworks/R.framework/Versions/2.1.1/Resources/include
-L/Library/Frameworks/R.framework/Versions/2.1.1/Resources/lib -lR
$ ./a.out Bus error
$ gdb a.out
GNU gdb 5.3-20030128 (Apple version gdb-330.1) (Fri Jul 16 21:42:28 GMT 2004)
...
(gdb) run
Starting program: /Users/dfaden/Desktop/R/a.out
Reading symbols for shared libraries +......... done
Program received signal EXC_BAD_ACCESS, Could not access memory.
0x00000000 in ?? ()
(gdb) bt
#0 0x00000000 in ?? ()
Cannot access memory at address 0x0
Cannot access memory at address 0x0
#1 0x0054dd5c in REvprintf (format=0x5f0dc8 "%s", arg=0xbfffb5fc "")
at ../../../../R-2.1.1/src/main/printutils.c:541
#2 0x0054dab0 in REprintf (format=0xbfff9570 "Error: invalid
connection\n") at ../../../../R-2.1.1/src/main/printutils.c:458
#3 0x004de8c4 in verrorcall_dflt (call=0x0, format=0x5f0dc8 "%s",
ap=0xbfffda30 "???P") at ../../../../R-2.1.1/src/main/errors.c:470
#4 0x004dea1c in Rf_errorcall (call=0x5f0dc8, format=0xbfffb5fc "")
at ../../../../R-2.1.1/src/main/errors.c:514
#5 0x004deb38 in Rf_error (format=0xbfff9570 "Error: invalid
connection\n") at ../../../../R-2.1.1/src/main/errors.c:538
#6 0x004acc18 in getConnection (n=-1073769104) at
../../../../R-2.1.1/src/main/connections.c:95
#7 0x0054dc04 in Rvprintf (format=0x5fe004 " Nelder-Mead direct
search function minimizer\n", arg=0xbffffb7c "???P???X???`") at
../../../../R-2.1.1/src/main/printutils.c:494
#8 0x0054da70 in Rprintf (format=0xbfff9570 "Error: invalid
connection\n") at ../../../../R-2.1.1/src/main/printutils.c:445
#9 0x0051bcec in nmmin (n=6575856, Bvec=0x5f0dc8, X=0xbffffd58,
Fmin=0xbffffd60, fminfn=0x2b8c <parabola>, fail=0xbffffd68,
abstol=9.9999999999999998e-17, intol=1e-08, ex=0x0, alpha=1, bet=0.5,
gamm=2, trace=4, fncount=0xbffffd9c, maxit=1000) at
../../../../R-2.1.1/src/main/optim.c:667
#10 0x00002ce0 in main () at nmminDemo.c:30
-----
I'm working under Mac OS X 10.3.9 with gcc 3.3 and R
platform powerpc-apple-darwin7.9.0
arch powerpc
os darwin7.9.0
system powerpc, darwin7.9.0
status
major 2
minor 1.1
year 2005
month 06
day 20
language R
Thank you.
David
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
2006/4/14, Prof Brian Ripley <ripley at stats.ox.ac.uk>:
You appear to be trying to call nmmin from a standalone program. You linked against -lR, but failed to initialize R, hence the segfault. nmmin is not a part of R that is made available except to a running R process: it is documented in `Writing R Extensions' for use in R packages.
Thanks. Initializing R does seem to have fixed the problem. In this, I
have followed the code in tests/Embedding, in particular
embeddedRCall.c. I was led to this code by Section 7.1 of "Writing R
Extensions."
Below is the corrected code and an example session:
#include <R_ext/Applic.h>
#include <stdio.h>
double parabola(int n, double *par, void *ex) {
double xm = par[0] - 1;
return xm * xm + 13;
}
/*
* Copied from tests/Embedded/embeddedRCall.c:
*/
extern int Rf_initEmbeddedR(int argc, char *argv[]);
int main()
{
char *argv[]= {"nmminDemo", "--gui=none", "--silent"};
const int argc = 3;
double initial[1] = {1.5};
double result[1];
double value;
int convergenceCode;
/*
* The following values are based on the help
* page for optim.
*/
const double abstol = 1e-16;
const double reltol = 1e-8;
const double alpha = 1.0; /* reflection factor */
const double beta = 0.5; /* contraction factor */
const double gamm = 2.0; /* expansion factor */
const int trace = 0; /* tracing on */
int fncount;
const int maxit = 10000;
Rf_initEmbeddedR(argc, argv);
nmmin(1, initial, result, &value, parabola,
&convergenceCode, abstol, reltol,
NULL, alpha, beta, gamm,
trace, &fncount, maxit);
printf("fncount: %d\n", fncount);
printf("convergence code: %d\n", convergenceCode);
printf("min of %f at x = %f\n", value, result[0]);
return 0;
}
----
$ gcc nmminDemo.c -g
-I/Library/Frameworks/R.framework/Versions/2.1.1/Resources/include
-L/Library/Frameworks/R.framework/Versions/2.1.1/Resources/lib -lR
$ R CMD ./a.out
fncount: 24
convergence code: 0
min of 13.000000 at x = 1.000195
--
David Faden, dfaden at iastate.edu
AIM: pitulx