gnuastro-commits
[Top][All Lists]
Advanced

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

[gnuastro-commits] master 369d7b1: Random number generator seed with uns


From: Mohammad Akhlaghi
Subject: [gnuastro-commits] master 369d7b1: Random number generator seed with unsigned long type
Date: Wed, 10 Oct 2018 11:41:28 -0400 (EDT)

branch: master
commit 369d7b18fd05769b4535f7b27e9843a6c21584f2
Author: Mohammad Akhlaghi <address@hidden>
Commit: Mohammad Akhlaghi <address@hidden>

    Random number generator seed with unsigned long type
    
    Until now the type of the random number generator seed was `uint64_t'. But
    after testing on a 32-bit system, I noticed that there is no FITS 64-bit
    unsigned integer type on those machines, so when we were trying to write
    the seed in a FITS file header, the program would crash. The main
    complication was that GSL's random number generator seed has `unsigned
    long' type (which can differ on 32-bit or 64-bit systems).
    
    With this commit, there is a new type macro called
    `GAL_TYPE_ULONG'. Similar to `GAL_TYPE_LONG', it is set and build-time and
    is just an alias for the respective fixed-width type. This new Gnuastro
    type now enables the use of `unsigned long' for the `rng_seed' variable of
    the programs that use random numbers (MakeCatalog, MakeNoise and
    MakeProfiles).
---
 bin/mkcatalog/main.h             |  4 ++--
 bin/mkcatalog/ui.c               |  6 +++---
 bin/mkcatalog/upperlimit.c       |  8 ++++----
 bin/mknoise/main.h               |  2 +-
 bin/mknoise/mknoise.c            |  2 +-
 bin/mknoise/ui.c                 |  2 +-
 bin/mkprof/main.h                |  2 +-
 bin/mkprof/mkprof.c              |  2 +-
 bin/mkprof/mkprof.h              |  2 +-
 bin/mkprof/ui.c                  | 15 +++++----------
 doc/gnuastro.texi                |  6 ++++++
 lib/checkset.c                   |  3 ++-
 lib/fits.c                       |  4 ++--
 lib/gnuastro-internal/checkset.h |  2 +-
 lib/gnuastro-internal/timing.h   |  2 +-
 lib/gnuastro/type.h              |  6 ++++--
 lib/timing.c                     | 10 ++++++++--
 17 files changed, 44 insertions(+), 34 deletions(-)

diff --git a/bin/mkcatalog/main.h b/bin/mkcatalog/main.h
index 0461a7a..48c8182 100644
--- a/bin/mkcatalog/main.h
+++ b/bin/mkcatalog/main.h
@@ -205,8 +205,8 @@ struct mkcatalogparams
   pthread_mutex_t       mutex;  /* Mutex to change the total numbers.   */
   size_t      clumprowsfilled;  /* No. filled clump rows at this moment.*/
   gsl_rng                *rng;  /* Main random number generator.        */
-  uint64_t               seed;  /* Random number generator seed.        */
-  const char         *rngname;  /* Name of random number generator.     */
+  unsigned long int  rng_seed;  /* Random number generator seed.        */
+  const char        *rng_name;  /* Name of random number generator.     */
   size_t               rngmin;  /* Minimum possible value of RNG.       */
   size_t              rngdiff;  /* Difference of RNG max and min.       */
   uint8_t      uprangewarning;  /* A warning must be printed.           */
diff --git a/bin/mkcatalog/ui.c b/bin/mkcatalog/ui.c
index 00df798..64c5cf0 100644
--- a/bin/mkcatalog/ui.c
+++ b/bin/mkcatalog/ui.c
@@ -1345,7 +1345,7 @@ ui_preparations_upperlimit(struct mkcatalogparams *p)
           "that is reported as the upper-limit");
 
   /* Set the random number generator. */
-  p->rng=gal_checkset_gsl_rng(p->envseed, &p->rngname, &p->seed);
+  p->rng=gal_checkset_gsl_rng(p->envseed, &p->rng_name, &p->rng_seed);
 
   /* Keep the minimum and maximum values of the random number generator. */
   p->rngmin=gsl_rng_min(p->rng);
