LILAC
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
noise.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2014, Sam Schetterer, Nathan Kutz, University of Washington
3 Authors: Sam Schetterer
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 
10 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
11 
12 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
13 
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 
16 */
17 #ifndef NOISE_HPP
18 #define NOISE_HPP
19 #include "defs.hpp"
20 #include "types/type_operators.hpp"
21 #include <stdlib.h>
22 #include <time.h>
23 #include <acml.h>
24 namespace __HIDER__{
26  struct acml_rng{
27  constexpr static int rand_len=64;
28  constexpr static int genid=3;
29  double vals[rand_len];
30  double* const val_end;
31  double* cur_val;
32  int subid=1;
33  int id;
34  constexpr static int len_state=633;
37  int info;
39  int seed = (int)clock();
40  while(seed <= 1){
41  seed = (int)clock();
42  }
43  int lseed = 1;
44  drandinitialize(genid, subid, &seed, &lseed, state, &lstate, &info);
45  if(info == 1){
46  drandinitialize(genid, subid, &seed, &lseed, state, &lstate, &info);
47  }
48  drandgaussian(rand_len, 0, 1, state, vals, &info);
49  }
50  void fill(double* in, size_t len, double mean, double var){
51  drandgaussian(len, mean, var, state, in, &info);
52  }
53  double get_gauss_double(double mean, double sdev){
54  if(PREDICT(cur_val < val_end, 1)){
55  return mean + (*cur_val++)*sdev;
56  }
57  cur_val = (&(vals[0]))+1;
58  drandgaussian(rand_len, 0, 1, state, vals, &info);
59  return mean + vals[0]*sdev;
60  }
61  };
62 }
63 double get_norm_rand(double sigma);
65 
68 template<class T, class Lambda>
69 inline void gaussian_noise(T* inval, size_t len, double mean, double sdev, Lambda&& fnc){
70  // std::cout << "NOISE:" << std::endl;
72  static __HIDER__::acml_rng rng;
73  auto* rng_ptr = &rng; //This bypasses a compiler warning
74  typedef typename float_traits<T>::type real_type;
76  rng.fill((double*)inval, len, 0, sdev*sdev);
77  for(size_t i = 0; i < len; i++){
78  std::forward<Lambda>(fnc)(inval[i]);
79  }
80  }
81  else{
82  for(size_t i = 0; i < len; i++){
83  apply<T>::transform(inval[i],
84  [sdev, rng_ptr](real_type& val, size_t){
85  val = real_type(rng_ptr->get_gauss_double(0, sdev));
86  },
87  std::forward<Lambda>(fnc));
88  }
89  }
90 }
91 template<class T>
92 inline void gaussian_noise(T* inval, size_t len, double mean, double sdev){
93  gaussian_noise(inval, len, mean, sdev, [](T&){});
94 }
95 #endif