1 /*
2  * Copyright (c) 2007 - 2015 Joseph Gaeddert
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20  * THE SOFTWARE.
21  */
22 
23 #include <sys/resource.h>
24 #include "liquid.h"
25 
26 //
27 // BENCHMARK: uniform
28 //
benchmark_random_uniform(struct rusage * _start,struct rusage * _finish,unsigned long int * _num_iterations)29 void benchmark_random_uniform(struct rusage *_start,
30                               struct rusage *_finish,
31                               unsigned long int *_num_iterations)
32 {
33     // normalize number of iterations
34     *_num_iterations *= 10;
35 
36     float x = 0.0f;
37     unsigned long int i;
38 
39     // start trials
40     getrusage(RUSAGE_SELF, _start);
41     for (i=0; i<(*_num_iterations); i++) {
42         x += randf();
43         x += randf();
44         x += randf();
45         x += randf();
46     }
47     getrusage(RUSAGE_SELF, _finish);
48     *_num_iterations *= 4;
49 }
50 
51 //
52 // BENCHMARK: normal
53 //
benchmark_random_normal(struct rusage * _start,struct rusage * _finish,unsigned long int * _num_iterations)54 void benchmark_random_normal(struct rusage *_start,
55                              struct rusage *_finish,
56                              unsigned long int *_num_iterations)
57 {
58     // normalize number of iterations
59     *_num_iterations *= 1;
60 
61     float x = 0.0f;
62     unsigned long int i;
63 
64     // start trials
65     getrusage(RUSAGE_SELF, _start);
66     for (i=0; i<(*_num_iterations); i++) {
67         x += randnf();
68         x += randnf();
69         x += randnf();
70         x += randnf();
71     }
72     getrusage(RUSAGE_SELF, _finish);
73     *_num_iterations *= 4;
74 }
75 
76 //
77 // BENCHMARK: complex normal
78 //
benchmark_random_complex_normal(struct rusage * _start,struct rusage * _finish,unsigned long int * _num_iterations)79 void benchmark_random_complex_normal(struct rusage *_start,
80                                      struct rusage *_finish,
81                                      unsigned long int *_num_iterations)
82 {
83     // normalize number of iterations
84     *_num_iterations /= 2;
85 
86     float complex x = 0.0f;
87     unsigned long int i;
88 
89     // start trials
90     getrusage(RUSAGE_SELF, _start);
91     for (i=0; i<(*_num_iterations); i++) {
92         crandnf(&x);
93         crandnf(&x);
94         crandnf(&x);
95         crandnf(&x);
96     }
97     getrusage(RUSAGE_SELF, _finish);
98     *_num_iterations *= 4;
99 }
100 
101 //
102 // BENCHMARK: Weibull
103 //
benchmark_random_weibull(struct rusage * _start,struct rusage * _finish,unsigned long int * _num_iterations)104 void benchmark_random_weibull(struct rusage *_start,
105                               struct rusage *_finish,
106                               unsigned long int *_num_iterations)
107 {
108     // normalize number of iterations
109     *_num_iterations *= 2;
110 
111     float x=0.0f;
112     float alpha=1.0f;
113     float beta=2.0f;
114     float gamma=6.0f;
115     unsigned long int i;
116 
117     // start trials
118     getrusage(RUSAGE_SELF, _start);
119     for (i=0; i<(*_num_iterations); i++) {
120         x += randweibf(alpha,beta,gamma);
121         x += randweibf(alpha,beta,gamma);
122         x += randweibf(alpha,beta,gamma);
123         x += randweibf(alpha,beta,gamma);
124     }
125     getrusage(RUSAGE_SELF, _finish);
126     *_num_iterations *= 4;
127 }
128 
129 //
130 // BENCHMARK: Rice-K
131 //
benchmark_random_ricek(struct rusage * _start,struct rusage * _finish,unsigned long int * _num_iterations)132 void benchmark_random_ricek(struct rusage *_start,
133                             struct rusage *_finish,
134                             unsigned long int *_num_iterations)
135 {
136     // normalize number of iterations
137     *_num_iterations /= 3;
138 
139     float x = 0.0f;
140     float K=2.0f;
141     float omega=1.0f;
142     unsigned long int i;
143 
144     // start trials
145     getrusage(RUSAGE_SELF, _start);
146     for (i=0; i<(*_num_iterations); i++) {
147         x += randricekf(K,omega);
148         x += randricekf(K,omega);
149         x += randricekf(K,omega);
150         x += randricekf(K,omega);
151     }
152     getrusage(RUSAGE_SELF, _finish);
153     *_num_iterations *= 4;
154 }
155 
156