time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

samples-one-thread.c (4420B)


      1 #include <math.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 #include <time.h>
      5 
      6 const float PI = 3.14159265358979323846;
      7 
      8 #define N 1000000
      9 
     10 //Array helpers
     11 
     12 void array_print(float* array, int length)
     13 {
     14     for (int i = 0; i < length; i++) {
     15         printf("item[%d] = %f\n", i, array[i]);
     16     }
     17     printf("\n");
     18 }
     19 
     20 void array_fill(float* array, int length, float item)
     21 {
     22     int i;
     23     {
     24         for (i = 0; i < length; i++) {
     25             array[i] = item;
     26         }
     27     }
     28 }
     29 
     30 float array_sum(float* array, int length)
     31 {
     32     float output = 0.0;
     33     for (int i = 0; i < length; i++) {
     34         output += array[i];
     35     }
     36     return output;
     37 }
     38 
     39 void array_cumsum(float* array_to_sum, float* array_cumsummed, int length)
     40 {
     41     array_cumsummed[0] = array_to_sum[0];
     42     for (int i = 1; i < length; i++) {
     43         array_cumsummed[i] = array_cumsummed[i - 1] + array_to_sum[i];
     44     }
     45 }
     46 
     47 float rand_float(float to)
     48 {
     49     return ((float)rand() / (float)RAND_MAX) * to;
     50 }
     51 
     52 float ur_normal()
     53 {
     54     float u1 = rand_float(1.0);
     55     float u2 = rand_float(1.0);
     56     float z = sqrtf(-2.0 * log(u1)) * sin(2 * PI * u2);
     57     return z;
     58 }
     59 
     60 inline float random_uniform(float from, float to)
     61 {
     62     return ((float)rand() / (float)RAND_MAX) * (to - from) + from;
     63 }
     64 
     65 inline float random_normal(float mean, float sigma)
     66 {
     67     return (mean + sigma * ur_normal());
     68 }
     69 
     70 inline float random_lognormal(float logmean, float logsigma)
     71 {
     72     return expf(random_normal(logmean, logsigma));
     73 }
     74 
     75 inline float random_to(float low, float high)
     76 {
     77     const float NORMAL95CONFIDENCE = 1.6448536269514722;
     78     float loglow = logf(low);
     79     float loghigh = logf(high);
     80     float logmean = (loglow + loghigh) / 2;
     81     float logsigma = (loghigh - loglow) / (2.0 * NORMAL95CONFIDENCE);
     82     return random_lognormal(logmean, logsigma);
     83 }
     84 
     85 void array_random_to(float* array, int length, float low, float high)
     86 {
     87     int i;
     88     #pragma omp private(i)
     89     {
     90         #pragma omp for
     91         for (i = 0; i < length; i++) {
     92             array[i] = random_to(low, high);
     93         }
     94     }
     95 }
     96 
     97 void mixture(float (*samplers[])(void), float* weights, int n_dists, float* results, int results_length)
     98 {
     99     float sum_weights = array_sum(weights, n_dists);
    100     float* normalized_weights = malloc(n_dists * sizeof(float));
    101     for (int i = 0; i < n_dists; i++) {
    102         normalized_weights[i] = weights[i] / sum_weights;
    103     }
    104 
    105     float* cummulative_weights = malloc(n_dists * sizeof(float));
    106     array_cumsum(normalized_weights, cummulative_weights, n_dists);
    107 
    108     //create var holders
    109     float p1;
    110     int sample_index, i, own_length;
    111 
    112     {
    113 			for (int i = 0; i < results_length; i++) {
    114 					p1 = random_uniform(0, 1);
    115 					for (int j = 0; j < n_dists; j++) {
    116 							if (p1 < cummulative_weights[j]) {
    117 									results[i] = samplers[j]();
    118 									break;
    119 							}
    120 					}
    121 			}
    122     }
    123     free(normalized_weights);
    124     free(cummulative_weights);
    125 }
    126 
    127 float sample_0()
    128 {
    129     return 0;
    130 }
    131 
    132 float sample_1()
    133 {
    134     return 1;
    135 }
    136 
    137 float sample_few()
    138 {
    139     return random_to(1, 3);
    140 }
    141 
    142 float sample_many()
    143 {
    144     return random_to(2, 10);
    145 }
    146 
    147 int main()
    148 {
    149     //initialize randomness
    150     srand(1);
    151     
    152 		// clock_t start, end;
    153 		// start = clock();
    154 
    155 		// Toy example
    156 		// Declare variables in play
    157 		float p_a, p_b, p_c;
    158 		// printf("Max threads: %d\n", n_threads);
    159 		// omp_set_num_threads(n_threads);
    160 
    161 		// Initialize variables
    162 		p_a = 0.8;
    163 		p_b = 0.5;
    164 		p_c = p_a * p_b;
    165 
    166 		// Generate mixture
    167 		int n_dists = 4;
    168 		float weights[] = { 1 - p_c, p_c / 2, p_c / 4, p_c / 4 };
    169 		float (*samplers[])(void) = { sample_0, sample_1, sample_few, sample_many };
    170 
    171 		float* results = malloc(N * sizeof(float));
    172 		mixture(samplers, weights, n_dists, results, N);
    173 		printf("Sum(dist_mixture, N)/N = %f\n", array_sum(results, N) / N);
    174 		// array_print(dist_mixture[0], N);
    175 		
    176 		// end = clock();
    177 		// printf("Time (ms): %f\n", ((double)(end - start)) / (CLOCKS_PER_SEC * 1000));
    178 		// ^ Will only measure how long it takes the inner main to run, not the whole program, 
    179 		// including e.g., loading the program into memory or smth.
    180 		// Also CLOCKS_PER_SEC in POSIX is a constant equal to 1000000.
    181 		// See: https://stackoverflow.com/questions/10455905/why-is-clocks-per-sec-not-the-actual-number-of-clocks-per-second
    182     return 0;
    183 }