for (int y = 0; y < dstInit + dstSteady + 1; y++) { r.add("  "+sdepname+"->setDst2SrcDependency("+y+","+sdep.getSrcPhase4DstPhase(y)+");\n"); }
{ int prevVal = sdep.getSrcPhase4DstPhase(0); int prevValAt = 0; final String seg1 = "  "+sdepname+"->setDst2SrcDependency("; final String seg2 = ");\n";  for (int y = 0; y < dstInit + dstSteady + 1; y++) { int thisVal = sdep.getSrcPhase4DstPhase(y); if (thisVal != prevVal) { if (y == prevValAt + 1) { r.add(seg1 + prevValAt + "," + prevVal + seg2); } else { r.add("  for (int __i = " + prevValAt + "; __i < " + y + "; __i++) {\n"); r.add("  " + seg1 + "__i," + prevVal + seg2); r.add("  }\n"); } prevValAt = y; prevVal = thisVal; } } r.add("  for (int __i = " + prevValAt + "; __i < " + (dstInit + dstSteady + 1) + "; __i++) {\n"); r.add("  " + seg1 + "__i," + prevVal + seg2); r.add("  }\n"); }
    public Vector generateRunFunction(String init_f, String main_f,                                      List/*String*/ cleanupCode) {    	Vector r = new Vector();	r.add("\n");	//  +=============================+	//  | Init State                  |	//  +=============================+	if (!isEliminated) {	    Iterator iter2 = fusedWith.iterator();	    while (iter2.hasNext()) {		FlatNode tmp = (FlatNode)iter2.next();		int fid = NodeEnumerator.getNodeId(tmp);		r.add("extern void __init_state_"+fid+"();\n");		r.add("\n");	    }	}	    	r.add("void __init_state_"+id+"() {\n");	if (!isEliminated) {	    Iterator iter2 = fusedWith.iterator();	    while (iter2.hasNext()) {		FlatNode tmp = (FlatNode)iter2.next();		int fid = NodeEnumerator.getNodeId(tmp);		r.add("  __init_state_"+fid+"();\n");	    }	}	if (init_f != null) r.add("  "+init_f+"();\n");	r.add("  if (save_state::load_state("+id+", &__steady_"+id+", __read_thread__"+id+") == -1) pthread_exit(NULL);\n");	//r.add("  __number_of_iterations_"+id+" = __max_iteration - __steady_"+id+";\n");	r.add("}\n");	r.add("\n");	//  +=============================+	//  | Main Function               |	//  +=============================+	r.add("void __main__"+id+"() {\n");	r.add("  int _tmp; // modified\n");	r.add("  int _steady = __steady_"+id+";\n");	r.add("  int _number = __max_iteration;\n");	r.add("\n");	 	if (msg_to.size() > 0) {	    r.add("  send_credits__"+id+"();\n");	}	r.add("  if (_steady == 0) {\n");		if (oper instanceof SIRFilter) {	    r.add("  __init_pop_buf__"+id+"();\n");	}		if (oper instanceof SIRTwoStageFilter) {	    r.add("    "+((SIRTwoStageFilter)oper).getInitWork().getName()+"__"+id+"();\n");	    init_counts--;	}	if (init_counts > 0) {	    r.add("    for (_tmp = 0; _tmp < "+init_counts+"; _tmp++) {\n");	    if (oper instanceof SIRFilter) {		r.add("      //check_status__"+id+"();\n");		r.add("      if (*__state_flag_"+id+" == EXIT_THREAD) exit_thread(__thread_"+id+");\n");		if (msg_from.size() > 0) {		    r.add("      check_messages__"+id+"();\n");		}		r.add("      __update_pop_buf__"+id+"();\n");	    }	    r.add("      "+work_function+"(1);\n");	    if (oper instanceof SIRFilter) {		if (msg_to.size() > 0 || msg_from.size() > 0) {		    r.add("      __counter_"+id+"++;"); 		}		if (msg_to.size() > 0) {		    r.add("      send_credits__"+id+"();\n");		}	    }	    r.add("    }\n");	}	r.add("  }\n");	r.add("  _steady++;\n");	r.add("  for (; _steady <= _number; _steady++) {\n");	if (steady_counts > 1) {	    r.add("    for (_tmp = 0; _tmp < "+steady_counts+"; _tmp++) {\n");	}	if (oper instanceof SIRFilter) {	    r.add("      //check_status__"+id+"();\n");	    r.add("      if (*__state_flag_"+id+" == EXIT_THREAD) exit_thread(__thread_"+id+");\n");	    if (msg_from.size() > 0) {		r.add("      check_messages__"+id+"();\n");	    }	    r.add("      __update_pop_buf__"+id+"();\n");	}	r.add("      "+work_function+"(1);\n");	if (oper instanceof SIRFilter) {	    if (msg_to.size() > 0 || msg_from.size() > 0) {		r.add("      __counter_"+id+"++;\n"); 	    }	    if (msg_to.size() > 0) {		r.add("      send_credits__"+id+"();\n");	    }	}	if (steady_counts > 1) {	    r.add("    }\n");	}	r.add("#ifdef __CHECKPOINT_FREQ\n");	r.add("    if (_steady % __CHECKPOINT_FREQ == 0)\n");	r.add("      save_state::save_to_file(__thread_"+id+", _steady, __write_thread__"+id+");\n");	r.add("#endif\n");		r.add("  }\n");	r.add("}\n");	r.add("\n");	//  +=============================+	//  | Run Function                |	//  +=============================+	r.add("void run_"+id+"() {\n");	r.add("  __init_sockets_"+id+"(check_status_during_io__"+id+");\n");	for (Iterator i = msg_to.iterator(); i.hasNext(); ) {		    SIRFilter sender = (SIRFilter)oper;	    SIRFilter receiver = (SIRFilter)i.next();	    int fromID = id;	    int toID = NodeEnumerator.getSIROperatorId(receiver);	    	    boolean downstream = LatencyConstraints.isMessageDirectionDownstream(sender, receiver);	    r.add("\n  //SDEP from: "+fromID+" to: "+toID+";\n");	    	    streamit.scheduler2.constrained.Scheduler cscheduler =		streamit.scheduler2.constrained.Scheduler.createForSDEP(ClusterBackend.topStreamIter);			    streamit.scheduler2.iriter.Iterator firstIter = 		IterFactory.createFactory().createIter(sender);	    streamit.scheduler2.iriter.Iterator lastIter = 		IterFactory.createFactory().createIter(receiver);				    streamit.scheduler2.SDEPData sdep = null;	    if (downstream) {		r.add("  //message sent downstream;\n");		try {		    sdep = cscheduler.computeSDEP(firstIter, lastIter);		} catch (streamit.scheduler2.constrained.NoPathException ex) {}	    	    } else {		r.add("  //message sent upstream;\n");				try {		    sdep = cscheduler.computeSDEP(lastIter, firstIter);		} catch (streamit.scheduler2.constrained.NoPathException ex) {}	    }			    int srcInit = sdep.getNumSrcInitPhases();	    int srcSteady = sdep.getNumSrcSteadyPhases();			    int dstInit = sdep.getNumDstInitPhases();	    int dstSteady = sdep.getNumDstSteadyPhases();			    String sdepname = "sdep_"+fromID+"_"+toID;			    r.add("  "+sdepname+" = new sdep("+		  srcInit+","+dstInit+","+		  srcSteady+","+dstSteady+");\n");	    // TODO:  this loop can get too large to unroll at compile time:	    // one example of size 1x10^6 cause gcc to crash.	    // If > threshold size then find contiguous sections with	    // the same sdep.getSrcPhase4DstPhase(y) and generate loops	    // rather than unrolling	    for (int y = 0; y < dstInit + dstSteady + 1; y++) {		r.add("  "+sdepname+"->setDst2SrcDependency("+y+","+sdep.getSrcPhase4DstPhase(y)+");\n");	    }	}	//r.add("  __steady_"+id+" = __init_iter;\n");	r.add("  __init_state_"+id+"();\n");	r.add("\n");	if (!data_out.iterator().hasNext()) {	    r.add("  timer t1;\n");	    /*	    r.add("  //peek one item from all incoming data streams\n");	    i = data_in.iterator();	    while (i.hasNext()) {		NetStream in = (NetStream)i.next();		r.add("  "+in.consumer_name()+".peek(0);\n");	    }	    */	    r.add("  __peek_sockets_"+id+"();\n");	    r.add("  t1.start();\n");	}	//if (main_f != null) r.add("  "+main_f+"();\n");	r.add("  __main__"+id+"();\n");	if (!data_out.iterator().hasNext()) {	    r.add("  t1.stop();\n");	    r.add("  if (__timer_enabled) t1.output(stderr);\n");	}	r.add("\n");        for (Iterator cleanIt = cleanupCode.iterator(); cleanIt.hasNext();) {        r.add(cleanIt.next().toString());    }	r.add("  __flush_sockets_"+id+"();\n");	r.add("  pthread_exit(NULL);\n");	r.add("}\n");	return r;    }