p.print("    printf(\"Number of Iterations: %d\\n\", __max_iteration);\n");
p.print("    printf(\"Number of Iterations: %d (%d)\\n\", __max_iteration, __max_iteration * "+implicit_mult+");\n");
    public static void generateFusionFile(DiscoverSchedule d_sched, int implicit_mult) {		int threadNumber = NodeEnumerator.getNumberOfNodes();		TabbedPrintWriter p;	StringWriter str; 		str = new StringWriter();        p = new TabbedPrintWriter(str);		p.print("#include <pthread.h>\n");	p.print("#include <unistd.h>\n");	p.print("#include <signal.h>\n");	p.print("#include <string.h>\n");	p.print("#include <stdlib.h>\n");	p.print("#include <stdio.h>\n");	p.println();	p.print("#include <netsocket.h>\n");	p.print("#include <node_server.h>\n");	p.print("#include <init_instance.h>\n");	p.print("#include <master_server.h>\n");	p.print("#include <save_state.h>\n");	p.print("#include <save_manager.h>\n");	p.print("#include <delete_chkpts.h>\n");	p.print("#include <object_write_buffer.h>\n");	p.print("#include <read_setup.h>\n");	p.print("#include <ccp.h>\n");	p.print("#include <read_setup.h>\n");	p.print("#include \"fusion.h\"\n");	p.println();		p.print("int __max_iteration;\n");	p.print("int __timer_enabled = 0;\n");	p.print("int __frequency_of_chkpts;\n");	p.println();	for (int i = 0; i < threadNumber; i++) {	    FlatNode node = NodeEnumerator.getFlatNode(i);	    SIROperator oper = (SIROperator)node.contents;	    Vector out = RegisterStreams.getNodeOutStreams(oper);	    	    for (int s = 0; s < out.size(); s++) {		NetStream stream = (NetStream)out.elementAt(s);		int src = stream.getSource();		int dst = stream.getDest();		String type = stream.getTypeToC();		p.print(type+" BUFFER_"+src+"_"+dst+"[__BUF_SIZE_MASK_"+src+"_"+dst+" + 1];\n");		p.print("int HEAD_"+src+"_"+dst+" = 0;\n");		p.print("int TAIL_"+src+"_"+dst+" = 0;\n");	    }	}		p.println();	for (int i = 0; i < threadNumber; i++) {	    FlatNode node = NodeEnumerator.getFlatNode(i);	    int id = NodeEnumerator.getSIROperatorId(node.contents);	    //if (!ClusterFusion.isEliminated(tmp)) {	    	    //p.print("extern void __declare_sockets_"+i+"();\n");	    //p.print("extern void __init_sockets_"+i+"(void (*)());\n");	    if (node.contents instanceof SIRFilter) {		//p.print("extern void __init_pop_buf__"+i+"();\n");		//p.print("extern void __update_pop_buf__"+i+"();\n");		p.print("extern void "+((SIRFilter)node.contents).getInit().getName()+"__"+id+"();\n");	    }	    if (node.contents instanceof SIRTwoStageFilter) {		String initWork = ((SIRTwoStageFilter)node.contents).getInitWork().getName()+"__"+id;		p.print("extern void "+initWork+"();\n");	    }	    p.print("extern void "+get_work_function(node.contents)+"(int);\n");	    /*	    String work_n = get_work_n_function(node.contents);	    if (work_n != null) {		p.print("extern void "+work_n+"(int n);\n");	    }	    */	}	p.println();	p.print("int main(int argc, char **argv) {\n");	p.print("  ");	p.print("  read_setup::read_setup_file();\n");	p.print("  __max_iteration = read_setup::max_iteration;\n");	p.print("  for (int a = 1; a < argc; a++) {\n");	p.print("    if (argc > a + 1 && strcmp(argv[a], \"-i\") == 0) {\n");	p.print("      int tmp;\n");	p.print("      sscanf(argv[a + 1], \"%d\", &tmp);\n");	p.print("      printf(\"Number of Iterations: %d\\n\", tmp);\n");	p.print("      __max_iteration = tmp;\n");	p.print("    }\n");	p.print("  }\n");	p.print("  if ("+implicit_mult+" > 1) {\n");	p.print("    printf(\"Implicit multiplicity: "+implicit_mult+"\\n\");\n");	p.print("    int tmp;\n");	p.print("    tmp = __max_iteration / "+implicit_mult+";\n");	p.print("    if (__max_iteration % "+implicit_mult+" > 0) tmp++;\n");	p.print("    __max_iteration = tmp;\n");	p.print("    printf(\"Number of Iterations: %d\\n\", __max_iteration);\n");	p.print("  }\n");	/*	for (int i = 0; i < threadNumber; i++) {	    //FlatNode tmp = NodeEnumerator.getFlatNode(i);	    //if (!ClusterFusion.isEliminated(tmp)) {	    	    p.print("__declare_sockets_"+i+"();");	}	p.println();	p.println();	p.print("  init_instance::initialize_sockets();");	p.println();	p.println();	p.print("  ");	for (int i = 0; i < threadNumber; i++) {	    //FlatNode tmp = NodeEnumerator.getFlatNode(i);	    //if (!ClusterFusion.isEliminated(tmp)) {	    	    p.print("__init_sockets_"+i+"(NULL);");	}	p.println();	p.println();	*/	int n_phases = d_sched.getNumberOfPhases();	p.print("// number of phases: "+n_phases+"\n");	p.println();	p.println();	p.print("  // ============= Initialization =============\n");	p.println();	for (int ph = 0; ph < n_phases; ph++) {		    //p.print("  // ============= Phase: "+ph+" =============\n");	    HashSet phase = d_sched.getAllOperatorsInPhase(ph);	    Iterator iter = phase.iterator();	    while (iter.hasNext()) {		SIROperator oper = (SIROperator)iter.next();		int id = NodeEnumerator.getSIROperatorId(oper);		FlatNode node = NodeEnumerator.getFlatNode(id);		Integer init = (Integer)ClusterBackend.initExecutionCounts.get(node);		int init_int = 0;		if (init != null) init_int = (init).intValue();		Integer steady = (Integer)ClusterBackend.steadyExecutionCounts.get(node);		int steady_int = 0;		if (steady != null) steady_int = (steady).intValue();		if (steady_int > 0) {		    if (init_int > 0) {						if (oper instanceof SIRFilter) {			    			    //if (ph > 0) p.print("  __init_pop_buf__"+id+"(); "); else p.print("  ");			    p.print("  ");			    p.print(((SIRFilter)oper).getInit().getName()+"__"+id+"(); ");			    			} else {			    p.print("  ");			}						if (oper instanceof SIRTwoStageFilter) {			    String initWork = ((SIRTwoStageFilter)node.contents).getInitWork().getName()+"__"+id;			    p.print("  "+initWork+"();");			    if (init_int > 1) {				p.print("  "+get_work_function(oper)+"("+					(init_int-1)+");");			    }			    			} else {			    p.print("  "+get_work_function(oper)+"("+init_int+");");			}			//p.print(get_loop(init_int, get_work_function(oper)+"();"));			p.println();					    } else {						if (oper instanceof SIRFilter) {					    			    //if (ph > 0) p.print("  __init_pop_buf__"+id+"(); ");			    p.print("  ");			    p.print(((SIRFilter)oper).getInit().getName()+"__"+id+"();");			    p.println();			}		    }		}	    }	    //p.println();	}	p.println();	p.print("  // ============= Steady State =============\n");	p.println();	p.print("  for (int n = 0; n < (__max_iteration / __MULT); n++) {\n");	for (int ph = 0; ph < n_phases; ph++) {		    //p.print("  // ============= Phase: "+ph+" =============\n");	    HashSet phase = d_sched.getAllOperatorsInPhase(ph);	    Iterator iter = phase.iterator();	    while (iter.hasNext()) {		SIROperator oper = (SIROperator)iter.next();		int id = NodeEnumerator.getSIROperatorId(oper);		FlatNode node = NodeEnumerator.getFlatNode(id);		Integer init = (Integer)ClusterBackend.initExecutionCounts.get(node);		int init_int = 0;		if (init != null) init_int = (init).intValue();		Integer steady = (Integer)ClusterBackend.steadyExecutionCounts.get(node);		int steady_int = 0;		if (steady != null) steady_int = (steady).intValue();		if (steady_int > 0) {		    Vector out = RegisterStreams.getNodeOutStreams(oper);		    for (int i = 0; i < out.size(); i++) {			NetStream s = (NetStream)out.elementAt(i);			int _s = s.getSource();			int _d = s.getDest();			p.print("    #ifdef __NOPEEK_"+_s+"_"+_d+"\n");			p.print("    HEAD_"+_s+"_"+_d+" = 0; TAIL_"+_s+"_"+_d+" = 0;\n");			p.print("    #endif\n");		    }		    		    p.print("    "+get_work_function(oper)+"("+steady_int+"*__MULT);");		}		p.println();	    }	}		p.print("  }\n");	p.print("  for (int n = 0; n < (__max_iteration % __MULT); n++) {\n");	for (int ph = 0; ph < n_phases; ph++) {		    HashSet phase = d_sched.getAllOperatorsInPhase(ph);	    Iterator iter = phase.iterator();	    while (iter.hasNext()) {		SIROperator oper = (SIROperator)iter.next();		int id = NodeEnumerator.getSIROperatorId(oper);		FlatNode node = NodeEnumerator.getFlatNode(id);		Integer init = (Integer)ClusterBackend.initExecutionCounts.get(node);		int init_int = 0;		if (init != null) init_int = (init).intValue();		Integer steady = (Integer)ClusterBackend.steadyExecutionCounts.get(node);		int steady_int = 0;		if (steady != null) steady_int = (steady).intValue();		if (steady_int > 0) {		    Vector out = RegisterStreams.getNodeOutStreams(oper);		    for (int i = 0; i < out.size(); i++) {			NetStream s = (NetStream)out.elementAt(i);			int _s = s.getSource();			int _d = s.getDest();			p.print("    #ifdef __NOPEEK_"+_s+"_"+_d+"\n");			p.print("    HEAD_"+_s+"_"+_d+" = 0; TAIL_"+_s+"_"+_d+" = 0;\n");			p.print("    #endif\n");		    }		    		    p.print("    "+get_work_function(oper)+"("+steady_int+");");		}		p.println();	    }	}		p.print("  }\n");	p.print("  return 0;");	p.print("}");	p.println();	try {	    FileWriter fw = new FileWriter("fusion.cpp");	    fw.write(str.toString());	    fw.close();	}	catch (Exception e) {	    System.err.println("Unable to write fusion.cpp");	}    }