// Example 2.2. See page 9 of the FG tutorial. #include "FG.h" #include using namespace std; int func1(FG_stage, FG_stage_params); int func2(FG_stage, FG_stage_params); int func3(FG_stage, FG_stage_params); int main() { // Declare the pipeline variables. int buffer_size = sizeof(int); int num_rounds = 1; // Declare the stages. FG_pipeline_stage_helper stage1 = new FG_pipeline_stage_helper_info(), stage2 = new FG_pipeline_stage_helper_info(), stage3 = new FG_pipeline_stage_helper_info(); // Assign a function to each stage. stage1->set_func(func1, NULL); stage2->set_func(func2, NULL); stage3->set_func(func3, NULL); // Store the stages in a NULL-terminated array. FG_pipeline_stage_helper stages[] = {stage1, stage2, stage3, NULL}; // Instantiate the pipeline. FG_pipeline my_pipeline = FG_pipeline_info::create(stages); // Set the pipeline variables. my_pipeline->set_buff_size(buffer_size); my_pipeline->set_num_rounds(num_rounds); // Fix the pipeline, which also runs it, and store the return code. int error = my_pipeline->fix(); // Display the return code. cout << "FINISHED PIPELINE WITH ERROR CODE " << error << endl; // Dismantle the pipeline. my_pipeline->dismantle(); return 0; } // func1, run by stage 1. int func1(FG_stage my_stage, FG_stage_params my_params) { // Accept a thumbnail from the preceding stage (the source). FG_pipeline_thumbnail thumb = my_stage->accept_thumbnail(); // Store a single int value in the buffer. int *buff = (int *) thumb->get_address(); *buff = 8; // Convey the thumbnail to the next stage (stage 2). my_stage->convey_thumbnail(thumb); return 0; } // func2, run by stage 2. int func2(FG_stage my_stage, FG_stage_params my_params) { // Accept a thumbnail from the preceding stage (stage 1). FG_pipeline_thumbnail thumb = my_stage->accept_thumbnail(); // Change the int value in the buffer. int *buff = (int *) thumb->get_address(); *buff += 20; // Convey the thumbnail to the next stage (stage 3). my_stage->convey_thumbnail(thumb); return 0; } // func3, run by stage 3. int func3(FG_stage my_stage, FG_stage_params my_params) { // Accept a thumbnail from the preceding stage (stage 1). FG_pipeline_thumbnail thumb = my_stage->accept_thumbnail(); // Print the value that is in the buffer. int *buff = (int *) thumb->get_address(); cout << "result: " << *buff << endl; // Convey the thumbnail to the next stage (the sink). my_stage->convey_thumbnail(thumb); return 0; }