@@ -1546,8 +1546,8 @@ ui_read_check_inputs_setup(int argc, char *argv[], struct 
mkcatalogparams *p)
                p->upmaskfile, p->cp.hdu);
       if(p->upperlimit)
         {
-          printf("  - Random number generator name: %s\n", p->rngname);
-          printf("  - Random number generator seed: %"PRIu64"\n", p->seed);
+          printf("  - Random number generator name: %s\n", p->rng_name);
+          printf("  - Random number generator seed: %lu\n", p->rng_seed);
         }
     }
 }
diff --git a/bin/mkcatalog/upperlimit.c b/bin/mkcatalog/upperlimit.c
index bdcd3fb..44a76fe 100644
--- a/bin/mkcatalog/upperlimit.c
+++ b/bin/mkcatalog/upperlimit.c
@@ -301,11 +301,11 @@ upperlimit_write_comments(struct mkcatalogparams *p,
       gal_list_str_add(comments, str, 0);
     }
 
-  if( asprintf(&str, "Random number generator name: %s", p->rngname)<0 )
+  if( asprintf(&str, "Random number generator name: %s", p->rng_name)<0 )
     error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
   gal_list_str_add(comments, str, 0);
 
-  if( asprintf(&str, "Random number generator seed: %"PRIu64, p->seed)<0 )
+  if( asprintf(&str, "Random number generator seed: %lu", p->rng_seed)<0 )
     error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
   gal_list_str_add(comments, str, 0);
 
@@ -723,7 +723,7 @@ upperlimit_calculate(struct mkcatalog_passparams *pp)
   struct mkcatalogparams *p=pp->p;
 
   /* First find the upper limit magnitude for this object. */
-  upperlimit_one_tile(pp, pp->tile, p->seed+pp->object, 0);
+  upperlimit_one_tile(pp, pp->tile, p->rng_seed+pp->object, 0);
 
   /* If a clumps image is present (a clump catalog is requested) and this
      object has clumps, then find the upper limit magnitude for the clumps
@@ -751,7 +751,7 @@ upperlimit_calculate(struct mkcatalog_passparams *pp)
          IDs. */
       for(i=0;i<pp->clumpsinobj;++i)
         {
-          seed = p->seed + p->numobjects + p->numclumps * pp->object + i;
+          seed = p->rng_seed + p->numobjects + p->numclumps * pp->object + i;
           upperlimit_one_tile(pp, &clumptiles[i], seed, i+1);
         }
 
diff --git a/bin/mknoise/main.h b/bin/mknoise/main.h
index c443f2c..634a08b 100644
--- a/bin/mknoise/main.h
+++ b/bin/mknoise/main.h
@@ -55,7 +55,7 @@ struct mknoiseparams
   double      background;    /* Background in units of brightness.       */
   gsl_rng           *rng;    /* Main instance of random number generator.*/
   const char   *rng_name;    /* The type/name of the Random number gen.  */
-  uint64_t      rng_seed;    /* Seed of Random number generator.         */
+  unsigned long rng_seed;    /* Seed of Random number generator.         */
   time_t         rawtime;    /* Starting time of the program.            */
 };
 
diff --git a/bin/mknoise/mknoise.c b/bin/mknoise/mknoise.c
index 5c9d085..954fe85 100644
--- a/bin/mknoise/mknoise.c
+++ b/bin/mknoise/mknoise.c
@@ -91,7 +91,7 @@ convertsaveoutput(struct mknoiseparams *p)
                             "Random number generator (by GSL) type.",
                             0, NULL);
   strcpy(keyname5, "RNGSEED");
-  gal_fits_key_list_add_end(&headers, GAL_TYPE_UINT64, keyname5, 0,
+  gal_fits_key_list_add_end(&headers, GAL_TYPE_ULONG, keyname5, 0,
                             &p->rng_seed, 0,
                             "Random number generator (by GSL) seed.",
                             0, NULL);
diff --git a/bin/mknoise/ui.c b/bin/mknoise/ui.c
index 54a807d..471328d 100644
--- a/bin/mknoise/ui.c
+++ b/bin/mknoise/ui.c
@@ -400,7 +400,7 @@ ui_read_check_inputs_setup(int argc, char *argv[], struct 
mknoiseparams *p)
              ctime(&p->rawtime));
       sprintf(message, "Random number generator type: %s", p->rng_name);
       gal_timing_report(NULL, message, 1);
-      sprintf(message, "Random number generator seed: %"PRIu64, p->rng_seed);
+      sprintf(message, "Random number generator seed: %lu", p->rng_seed);
       gal_timing_report(NULL, message, 1);
     }
 }
