source: branches/RWT/soft/validation/scripts/TestGenerator/EnsembleVariables.hpp @ 843

Last change on this file since 843 was 843, checked in by devigne, 9 years ago

RWT Commit : Add soft directory.
Soft directory contains scripts used to validate the RWT protocol

File size: 6.2 KB
Line 
1
2#ifndef _ensemblevariables_hpp_
3#define _ensemblevariables_hpp_
4
5#include "functions.h"
6#include "Variable.hpp"
7
8#include <cstdio>
9#include <iostream>
10#include <sstream>
11#include <iomanip>
12
13using namespace std;
14
15class EnsembleVariables {
16
17   int nb_vars;
18   int nb_procs;
19   int nb_diff_CL;
20   int cache_lines;
21   int line_size;
22   int nb_lines_uncached;
23   int tab_size_digits;
24   Variable * LesVariables;
25
26   void setnPubROWO(int n, int mode) {
27      // mode = 0 : -> RO; mode = 1 : -> WO; mode = 2 -> Private
28      // Les seules variables pouvant être mises en RO, en WO ou en private sont les variables publiques RW
29      int nb_pub_rw = 0;
30      for (int i = 0; i < nb_vars; i++) {
31         if (LesVariables[i].getProc() == -1 && LesVariables[i].isRW()) {
32            nb_pub_rw += 1;
33         }
34      }
35      if (n > nb_pub_rw) {
36         fprintf(stderr,"Error in function %s: cannot change attribute on as many variables\n",__func__);
37      }
38      int index;
39      int index_to_set;
40      for (int i = 0; i < n; i++) {
41         index = randint(0,nb_pub_rw - 1);
42         index_to_set = 0;
43         for (int j = 0; j < index; j++) {
44            index_to_set++;
45            while (LesVariables[index_to_set].getProc() != -1 || !LesVariables[index_to_set].isRW()) {
46               index_to_set++;
47            }
48         }
49
50         if (mode == 0) {
51            LesVariables[index_to_set].setRO();
52         }
53         else if (mode == 1) {
54            LesVariables[index_to_set].setWO();
55         }
56         else {
57            assert(mode == 2);
58            int proc = randint(0,nb_procs - 1);
59            LesVariables[index_to_set].setPrivate(proc);
60         }
61         nb_pub_rw--;
62      }
63
64   }
65
66   public:
67
68   EnsembleVariables(int nb_vars, int nb_procs, int nb_diff_CL, int cache_lines, int line_size, int nb_lines_uncached, int tab_size) {
69      this->nb_vars = nb_vars;
70      this->nb_procs = nb_procs;
71      this->nb_diff_CL = nb_diff_CL;
72      this->cache_lines = cache_lines;
73      this->line_size = line_size;
74      this->nb_lines_uncached = nb_lines_uncached;
75      this->tab_size_digits = (int) ceil(log10(tab_size));
76      LesVariables = new Variable[nb_vars + nb_lines_uncached*line_size];
77      for (int i = nb_vars; i < nb_vars + nb_lines_uncached * line_size; i++) {
78         LesVariables[i].setUncached();
79         LesVariables[i].setROorWO();
80      }
81   }
82
83   void setnPubRO(int n) {
84      setnPubROWO(n, 0);
85   }
86
87   void setnPubWO(int n) {
88      setnPubROWO(n, 1);
89   }
90
91   void setnPrivate(int n) {
92      setnPubROWO(n, 2);
93   }
94
95   void setnPrivROWO(void) {
96      // change les variables privées en RO ou WO, avec une probabilité de 20% pour chaque
97      // Eventuellement changer l'interface en : changer strictement n variables privées en RO ou WO, comme pour le cas public
98      for (int i = 0; i < nb_vars; i++) {
99         if (LesVariables[i].getProc() != -1 && LesVariables[i].isRW()) {
100            int r = randint(1,5);
101            if (r == 1) {
102               LesVariables[i].setRO();
103            }
104            else if (r == 2) {
105               LesVariables[i].setWO();
106            }
107         }
108      }
109   }
110
111   string writeVariablesNature() {
112      assert(nb_vars % line_size == 0);
113
114      stringstream res;
115      res << "/*******************" << endl;
116      res << "* Variables Nature *" << endl;
117      res << "*******************/" << endl;
118
119      for (int i = 0; i < (nb_vars / line_size) + nb_lines_uncached; i++) {
120         if (i == 0) {
121            res << "/*";
122         }
123         else {
124            res << " *";
125         }
126         for (int j = 0; j < line_size; j++) {
127            int var_index = i * line_size + j;
128            int tab_index = index2realindex(var_index, nb_diff_CL, cache_lines, line_size);
129            res << "  [" << setw(tab_size_digits) << tab_index << "]";
130            if (LesVariables[var_index].isPublic()) {
131               res << "Pub ";
132            }
133            else {
134               res << setw(3) << LesVariables[var_index].getProc() << " ";
135            }
136            if (LesVariables[var_index].isRW()) {
137               res << "RW ";
138            }
139            else if (LesVariables[var_index].isRO()) {
140               res << "RO ";
141            }
142            else {
143               assert(LesVariables[var_index].isWO());
144               res << "WO ";
145            }
146            if (LesVariables[var_index].isUnc()) {
147               res << "(U)";
148            }
149            else {
150               res << "   ";
151            }
152         }
153         res << endl;
154      }
155      res << " */" << endl;
156      return res.str();
157   }
158
159   string writePrivateAccesses(int proc_id) {
160      stringstream res;
161      for (int i = 0; i < nb_vars; i++) {
162         int tab_index = index2realindex(i, nb_diff_CL, cache_lines, line_size);
163         if (LesVariables[i].isPrivate(proc_id)) {
164            if (LesVariables[i].isRW()) {
165               res << "   tab[" << tab_index << "]++; // variable privee au proc " << proc_id << endl;
166            }
167            else if (LesVariables[i].isRO()) {
168               res << "   local_var = tab[" << tab_index << "]; // variable privee au proc " << proc_id << " et en RO" << endl;
169            }
170            else {
171               assert(LesVariables[i].isWO());
172               res << "   tab[" << tab_index << "] = 1; // variable privee au proc " << proc_id << " et en WO" << endl;
173            }
174         }
175      }
176      return res.str();
177   }
178
179
180   int getVarCount(int proc_id) {
181      // Retourne le nombre de variables pouvant être utilisées par le processeur proc_id
182      int n = 0;
183      for (int i = 0; i < nb_vars + nb_lines_uncached * line_size; i++) {
184         if (LesVariables[i].isPublic() || LesVariables[i].isPrivate(proc_id)) {
185            n++;
186         }
187      }
188      return n;
189   }
190
191   bool isPublic(int var_index) {
192      return LesVariables[var_index].isPublic();
193   }
194
195   bool isRW(int var_index) {
196      return LesVariables[var_index].isRW();
197   }
198
199   bool isRO(int var_index) {
200      return LesVariables[var_index].isRO();
201   }
202
203   bool isWO(int var_index) {
204      return LesVariables[var_index].isWO();
205   }
206
207   bool isUnc(int var_index) {
208      return LesVariables[var_index].isUnc();
209   }
210
211   bool isPrivate(int var_index, int proc_id) {
212      return LesVariables[var_index].isPrivate(proc_id);
213   }
214
215};
216
217#endif
218
Note: See TracBrowser for help on using the repository browser.