Add patch code to Neuron on Ubuntu

Post Reply
jackfolla
Posts: 48
Joined: Wed Jul 07, 2010 7:42 am

Add patch code to Neuron on Ubuntu

Post by jackfolla »

Dear all,
I want apply a patch code to Neuron on my Ubuntu v. 10.10.

This patch modifies 3 files: "fadvance.c" (in "/home/MY_NAME/neuron/nrn-7.1/src/nrnoc"), "hoc_init.hoc" and "math.c" (both in "/home/MY_NAME/neuron/nrn-7.1/src/oc").

In "/home/MY_NAME/neuron/nrn-7.1" I ran:

$ patch p1 < roundoff.patch ("roundoff.patch" is a patch that hines sent me some time ago).

Later, in "/home/MY_NAME/neuron/nrn-7.1/src" I ran:
$ make clean
$ make

Finally, when I call "dbl_precision()" (that is one of the functions added to Neuron) in test file hhchain.hoc, this is the output:

Code: Select all

nrniv: dbl_precision undefined function
 in hhchain.hoc near line 21
 }
  ^
        dbl_precision(53)
I have done something wrong?

Thanks for your help.

Pasquale.
hines
Site Admin
Posts: 1687
Joined: Wed May 18, 2005 3:32 pm

Re: Add patch code to Neuron on Ubuntu

Post by hines »

Later, in "/home/MY_NAME/neuron/nrn-7.1/src" I ran:
$ make clean
$ make
You also need to do
make install
jackfolla
Posts: 48
Joined: Wed Jul 07, 2010 7:42 am

Re: Add patch code to Neuron on Ubuntu

Post by jackfolla »

Right, I forgot this last step.

Now, there are this two situations:

1) On my old PC (dual-core 32 bit, with Ubuntu 9.04), the test "hhchain.hoc" returns this output:

Code: Select all

22.0516626398788453
22.0516626399454623
mantissa precision 53 bits, result diff 6.66169e-11
22.0516626398788453
22.0516626399454623
mantissa precision 52 bits, result diff 6.66169e-11
22.0516626398670113
22.0516626398781455
mantissa precision 51 bits, result diff 1.11342e-11
22.0516626398671463
22.0516626399858708
mantissa precision 50 bits, result diff 1.18725e-10
22.0516626399963727
22.0516626398808278
mantissa precision 49 bits, result diff -1.15545e-10
22.0516626398756515
22.0516626399483116
mantissa precision 48 bits, result diff 7.26601e-11
22.0516626399931681
22.0516626399820552
mantissa precision 47 bits, result diff -1.11129e-11
22.0516626399841371
22.0516626399790923
mantissa precision 46 bits, result diff -5.04485e-12
22.0516626399930331
22.0516626399897078
mantissa precision 45 bits, result diff -3.32534e-12
22.0516626400203322
22.0516626398273203
mantissa precision 44 bits, result diff -1.93012e-10
22.0516626399274855
22.0516626399292974
mantissa precision 43 bits, result diff 1.81188e-12
22.0516626398620659
22.0516626398641122
mantissa precision 42 bits, result diff 2.04636e-12
22.0516626397934203
22.051662639873669
mantissa precision 41 bits, result diff 8.02487e-11
22.0516626396694591
22.0516626398787992
mantissa precision 40 bits, result diff 2.0934e-10
22.051662639652335
22.05166263982602
mantissa precision 39 bits, result diff 1.73685e-10
22.0516626392801385
22.0516626392801385
mantissa precision 38 bits, result diff 0
22.0516626390251105
22.0516626390251105
mantissa precision 37 bits, result diff 0
22.0516626378542071
22.0516626379133243
mantissa precision 36 bits, result diff 5.91172e-11
22.0516626360098655
22.0516626360098655
mantissa precision 35 bits, result diff 0
22.0516626330289114
22.0516626330289114
mantissa precision 34 bits, result diff 0
22.0516626235977498
22.0516626235977498
mantissa precision 33 bits, result diff 0
22.0516626094363062
22.0516626094363062
mantissa precision 32 bits, result diff 0
	32 
So it runs very well.

2) On my new PC (MacBook Pro, dual-core 64bit, with Ubuntu 10.10) the output is the following:

Code: Select all

22.0516626398731006
22.0516626398923066
mantissa precision 53 bits, result diff 1.9206e-11
22.0516626398731006
22.0516626398923066
mantissa precision 52 bits, result diff 1.9206e-11
22.0516626398731006
22.0516626398923066
mantissa precision 51 bits, result diff 1.9206e-11
22.0516626398731006
22.0516626398923066
mantissa precision 50 bits, result diff 1.9206e-11
......................
......................
22.0516626398731006
22.0516626398923066
mantissa precision 33 bits, result diff 1.9206e-11
22.0516626398731006
22.0516626398923066
mantissa precision 32 bits, result diff 1.9206e-11
	32 
