OpenCores
URL https://opencores.org/ocsvn/an-fpga-implementation-of-low-latency-noc-based-mpsoc/an-fpga-implementation-of-low-latency-noc-based-mpsoc/trunk

Subversion Repositories an-fpga-implementation-of-low-latency-noc-based-mpsoc

[/] [an-fpga-implementation-of-low-latency-noc-based-mpsoc/] [trunk/] [mpsoc/] [src_c/] [synfull/] [traffic-generator/] [src/] [MyRand.h] - Blame information for rev 54

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 54 alirezamon
/*
2
Copyright (c) 2014, Mario Badr
3
All rights reserved.
4
 
5
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6
 
7
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8
 
9
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.
10
 
11
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.
12
*/
13
/*
14
 * MyRand.h
15
 *
16
 *  Created on: 2013-05-15
17
 *      Author: badrmari
18
 */
19
 
20
#ifndef MYRAND_H_
21
#define MYRAND_H_
22
 
23
#include <memory>
24
#include <vector>
25
#include <map>
26
#include <random>
27
#include <iostream>
28
 
29
 
30
#define RND_ENG_NUM   4
31
#define DEFAULT_ENG   0
32
#define hierClass_ENG 1
33
#define INIT_MSG_ENG  2
34
#define REACT_ENG     3
35
 
36
 
37
extern std::mt19937 mt_rng [RND_ENG_NUM];
38
 
39
template <class T>
40
class DiscreteDistribution {
41
public:
42
        DiscreteDistribution() { dist = nullptr;}
43
 
44
        ~DiscreteDistribution() {
45
                //delete dist;
46
        }
47
 
48
        void Add(T value) { values.push_back(value);
49
        //delete dist;
50
        dist = nullptr; }
51
 
52
        int Generate(int num) {
53
                if(values.size() == 0) {
54
                        return -1;
55
                }
56
 
57
                create_dist();
58
                return (*dist)(mt_rng[num]);
59
        }
60
 
61
        void Print() {
62
                if(values.size() == 0) {
63
                        return;
64
                }
65
 
66
                create_dist();
67
 
68
                std::vector<double> prob = dist->probabilities();
69
                std::vector<double>::iterator it;
70
 
71
                for(it = prob.begin(); it != prob.end(); it++) {
72
                        std::cout << *it << ",";
73
                }
74
                std::cout << std::endl;
75
        }
76
private:
77
        std::discrete_distribution<>* dist;
78
        std::vector<T> values;
79
 
80
        void create_dist() {
81
                if(dist == nullptr) {
82
                        dist = new std::discrete_distribution<>
83
                        (values.begin(), values.end());
84
                }
85
        }
86
};
87
 
88
class ExponentialDistribution {
89
public:
90
        ExponentialDistribution(double lambda, int intervals);
91
 
92
        std::map<int, int> Generate(int samples);
93
private:
94
        double _lambda;
95
        int _intervals;
96
};
97
 
98
class BernoulliDistribution {
99
public:
100
        BernoulliDistribution(double p) {
101
                dist = new std::bernoulli_distribution(p);
102
        }
103
 
104
        ~BernoulliDistribution() {
105
                //delete dist;
106
        }
107
 
108
        void SetProbability(double p) {
109
                //delete dist;
110
                dist = new std::bernoulli_distribution(p);
111
        }
112
 
113
        bool Generate(int num) {
114
                return (*dist)(mt_rng[num]);
115
        }
116
 
117
private:
118
        std::bernoulli_distribution* dist;
119
};
120
 
121
class UniformDistribution {
122
public:
123
        UniformDistribution(int min, int max) {
124
                dist = new std::uniform_int_distribution<int>(min, max);
125
        }
126
        ~UniformDistribution() {
127
                //delete dist;
128
        }
129
 
130
        void SetMinMax(int min, int max) {
131
                //delete dist;
132
                dist = new std::uniform_int_distribution<int>(min, max);
133
        }
134
 
135
        int Generate(int num) {
136
                return (*dist)(mt_rng[num]);
137
        }
138
private:
139
        std::uniform_int_distribution<int>* dist;
140
};
141
 
142
#endif /* MYRAND_H_ */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.