2015-11-03 61 views
2

我正在通过指向this C source file的函数gamma()的R源代码。R语言源代码 - C代码中的NOMORE_FOR_THREADS是什么

代码的片段如下:

#ifdef NOMORE_FOR_THREADS 
    static int ngam = 0; 
    static double xmin = 0, xmax = 0., xsml = 0., dxrel = 0.; 

    /* Initialize machine dependent constants, the first time gamma() is called. 
    FIXME for threads ! */ 
    if (ngam == 0) { 
    ngam = chebyshev_init(gamcs, 42, DBL_EPSILON/20);/*was .1*d1mach(3)*/ 
    gammalims(&xmin, &xmax);/*-> ./gammalims.c */ 
    xsml = exp(fmax2(log(DBL_MIN), -log(DBL_MAX)) + 0.01); 
    /* = exp(.01)*DBL_MIN = 2.247e-308 for IEEE */ 
    dxrel = sqrt(DBL_EPSILON);/*was sqrt(d1mach(4)) */ 
    } 
#else 
/* For IEEE double precision DBL_EPSILON = 2^-52 = 2.220446049250313e-16 : 
* (xmin, xmax) are non-trivial, see ./gammalims.c 
* xsml = exp(.01)*DBL_MIN 
* dxrel = sqrt(DBL_EPSILON) = 2^-26 
*/ 
# define ngam 22 
# define xmin -170.5674972726612 
# define xmax 171.61447887182298 
# define xsml 2.2474362225598545e-308 
# define dxrel 1.490116119384765696e-8 
#endif 

现在我明白了,这一段代码初始化的变量值像ngamxsmldxrel等,但我不明白什么是NOMORE_FOR_THREADS

我找不到定义的位置,所以我很困惑它何时会进入#ifdef子句,或者只是进入#else子句并根据IEEE标准定义变量。

如果任何人都可以对NOMORE_FOR_THREADS或其定义的位置有所了解,那将非常有帮助。

感谢您的帮助。

回答

2

貌似是#ifdef在修订13433加入:

> svn log -vr13433 
------------------------------------------------------------------------ 
r13433 | maechler | 2001-04-10 10:05:54 -0500 (Tue, 10 Apr 2001) | 2 lines 
Changed paths: 
    M /trunk/src/nmath/beta.c 
    M /trunk/src/nmath/gamma.c 
    M /trunk/src/nmath/lgamma.c 
    M /trunk/src/nmath/lgammacor.c 

globals now #ifdef NOMORE_FOR_THREADS 

------------------------------------------------------------------------ 

而这里和以前版本的差异:

> svn diff -r13432:13433 src/nmath/gamma.c 
Index: src/nmath/gamma.c 
=================================================================== 
--- src/nmath/gamma.c (revision 13432) 
+++ src/nmath/gamma.c (revision 13433) 
@@ -1,7 +1,7 @@ 
/* 
    * Mathlib : A C Library of Special Functions 
    * Copyright (C) 1998 Ross Ihaka 
- * Copyright (C) 2000 The R Development Core Team 
+ * Copyright (C) 2000-2001 The R Development Core Team 
    * 
    * This program is free software; you can redistribute it and/or modify 
    * it under the terms of the GNU General Public License as published by 
@@ -40,7 +40,7 @@ 

double gammafn(double x) 
{ 
- static /* const */ double gamcs[42] = { 
+ const double gamcs[42] = { 
    +.8571195590989331421920062399942e-2, 
    +.4415381324841006757191315771652e-2, 
    +.5685043681599363378632664588789e-1, 
@@ -89,6 +89,7 @@ 
    double y; 
    double sinpiy, value; 

+#ifdef NOMORE_FOR_THREADS 
    static int ngam = 0; 
    static double xmin = 0, xmax = 0., xsml = 0., dxrel = 0.; 

@@ -101,6 +102,18 @@ 
    /* = exp(.01)*DBL_MIN = 2.247e-308 for IEEE */ 
    dxrel = sqrt(1/DBL_EPSILON);/*was (1/d1mach(4)) */ 
    } 
+#else 
+/* For IEEE double precision DBL_EPSILON = 2^-52 = 2.220446049250313e-16 : 
+ * (xmin, xmax) are non-trivial, see ./gammalims.c 
+ * xsml = exp(.01)*DBL_MIN 
+ * dxrel = sqrt(1/DBL_EPSILON) = 2^26 
+*/ 
+# define ngam 22 
+# define xmin -170.5674972726612 
+# define xmax 171.61447887182298 
+# define xsml 2.2474362225598545e-308 
+# define dxrel 67108864. 
+#endif 

    if(ISNAN(x)) return x; 

我没有看到NOMORE_FOR_THREADS任何地方定义在源代码中该修订版,接下来的几百次修订版,或者在当前的修订版中。有可能(尽管看起来不太可能)可以在某些系统头文件中定义它。它似乎更有可能通过命令行参数来指定。

+0

有可能这段代码什么都不做,因为R是单线程的? – Sid

+0

@Sid:可能的,并且可能支持来自其他应用程序的R数学库的多线程使用。但这只是猜测。 –

+0

也许这是来自Fortan的翻译或者从未完成的东西......从R调用的C程序当然可以使用OpenMP或pthreads,因此它们可能从未完成实现。同时,看起来更像是他们正在检查可能定义NOMORE_FOR_THREADS的特定平台的精度,但我意识到这不是一个明确的答案。 – JimLohse