Intel® Advisor User Guide

ID 766448
Date 12/16/2022
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Generating C++ Stubs

To generate stubs for a working C++ application:

  1. Create a graph in the canvas as described in the Adding Nodes, Edges, and Ports section.

  2. Save the graph as described in the Saving a Graph to a File section.

  3. Click the Generate C++ icon on the toolbar to create C++ files.

Generate C++ Stubs for a Hello World Sample

For example, below is a three-node graph you can use to create a Hello World sample. This graph consists of a source_node followed by two continue_node objects. The first node is named s0 and the next two nodes are named c0 and c1. All nodes have continue_msg objects as their input and/or output types. The body of each node is defined by its C++ Function Object field, as shown below.

To generate C++ stubs from this Hello World sample graph:

  1. Create the sample graph by adding a source_node followed by two continue_node objects and connect them with edges. Modify the node names in the Node Properties: name the source_node as s0 and the next two continue_node objects as c0 and c1.

  2. Set the following properties to the nodes:

    Node Name

    Node Type

    Input Port Type

    Output Port Type

    C++ Function Object

    s0

    source_node

    None

    continue_msg

    [](continue_msg &c) -> bool {
    static bool done = false;
    if (!done) {
    done = true;
    return true;
    } else {
    return false;
    }
    }
                

    c0

    continue_node

    continue_msg

    continue_msg

    [](const continue_msg &m) -> continue_msg {
    printf("Hello");
    return m;
    }
                

    c1

    continue_node

    continue_msg

    continue_msg

    [](const continue_msg &m) -> continue_msg {
    printf(" World!\n");
    return m;
    }
                
  3. Click the Save icon on the toolbar to save this graph as HelloWorld.graphml.

  4. Click the Generate C++ icon on the toolbar to generate the C++ stubs.

The generation of the stub files should be reported as successful:

The result of C++ code generation is one file located in the same directory where the GraphML* file is last saved. The file name generated is HelloWorld_stubs.cpp. It should contain the following code:

//
// Automatically generated by Flow Graph Analyzer: 
// C++ Code Generator Plugin version XYZ
//

#define TBB_PREVIEW_FLOW_GRAPH_NODES 1
#include "tbb/flow_graph.h"
#include "tbb/tick_count.h"
#include "tbb/atomic.h"
#include <cstdlib>

using namespace std;
using namespace tbb;
using namespace tbb::flow;

size_t key_from_message(char *k) {
    return reinterpret_cast<size_t>(k);
}
      
template<typename T>
size_t key_from_message(const T &k) {
    return static_cast<size_t>(k);
}
          
static void spin_for( double weight = 0.0 ) {
    if ( weight > 0.0 ) {
      tick_count t1, t0 = tick_count::now();
      const double weight_multiple = 1e-6;
      const double end_time = weight_multiple * weight;
      do {
        t1 = tick_count::now();
      } while ( (t1-t0).seconds() < end_time );
    }
}
          
int build_and_run_HelloWorld_g0() {
    graph HelloWorld_g0;

    source_node< continue_msg > s0( HelloWorld_g0,  
    [](continue_msg &c) -> bool {
      static bool done = false;
      if (!done) {
        done = true;
        return true;
      } else {
        return false;
      }
    }, false);
          
    continue_node< continue_msg > c0( HelloWorld_g0, 0,
    [](const continue_msg &m) -> continue_msg {
      printf("Hello");
      return m;
    });
          
    continue_node< continue_msg > c1( HelloWorld_g0, 0,
    [](const continue_msg &m) -> continue_msg {
      printf(" World!\n");
      return m;
    });
          
    #if TBB_PREVIEW_FLOW_GRAPH_TRACE
    HelloWorld_g0.set_name("HelloWorld_g0");
    s0.set_name("s0");
    c0.set_name("c0");
    c1.set_name("c1");
    #endif
          
    make_edge( s0, c0 );
    make_edge( c0, c1 );
          
    s0.activate();    
    HelloWorld_g0.wait_for_all();
    return 0;
}
          
int main(int argc, char *argv[]) {
 return build_and_run_HelloWorld_g0();
}

In the code above, note the s0, c0, and c1 nodes reflect the properties described in the previous table.

If you have the paths to the Intel® oneAPI Threading Building Blocks (oneTBB) library set up in your environment, you can build this application from a command prompt:

  • On a Windows* system, run the following command from a Microsoft Visual Studio* command prompt:

    cl /EHsc HelloWorld_stubs.cpp tbb.lib
  • On a Linux* system, run the following command:

    g++ -std=c++11 HelloWorld_stubs.cpp -ltbb

In addition, Flow Graph Analyzer allows you to control execution policies for nodes, such as setting lightweight for computational nodes and asynchronous nodes. If you set lightweight policies for any node, the current code generator generates stubs for oneTBB.

See more samples demonstrating this feature in the samples/code_generation directory.