include/optik/random.h
static inline struct zipf_arr*
zipf_get_rand_array(double zipf_alpha,
		    size_t num_vals,
		    const int max,
		    const int id)
{
  if (num_vals == 0)
    {
      int log2 = log2f((double) max);
      int multi = ZIPF_ARR_SIZE_MUL - log2;
      if (multi < ZIPF_ARR_SIZE_MUL_MIN)
	{
	  multi = ZIPF_ARR_SIZE_MUL_MIN;
	}
      num_vals = multi * max;
    }


  struct zipf_arr* za = malloc(sizeof(struct zipf_arr) + num_vals * sizeof(int));
  assert(za != NULL);
  za->size = num_vals;
  za->max = max;
  za->i = 0;
  
  char fname[128];
  sprintf(fname, "data/zipf_rand_%d_%zu_%d.dat", id, num_vals, max);
  //  printf("--- %s\n", fname);
  FILE* rand_file = fopen(fname, "r");
  if (rand_file == NULL)
    {
      printf("--- [%-2d] Creating rand file with %zu vals\n", id, num_vals);
      rand_file = fopen(fname, "w+");
      int file_ok = (rand_file != NULL);
      int i;
      for (i = 0; i < num_vals; i++)
	{
	  za->vals[i] = zipf(zipf_alpha, max);
	  if (file_ok)
	    {
	      fprintf(rand_file, "%d\n", za->vals[i]);
	    }
	}
    }
  else
    {
      int i;
      for (i = 0; i < num_vals; i++)
	{
	  int val;
	  __attribute__((unused)) int ret = fscanf(rand_file, "%d", &val);
	  za->vals[i] = val;
	}      
    }

  if (rand_file != NULL)
    {
      fclose(rand_file);
    }

#if ZIPF_STATS == 1
  za->stats = calloc(max, sizeof(int));
  assert(za->stats != NULL);
#endif

  return za;
}
/* Create and return an array of num_vals zipf random values.
   @param num_vals: if num_vals == 0: automatically infers the num_vals to allocate
 */
Create and return an array of num_vals zipf random values.
Create and return an array of num_vals zipf random values .
static inline struct zipf_arr*
zipf_get_rand_array(double zipf_alpha,
		    size_t num_vals,
		    const int max,
		    const int id)
{
  if (num_vals == 0)
    {
      int log2 = log2f((double) max);
      int multi = ZIPF_ARR_SIZE_MUL - log2;
      if (multi < ZIPF_ARR_SIZE_MUL_MIN)
	{
	  multi = ZIPF_ARR_SIZE_MUL_MIN;
	}
      num_vals = multi * max;
    }
  struct zipf_arr* za = malloc(sizeof(struct zipf_arr) + num_vals * sizeof(int));
  assert(za != NULL);
  za->size = num_vals;
  za->max = max;
  za->i = 0;
  char fname[128];
  sprintf(fname, "data/zipf_rand_%d_%zu_%d.dat", id, num_vals, max);
  FILE* rand_file = fopen(fname, "r");
  if (rand_file == NULL)
    {
      printf("--- [%-2d] Creating rand file with %zu vals\n", id, num_vals);
      rand_file = fopen(fname, "w+");
      int file_ok = (rand_file != NULL);
      int i;
      for (i = 0; i < num_vals; i++)
	{
	  za->vals[i] = zipf(zipf_alpha, max);
	  if (file_ok)
	    {
	      fprintf(rand_file, "%d\n", za->vals[i]);
	    }
	}
    }
  else
    {
      int i;
      for (i = 0; i < num_vals; i++)
	{
	  int val;
	  __attribute__((unused)) int ret = fscanf(rand_file, "%d", &val);
	  za->vals[i] = val;
	}      
    }
  if (rand_file != NULL)
    {
      fclose(rand_file);
    }
#if ZIPF_STATS == 1
  za->stats = calloc(max, sizeof(int));
  assert(za->stats != NULL);
#endif
  return za;
}
static inline struct zipf_arr * zipf_get_rand_array ( double zipf_alpha , size_t num_vals , const int max , const int id ) { if ( num_vals == 0 ) { int log2 = log2f ( ( double ) max ) ; int multi = ZIPF_ARR_SIZE_MUL - log2 ; if ( multi < ZIPF_ARR_SIZE_MUL_MIN ) { multi = ZIPF_ARR_SIZE_MUL_MIN ; } num_vals = multi * max ; } struct zipf_arr * za = malloc ( sizeof ( struct zipf_arr ) + num_vals * sizeof ( int ) ) ; assert ( za != NULL ) ; za -> size = num_vals ; za -> max = max ; za -> i = 0 ; char fname [ 128 ] ; sprintf ( fname , " " , id , num_vals , max ) ; FILE * rand_file = fopen ( fname , " " ) ; if ( rand_file == NULL ) { printf ( " \n " , id , num_vals ) ; rand_file = fopen ( fname , " " ) ; int file_ok = ( rand_file != NULL ) ; int i ; for ( i = 0 ; i < num_vals ; i ++ ) { za -> vals [ i ] = zipf ( zipf_alpha , max ) ; if ( file_ok ) { fprintf ( rand_file , " \n " , za -> vals [ i ] ) ; } } } else { int i ; for ( i = 0 ; i < num_vals ; i ++ ) { int val ; __attribute__ ( ( unused ) ) int ret = fscanf ( rand_file , " " , & val ) ; za -> vals [ i ] = val ; } } if ( rand_file != NULL ) { fclose ( rand_file ) ; } #if ZIPF_STATS == 1 za -> stats = calloc ( max , sizeof ( int ) ) ; assert ( za -> stats != NULL ) ; #endif return za ; }
Create and return an array of num_vals zipf random values.
Create and return an array of num_vals zipf random values .
//  printf("--- %s\n", fname);