toon-members
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Toon-members] TooN internal/allocator.hh internal/debug.hh To...


From: Edward Rosten
Subject: [Toon-members] TooN internal/allocator.hh internal/debug.hh To...
Date: Mon, 27 Apr 2009 16:58:27 +0000

CVSROOT:        /cvsroot/toon
Module name:    TooN
Changes by:     Edward Rosten <edrosten>        09/04/27 16:58:27

Modified files:
        internal       : allocator.hh debug.hh 
        .              : TooN.h 

Log message:
        Added some debugging macros to initialize data to values likely to cause
        failure.
        
        TOON_INITIALIZE_NAN  initialize all data to NaN
        
        TOON_INITIALIZE_VAL  initialize all data to the expansion of this macro
        
        TOON_INITIALIZE_RANDOM initialize all data to random bytes. This will 
not 
        compile if you make a Vector of a non-POD type. This is seeded off 
        std::time, so the results will be nicely non-reproducable to a 
granularity
        of 1 second. A very cheap and nasty random number generator has been 
used. 

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/allocator.hh?cvsroot=toon&r1=1.29&r2=1.30
http://cvs.savannah.gnu.org/viewcvs/TooN/internal/debug.hh?cvsroot=toon&r1=1.3&r2=1.4
http://cvs.savannah.gnu.org/viewcvs/TooN/TooN.h?cvsroot=toon&r1=1.37&r2=1.38

Patches:
Index: internal/allocator.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/allocator.hh,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -b -r1.29 -r1.30
--- internal/allocator.hh       21 Apr 2009 10:45:00 -0000      1.29
+++ internal/allocator.hh       27 Apr 2009 16:58:27 -0000      1.30
@@ -53,7 +53,9 @@
 {
 public:
        StackOrHeap()
-       {}
+       {
+               debug_initialize(my_data, Size);        
+       }
 
        Precision my_data[Size];
 };
@@ -62,7 +64,9 @@
 {
 public:
                StackOrHeap()
-       {}
+       {
+               debug_initialize(my_data, Size);        
+       }
 
        double my_data[Size] TOON_ALIGN8 ;
 };
@@ -72,7 +76,10 @@
 {
        public:
                StackOrHeap()
-               :my_data(new Precision[Size]){}
+               :my_data(new Precision[Size])
+               {
+                       debug_initialize(my_data, Size);        
+               }
 
 
                ~StackOrHeap()
@@ -122,10 +129,16 @@
 
        VectorAlloc(int s)
        :my_data(new Precision[s]), my_size(s)
-       { }
+       { 
+               debug_initialize(my_data, my_size);     
+       }
 
        template <class Op>
-       VectorAlloc(const Operator<Op>& op) : my_data(new 
Precision[op.size()]), my_size(op.size()) {}
+       VectorAlloc(const Operator<Op>& op) 
+       : my_data(new Precision[op.size()]), my_size(op.size()) 
+       {
+               debug_initialize(my_data, my_size);     
+       }
 
        int size() const {
                return my_size;
@@ -281,14 +294,18 @@
        MatrixAlloc(int r, int c)
                :RowSizeHolder<R>(r),
                 ColSizeHolder<C>(c),
-                my_data(new Precision[num_rows()*num_cols()]) {
+        my_data(new Precision[num_rows()*num_cols()]) 
+       {
+               debug_initialize(my_data, num_rows()*num_cols());       
        }
 
        template <class Op>     MatrixAlloc(const Operator<Op>& op)
                :RowSizeHolder<R>(op),
                 ColSizeHolder<C>(op),
                 my_data(new Precision[num_rows()*num_cols()])
-       {}
+       {
+               debug_initialize(my_data, num_rows()*num_cols());       
+       }
 
        ~MatrixAlloc() {
                delete[] my_data;

Index: internal/debug.hh
===================================================================
RCS file: /cvsroot/toon/TooN/internal/debug.hh,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -b -r1.3 -r1.4
--- internal/debug.hh   26 Jan 2009 18:45:18 -0000      1.3
+++ internal/debug.hh   27 Apr 2009 16:58:27 -0000      1.4
@@ -19,4 +19,73 @@
        #else
                static inline void check_index(int, int){}
        #endif
+
+       #if defined TOON_INITIALIZE_NAN
+               template<class P> static void debug_initialize(P* data, int n)
+               {       
+                       using std::numeric_limits;
+                       for(int i=0; i < n; i++)
+                               data[i] = numeric_limits<P>::signaling_NaN() 
+               }
+       #elif defined TOON_INITIALIZE_VAL
+               template<class P> static void debug_initialize(P* data, int n)
+               {       
+                       for(int i=0; i < n; i++)
+                               data[i] = TOON_INITIALIZE_VAL;
+               }
+       #elif defined TOON_INITIALIZE_RANDOM
+               union intbits
+               {
+                       unsigned long i;
+                       char c[4];
+               };
+
+               template<class P> union datafail
+               {
+                       int i;
+                       P p;
+               };
+
+
+               template<class P> static void debug_initialize(P* data, int n)
+               {
+                       //Get a random seed. Precision limited to 1 second.
+                       static intbits random = { ((std::time(NULL) & 
0xffffffff) *1664525L + 1013904223L)& 0xffffffff};
+                       unsigned char* cdata = reinterpret_cast<unsigned 
char*>(data);
+
+                       int bytes = sizeof(P)*n, i=0;
+                       
+                       //Do nothing except for noisy failure with non-POD 
types.
+                       datafail<P> d={0};
+                       bytes+=d.i;
+                       
+                       switch(bytes & 0x3 )
+                       {
+                               for(i=0; i < bytes;)
+                               {
+                                       //Really evil random number generator 
from NR.
+                                       //About 4x faster than Mersenne 
twister. Good quality
+                                       //is not needed, since it's only used 
to shake up the program
+                                       //state to test for uninitialized 
values. Also, it doesn't disturb
+                                       //the standard library random number 
generator.
+                                       
+                                       case 0:
+                                               cdata[i++] = random.c[0];       
+                                       case 3:
+                                               cdata[i++] = random.c[1];       
+                                       case 2:
+                                               cdata[i++] = random.c[2];       
+                                       case 1:
+                                               cdata[i++] = random.c[3];       
+                                       random.i = (1664525L * random.i + 
1013904223L) & 0xffffffff;
+                               }
+                       }
+               }
+       #else
+               template<class P> static void debug_initialize(P*, int)
+               {
+               }
+       #endif
+       
+
 }

Index: TooN.h
===================================================================
RCS file: /cvsroot/toon/TooN/TooN.h,v
retrieving revision 1.37
retrieving revision 1.38
diff -u -b -r1.37 -r1.38
--- TooN.h      27 Apr 2009 10:56:18 -0000      1.37
+++ TooN.h      27 Apr 2009 16:58:27 -0000      1.38
@@ -38,6 +38,10 @@
 #include <TooN/internal/config.hh>
 #include <TooN/internal/typeof.hh>
 
+#ifdef TOON_INITIALIZE_RANDOM
+#include <ctime>
+#endif
+
 namespace TooN
 {
        




reply via email to

[Prev in Thread] Current Thread [Next in Thread]