diff --git a/bin/mkprof/main.h b/bin/mkprof/main.h
index 4f6fa4e..610ee9b 100644
--- a/bin/mkprof/main.h
+++ b/bin/mkprof/main.h
@@ -170,7 +170,7 @@ struct mkprofparams
   float                  *t;  /* Truncation distance.                     */
   gsl_rng              *rng;  /* Main instance of random number generator.*/
   const char      *rng_name;  /* Name of random number generator.         */
-  uint64_t         rng_seed;  /* Fixed seed of random number generator.   */
+  unsigned long    rng_seed;  /* Fixed seed of random number generator.   */
   time_t            rawtime;  /* Starting time of the program.            */
   double               *cat;  /* Input catalog.                           */
   gal_data_t           *log;  /* Log data to be printed.                  */
diff --git a/bin/mkprof/mkprof.c b/bin/mkprof/mkprof.c
index 60330e3..bb1cdf6 100644
--- a/bin/mkprof/mkprof.c
+++ b/bin/mkprof/mkprof.c
@@ -200,7 +200,7 @@ saveindividual(struct mkonthread *mkp)
   gal_fits_key_list_add(&keys, GAL_TYPE_STRING, "RNGNAME", 0,
                         (void *)(p->rng_name), 0,
                         "Name of random number generator", 0, NULL);