In case 2), it is as if the function "dbl_precision" wasn't called.

Maybe there are differences between the two versions of Neuron installed on both the PCs, but it is very strange...
hines
Site Admin
Posts: 1687
Joined: Wed May 18, 2005 3:32 pm

Re: Add patch code to Neuron on Ubuntu

Post by hines »

The diagnosis would require verifying that the versions are the same on the two machines.
Then on the macbook pro one would have to start putting printf statements into
nrn_dbl_truncate_precision in nrn/src/math.h
to see what is going wrong with the arithmetic.
ilogb, scalbn, and trunc should be part of <math.h>
jackfolla
Posts: 48
Joined: Wed Jul 07, 2010 7:42 am

Re: Add patch code to Neuron on Ubuntu

Post by jackfolla »

Dear hines,
the versions on the two machines are the same.

I tried (on MacBook Pro) to insert printf in "nrn_dbl_truncate_precision()", but it does not print anything.

At this time, I don't have the opportunity to try the same thing on the other PC (because I'm not at home). I'll try later.

File math.h not is in "nrn-7.1/src", but only in "nrn-7.1/src/ivos/OS". This file contains the following code:

Code: Select all

/*
 * Copyright (c) 1991 Stanford University
 * Copyright (c) 1991 Silicon Graphics, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software and 
 * its documentation for any purpose is hereby granted without fee, provided
 * that (i) the above copyright notices and this permission notice appear in
 * all copies of the software and related documentation, and (ii) the names of
 * Stanford and Silicon Graphics may not be used in any advertising or
 * publicity relating to the software without the specific, prior written
 * permission of Stanford and Silicon Graphics.
 * 
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
 *
 * IN NO EVENT SHALL STANFORD OR SILICON GRAPHICS BE LIABLE FOR
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
 * OF THIS SOFTWARE.
 */

#ifndef os_math_h
#define os_math_h

#ifdef WIN32
#ifdef max
#undef max
#endif
#ifdef min
#undef min
#endif
#endif 

#include <OS/enter-scope.h>

/*
 * Common math operations on built-in types.
 */

#define declare_binary_minmax(Type) \
    static Type min(Type a, Type b); \
    static Type max(Type a, Type b)

#define implement_binary_minmax(Type) \
    inline Type Math::min(Type a, Type b) { return a < b ? a : b; } \
    inline Type Math::max(Type a, Type b) { return a > b ? a : b; }

#define declare_4_minmax(Type) \
    static Type min(Type a, Type b, Type c, Type d); \
    static Type max(Type a, Type b, Type c, Type d)

/*
 * Compiler isn't smart enough to figure out how to do a 4-way min inline
 * with single nested if-then-else.
 */

#define implement_4_minmax(Type) \
    inline Type Math::min(Type a, Type b, Type c, Type d) { \
	Type r1 = min(a, b), r2 = min(c, d); \
	    return min(r1, r2); \
	} \
\
    inline Type Math::max(Type a, Type b, Type c, Type d) { \
	Type r1 = max(a, b), r2 = max(c, d); \
	return max(r1, r2); \
    }

class Math {
public:
    declare_binary_minmax(int);
    declare_binary_minmax(unsigned);
    declare_binary_minmax(long);
    declare_binary_minmax(unsigned long);
    declare_binary_minmax(float);
    declare_binary_minmax(double);
    declare_4_minmax(int);
    declare_4_minmax(float);
    declare_4_minmax(double);

    static int abs(int);
    static long abs(long);
    static double abs(double);

    static int round(float);
    static int round(double);

    static boolean equal(float x, float y, float e);
    static boolean equal(double x, double y, double e);
};

implement_binary_minmax(int)
implement_binary_minmax(unsigned)
implement_binary_minmax(long)
implement_binary_minmax(unsigned long)
implement_binary_minmax(float)
implement_binary_minmax(double)

implement_4_minmax(int)
implement_4_minmax(float)
implement_4_minmax(double)

inline int Math::round(float x) { return x > 0 ? int(x+0.5) : -int(-x+0.5); }
inline int Math::round(double x) { return x > 0 ? int(x+0.5) : -int(-x+0.5); }

inline boolean Math::equal(float x, float y, float e) {
    return x - y < e && y - x < e;
}

inline boolean Math::equal(double x, double y, double e) {
    return x - y < e && y - x < e;
}

#endif
Perhaps to "nrn/src/oc/math.c" you're referring?

