Blame view

circus/src/jcircus/discardedparallelism/DiscardedParallelism.java 14 KB
8d0dc533f   Madiel de Souza Conserva Filho   first
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
  package jcircus.discardedparallelism;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import jcircus.complementaryenvs.CallProc2ChannelSetEnv;
  import jcircus.complementaryenvs.PId2PNameEnv;
  import jcircus.complementaryenvs.ProcProcessParaEnv;
  import jcircus.environment.ChanInfoEnv;
  import jcircus.environment.ProcChanUseEnv;
  import jcircus.util.ChanUse;
  import jcircus.util.Constants;
  import jcircus.util.Util;
  
  public class DiscardedParallelism {
  	//Versão do método instChannelSimple para compartilhamento de front-end;
  	public static String instChannelSimple(String channelName, ProcChanUseEnv chanMsEnv, 
              Integer procId, boolean isMain, ProcProcessParaEnv procProcessParaEnv, PId2PNameEnv id2name, CallProc2ChannelSetEnv cp2cse) {
          String code = "";
          //List<List <Integer>> ids = resolveUndefinedChannels(chanMsEnv, isMain, channelName, procProcessParaEnv); //By Angela Freitas
          List<List <Integer>> ids = resolveUndefinedChannels(chanMsEnv, isMain, channelName, procProcessParaEnv, id2name, cp2cse);
          
          // Declaration of chanInfo
          code += "
  " + Constants.CLS_CHINFO + " chanInfo_" + channelName + " = new " + Constants.CLS_CHINFO + "();";
          
          // Initialization of chanInfo
          /*for (int i = 0; i < ids.size(); i++) { //By Angela Freitas
              Integer subProcId = (Integer) ids.get(i);
              code = code + "
  chanInfo_" + channelName + 
                      ".put(new Integer(" + subProcId + "), new Integer(" + i + "));";
          }*/
          for (int i = 0; i < ids.size(); i++) { //By Samuel Barrocas, 16/12/2010, 18:14hs
              for (int j = 0; j < ids.get(i).size(); j++) {
          		Integer subProcId = (Integer) ids.get(i).get(j);
              	code = code + "
  chanInfo_" + channelName + 
                      	".put(new Integer(" + subProcId + "), new Integer(" + i + "));";
              }
          }
  
          // Instantiation of GeneralChannel
          /*code += "
  this." + channelName + " = new GeneralChannel(" +
          	"new Any2OneChannel(), chanInfo_" + channelName + 
          	", new Integer(" + procId.intValue() + "));";*/ //Angela's, JCSP antigo
  
          code += "
  this." + channelName + " = new /*IV*/ GeneralChannel(" +
          	"org.jcsp.lang.Channel.any2one (), chanInfo_" + channelName + 
          	", new Integer(" + procId.intValue() + "));"; //Sam's, JCSP novo
  
          return code;
      }
      //VERSÃO DO MÉTODO resolveUndefinedChannels para compartilhamento de front-end
  	//Este método originalmente foi implementado por Angela Freitas, 
  		//mas foi refeito com a intenção de permitir a implementação do paralelismo com compartilhamento de front-end... 
  
      public static List<List <Integer>> resolveUndefinedChannels(ProcChanUseEnv chanMSEnv,
              boolean isMain, String channelName, ProcProcessParaEnv procProcessParaEnv, PId2PNameEnv id2name, CallProc2ChannelSetEnv cp2ces) {
          // Creates an array
          List <Integer> [] array;
          List <Integer> processesOnChannel = new ArrayList <Integer> ();// = new List <Integer> ();
          //processesOnChannel.add(new Integer (0)); //De olho nesta linha, pode ser que ela gere erro, acrescentei para tirar o IndexOutOfBouds que deu em Translator2Java, linha 656
          List <List <Integer>> processesNotOnChannel = new ArrayList <List <Integer>> ();
          processesNotOnChannel.add(new ArrayList <Integer> ());
  
          boolean writerDefined = false;
          int index = 0;
          boolean shared = false;
  
          if (!chanMSEnv.isSync()) {
              // There is no synchronization involving the channel
              // Will take part in gui, in case isMain is true
              
              if (!isMain) {
              	System.out.println ("SITUAÇÃO 1");
                  array = new List [chanMSEnv.size()];
                  // If this is being called from a constructor of a process (that is,
                  // the channel is hidden, then there is no concern about the gui.
                  // We just add all the processes in chanMSEnv to any position
                  // of the array.
                  Iterator it = chanMSEnv.iteratorKeys();
                  while(it.hasNext()) {
                      Integer procId = (Integer) it.next();
  
                      //Abaixo, by Samuel Barrocas 
                      //TODO retirar este trecho de código (Abaixo..., e Acima...), 
                      	//pois neste caso não temos sincronização. 
                      	//Como não temos sincronização (!chanMSEnv.isSync()), não temos interleaving, 
                      		//e portanto não há a necessidade de compartilhar front-end.
                      ChanInfoEnv chanInfoEnv = Util.getChannelMSEnvAnn(procProcessParaEnv.get(id2name.get(procId))); //(procId);
                      Iterator itChannels = chanInfoEnv.iteratorKeys();
                      boolean containsChannel = false;
                      while (itChannels.hasNext()) {
                      	if (itChannels.toString().equals(channelName)) {
                      		containsChannel = true;
                      	}
                      }
                      if (containsChannel) {
                      	processesOnChannel.add(procId);
                      	array [0].add(procId);
                      }
                      //Acima, by Samuel Barrocas                    
                      processesNotOnChannel.add(new ArrayList <Integer> ()); //By Samuel Barrocas
                      processesNotOnChannel.get(index).add(procId);
                      array[index] = processesNotOnChannel.get(index);
                      index++;
                  }
  
              } else {
              	System.out.println ("SITUAÇÃO 2");
                  array = new List [chanMSEnv.size()+1]; // +1 for the gui
                  // Checks if the process will be input or output
                  ChanUse chanUseNotSync = chanMSEnv.getChanUseGuiNotSyncChannel();
  
                  if(chanUseNotSync.equals(ChanUse.Input)) { //TESTADO
                  	System.out.println ("SITUAÇÃO 2.1");
                      // gui will be output
                  	List <Integer> l_int = new ArrayList <Integer> (); //By Samuel Barrocas
                  	l_int.add(new Integer (-1)); //By Samuel Barrocas
                  	array[0] = l_int; //By Samuel Barrocas
                  	//array[0] = new Integer(-1); //By Angela Freitas
  
                      // iterates over the array, inserting the other positions
                      index = 1;
                      Iterator it = chanMSEnv.iteratorKeys();
                      while(it.hasNext()) {
                          Integer procId = (Integer) it.next();
                          ChanUse chanUse = chanMSEnv.get(procId);
                          processesNotOnChannel.add(new ArrayList <Integer> ()); //By Samuel Barrocas
                          processesNotOnChannel.get(index).add(procId); //By Samuel Barrocas
                          array[index] = processesNotOnChannel.get(index); //By Samuel Barrocas
                          //array[index] = procId; //By Angela Freitas
                          index++;
                      }
  
                  } else {
                  	System.out.println ("SITUAÇÃO 2.2"); //TESTADO
                      // gui will be input
                  	List <Integer> l_int = new ArrayList <Integer> (); //By Samuel Barrocas
                  	l_int.add(new Integer (-1)); //By Samuel Barrocas
                      array[1] = l_int; //By Samuel Barrocas
                      //array[1] = new Integer(-1); //By Angela Freitas
                      index = 2;
                      writerDefined = false;
                      Iterator it = chanMSEnv.iteratorKeys();
                      while(it.hasNext()) {
                          Integer procId = (Integer) it.next();
                          ChanUse chanUse = chanMSEnv.get(procId);
  
                          // The first Undefined found goes to the writer position.
                          // There will be a bug if there is more than one Undefined
                          // because then only one of them will go to the writer
                          // position, therefore the others will try to read the 
                          // channel instead of write.
                          if (chanUse.equals(ChanUse.Undefined) && !writerDefined) { //TODO? //Samuel Barrocas
                              processesNotOnChannel.add(new ArrayList <Integer> ()); //By Samuel Barrocas
                              processesNotOnChannel.get(0).add(procId); //By Samuel Barrocas
                              array[0] = processesNotOnChannel.get(0); //By Samuel Barrocas
                              //array[0] = procId; //By Angela Freitas
                              writerDefined = true;
                          } else { //TODO?
                              if (index == chanMSEnv.size() + 1) {
                                  // Reached the end and the writer had not been defined
                                  processesNotOnChannel.add(new ArrayList <Integer> ()); //By Samuel Barrocas
                                  processesNotOnChannel.get(0).add(procId); //By Samuel Barrocas
                                  array[0] = processesNotOnChannel.get(0); //By Samuel Barrocas
                                  //array[0] = procId; //By Angela Freitas
                              }
                          }
                      }
                  }
              }
          } else {
          	System.out.println ("SITUAÇÃO 3");
              array = new List [chanMSEnv.size()];
              for (int i = 0; i < chanMSEnv.size(); i++) {
              	array [i] = new ArrayList <Integer> ();
              }
              // There is simple, or multiple synchronization involving the channel
              index = 1;
              writerDefined = false;
              Iterator it = chanMSEnv.iteratorKeys();
              while(it.hasNext()) {
                  Integer procId = (Integer) it.next();
                  ChanUse chanUse = chanMSEnv.get(procId);
  
                  if (chanUse.equals(ChanUse.Output) && !writerDefined) {
                  	System.out.println ("SITUAÇÃO 3.1");
                      // Output channel
                      writerDefined = true;
  
                  	//Abaixo, by Samuel Barrocas
                      ChanInfoEnv chanInfoEnv = Util.getChannelMSEnvAnn(procProcessParaEnv.get(id2name.get(procId)));
                      Iterator itChannels = chanInfoEnv.iteratorKeys();
                      boolean containsChannel = false;
                      while (itChannels.hasNext()) {
                      	if (itChannels.next().toString().equals(channelName)) {
                      		containsChannel = true;
                      	}
                      }
                      if (containsChannel && !cp2ces.get(procId).contains(channelName)) {
                      	processesOnChannel.add(procId);
                      	array [0].add(procId);
                      	shared = true;
                      	System.out.println ("array[0].add (" + procId + ")");
                      }
                      else {
                      	if (index == array.length && !shared)
                      		array [0].add(procId);
                      	else {
                      		array [index].add(procId);
                      		index++;
                      	}
                      	System.out.println ("array [index++].add (" + procId + ")");
                      }
                      //Acima, by Samuel Barrocas
  
                      //processesNotOnChannel.add(new ArrayList <Integer> ()); //By Samuel Barrocas
                      //processesNotOnChannel.get(0).add(procId); //By Samuel Barrocas
                      //array[0] = processesNotOnChannel.get(0); //By Samuel Barrocas
                      //array[0] = procId; //By Angela Freitas
  
                  } else {
                  	System.out.println ("SITUAÇÃO 3.2"); //TESTADO
                      // Input channel or Undefined channel
  
                  	//Abaixo, by Samuel Barrocas
                      ChanInfoEnv chanInfoEnv = Util.getChannelMSEnvAnn(procProcessParaEnv.get(id2name.get(procId)));
                      Iterator itChannels = chanInfoEnv.iteratorKeys();
                      boolean containsChannel = false;
                      while (itChannels.hasNext()) {
                      	if (itChannels.next().toString().equals(channelName)) {
                      		containsChannel = true;
                      	}
                      }
                      if (containsChannel && !cp2ces.get(procId).contains(channelName)) {
                      	processesOnChannel.add(procId);
                      	array [0].add(procId);
                      	shared = true;
                      	System.out.println ("array[0].add (" + procId + ")");
                      }
                      else {
                      	if (index == array.length && !shared)
                      		array [0].add(procId);
                      	else {
                      		array [index].add(procId);
                      		index++;
                      	}
                      	System.out.println ("array [index++].add (" + procId + ")");
                      }
                      //Acima, by Samuel Barrocas
  
                      //index++; //By Angela Freitas, comentado em 16/12/2010, às 20:30hs, pouco depois de eu colar o trecho de código acima, delimitado por "Acima...", e "Abaixo..."
  
                      // This is the last element and there is any writer in the array
                      /*if (index == array.length)
                          index = 0;*/
  
                      processesNotOnChannel.add(new ArrayList <Integer> ()); //By Samuel Barrocas
                      processesNotOnChannel.get(index).add(procId); //By Samuel Barrocas
                      array[index] = processesNotOnChannel.get(0); //By Samuel Barrocas
                      //array[index] = procId; //By Angela Freitas
                  }
              }
          }
          // Transforms the array into a list
          List<List <Integer>> list = new ArrayList<List <Integer>>();
          for (int i=0; i<array.length; i++) {
              list.add(array[i]);
          }
          
          return list;
      }
  }