13.7.6. Macro “relauncherCodeFlowrateThreaded.C

13.7.6.1. Objective

The goal of this macro is to show how to handle a code run on several threads. In order to this, the usual sequential runner will be removed and another runner will be called to do the job. The flowrate code is provided with Uranie and has been also used and discussed throughout these macros.

13.7.6.2. Macro

    TAttribute *rw = new TAttribute("rw");
    TAttribute *r = new TAttribute("r");
    TAttribute *tu = new TAttribute("tu");
    TAttribute *tl = new TAttribute("tl");
    TAttribute *hu = new TAttribute("hu");
    TAttribute *hl = new TAttribute("hl");
    TAttribute *l = new TAttribute("l");
    TAttribute *kw = new TAttribute("kw");

    // Create the output attribute
    TAttribute *yhat = new TAttribute("yhat");
    TAttribute *d = new TAttribute("d");

    // Set the reference input file and the key for each input attributes
    TFlatScript fin("flowrate_input_with_values_rows.in");
    fin.setInputs(8, rw, r, tu, tl, hu, hl, l, kw);

    // The output file of the code
    TFlatResult fout("_output_flowrate_withRow_.dat");
    fout.setOutputs(2, yhat, d);// Passing the attributes to the output file

    // Constructing the code
    TCodeEval mycode( "flowrate -s -r" );    
    mycode.setOldTmpDir();
    mycode.addInputFile(&fin); // Adding the input file
    mycode.addOutputFile(&fout); // Adding the output file

    // Fix the number of threads
    int nthread = 3;
    // Create the Threaded runner
    TThreadedRun run(&mycode, nthread);
    run.startSlave(); // Start the master 
    if (run.onMaster())
    {
        // Create the TDataServer
        TDataServer *tds = new TDataServer("foo","test");
        mycode.addAllInputs(tds);
        tds->fileDataRead("flowrateUniformDesign.dat", kFALSE, kTRUE);

        TLauncher2 lanceur(tds, &run);

        // resolution
        lanceur.solverLoop();
        run.stopSlave(); // Stop the slaves (necessary even for a sequential)
  
        // Draw the result
        TCanvas *can = new TCanvas("pouet","foo",1);
        tds->Draw("yhat:rw","","colZ");
    }

The only difference when comparing this macro to the previous one (see Macro) is the runner creation:

    // Fix the number of threads
    int nthread = 3;
    // Create the Threaded runner
    TThreadedRun run(&mycode, nthread);

The TSequentialRun object becomes a TThreadedRun object whose construction request on top of the assessor, the number of threads to be used. Apart from that, the master is created and the rest is crystal clear, leading to the following plot.

13.7.6.3. Graph

../../_images/relauncherCodeFlowrateThreaded.png

Figure 13.49 Representation of the output as a function of the first input with a colZ option