Finally, I thought the difference could be that MacBook Pro is a 64 bit machine, while the other PC is a 32 bit machine.

However, i'll try to do more tests.

Thanks for your help.
Pasquale.
jackfolla
Posts: 48
Joined: Wed Jul 07, 2010 7:42 am

Re: Add patch code to Neuron on Ubuntu

Post by jackfolla »

Dear hines,
I just checked on my old PC:

Inserting a printf("Hello world") instruction into "function nrn_dbl_truncate_precision()", it prints many times, for each iteration, the string "Hello world".
Instead, on MacBook this does not occur.

So in fadvance.c into "if (nrn_significand_precision) {...}" I've inserted a printf:

Code: Select all

static void update(NrnThread* _nt)
{

	int i, i1, i2;
#define DEF_PRECISION 1
	i1 = 0;
	i2 = _nt->end;
#if DEF_PRECISION
{
	int nrn_significand_precision;
	double nrn_dbl_truncate(double);
	if (nrn_significand_precision) {
		[b]printf(" Into IF\n");[/b]
		for (i = i1; i < i2; ++i) {
			VEC_RHS(i) = nrn_dbl_truncate(VEC_RHS(i));
		}
	}
}
On MacBook it does not print. On my old PC it prints.
So I have understand that the problem depends on "nrn_significan_precision" value: in fact, printng its value (as integer "%d"), On MacBook it is always 0, on my old PC it alternates between 139712320 and 139712448.

Thus, I deleted the IF on fadvance.c:

Code: Select all

static void update(NrnThread* _nt)
{

	int i, i1, i2;
#define DEF_PRECISION 1
	i1 = 0;
	i2 = _nt->end;
#if DEF_PRECISION
{
	int nrn_significand_precision;
	double nrn_dbl_truncate(double);
	for (i = i1; i < i2; ++i) {
		VEC_RHS(i) = nrn_dbl_truncate(VEC_RHS(i));
	}
}
In this way, on MacBook hhchain.hoc runs:

Code: Select all

22.0516626398731006
22.0516626398923066
mantissa precision 53 bits, result diff 1.9206e-11
22.0516626398731006
22.0516626398923066
mantissa precision 52 bits, result diff 1.9206e-11
22.0516626401501981
22.0516626398808455
mantissa precision 51 bits, result diff -2.69353e-10
22.0516626398827142
22.0516626398672848
mantissa precision 50 bits, result diff -1.54294e-11
22.0516626401661107
22.0516626398747846
mantissa precision 49 bits, result diff -2.91326e-10
22.0516626398804121
22.0516626398990425
mantissa precision 48 bits, result diff 1.86304e-11
22.0516626398947579
22.0516626398793676
mantissa precision 47 bits, result diff -1.53904e-11
22.0516626398899831
22.0516626398858904
mantissa precision 46 bits, result diff -4.09273e-12
22.0516626401445563
22.0516626398567155
mantissa precision 45 bits, result diff -2.87841e-10
22.0516626401564864
22.0516626401397531
mantissa precision 44 bits, result diff -1.67333e-11
22.0516626398814921
22.0516626398833964
mantissa precision 43 bits, result diff 1.90425e-12
22.0516626398728661
22.051662639855671
mantissa precision 42 bits, result diff -1.71951e-11
22.0516626398422062
22.0516626400936246
mantissa precision 41 bits, result diff 2.51418e-10
22.051662639754241
22.051662639754241
mantissa precision 40 bits, result diff 0
22.0516626396554756
22.0516626396554756
mantissa precision 39 bits, result diff 0
22.0516626395717452
22.0516626395717452
mantissa precision 38 bits, result diff 0
22.0516626389630375
22.0516626389630375
mantissa precision 37 bits, result diff 0
22.0516626378978913
22.0516626378978913
mantissa precision 36 bits, result diff 0
22.0516626358508461
22.0516626358508461
mantissa precision 35 bits, result diff 0
22.051662630983202
22.051662630983202
mantissa precision 34 bits, result diff 0
22.051662624063411
22.051662624063411
mantissa precision 33 bits, result diff 0
22.0516626094371588
22.0516626094371588
mantissa precision 32 bits, result diff 0
	32 
Now, let me know if my modification can lead to errors.

Should however be noted that the values printed by the two PCs are slightly different; but I think this depends on the different architectures (32 and 64 bits).
hines
Site Admin
Posts: 1687
Joined: Wed May 18, 2005 3:32 pm

Re: Add patch code to Neuron on Ubuntu

Post by hines »

Code: Select all

#if DEF_PRECISION
{
   int nrn_significand_precision;
It is a miracle this ever worked at all. The line should read
extern int nrn_significand_precision;

Make this change on both machines and the problem should go away.
Post Reply