source: XIOS/dev/dev_trunk_graph/src/workflow_graph.cpp @ 2019

Last change on this file since 2019 was 2019, checked in by yushan, 9 months ago

Graph intermedia commit to a tmp branch

File size: 12.3 KB
Line 
1#include "workflow_graph.hpp"
2#include "cxios.hpp"
3
4namespace xios
5{
6
7  std::unordered_map <int, graph_info_box_node> *CWorkflowGraph::mapFilters_ptr_with_info = 0;
8
9  std::unordered_map <int, graph_info_box_edge > *CWorkflowGraph::mapFieldToFilters_ptr_with_info = 0;
10
11  std::unordered_map <size_t, int> *CWorkflowGraph::mapHashFilterID_ptr = 0;
12 
13  std::unordered_map <StdString, int > *CWorkflowGraph::mapContext_ptr = 0;
14
15
16  std::vector<graph_node_object> *CWorkflowGraph::vectorOfNodes_ = 0;
17  std::vector<graph_edge_object> *CWorkflowGraph::vectorOfEdges_ = 0;
18  std::vector<StdString> *CWorkflowGraph::vectorOfContexts_ = 0;
19 
20
21  std::vector<graph_node_object> *CWorkflowGraph::vectorOfNodes_srv_ = 0;
22  std::vector<graph_edge_object> *CWorkflowGraph::vectorOfEdges_srv_ = 0;
23  std::vector<StdString> *CWorkflowGraph::vectorOfContexts_srv_ = 0;
24
25  bool CWorkflowGraph::clientGraphBuilt = false;
26  bool CWorkflowGraph::serverGraphBuilt = false;
27
28  int CWorkflowGraph::filterIdGenerator = 0;
29  int CWorkflowGraph::edgeIdGenerator = 0;
30
31
32  vector <StdString> CWorkflowGraph::filters;
33  vector <StdString> CWorkflowGraph::fields;
34  vector <pair<int, int> > CWorkflowGraph::fieldsToFilters;
35
36  bool CWorkflowGraph::build_begin = false;
37
38
39  CWorkflowGraph::CWorkflowGraph()
40  { }
41
42
43//******************************************************
44
45 
46
47  void CWorkflowGraph::drawWorkFlowGraph_client()
48  TRY
49  {
50    if(vectorOfNodes_ && vectorOfEdges_) 
51    {
52      std::cout<<"\n\nbuild workflow graph ..."<<std::endl;
53      for(int i=0; i<vectorOfNodes_->size(); i++)
54      {
55        std::cout<<"Node["<<i<<"] is "<<(*vectorOfNodes_)[i].filter_name<<std::endl;
56      }
57     
58      for(int i=0; i<vectorOfEdges_->size(); i++)
59      {
60        std::cout<<"Edge["<<i<<"] from "<<(*vectorOfEdges_)[i].from<<" to "<<(*vectorOfEdges_)[i].to<<std::endl;
61      }
62      std::cout<<"\nend workflow graph ...\n\n"<<std::endl;
63     
64      outputWorkflowGraph_client();
65    }
66    else std::cout<<"graph information not complete"<<std::endl;
67  }
68  CATCH
69 
70
71  void CWorkflowGraph::drawWorkFlowGraph_server()
72  TRY
73  {
74    if(vectorOfNodes_srv_ && vectorOfEdges_srv_) 
75    {
76      std::cout<<"\n\nServer side : build workflow graph ..."<<std::endl;
77      for(int i=0; i<vectorOfNodes_srv_->size(); i++)
78      {
79        std::cout<<"Node["<<i<<"] is "<<(*vectorOfNodes_srv_)[i].filter_name<<std::endl;
80      }
81     
82      for(int i=0; i<vectorOfEdges_srv_->size(); i++)
83      {
84        std::cout<<"Edge["<<i<<"] from "<<(*vectorOfEdges_srv_)[i].from<<" to "<<(*vectorOfEdges_srv_)[i].to<<std::endl;
85      }
86      std::cout<<"\nend workflow graph ...\n\n"<<std::endl;
87     
88      outputWorkflowGraph_server();
89    }
90    else std::cout<<"Server side : graph information not complete"<<std::endl;
91
92  }
93  CATCH
94 
95  void CWorkflowGraph::addEdge(int from, int to, CDataPacketPtr packet)
96  TRY
97  {
98    if(from<0) return;
99
100    if(CXios::isClient)
101    {
102      if(!vectorOfEdges_) vectorOfEdges_ = new std::vector<graph_edge_object>;
103      std::string currentContextId = CContext::getCurrent()->getId();
104     
105      graph_edge_object edge_obj;   
106      edge_obj.from = from;
107      edge_obj.to = to;
108      edge_obj.date = packet->date;
109      edge_obj.timestamp = packet->timestamp;
110      edge_obj.field = packet->graphPackage->currentField;
111      for(int i=0; i<vectorOfContexts_->size(); i++)
112      {
113        if(vectorOfContexts_->at(i) == currentContextId)
114        {
115          edge_obj.context = i;     
116          edge_obj.context_id = currentContextId;     
117          break;
118        }
119      } 
120      edge_obj.attributes = packet->graphPackage->currentField->recordXiosAttributes();
121     
122      vectorOfEdges_->push_back(edge_obj);
123      std::cout<<"****************** Add Edge from "<<from<<" to "<<to<<std::endl; 
124      vectorOfNodes_->at(from).filter_filled = true;
125    }
126    else
127    {
128      if(!vectorOfEdges_srv_) vectorOfEdges_srv_ = new std::vector<graph_edge_object>;
129      std::string currentContextId = CContext::getCurrent()->getId();
130     
131      graph_edge_object edge_obj;   
132      edge_obj.from = from;
133      edge_obj.to = to;
134      edge_obj.date = packet->date;
135      edge_obj.timestamp = packet->timestamp;
136      edge_obj.field = packet->graphPackage->currentField;
137      for(int i=0; i<vectorOfContexts_srv_->size(); i++)
138      {
139        if(vectorOfContexts_srv_->at(i) == currentContextId)
140        {
141          edge_obj.context = i;     
142          edge_obj.context_id = currentContextId;     
143          break;
144        }
145      }
146      edge_obj.attributes = packet->graphPackage->currentField->recordXiosAttributes();
147     
148      vectorOfEdges_srv_->push_back(edge_obj);
149      std::cout<<"****************** Server side : Add Edge from "<<from<<" to "<<to<<std::endl; 
150      vectorOfNodes_srv_->at(from).filter_filled = true;
151
152    }
153  }
154  CATCH
155 
156
157  void CWorkflowGraph::addNode(StdString filterName, int filterClass, bool filterFilled, int entryNb, CDataPacketPtr packet)
158  TRY
159  {
160    if(CXios::isClient)
161    {
162      if(!vectorOfNodes_) vectorOfNodes_ = new std::vector<graph_node_object>;
163      if(!vectorOfContexts_) vectorOfContexts_ = new std::vector<StdString>;
164      std::string currentContextId = CContext::getCurrent()->getId();
165      if ( std::find(vectorOfContexts_->begin(), vectorOfContexts_->end(), currentContextId) == vectorOfContexts_->end() )
166         vectorOfContexts_->push_back(currentContextId);
167     
168     
169      graph_node_object node_obj;   
170      node_obj.filter_name = filterName;
171      node_obj.filter_class = filterClass;
172      node_obj.filter_filled = filterFilled;
173      node_obj.expected_entry_nb = entryNb;
174      node_obj.date = packet->date;
175      node_obj.timestamp = packet->timestamp;
176      for(int i=0; i<vectorOfContexts_->size(); i++)
177      {
178        if(vectorOfContexts_->at(i) == currentContextId)
179        {
180          node_obj.context = i;     
181          node_obj.context_id = currentContextId;     
182          break;
183        }
184      }   
185      node_obj.attributes = packet->graphPackage->currentField->recordXiosAttributes();
186
187      vectorOfNodes_->push_back(node_obj);
188      std::cout<<"****************** Add node "<<filterName<<std::endl;   
189    }
190    else
191    { 
192      if(!vectorOfNodes_srv_) vectorOfNodes_srv_ = new std::vector<graph_node_object>;
193      if(!vectorOfContexts_srv_) vectorOfContexts_srv_ = new std::vector<StdString>;
194      std::string currentContextId = CContext::getCurrent()->getId();
195      if ( std::find(vectorOfContexts_srv_->begin(), vectorOfContexts_srv_->end(), currentContextId) == vectorOfContexts_srv_->end() )
196         vectorOfContexts_srv_->push_back(currentContextId);
197     
198      graph_node_object node_obj;   
199      node_obj.filter_name = filterName;
200      node_obj.filter_class = filterClass;
201      node_obj.filter_filled = filterFilled;
202      node_obj.expected_entry_nb = entryNb;
203      node_obj.date = packet->date;
204      node_obj.timestamp = packet->timestamp;
205      for(int i=0; i<vectorOfContexts_srv_->size(); i++)
206      {
207        if(vectorOfContexts_srv_->at(i) == currentContextId)
208        {
209          node_obj.context = i;     
210          node_obj.context_id = currentContextId;     
211          break;
212        }
213      } 
214      node_obj.attributes = packet->graphPackage->currentField->recordXiosAttributes();
215
216      vectorOfNodes_srv_->push_back(node_obj);
217      std::cout<<"******************Server side : Add node "<<filterName<<std::endl; 
218    }
219
220  }
221  CATCH
222
223  int CWorkflowGraph::getNodeSize()
224  TRY
225  {
226    if(CXios::isClient)
227    {
228      return !vectorOfNodes_? 0 : vectorOfNodes_->size();
229    }
230    else
231    {
232      return !vectorOfNodes_srv_? 0 : vectorOfNodes_srv_->size();
233    }
234  }
235  CATCH
236
237
238
239  void CWorkflowGraph::outputWorkflowGraph_client()
240  {
241    int graph_rank;
242    MPI_Comm_rank(MPI_COMM_WORLD, &graph_rank);
243    std::ofstream *outfiles;
244
245    outfiles = new std::ofstream[vectorOfContexts_->size()];
246
247    for(int ctx=0; ctx<vectorOfContexts_->size(); ctx++)
248    {
249      StdString graphFileName="graph_data_"+vectorOfContexts_->at(ctx)+"_client_"+to_string(graph_rank)+".json";
250      outfiles[ctx].open(graphFileName); 
251   
252      outfiles[ctx] << "{\"nodes\":["<<std::endl;
253    }
254    for(int i=0; i<vectorOfNodes_->size(); i++)
255    {
256      int ctx = vectorOfNodes_->at(i).context;
257      if(i!=0) outfiles[ctx] << ",";
258      outfiles[ctx] << "{\"id\":"<<i<<","<<std::endl;
259      outfiles[ctx] << "\"label\":"<<"\""<<vectorOfNodes_->at(i).filter_name<<"\","<<std::endl;
260      outfiles[ctx] << "\"class\":"<<vectorOfNodes_->at(i).filter_class<<","<<std::endl;
261      outfiles[ctx] << "\"filled\":"<<!(vectorOfNodes_->at(i).filter_filled)<<","<<std::endl;
262      outfiles[ctx] << "\"context\":"<<"\""<<vectorOfNodes_->at(i).context_id<<"\","<<std::endl;
263      outfiles[ctx] << "\"entry\":"<<"\""<<vectorOfNodes_->at(i).expected_entry_nb<<"\","<<std::endl;
264      outfiles[ctx] << "\"attributes\":"<<"\""<<vectorOfNodes_->at(i).attributes<<"\","<<std::endl;
265      outfiles[ctx] << "\"type\":"<<"\"\"}"<<std::endl;
266    } 
267    for(int ctx=0; ctx<vectorOfContexts_->size(); ctx++)
268    {
269      outfiles[ctx] << std::endl<<"],"<<std::endl<<"\"edges\" : ["<<std::endl;
270    }
271    for(int i=0; i<vectorOfEdges_->size(); i++)
272    {
273      int ctx = vectorOfEdges_->at(i).context;
274      if(i!=0) outfiles[ctx] << ",";
275      outfiles[ctx] << "{\"id\":"<<i<<","<<std::endl;
276      outfiles[ctx] << "\"from\":"<<vectorOfEdges_->at(i).from<<","<<std::endl;
277      outfiles[ctx] << "\"to\":"<<vectorOfEdges_->at(i).to<<","<<std::endl;
278      outfiles[ctx] << "\"label\":"<<"\""<<vectorOfEdges_->at(i).field->getId()<<"\\n"<<vectorOfEdges_->at(i).date<<"\","<<std::endl;
279      outfiles[ctx] << "\"context\":"<<"\""<<vectorOfEdges_->at(i).context_id<<"\","<<std::endl;
280      outfiles[ctx] << "\"attributes\":"<<"\""<<vectorOfEdges_->at(i).attributes<<"\","<<std::endl;
281      outfiles[ctx] << "\"date\":"<<"\""<<vectorOfEdges_->at(i).date<<"\"}"<<std::endl;
282    }
283    for(int ctx=0; ctx<vectorOfContexts_->size(); ctx++)
284    {
285      outfiles[ctx] << std::endl<<"]}"<<std::endl;
286      outfiles[ctx].close();
287    }
288  }
289 
290
291  void CWorkflowGraph::outputWorkflowGraph_server()
292  {
293    int graph_rank;
294    MPI_Comm_rank(MPI_COMM_WORLD, &graph_rank);
295    std::ofstream *outfiles;
296
297    outfiles = new std::ofstream[vectorOfContexts_srv_->size()];
298
299    for(int ctx=0; ctx<vectorOfContexts_srv_->size(); ctx++)
300    {
301      StdString graphFileName="graph_data_"+vectorOfContexts_srv_->at(ctx)+"_client_"+to_string(graph_rank)+".json";
302      outfiles[ctx].open(graphFileName); 
303   
304      outfiles[ctx] << "{\"nodes\":["<<std::endl;
305    }
306    for(int i=0; i<vectorOfNodes_srv_->size(); i++)
307    {
308      int ctx = vectorOfNodes_srv_->at(i).context;
309      if(i!=0) outfiles[ctx] << ",";
310      outfiles[ctx] << "{\"id\":"<<i<<","<<std::endl;
311      outfiles[ctx] << "\"label\":"<<"\""<<vectorOfNodes_srv_->at(i).filter_name<<"\","<<std::endl;
312      outfiles[ctx] << "\"class\":"<<vectorOfNodes_srv_->at(i).filter_class<<","<<std::endl;
313      outfiles[ctx] << "\"filled\":"<<!(vectorOfNodes_srv_->at(i).filter_filled)<<","<<std::endl;
314      outfiles[ctx] << "\"context\":"<<"\""<<vectorOfNodes_srv_->at(i).context_id<<"\","<<std::endl;
315      outfiles[ctx] << "\"entry\":"<<"\""<<vectorOfNodes_srv_->at(i).expected_entry_nb<<"\","<<std::endl;
316      outfiles[ctx] << "\"attributes\":"<<"\""<<vectorOfNodes_srv_->at(i).attributes<<"\","<<std::endl;
317      outfiles[ctx] << "\"type\":"<<"\"\"}"<<std::endl;
318    } 
319    for(int ctx=0; ctx<vectorOfContexts_srv_->size(); ctx++)
320    {
321      outfiles[ctx] << std::endl<<"],"<<std::endl<<"\"edges\" : ["<<std::endl;
322    }
323    for(int i=0; i<vectorOfEdges_srv_->size(); i++)
324    {
325      int ctx = vectorOfEdges_srv_->at(i).context;
326      if(i!=0) outfiles[ctx] << ",";
327      outfiles[ctx] << "{\"id\":"<<i<<","<<std::endl;
328      outfiles[ctx] << "\"from\":"<<vectorOfEdges_srv_->at(i).from<<","<<std::endl;
329      outfiles[ctx] << "\"to\":"<<vectorOfEdges_srv_->at(i).to<<","<<std::endl;
330      outfiles[ctx] << "\"label\":"<<"\""<<vectorOfEdges_srv_->at(i).field->getId()<<"\\n"<<vectorOfEdges_srv_->at(i).date<<"\","<<std::endl;
331      outfiles[ctx] << "\"context\":"<<"\""<<vectorOfEdges_srv_->at(i).context_id<<"\","<<std::endl;
332      outfiles[ctx] << "\"attributes\":"<<"\""<<vectorOfEdges_srv_->at(i).attributes<<"\","<<std::endl;
333      outfiles[ctx] << "\"date\":"<<"\""<<vectorOfEdges_srv_->at(i).date<<"\"}"<<std::endl;
334    }
335    for(int ctx=0; ctx<vectorOfContexts_srv_->size(); ctx++)
336    {
337      outfiles[ctx] << std::endl<<"]}"<<std::endl;
338      outfiles[ctx].close();
339    }
340  }
341}
342
Note: See TracBrowser for help on using the repository browser.