00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077 #ifndef opt_fb_INCLUDED
00078 #define opt_fb_INCLUDED "opt_fb.h"
00079
00080 #ifndef fb_whirl_INCLUDED
00081 #include "fb_whirl.h"
00082 #endif
00083 #ifndef opt_defs_INCLUDED
00084 #include "opt_defs.h"
00085 #endif
00086 #ifndef opt_bb_INCLUDED
00087 #include "opt_bb.h"
00088 #endif
00089
00090 #ifndef opt_cfg_INCLUDED
00091 class OPT_FEEDBACK;
00092 #include "opt_cfg.h"
00093 #endif
00094 #ifndef opt_cfg_trans_INCLUDED
00095 #include "opt_cfg_trans.h"
00096 #endif
00097
00098
00099
00100
00101 #define IDTYPE_NULL 0
00102
00103
00104
00105 struct OPT_FB_EDGE {
00106
00107
00108 IDTYPE source;
00109 IDTYPE destination;
00110
00111 FB_EDGE_TYPE edge_type;
00112 FB_FREQ freq;
00113
00114 OPT_FB_EDGE( IDTYPE src, IDTYPE dst,
00115 FB_EDGE_TYPE typ = FB_EDGE_UNINIT,
00116 FB_FREQ freq = FB_FREQ_UNINIT ) :
00117 source(src),
00118 destination(dst),
00119 edge_type(typ),
00120 freq(freq) {}
00121
00122 void Print( IDTYPE id, FILE *fp = stderr ) const {
00123 char buffer[FB_EDGE_TYPE_NAME_LENGTH];
00124 FB_EDGE_TYPE_sprintf( buffer, edge_type );
00125
00126 fprintf( fp, "Edge[%3d]: (%3d --> %3d) : freq = ",
00127 id, source, destination );
00128 freq.Print( fp );
00129 fprintf( fp, " : %s\n", buffer );
00130 }
00131 };
00132
00133
00134
00135 struct OPT_FB_NODE {
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148 typedef vector<IDTYPE, mempool_allocator<IDTYPE> > EDGES;
00149 typedef EDGES::const_iterator EDGES_ITER;
00150
00151 EDGES incoming_edges;
00152 EDGES outgoing_edges;
00153
00154 #ifdef KEY
00155 WN* orig_wn;
00156 #endif
00157
00158 INT update_count;
00159 bool in_out_same;
00160
00161 FB_FREQ freq_total_in;
00162 FB_FREQ freq_total_out;
00163
00164 INT unknown_in;
00165 INT unknown_out;
00166
00167 INT unexact_in;
00168 INT unexact_out;
00169
00170 OPT_FB_NODE( MEM_POOL *pool )
00171 : incoming_edges(mempool_allocator<IDTYPE>(pool)),
00172 outgoing_edges(mempool_allocator<IDTYPE>(pool)),
00173 update_count(0), in_out_same(TRUE),
00174 unknown_in(0), unknown_out(0),
00175 unexact_in(0), unexact_out(0),
00176 freq_total_in(FB_FREQ_ZERO), freq_total_out(FB_FREQ_ZERO) {
00177 incoming_edges.reserve(2);
00178 outgoing_edges.reserve(2);
00179 }
00180
00181 void Print( IDTYPE id, FILE *fp = stderr ) const {
00182 INT t;
00183 fprintf( fp, "Node[%d]: in_out_same %c, update_count %d\n"
00184 " in: unknown %d, unexact %d, freq_total ",
00185 id, ( in_out_same ? 'Y' : 'N' ), update_count,
00186 unknown_in, unexact_in );
00187 freq_total_in.Print( fp );
00188 fprintf( fp, ", edges [" );
00189 for ( t = 0; t < incoming_edges.size(); t++ ) {
00190 fprintf( fp, " %d", incoming_edges[t] );
00191 }
00192 fprintf( fp, " ],\n out: unknown %d, unexact %d, freq_total ",
00193 unknown_out, unexact_out );
00194 freq_total_out.Print( fp );
00195 fprintf( fp, ", edges [" );
00196 for ( t = 0; t < outgoing_edges.size(); t++ ) {
00197 fprintf( fp, " %d", outgoing_edges[t] );
00198 }
00199 fprintf( fp, " ]\n" );
00200 }
00201 };
00202
00203
00204
00205 class OPT_FEEDBACK {
00206 private:
00207 MEM_POOL *_mem_pool;
00208 bool _trace;
00209 bool _trace_draw;
00210 bool _trace_before;
00211 bool _trace_prop;
00212 vector<OPT_FB_NODE, mempool_allocator<OPT_FB_NODE> > _fb_opt_nodes;
00213 vector<OPT_FB_EDGE, mempool_allocator<OPT_FB_EDGE> > _fb_opt_edges;
00214
00215 bool Edge_has_freq( IDTYPE nx_src, IDTYPE nx_dst ) const;
00216 IDTYPE Find_edge_by_type( IDTYPE nx, FB_EDGE_TYPE edge_type ) const;
00217 FB_FREQ Get_edge_freq_by_type( IDTYPE nx, FB_EDGE_TYPE edge_type ) const;
00218 IDTYPE Get_node_successor( IDTYPE nx ) const;
00219
00220 void Add_node( IDTYPE nx_new );
00221 void Add_edge( IDTYPE nx_src, IDTYPE nx_dst,
00222 FB_EDGE_TYPE edge_type, FB_FREQ freq );
00223 void Remove_edge( IDTYPE ex );
00224 void Change_edge_freq( IDTYPE ex, FB_FREQ new_freq );
00225 void Set_edge_dest( IDTYPE ex, IDTYPE nx_dst_new );
00226
00227
00228 void Freq_propagate_edge_in( OPT_FB_NODE& node,
00229 IDTYPE edge_id, FB_FREQ freq );
00230 void Freq_propagate_edge_out( OPT_FB_NODE& node,
00231 IDTYPE edge_id, FB_FREQ freq );
00232 void Freq_propagate_node_in( IDTYPE nx );
00233 void Freq_propagate_node_out( IDTYPE nx );
00234 void Freq_propagate();
00235
00236
00237 char *Node_label( IDTYPE nx ) const;
00238
00239 public:
00240 OPT_FEEDBACK( CFG *cfg, MEM_POOL *pool );
00241 ~OPT_FEEDBACK();
00242
00243 void Emit_feedback( WN *wn, BB_NODE *bb ) const;
00244
00245 bool Trace() const { return _trace; }
00246
00247 FB_FREQ Get_node_freq_in( IDTYPE nx ) const {
00248 return _fb_opt_nodes[nx].freq_total_in;
00249 }
00250 FB_FREQ Get_node_freq_out( IDTYPE nx ) const {
00251 return _fb_opt_nodes[nx].freq_total_out;
00252 }
00253 FB_FREQ Get_edge_freq( IDTYPE nx_src, IDTYPE nx_dst ) const;
00254 float Get_pred_prob( IDTYPE nx_src, IDTYPE nx_dst ) const;
00255 float Get_succ_prob( IDTYPE nx_src, IDTYPE nx_dst ) const;
00256
00257 void Delete_edge( IDTYPE nx_src, IDTYPE nx_dst );
00258 void Move_edge_dest( IDTYPE nx_src, IDTYPE nx_dst_old, IDTYPE nx_dst_new );
00259 void Move_incoming_edges_dest( IDTYPE nx_dst_old, IDTYPE nx_dst_new );
00260 void Merge_outgoing_edges(IDTYPE nx_src, IDTYPE nx_dst_new);
00261 void Delete_node( IDTYPE nx );
00262 void Split_edge( IDTYPE nx_src, IDTYPE nx_mid, IDTYPE nx_dst );
00263 void Split_node( IDTYPE nx_old, IDTYPE nx_new );
00264
00265 void Clone_edge( IDTYPE nx_src_old, IDTYPE nx_dst_old,
00266 IDTYPE nx_src_new, IDTYPE nx_dst_new, float scale );
00267 void Clone_zone( zone& z, std::map<vertex_id, vertex_id>& nx_old_to_new );
00268
00269 void Print( FILE *fp = stderr ) const;
00270 FB_VERIFY_STATUS Verify( CFG *cfg, const char *const phase );
00271
00272
00273 void Print_node( FILE *fp, IDTYPE nx ) const;
00274 void Print_edge( FILE *fp, IDTYPE src_nx, IDTYPE dst_nx ) const;
00275 void Draw() const;
00276 void Display( WN *wn_tree, const char *caller ) const;
00277 };
00278
00279 void dV_view_fb_opt_cfg( const OPT_FEEDBACK& cfg,
00280 WN *wn_tree, const char *caller );
00281
00282 #endif