-  gal_fits_key_list_add(&keys, GAL_TYPE_LONG, "RNGSEED", 0,
+  gal_fits_key_list_add(&keys, GAL_TYPE_ULONG, "RNGSEED", 0,
                         &mkp->rng_seed, 0, "Seed of random number generator",
                         0, NULL);
   gal_fits_key_list_add(&keys, GAL_TYPE_SIZE_T, "NUMRANDOM", 0,
diff --git a/bin/mkprof/mkprof.h b/bin/mkprof/mkprof.h
index 196e1d6..fec9d03 100644
--- a/bin/mkprof/mkprof.h
+++ b/bin/mkprof/mkprof.h
@@ -48,7 +48,7 @@ struct mkonthread
   long            *onaxes;   /* Sides of the unover-sampled image.    */
   long        fpixel_i[2];   /* fpixel_i before running overlap.      */
   int          correction;   /* ==1: correct the pixels afterwards.   */
-  long           rng_seed;   /* Seed used to generate this profile.   */
+  unsigned long  rng_seed;   /* Seed used to generate this profile.   */
 
   /* Random number generator: */
   gsl_rng            *rng;   /* Copy of main random number generator. */
diff --git a/bin/mkprof/ui.c b/bin/mkprof/ui.c
index ef0580e..8ad4520 100644
--- a/bin/mkprof/ui.c
+++ b/bin/mkprof/ui.c
@@ -1439,16 +1439,11 @@ ui_print_intro(struct mkprofparams *p)
     error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
   gal_timing_report(NULL, jobname, 1);
   free(jobname);
-  if(p->envseed)
-    {
-      if( asprintf(&jobname, "RNG seed for all profiles: %lu",
-                   gsl_rng_default_seed)<0 )
-        error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
-      gal_timing_report(NULL, jobname, 1);
-      free(jobname);
-    }
-  else
-    gal_timing_report(NULL, "RNG seed differs for each profile.", 1);
+
+  if( asprintf(&jobname, "Basic RNG seed: %lu", p->rng_seed)<0 )
+    error(EXIT_FAILURE, 0, "%s: asprintf allocation", __func__);
+  gal_timing_report(NULL, jobname, 1);
+  free(jobname);
 
   if(p->kernel==NULL)
     {
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index e3bc4c8..2def26c 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -22795,6 +22795,12 @@ Identifier for a @code{size_t} type. This is just an 
alias to
 respectively.
 @end deffn
 
address@hidden {Global integer}  GAL_TYPE_ULONG
+Identifier for a @code{unsigned long} type. This is just an alias to
address@hidden, or @code{uint64} types for 32-bit, or 64-bit systems
+respectively.
address@hidden deffn
+
 @deffn {Global integer}  GAL_TYPE_LONG
 Identifier for a @code{long} type. This is just an alias to @code{int32},
 or @code{int64} types for 32-bit, or 64-bit systems respectively.
diff --git a/lib/checkset.c b/lib/checkset.c
index b58e96f..063de37 100644
--- a/lib/checkset.c
+++ b/lib/checkset.c
@@ -50,7 +50,8 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
    environment. This function is designed to make the job easier for any
    program using GSL's RNG. If the user doesn't want to set the */
 gsl_rng *
-gal_checkset_gsl_rng(uint8_t envseed_bool, const char **name, uint64_t *seed)
+gal_checkset_gsl_rng(uint8_t envseed_bool, const char **name,
+                     unsigned long int *seed)
 {
   gsl_rng *rng;
 
diff --git a/lib/fits.c b/lib/fits.c
index ad6a854..9e6f77e 100644
--- a/lib/fits.c
+++ b/lib/fits.c
@@ -300,8 +300,8 @@ gal_fits_type_to_datatype(uint8_t type)
     case GAL_TYPE_STRING:           return TSTRING;
 
     /* Types that depend on the host system. The C standard says that the
-       `short', `int' and `long' types are ATLEAST 2, 2, 4 bytes, so be
-       safe, we will checking all of them for the 32-bit types.*/
+       `short', `int' and `long' types are ATLEAST 2, 2, 4 bytes, so to be
+       safe, we will check all of them for the 32-bit types.*/
     case GAL_TYPE_UINT16:
       w=2;
       if     ( sizeof(short)    == w )   return TUSHORT;
diff --git a/lib/gnuastro-internal/checkset.h b/lib/gnuastro-internal/checkset.h
index 204c325..e528b6e 100644
--- a/lib/gnuastro-internal/checkset.h
+++ b/lib/gnuastro-internal/checkset.h
@@ -55,7 +55,7 @@ __BEGIN_C_DECLS  /* From C++ preparations */
 /**************************************************************/
 gsl_rng *
 gal_checkset_gsl_rng(uint8_t envseed_bool, const char **name,
-                     uint64_t *seed);
+                     unsigned long int *seed);
 
 
 
diff --git a/lib/gnuastro-internal/timing.h b/lib/gnuastro-internal/timing.h
index d43d76a..245b1a0 100644
--- a/lib/gnuastro-internal/timing.h
+++ b/lib/gnuastro-internal/timing.h
@@ -52,7 +52,7 @@ __BEGIN_C_DECLS  /* From C++ preparations */
 #define GAL_TIMING_VERB_MSG_LENGTH_V     50
 #define GAL_TIMING_VERB_MSG_LENGTHS_2_V  65
 
-long
+unsigned long
 gal_timing_time_based_rng_seed();
 
 void
diff --git a/lib/gnuastro/type.h b/lib/gnuastro/type.h
index 975ce17..e3ae316 100644
--- a/lib/gnuastro/type.h
+++ b/lib/gnuastro/type.h
@@ -106,9 +106,11 @@ enum gal_types
 #endif
 
 #if GAL_CONFIG_SIZEOF_LONG == 4
-#define GAL_TYPE_LONG GAL_TYPE_INT32
+#define GAL_TYPE_LONG  GAL_TYPE_INT32
+#define GAL_TYPE_ULONG GAL_TYPE_UINT32
 #elif GAL_CONFIG_SIZEOF_LONG == 8
-#define GAL_TYPE_LONG GAL_TYPE_INT64
+#define GAL_TYPE_LONG  GAL_TYPE_INT64
+#define GAL_TYPE_ULONG GAL_TYPE_UINT64
 #endif
 
 
diff --git a/lib/timing.c b/lib/timing.c
index cdb88fb..86b66e4 100644
--- a/lib/timing.c
+++ b/lib/timing.c
@@ -31,8 +31,14 @@ along with Gnuastro. If not, see 
<http://www.gnu.org/licenses/>.
 
 /* Micro-second based timer, which can be used to generate random numbers.
    The type of `tv_sec' and `tv_usec' is `long int' (from the GNU C Library
-   manual), so this function will also return long. */
-long
+   manual). But the expected type used by GSL's random number generator is
+   `unsigned long int'. Since the only random number generator that is
+   currently in Gnuastro is GSL's (and it asks for seeds of type `unsigned
+   long int'), this function will return in `unsigned long int'. Note that
+   `unsigned long' will be able to hold any positive `long' integer, which
+   is the case for `tv_sec' and `tv_usec': they are both positive, while
+   the opposite isn't true. */
+unsigned long int
 gal_timing_time_based_rng_seed()
 {
   struct timeval tv;



reply via email to

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