NetworkManager.C 72.9 KB
Newer Older
hrchilds's avatar
hrchilds committed
1 2 3 4 5 6
#include <NetworkManager.h>
#include <DataNetwork.h>
#include <DebugStream.h>
#include <avtDatabaseFactory.h>
#include <LoadBalancer.h>
#include <MaterialAttributes.h>
hrchilds's avatar
hrchilds committed
7
#include <avtExpressionEvaluatorFilter.h>
hrchilds's avatar
hrchilds committed
8
#include <ImproperUseException.h>
hrchilds's avatar
hrchilds committed
9
#include <InvalidVariableException.h>
hrchilds's avatar
hrchilds committed
10 11 12 13 14 15
#include <DatabaseException.h>
#include <NoInputException.h>
#include <avtPluginFilter.h>
#include <OperatorPluginManager.h>
#include <OperatorPluginInfo.h>
#include <PickAttributes.h>
hrchilds's avatar
hrchilds committed
16
#include <avtExtents.h>
hrchilds's avatar
hrchilds committed
17
#include <avtNullData.h>
hrchilds's avatar
hrchilds committed
18
#include <avtDatabaseMetaData.h>
hrchilds's avatar
hrchilds committed
19 20 21
#include <avtDataObjectQuery.h>
#include <avtCompactnessQuery.h>
#include <avtEulerianQuery.h>
hrchilds's avatar
hrchilds committed
22 23
#include <avtAreaBetweenCurvesQuery.h>
#include <avtCycleQuery.h>
hrchilds's avatar
hrchilds committed
24
#include <avtFileWriter.h>
hrchilds's avatar
hrchilds committed
25 26 27 28
#include <avtIntegrateQuery.h>
#include <avtL2NormQuery.h>
#include <avtL2NormBetweenCurvesQuery.h>
#include <avtTimeQuery.h>
hrchilds's avatar
hrchilds committed
29 30
#include <avtLocateCellQuery.h>
#include <avtPickQuery.h>
hrchilds's avatar
hrchilds committed
31
#include <avtCurvePickQuery.h>
hrchilds's avatar
hrchilds committed
32
#include <avtOriginalDataMinMaxQuery.h>
hrchilds's avatar
hrchilds committed
33 34
#include <avtOriginalDataNumNodesQuery.h>
#include <avtOriginalDataNumZonesQuery.h>
hrchilds's avatar
hrchilds committed
35
#include <avtActualDataMinMaxQuery.h>
hrchilds's avatar
hrchilds committed
36 37
#include <avtActualDataNumNodesQuery.h>
#include <avtActualDataNumZonesQuery.h>
hrchilds's avatar
hrchilds committed
38 39 40 41 42 43 44
#include <avtSourceFromAVTImage.h>
#include <avtSourceFromImage.h>
#include <avtSourceFromNullData.h>
#include <avtTotalSurfaceAreaQuery.h>
#include <avtTotalVolumeQuery.h>
#include <avtTotalRevolvedVolumeQuery.h>
#include <avtTotalRevolvedSurfaceAreaQuery.h>
hrchilds's avatar
hrchilds committed
45
#include <avtVariableQuery.h>
hrchilds's avatar
hrchilds committed
46 47
#include <avtVariableSummationQuery.h>
#include <avtWeightedVariableSummationQuery.h>
hrchilds's avatar
hrchilds committed
48 49
#include <avtWholeImageCompositer.h>
#include <avtPlot.h>
hrchilds's avatar
hrchilds committed
50
#include <CompactSILRestrictionAttributes.h>
hrchilds's avatar
hrchilds committed
51
#include <VisWindow.h>
hrchilds's avatar
hrchilds committed
52 53 54
#include <ParsingExprList.h>
#include <EngineExprNode.h>
#include <ParserInterface.h>
hrchilds's avatar
hrchilds committed
55 56
#include <PlotPluginManager.h>
#include <PlotPluginInfo.h>
hrchilds's avatar
hrchilds committed
57 58
#ifdef PARALLEL
#include <mpi.h>
hrchilds's avatar
hrchilds committed
59
#include <parallel.h>
hrchilds's avatar
hrchilds committed
60
#endif
hrchilds's avatar
hrchilds committed
61
#include <TimingsManager.h>
hrchilds's avatar
hrchilds committed
62

hrchilds's avatar
hrchilds committed
63 64 65
#include <set>
using std::set;

hrchilds's avatar
hrchilds committed
66
static double RenderBalance(int numTrianglesIHave);
hrchilds's avatar
hrchilds committed
67 68 69 70 71 72 73 74 75 76 77 78

// ****************************************************************************
//  Method: NetworkManager default constructor
//
//  Programmer: Jeremy Meredith
//  Creation:   September 29, 2000
//
//  Modifications:
//
//    Kathleen Bonnell, Tue Nov 20 12:35:54 PST 2001
//    Initialize requireOriginalCells. 
//
hrchilds's avatar
hrchilds committed
79 80 81
//    Hank Childs, Mon Jan  5 16:04:57 PST 2004
//    Initialize uniqueNetworkId.
//
hrchilds's avatar
hrchilds committed
82 83 84
//    Hank Childs, Thu Mar  4 09:11:46 PST 2004
//    Change instantiation of vis window, since it is now assumed to be nowin.
//
hrchilds's avatar
hrchilds committed
85 86 87
// ****************************************************************************
NetworkManager::NetworkManager(void) : virtualDatabases()
{
hrchilds's avatar
hrchilds committed
88
    //cerr << "NetworkManager::NetworkManager(void)" << endl;
hrchilds's avatar
hrchilds committed
89 90 91
    workingNet = NULL;
    loadBalancer = NULL;
    requireOriginalCells = false;
hrchilds's avatar
hrchilds committed
92
    uniqueNetworkId = 0;
hrchilds's avatar
hrchilds committed
93 94 95

    // stuff to support scalable rendering
    {
hrchilds's avatar
hrchilds committed
96
       viswin = new VisWindow();
hrchilds's avatar
hrchilds committed
97 98 99 100 101 102 103
       AnnotationAttributes newAtts = *(viswin->GetAnnotationAtts());
       newAtts.SetUserInfoFlag(false);
       newAtts.SetDatabaseInfoFlag(false);
       newAtts.SetLegendInfoFlag(false);
       newAtts.SetTriadFlag(false);
       newAtts.SetBboxFlag(false);
       newAtts.SetAxesFlag(false);
hrchilds's avatar
hrchilds committed
104
       newAtts.SetAxesFlag2D(false);
hrchilds's avatar
hrchilds committed
105 106 107 108 109 110 111 112 113 114 115 116
       viswin->SetAnnotationAtts(&newAtts);
       viswin->DisableUpdates();
    }
}

// ****************************************************************************
//  Method: NetworkManager destructor
//
//  Programmer: Jeremy Meredith
//  Creation:   September 29, 2000
//
//  Modifications:
hrchilds's avatar
hrchilds committed
117 118 119 120 121 122
//    Sean Ahern, Fri May 24 11:12:25 PDT 2002
//    Got rid of deletion of the database cache, since all DBs in there
//    will be deleted by deleting the network cache.
//
//    Hank Childs, Mon Jan  5 16:30:04 PST 2004
//    Don't assume the caches are valid.
hrchilds's avatar
hrchilds committed
123 124 125 126
//
// ****************************************************************************
NetworkManager::~NetworkManager(void)
{
hrchilds's avatar
hrchilds committed
127
    //cerr << "NetworkManager::~NetworkManager(void)" << endl;
hrchilds's avatar
hrchilds committed
128
    for (int i = 0; i < networkCache.size(); i++)
hrchilds's avatar
hrchilds committed
129 130
        if (networkCache[i] != NULL)
            delete networkCache[i];
hrchilds's avatar
hrchilds committed
131 132 133 134 135 136 137 138 139 140 141 142
}

// ****************************************************************************
//  Method: NetworkManager::ClearAllNetworks
//
//  Purpose:
//      Clears out the caches.  Really only used for checking for memory leaks.
//
//  Programmer: Hank Childs
//  Creation:   January 7, 2001
//
//  Modifications:
hrchilds's avatar
hrchilds committed
143 144 145 146 147
//    Sean Ahern, Tue May 14 14:35:42 PDT 2002
//    Retrofitted for my new network management techniques.
//
//    Sean Ahern, Tue May 21 16:09:56 PDT 2002
//    Told the underlying objects to free their data.
hrchilds's avatar
hrchilds committed
148
//
hrchilds's avatar
hrchilds committed
149 150 151
//    Hank Childs, Mon Jan  5 16:30:04 PST 2004
//    Don't clear out the vectors, so this can be consistent with how the
//    ClearNetworksWithDatabase works.
hrchilds's avatar
hrchilds committed
152
//
hrchilds's avatar
hrchilds committed
153 154 155
//    Brad Whitlock, Thu Feb 26 11:58:58 PDT 2004
//    I replaced the commented out cerr with debug3.
//
hrchilds's avatar
hrchilds committed
156 157 158 159 160
//    Hank Childs, Sun Mar  7 16:05:03 PST 2004
//    Delete the networks before the databases, since the networks have
//    references to the databases (and the previous ordering yielded dangling
//    pointers).
//
hrchilds's avatar
hrchilds committed
161 162 163 164
// ****************************************************************************
void
NetworkManager::ClearAllNetworks(void)
{
hrchilds's avatar
hrchilds committed
165
    debug3 << "NetworkManager::ClearAllNetworks(void)" << endl;
hrchilds's avatar
hrchilds committed
166 167
    int i;

hrchilds's avatar
hrchilds committed
168 169 170 171 172 173 174
    for (i = 0; i < networkCache.size(); i++)
    {
        if (networkCache[i] != NULL)
            delete networkCache[i];
        networkCache[i] = NULL;
    }

hrchilds's avatar
hrchilds committed
175 176 177 178 179 180 181
    for (i = 0; i < databaseCache.size(); i++)
    {
        if (databaseCache[i] != NULL)
            delete databaseCache[i];
        databaseCache[i] = NULL;
    }

hrchilds's avatar
hrchilds committed
182 183
    for (i = 0 ; i < globalCellCounts.size() ; i++)
    {
hrchilds's avatar
hrchilds committed
184
        globalCellCounts[i] = -1;
hrchilds's avatar
hrchilds committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    }
}

// ****************************************************************************
//  Method: NetworkManager::ClearNetworksWithDatabase
//
//  Purpose:
//      Clears out the caches that reference a specific database.
//
//  Arguments:
//      db      The name of a database.
//
//  Programmer: Hank Childs
//  Creation:   January 5, 2004
//
hrchilds's avatar
hrchilds committed
200 201 202 203
//  Modifications:
//    Brad Whitlock, Thu Feb 26 11:58:58 PDT 2004
//    I replaced the commented out cerr with debug3.
//
hrchilds's avatar
hrchilds committed
204 205 206 207
// ****************************************************************************
void
NetworkManager::ClearNetworksWithDatabase(const std::string &db)
{
hrchilds's avatar
hrchilds committed
208
    debug3 << "NetworkManager::ClearNetworksWithDatabase()" << endl;
hrchilds's avatar
hrchilds committed
209
    int i;
hrchilds's avatar
hrchilds committed
210

hrchilds's avatar
hrchilds committed
211 212 213 214
    // 
    // Clear out the networks before the databases.  This is because if we
    // delete the databases first, the networks will have dangling pointers.
    //
hrchilds's avatar
hrchilds committed
215
    for (i = 0; i < networkCache.size(); i++)
hrchilds's avatar
hrchilds committed
216 217 218 219 220 221 222 223 224 225
    {
        if (networkCache[i] != NULL)
        {
            NetnodeDB *ndb = networkCache[i]->GetNetDB();
            if (ndb != NULL)
            {
                if (ndb->GetFilename() == db)
                {
                    delete networkCache[i];
                    networkCache[i] = NULL;
hrchilds's avatar
hrchilds committed
226
                    globalCellCounts[i] = -1;
hrchilds's avatar
hrchilds committed
227 228 229 230
                }
            }
        }
    }
hrchilds's avatar
hrchilds committed
231

hrchilds's avatar
hrchilds committed
232 233 234 235 236 237 238 239 240 241 242
    for (i = 0; i < databaseCache.size(); i++)
    {
        if (databaseCache[i] != NULL)
        {
            if (databaseCache[i]->GetFilename() == db)
            {
                delete databaseCache[i];
                databaseCache[i] = NULL;
            }
        }
    }
hrchilds's avatar
hrchilds committed
243 244 245
}

// ****************************************************************************
hrchilds's avatar
hrchilds committed
246
//  Method: NetworkManager::GetDBFromCache
hrchilds's avatar
hrchilds committed
247 248
//
//  Purpose:
hrchilds's avatar
hrchilds committed
249 250 251 252 253 254 255 256 257 258 259 260 261
//      Get a DB from the cache.  If it's not there already, create it and
//      return it.
//
//  Programmer: Sean Ahern
//  Creation:   Mon Dec 23 12:28:30 PST 2002
//
//  Modifications:
//
//    Brad Whitlock, Wed May 14 12:56:16 PDT 2003
//    I made it get the metadata and the SIL if the time is greater than 0.
//    This ensures that we pick up the metadata and SIL for the time state
//    that we're intersted in.
//
hrchilds's avatar
hrchilds committed
262 263 264 265
//    Brad Whitlock, Mon Aug 25 16:51:35 PST 2003
//    Changed the return statement to CATCH_RETURN2 so bad things don't happen
//    when we use fake exceptions.
//
hrchilds's avatar
hrchilds committed
266 267 268
//    Mark C. Miller Thu Oct  9 10:59:27 PDT 2003
//    I modified to force GetMetaData and GetSIL if they're invariant 
//
hrchilds's avatar
hrchilds committed
269 270 271
//    Hank Childs, Tue Nov  4 14:19:05 PST 2003
//    Checked in work-around (HACK) to avoid apparent compiler bug on AIX.
//
hrchilds's avatar
hrchilds committed
272 273 274
//    Hank Childs, Mon Jan  5 16:49:28 PST 2004
//    Do not assume that all of the cached databases are non-NULL.
//
hrchilds's avatar
hrchilds committed
275 276 277
//    Hank Childs, Mon Mar  1 08:48:26 PST 2004
//    Send the time to the database factory as well.
//
hrchilds's avatar
hrchilds committed
278 279 280
//    Hank Childs, Mon Mar 22 11:10:43 PST 2004
//    Allow for the DB type to be explicitly specified.
//
hrchilds's avatar
hrchilds committed
281
// ****************************************************************************
hrchilds's avatar
hrchilds committed
282

hrchilds's avatar
hrchilds committed
283
NetnodeDB *
hrchilds's avatar
hrchilds committed
284 285
NetworkManager::GetDBFromCache(const string &filename, int time,
                               const char *format)
hrchilds's avatar
hrchilds committed
286
{
hrchilds's avatar
hrchilds committed
287
    //cerr << "NetworkManager::GetDBFromCache()" << endl;
hrchilds's avatar
hrchilds committed
288 289 290 291

    // If we don't have a load balancer, we're dead.
    if (loadBalancer == NULL)
    {
hrchilds's avatar
hrchilds committed
292 293
        debug1 << "Internal error: A load balancer was never registered." 
               << endl;
hrchilds's avatar
hrchilds committed
294 295 296
        EXCEPTION0(ImproperUseException);
    }

hrchilds's avatar
hrchilds committed
297 298
    // Find a matching database 
    NetnodeDB* cachedDB = NULL;
hrchilds's avatar
hrchilds committed
299 300
    for (int i = 0; i < databaseCache.size(); i++)
    {
hrchilds's avatar
hrchilds committed
301 302
        if (databaseCache[i] == NULL)
            continue;
hrchilds's avatar
hrchilds committed
303
        if (databaseCache[i]->GetFilename() == filename)
hrchilds's avatar
hrchilds committed
304
        {
hrchilds's avatar
hrchilds committed
305 306
            cachedDB = databaseCache[i];
            break;
hrchilds's avatar
hrchilds committed
307
        }
hrchilds's avatar
hrchilds committed
308 309 310 311 312 313 314 315 316 317
    }

    // got a match
    if (cachedDB != NULL)
    {

        // even if we found the DB in the cache,
        // we need to update the metadata if its time-varying
        if (!cachedDB->GetDB()->MetaDataIsInvariant() ||
            !cachedDB->GetDB()->SILIsInvariant())
hrchilds's avatar
hrchilds committed
318
        {
hrchilds's avatar
hrchilds committed
319 320
            cachedDB->GetDB()->GetMetaData(time);
            cachedDB->GetDB()->GetSIL(time);
hrchilds's avatar
hrchilds committed
321
        }
hrchilds's avatar
hrchilds committed
322 323 324

        cachedDB->SetDBInfo(filename, "", time);
        return cachedDB;
hrchilds's avatar
hrchilds committed
325 326 327 328 329 330 331 332 333 334
    }

    // No match.  Load a new DB.
    debug3 << "Loading new database" << endl;
    TRY
    {
        avtDatabase *db = NULL;
        NetnodeDB *netDB = NULL;
        const char *filename_c = filename.c_str();
        if (filename.substr(filename.length() - 6) == ".visit")
hrchilds's avatar
hrchilds committed
335
            db = avtDatabaseFactory::VisitFile(filename_c, time, format);
hrchilds's avatar
hrchilds committed
336
        else
hrchilds's avatar
hrchilds committed
337
            db = avtDatabaseFactory::FileList(&filename_c, 1, time, format);
hrchilds's avatar
hrchilds committed
338 339

        // If we want to open the file at a later timestep, get the
hrchilds's avatar
hrchilds committed
340 341 342 343
        // SIL so that it contains the right data.
        if ((time > 0) ||
            (!db->MetaDataIsInvariant()) ||
            (!db->SILIsInvariant()))
hrchilds's avatar
hrchilds committed
344 345 346
        {
            debug2 << "NetworkManager::AddDB: We were instructed to open "
                   << filename.c_str() << " at timestate=" << time
hrchilds's avatar
hrchilds committed
347
                   << " so we're reading the SIL early."
hrchilds's avatar
hrchilds committed
348 349 350 351 352 353 354 355 356
                   << endl;
            db->GetMetaData(time);
            db->GetSIL(time);
        }

        netDB = new NetnodeDB(db);
        databaseCache.push_back(netDB);

        netDB->SetDBInfo(filename, "", time);
hrchilds's avatar
hrchilds committed
357
        const   avtIOInformation & ioinfo = db->GetIOInformation(time);
hrchilds's avatar
hrchilds committed
358
        loadBalancer->AddDatabase(filename, ioinfo);
hrchilds's avatar
hrchilds committed
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
      
        // The code here should be:
        // CATCH_RETURN2(1, netDB);
        //
        // However: this is crashing on AIX when you do a re-open (see '3984).
        // After investigating this bug, I have determined the following:
        // (1) A valid pointer is being returned from this function.  However
        // the calling function receives NULL (leading to a seg fault).
        // (2) When compiling with fake exceptions and running under purify
        // on the Suns, there are no warnings.
        // (3) The catch return unwinds into 3 calls.  If you put a print
        // statement right before the last one (the return), everything is
        // returned correctly and there is no seg fault.
        //
        // I'm always very leery to call something a compiler bug.  However,
        // on different platforms there is no problem.  I think it is even
        // more fishy that things work if there is a print statement and don't
        // if there is not.  It certainly sounds like a compiler bug.
        //
        // Below is code that "makes this work".  It is essentially a
        // CATCH_RETURN2 with a print statement in the middle.  If CATCH_RETURN
        // were to ever change, this coding would have to change as well
        // (thus, I have introduced a maintenance problem).  As such, I put
        // a comment in VisItException.h referencing this comment.  If the
        // coding below can ever be replaced with an everyday CATCH_RETURN,
        // the comment in VisItException.h should be removed.
        //
#ifdef FAKE_EXCEPTIONS
           exception_delete(exception_caught); 
           jump_stack_top -= (1); 
           debug5 << "This debug statement (from the NetworkManager) is "
                  << "being issued to get around a compiler bug." << endl;
           return (netDB); 
#else
           return netDB;
#endif
hrchilds's avatar
hrchilds committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    }
    CATCH(DatabaseException)
    {
        debug1 << "ERROR - could not create database " << filename.c_str()
            << endl;
        RETHROW;
    }
    ENDTRY
}

// ****************************************************************************
//  Method: NetworkManager::StartNetwork
//
//  Purpose:
//      Start building the pipeline.
hrchilds's avatar
hrchilds committed
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
//
//  Programmer: Jeremy Meredith
//  Creation:   September 29, 2000
//
//  Modifications:
//
//    Jeremy Meredith, Thu Nov  2 14:07:34 PST 2000
//    Changed the file initialization.  Apparently substr() may not portably 
//    be a const method of strings....
//
//    Hank Childs, Tue Jun 12 20:01:55 PDT 2001
//    Incorporate data specifications and the load balancer.
//
//    Jeremy Meredith, Thu Jul 26 12:08:42 PDT 2001
//    Fixed a bug passing the data file name to the load balancer.
//
//    Hank Childs, Thu Jul 26 20:17:56 PDT 2001
//    Re-enable caching by checking data specifications.
//
//    Brad Whitlock, Mon Oct 22 18:33:37 PST 2001
//    Changed the exception keywords to macros.
//
//    Jeremy Meredith, Fri Nov  9 10:24:23 PST 2001
//    Replaced netPool with netMRU, and simply made it release its
//    data, not delete the whole network. Made is only set the data
//    specification, not the pipeline specification yet.
//
//    Hank Childs, Fri Dec 14 17:39:36 PST 2001
//    Add support for using a more compact version of sil attributes.
//
//    Sean Ahern, Mon May 13 17:24:25 PDT 2002
//    Refactored to start support for a tree-based network scheme.
//
//    Sean Ahern, Tue May 28 09:47:21 PDT 2002
//    Added tree-based networks.
//
//    Brad Whitlock, Fri Jun 28 14:03:49 PST 2002
//    I renamed Network to DataNetwork to make the Windows compiler happy.
//
//    Hank Childs, Thu Sep 12 11:11:16 PDT 2002
//    There was an issue with multiple networks sharing NetnodeDBs, so I
//    made them all have their own reference.
//
//    Jeremy Meredith, Thu Oct 24 16:03:39 PDT 2002
//    Added material options.
//
hrchilds's avatar
hrchilds committed
456 457 458 459 460 461
//    Sean Ahern, Mon Dec 23 12:38:17 PST 2002
//    Changed this dramatically.  Broke out a good portion of the logic
//    into GetDBFromCache.
//
//    Sean Ahern, Sat Mar  8 00:59:16 America/Los_Angeles 2003
//    Cleaned up unnecessarily complicated code.
hrchilds's avatar
hrchilds committed
462
//
hrchilds's avatar
hrchilds committed
463 464 465
//    Jeremy Meredith, Wed Jul 30 10:45:45 PDT 2003
//    Added the check for requiring full connectivity.
//
hrchilds's avatar
hrchilds committed
466 467 468
//    Jeremy Meredith, Fri Sep  5 15:23:13 PDT 2003
//    Added a flag for the new MIR algorithm.
//
hrchilds's avatar
hrchilds committed
469 470 471
//    Jeremy Meredith, Thu Oct 30 16:09:32 PST 2003
//    Added code to make sure varLeaves was non-empty before accessing it.
//
hrchilds's avatar
hrchilds committed
472 473 474
//    Jeremy Meredith, Fri Oct 31 13:05:26 PST 2003
//    Made the error message for no-real-variables more informative.
//
hrchilds's avatar
hrchilds committed
475 476 477 478
//    Hank Childs, Sun Mar  7 16:05:03 PST 2004
//    Do not make the database be a node for the network.  Also add the EEF
//    as a node.
//
hrchilds's avatar
hrchilds committed
479 480 481
//    Jeremy Meredith, Tue Mar 23 14:40:20 PST 2004
//    Added the file format as an argument.  Made use of it in GetDBFromCache.
//
hrchilds's avatar
hrchilds committed
482 483
// ****************************************************************************
void
hrchilds's avatar
hrchilds committed
484 485
NetworkManager::StartNetwork(const string &filename, const string &format,
                             const string &var,
hrchilds's avatar
hrchilds committed
486 487 488
                             int time,
                             const CompactSILRestrictionAttributes &atts,
                             const MaterialAttributes &matopts)
hrchilds's avatar
hrchilds committed
489
{
hrchilds's avatar
hrchilds committed
490
    //cerr << "NetworkManager::StartNetwork()" << endl;
hrchilds's avatar
hrchilds committed
491

hrchilds's avatar
hrchilds committed
492
    // Check to make sure that there is no existing network.
hrchilds's avatar
hrchilds committed
493 494
    // MCM_FIX: in automatic SR mode, this logic won't work.
#if 0
hrchilds's avatar
hrchilds committed
495
    if (workingNet != NULL)
hrchilds's avatar
hrchilds committed
496
    {
hrchilds's avatar
hrchilds committed
497
        char error[] = "Trying to create pipeline twice.";
hrchilds's avatar
hrchilds committed
498 499 500
        debug1 << error << endl;
        EXCEPTION1(ImproperUseException,error);
    }
hrchilds's avatar
hrchilds committed
501
#endif
hrchilds's avatar
hrchilds committed
502

hrchilds's avatar
hrchilds committed
503 504 505 506 507
    // If the variable is an expression, we need to find a "real" variable
    // name to work with.
    std::string leaf = var;
    ExprNode *tree = ParsingExprList::GetExpressionTree(leaf);
    while (tree != NULL)
hrchilds's avatar
hrchilds committed
508
    {
hrchilds's avatar
hrchilds committed
509 510 511
        const set<string> &varLeaves = tree->GetVarLeaves();
        if (varLeaves.empty())
        {
hrchilds's avatar
hrchilds committed
512 513
            EXCEPTION1(ImproperUseException,
                       "After parsing, expression has no real variables.");
hrchilds's avatar
hrchilds committed
514 515
        }
        leaf = *varLeaves.begin();
hrchilds's avatar
hrchilds committed
516 517
        tree = ParsingExprList::GetExpressionTree(leaf);
    }
hrchilds's avatar
hrchilds committed
518

hrchilds's avatar
hrchilds committed
519 520
    // Start up the DataNetwork and add the database to it.
    workingNet = new DataNetwork;
hrchilds's avatar
hrchilds committed
521
    NetnodeDB *netDB = GetDBFromCache(filename, time, format.c_str());
hrchilds's avatar
hrchilds committed
522 523
    workingNet->SetNetDB(netDB);
    netDB->SetDBInfo(filename, leaf, time);
hrchilds's avatar
hrchilds committed
524

hrchilds's avatar
hrchilds committed
525 526 527 528 529
    // Put an ExpressionEvaluatorFilter right after the netDB to handle
    // expressions that come up the pipe.
    avtExpressionEvaluatorFilter *f = new avtExpressionEvaluatorFilter();
    NetnodeFilter *filt = new NetnodeFilter(f, "ExpressionEvaluator");
    filt->GetInputNodes().push_back(netDB);
hrchilds's avatar
hrchilds committed
530

hrchilds's avatar
hrchilds committed
531 532
    // Push the ExpressionEvaluator onto the working list.
    workingNetnodeList.push_back(filt);
hrchilds's avatar
hrchilds committed
533

hrchilds's avatar
hrchilds committed
534
    workingNet->AddNode(filt);
hrchilds's avatar
hrchilds committed
535
    // Push the variable name onto the name stack.
hrchilds's avatar
hrchilds committed
536 537 538 539 540
    nameStack.push_back(var);
    debug5 << "NetworkManager::AddDB: Adding " << var.c_str()
           << " to the name stack" << endl;

    // Set up the data spec.
hrchilds's avatar
hrchilds committed
541
    avtSILRestriction_p silr =
hrchilds's avatar
hrchilds committed
542
        new avtSILRestriction(workingNet->GetNetDB()->GetDB()->GetSIL(time), atts);
hrchilds's avatar
hrchilds committed
543 544 545 546 547 548 549
    avtDataSpecification *dspec = new avtDataSpecification(var.c_str(), time, silr);

    // Set up some options from the data specification
    dspec->SetNeedMixedVariableReconstruction(matopts.GetForceMIR());
    dspec->SetNeedSmoothMaterialInterfaces(matopts.GetSmoothing());
    dspec->SetNeedCleanZonesOnly(matopts.GetCleanZonesOnly());
    dspec->SetNeedValidFaceConnectivity(matopts.GetNeedValidConnectivity());
hrchilds's avatar
hrchilds committed
550
    dspec->SetUseNewMIRAlgorithm(matopts.GetUseNewMIRAlgorithm());
hrchilds's avatar
hrchilds committed
551
    workingNet->SetDataSpec(dspec);
hrchilds's avatar
hrchilds committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

    // The plot starts out as NULL.
    workingNet->SetPlot(NULL);
}

// ****************************************************************************
// Method: NetworkManager::DefineDB
//
// Purpose: 
//   Defines a new database with the specified name and list of files.
//
// Arguments:
//    dbName   : The database name.
//    files    : The list of files that make up the database.
//    time     : The timestep that we want to examine.
hrchilds's avatar
hrchilds committed
567
//    format   : The file format type of the DB.
hrchilds's avatar
hrchilds committed
568 569 570 571 572 573 574 575 576 577
//   
// Programmer: Brad Whitlock
// Creation:   Tue Mar 25 13:41:08 PST 2003
//
// Modifications:
//   Brad Whitlock, Wed May 14 12:56:16 PDT 2003
//   I made it get the metadata and the SIL if the time is greater than 0.
//   This ensures that we pick up the metadata and SIL for the time state
//   that we're intersted in.
//
hrchilds's avatar
hrchilds committed
578 579 580
//   Hank Childs, Mon Jan  5 16:49:28 PST 2004
//   Account for databases in the cached lists that have been cleared.
//
hrchilds's avatar
hrchilds committed
581 582 583
//   Hank Childs, Mon Mar  1 08:48:26 PST 2004
//   Send the time to the database factory as well.
//
hrchilds's avatar
hrchilds committed
584 585
//   Hank Childs, Mon Mar 22 11:10:43 PST 2004
//   Send the file format type to the database factory.
hrchilds's avatar
hrchilds committed
586 587 588
// ****************************************************************************
void
NetworkManager::DefineDB(const string &dbName, const string &dbPath,
hrchilds's avatar
hrchilds committed
589
    const stringVector &files, int time, const string &format)
hrchilds's avatar
hrchilds committed
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
{
    //
    // We gotta have a load balancer
    //
    if (loadBalancer == NULL)
    {
        debug1 << "Internal error: A load balancer was never registered."
               << endl;
        EXCEPTION0(ImproperUseException);
    }

    // Prepend the path to the files.
    stringVector filesWithPath;
    filesWithPath.reserve(files.size());
    int i;
    for(i = 0; i < files.size(); ++i)
        filesWithPath.push_back(dbPath + files[i]);

    //
    // If the database is in the virtualDatabases map, then check the
    // new list of files against the old list of files.
    //
    StringVectorMap::const_iterator it = virtualDatabases.find(dbName);
    if(it != virtualDatabases.end())
    {
        // If the list of files is the same, return because we don't
        // need to change the list of files. If the list of files differ
        // then replace the list of files.
        if(filesWithPath == it->second)
        {
            debug3 << "NetworkManager::DefineDB: No need to change "
                   << "virtual database definition for "
                   << dbName.c_str() << endl;

            // The definition is the same so we don't need to change it
            // but we have to make sure that it exists in the database
            // cache or else we'll have to define it. It's possible that
            // a clear cache rpc could have purged it from the database
            // cache.
            bool found = false;
            for(i = 0; i < databaseCache.size() && !found; ++i)
hrchilds's avatar
hrchilds committed
631 632 633
            {
                if (databaseCache[i] == NULL)
                    continue;
hrchilds's avatar
hrchilds committed
634
                found = (databaseCache[i]->GetFilename() == dbName);
hrchilds's avatar
hrchilds committed
635
            }
hrchilds's avatar
hrchilds committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655

            // If the database was in the database cache, we can return.
            if(found)
                return;
        }
        else
        {
            debug3 << "NetworkManager::DefineDB: Changed "
                   << "virtual database definition for "
                   << dbName.c_str() << endl;
            virtualDatabases[dbName] = filesWithPath;
        }
    }

    //
    // Remove from the database cache databases that have the same name.
    //
    std::vector<NetnodeDB*> databaseBadMatches;
    for (i = 0; i < databaseCache.size(); i++)
    {
hrchilds's avatar
hrchilds committed
656 657
        if (databaseCache[i] == NULL)
            continue;
hrchilds's avatar
hrchilds committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
        if (databaseCache[i]->GetFilename() != dbName)
            databaseBadMatches.push_back(databaseCache[i]);
    }
    databaseCache = databaseBadMatches;

    const char **names = 0;
    TRY
    {
        // Open the database
        avtDatabase *db = NULL;
        const char *dbName_c = dbName.c_str();

        if (filesWithPath.size() > 0)
        {
            // Make an array of pointers that we can pass to the database
            // factory so we can create a database based on a virtual
            // file.
            names = new const char *[filesWithPath.size()];
            for(int i = 0; i < filesWithPath.size(); ++i)
                names[i] = filesWithPath[i].c_str();
hrchilds's avatar
hrchilds committed
678
            db = avtDatabaseFactory::FileList(names, filesWithPath.size(),
hrchilds's avatar
hrchilds committed
679
                                              time, format.c_str());
hrchilds's avatar
hrchilds committed
680 681 682 683 684 685 686 687 688
            delete [] names;
            names = 0;

            // Add the virtual database to the virtual database map.
            virtualDatabases[dbName] = filesWithPath;
            debug3 << "NetworkManager::DefineDB: Added new virtual database "
                   << "definition for " << dbName.c_str() << endl;
        }
        else if (dbName.substr(dbName.length() - 6) == ".visit")
hrchilds's avatar
hrchilds committed
689
            db = avtDatabaseFactory::VisitFile(dbName_c, time, format.c_str());
hrchilds's avatar
hrchilds committed
690
        else
hrchilds's avatar
hrchilds committed
691 692
            db = avtDatabaseFactory::FileList(&dbName_c, 1, time,
                                              format.c_str());
hrchilds's avatar
hrchilds committed
693 694 695

        // If we want to open the file at a later timestep, get the
        // metadata and the SIL so that it contains the right data.
hrchilds's avatar
hrchilds committed
696 697 698
        if ((time > 0) ||
            (!db->MetaDataIsInvariant()) || 
            (!db->SILIsInvariant()))
hrchilds's avatar
hrchilds committed
699 700 701 702 703 704 705 706 707 708 709 710
        {
            debug2 << "NetworkManager::DefineDB: We were instructed to define "
                       << dbName.c_str() << " at timestate=" << time
                       << " so we're reading the MetaData and SIL early."
                       << endl;
            db->GetMetaData(time);
            db->GetSIL(time);
        }

        // Create a netnode using the opened database and stick it in
        // the database cache.
        NetnodeDB *netDB = new NetnodeDB(db);
hrchilds's avatar
hrchilds committed
711
        netDB->SetDBInfo(dbName, "", time);
hrchilds's avatar
hrchilds committed
712 713 714
        databaseCache.push_back(netDB);

        // Add the database to the load balancer.
hrchilds's avatar
hrchilds committed
715
        const   avtIOInformation & ioinfo = db->GetIOInformation(time);
hrchilds's avatar
hrchilds committed
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
        loadBalancer->AddDatabase(dbName, ioinfo);
    }
    CATCH(DatabaseException)
    {
        delete [] names;
        debug1 << "ERROR - could not define database " << dbName.c_str()
               << endl;
        RETHROW;
    }
    ENDTRY 
}

// ****************************************************************************
//  Method: NetworkManager::AddFilter
//
//  Purpose:
//      Create a new filter and add it to the network
//
//  Programmer: Jeremy Meredith
//  Creation:   February 28, 2001
//
//  Modifications:
//    Jeremy Meredith, Sun Mar  4 16:58:13 PST 2001
//    Made it use (a new) PluginManager to create the operator.
//
//    Hank Childs, Thu Apr 12 15:34:22 PDT 2001
//    Added check to make sure that there were enough filters to compare to.
//
//    Jeremy Meredith, Thu Jul 26 03:39:21 PDT 2001
//    Added support for the new (real) operator plugin manager.
//
//    Jeremy Meredith, Fri Sep 28 13:32:56 PDT 2001
//    Made use of plugin manager keying off of ids instead of indexes.
//
//    Jeremy Meredith, Fri Nov  9 10:25:46 PST 2001
//    Added an error check, and renamed type to filtertype.
//
//    Sean Ahern, Tue May 28 08:52:48 PDT 2002
//    Added binary operators.
//
// ****************************************************************************
void
NetworkManager::AddFilter(const string &filtertype,
                          const AttributeGroup *atts,
                          const unsigned int nInputs)
{
hrchilds's avatar
hrchilds committed
762
    //cerr << "NetworkManager::AddFilter()" << endl;
hrchilds's avatar
hrchilds committed
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783
    // Check that we have a network to work on.
    if (workingNet == NULL)
    {
        debug1 << "Adding a filter to a non-existent network." << endl;
        EXCEPTION0(ImproperUseException);
    }

    avtPluginFilter *f = OperatorPluginManager::Instance()->
        GetEnginePluginInfo(filtertype)->AllocAvtPluginFilter();
    f->SetAtts(atts);
    if (nInputs != 1)
    {
        debug1 << "Unable to handle filters with nInputs (" << nInputs << ") > 1"
            << endl;
        EXCEPTION0(ImproperUseException);
    }

    NetnodeFilter *filt = new NetnodeFilter(f, filtertype);
    std::vector<Netnode*> &filtInputs = filt->GetInputNodes();
    for (int i = 0; i < nInputs; i++)
    {
hrchilds's avatar
hrchilds committed
784
        // Pull a node off the working list and push it onto the filter
hrchilds's avatar
hrchilds committed
785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
        // inputs.
        Netnode *n = workingNetnodeList.back();
        workingNetnodeList.pop_back();
        filtInputs.push_back(n);
    }
    // Push the filter onto the working list.
    workingNetnodeList.push_back(filt);
    workingNet->AddNode(filt);
}

// ****************************************************************************
//  Method:  NetworkManager::MakePlot
//
//  Purpose:
//    Create the plot for the network.
//
//  Programmer:  Jeremy Meredith
//  Creation:    March  1, 2001
//
//  Modifications:
//    Jeremy Meredith, Sun Mar  4 16:58:13 PST 2001
//    Made it use (a new) PluginManager to create the plot.
//
//    Jeremy Meredith, Tue Mar 20 12:05:50 PST 2001
//    Made it use the new plugin manager.  For now we do a reverse lookup
//    of the name to an index.
//
//    Jeremy Meredith, Fri Sep 28 13:32:56 PDT 2001
//    Made use of plugin manager keying off of ids instead of indexes.
//
//    Jeremy Meredith, Fri Nov  9 10:26:18 PST 2001
//    Allowed it to reuse plots so we can cache whole networks again.
//
//    Jeremy Meredith, Fri Feb  8 10:37:59 PST 2002
//    Made sure the total number of filters was the same when matching
//    networks (since when a plot is applied, no more filter can be).
//
// ****************************************************************************
void
NetworkManager::MakePlot(const string &id, const AttributeGroup *atts)
{
hrchilds's avatar
hrchilds committed
826
    //cerr << "NetworkManager::MakePlot()" << endl;
hrchilds's avatar
hrchilds committed
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
    if (workingNet == NULL)
    {
        debug1 << "Adding a plot to a non-existent network." << endl;
        EXCEPTION0(ImproperUseException);
    }

    if (*workingNet->GetPlot() != NULL)
    {
        debug1 << "Trying to stipulate plot type more than once." << endl;
        EXCEPTION0(ImproperUseException);
    }

    // Checking to see if the network has been built successfully.
    if (workingNetnodeList.size() != 1)
    {
hrchilds's avatar
hrchilds committed
842 843
        debug1 << "Network building still in progress.  Filter required to "
               << "absorb" << workingNetnodeList.size() << " nodes."  << endl;
hrchilds's avatar
hrchilds committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
        EXCEPTION0(ImproperUseException);
    }

    avtPlot *p = PlotPluginManager::Instance()->
                                GetEnginePluginInfo(id)->AllocAvtPlot();
    workingNet->SetPlot(p);
    workingNet->GetPlot()->SetAtts(atts);
    workingNet->SetPlottype(id);
}

// ****************************************************************************
//  Method:  NetworkManager::EndNetwork
//
//  Purpose:
//    Closes off the new network, assigns a pipeline specification, and a
//    network id.
//
//  Programmer:  Jeremy Meredith
//  Creation:    November  9, 2001
//
hrchilds's avatar
hrchilds committed
864 865 866 867 868
//  Modifications:
//
//    Hank Childs, Mon Jan  5 16:04:57 PST 2004
//    Make sure that the network id is always unique for each network.
//
hrchilds's avatar
hrchilds committed
869 870 871 872
// ****************************************************************************
int
NetworkManager::EndNetwork(void)
{
hrchilds's avatar
hrchilds committed
873
    //cerr << "NetworkManager::EndNetwork(void)" << endl;
hrchilds's avatar
hrchilds committed
874 875 876 877 878 879 880 881 882 883 884
    // Checking to see if the network has been built successfully.
    if (workingNetnodeList.size() != 1)
    {
        debug1 << "Network building still in progress.  Filter required to "
               << "absorb" << workingNetnodeList.size() << " nodes."  << endl;

        EXCEPTION0(ImproperUseException);
    }

    // set the pipeline specification
    int pipelineIndex =
hrchilds's avatar
hrchilds committed
885
        loadBalancer->AddPipeline(workingNet->GetNetDB()->GetFilename());
hrchilds's avatar
hrchilds committed
886 887 888 889 890 891 892 893
    avtPipelineSpecification_p pspec =
        new avtPipelineSpecification(workingNet->GetDataSpec(), pipelineIndex);
    workingNet->SetPipelineSpec(pspec);

    // Connect the workingNet to the workingNetnodeList.
    workingNet->SetTerminalNode(workingNetnodeList[0]);

    // Push the working net onto the network caches.
hrchilds's avatar
hrchilds committed
894
    workingNet->SetID(uniqueNetworkId++);
hrchilds's avatar
hrchilds committed
895
    networkCache.push_back(workingNet);
hrchilds's avatar
hrchilds committed
896
    globalCellCounts.push_back(-1);
hrchilds's avatar
hrchilds committed
897 898 899 900

    return workingNet->GetID();
}

hrchilds's avatar
hrchilds committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
// ****************************************************************************
//  Method:  NetworkManager::CancelNetwork
//
//  Purpose:
//    Terminates the current network in case of an error.
//
//  Programmer:  Jeremy Meredith
//  Creation:    November  6, 2003
//
// ****************************************************************************
void
NetworkManager::CancelNetwork(void)
{
    workingNet = NULL;
    workingNetnodeList.clear();
}

hrchilds's avatar
hrchilds committed
918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
// ****************************************************************************
//  Method:  NetworkManager::UseNetwork
//
//  Purpose:
//    Allows a whole network to be reused automatically, without
//    needing to specify a database, filters, or plot.
//
//  Arguments:
//    id         the id of the network to reuse.
//
//  Programmer:  Jeremy Meredith
//  Creation:    November  9, 2001
//
//  Modifications:
//
//    Hank Childs, Wed Nov 21 11:41:59 PST 2001
//    Tell the load balancer to reset the pipeline if we re-use the network.
//
hrchilds's avatar
hrchilds committed
936 937 938
//    Hank Childs, Mon Jan  5 16:39:06 PST 2004
//    Make sure that the id requested hasn't already been cleared.
//
hrchilds's avatar
hrchilds committed
939 940 941 942
// ****************************************************************************
void
NetworkManager::UseNetwork(int id)
{
hrchilds's avatar
hrchilds committed
943
    //cerr << "NetworkManager::UseNetwork()" << endl;
hrchilds's avatar
hrchilds committed
944 945
    if (workingNet)
    {
hrchilds's avatar
hrchilds committed
946 947
        debug1 << "Internal error: UseNetwork called with an open network" 
               << endl;
hrchilds's avatar
hrchilds committed
948 949 950 951 952 953 954 955 956 957 958 959
        EXCEPTION0(ImproperUseException);
    }

    if (id >= networkCache.size())
    {
        debug1 << "Internal error: asked to reuse network ID (" << id
            << " >= num saved networks (" << networkCache.size() << ")"
            << endl;
        EXCEPTION0(ImproperUseException);
    }

    // re-access the given network
hrchilds's avatar
hrchilds committed
960 961 962 963 964 965 966
    if (networkCache[id] == NULL)
    {
        debug1 << "Asked to use a network that was cleared out previously."
                << "  (presumably because a database was re-opened)." << endl;
        EXCEPTION0(ImproperUseException);
    }
 
hrchilds's avatar
hrchilds committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
    workingNet = networkCache[id];
    int pipelineIndex = workingNet->GetPipelineSpec()->GetPipelineIndex();
    loadBalancer->ResetPipeline(pipelineIndex);

    if (id != workingNet->GetID())
    {
        debug1 << "Internal error: network at position[" << id <<
            "] does not have same id (" << workingNet->GetID() << ")"
            << endl;
        EXCEPTION0(ImproperUseException);
    }
}

// ****************************************************************************
//  Method: NetworkManager::GetPlot
//
//  Purpose:
//      Gets the plot for the current working network 
//
//  Programmer: Mark C. Miller 
//  Creation:   November 19, 2002
//
// ****************************************************************************
avtPlot_p
NetworkManager::GetPlot(void)
{
hrchilds's avatar
hrchilds committed
993
    //cerr << "NetworkManager::GetPlot(void)" << endl;
hrchilds's avatar
hrchilds committed
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
    if (workingNet == NULL)
    {
        EXCEPTION0(ImproperUseException);
    }

    return workingNet->GetPlot();
}

// ****************************************************************************
//  Method: NetworkManager::GetCurrentNetworkId
//
//  Purpose:
//      Gets the network id of the current network.
//
//  Programmer: Hank Childs
//  Creation:   September 9, 2002
//
// ****************************************************************************
int
NetworkManager::GetCurrentNetworkId(void)
{
hrchilds's avatar
hrchilds committed
1015
    //cerr << "NetworkManager::GetCurrentNetworkId(void)" << endl;
hrchilds's avatar
hrchilds committed
1016 1017 1018 1019 1020 1021 1022 1023
    if (workingNet == NULL)
    {
        EXCEPTION0(ImproperUseException);
    }

    return workingNet->GetID();
}

hrchilds's avatar
hrchilds committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
// ****************************************************************************
//  Method: NetworkManager::GetTotalGlobalCellCounts
//
//  Purpose:
//      Gets the network id of the current network.
//
//  Programmer: Hank Childs
//  Creation:   September 9, 2002
//
// ****************************************************************************
int
NetworkManager::GetTotalGlobalCellCounts(void) const
{
   int i, sum = 0;
   for (i = 0; i < globalCellCounts.size(); i++)
hrchilds's avatar
hrchilds committed
1039 1040 1041 1042
   {
      if (globalCellCounts[i] >= 0)
          sum += globalCellCounts[i];
   }
hrchilds's avatar
hrchilds committed
1043 1044 1045 1046
   return sum;
}


hrchilds's avatar
hrchilds committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
// ****************************************************************************
//  Method: NetworkManager::DoneWithNetwork
//
//  Purpose:
//      Indicates that we are done with a network -- we can clean up memory, 
//      etc.
//
//  Programmer: Hank Childs
//  Creation:   September 9, 2002
//
hrchilds's avatar
hrchilds committed
1057 1058 1059 1060 1061
//  Modifications:
//
//    Hank Childs, Mon Jan  5 16:39:06 PST 2004
//    Make sure that this isn't called on a previously cleared network.
//
hrchilds's avatar
hrchilds committed
1062 1063 1064 1065
//    Hank Childs, Thu Mar 18 16:06:56 PST 2004
//    If this plot is currently located in the vis window (for SR-mode), then
//    clear it out of the window before cleaning up the network.
//
hrchilds's avatar
hrchilds committed
1066 1067 1068 1069
// ****************************************************************************
void
NetworkManager::DoneWithNetwork(int id)
{
hrchilds's avatar
hrchilds committed
1070
    //cerr << "NetworkManager::DoneWithNetwork()" << endl;
hrchilds's avatar
hrchilds committed
1071 1072 1073 1074 1075 1076 1077 1078
    if (id >= networkCache.size())
    {
        debug1 << "Internal error: asked to reuse network ID (" << id
            << " >= num saved networks (" << networkCache.size() << ")"
            << endl;
        EXCEPTION0(ImproperUseException);
    }

hrchilds's avatar
hrchilds committed
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
    for (int i = 0 ; i < plotsCurrentlyInWindow.size() ; i++)
    {
        if (plotsCurrentlyInWindow[i] == id)
        {
            viswin->ClearPlots();
            plotsCurrentlyInWindow.clear();
            break;
        }
    }

hrchilds's avatar
hrchilds committed
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
    if (networkCache[id] != NULL)
    {
        networkCache[id]->ReleaseData();
        globalCellCounts[id] = -1;
    }
    else
    {
        debug1 << "Warning: DoneWithNetwork called on previously cleared "
               << "network." << endl;
    }
hrchilds's avatar
hrchilds committed
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
}

// ****************************************************************************
//  Method: NetworkManager::UpdatePlotAtts
//
//  Purpose:
//      Updates the plot attributes for a network.
//
//  Arguments:
//      id      The id of the network.
//      atts    The new attributes for the plot.
//
//  Programmer: Hank Childs
//  Creation:   November 30, 2001
//
hrchilds's avatar
hrchilds committed
1114 1115 1116 1117 1118
//  Modifications:
//
//    Hank Childs, Mon Jan  5 16:39:06 PST 2004
//    Make sure the network hasn't already been cleared.
//
hrchilds's avatar
hrchilds committed
1119 1120 1121 1122
// ****************************************************************************
void
NetworkManager::UpdatePlotAtts(int id, const AttributeGroup *atts)
{
hrchilds's avatar
hrchilds committed
1123
    //cerr << "NetworkManager::UpdatePlotAtts()" << endl;
hrchilds's avatar
hrchilds committed
1124 1125 1126 1127 1128 1129 1130 1131
    if (id >= networkCache.size())
    {
        debug1 << "Internal error: asked to reuse network ID (" << id
            << ") >= num saved networks (" << networkCache.size() << ")"
            << endl;
        EXCEPTION0(ImproperUseException);
    }

hrchilds's avatar
hrchilds committed
1132 1133 1134 1135 1136 1137
    if (networkCache[id] == NULL)
    {
        debug1 << "Asked to update the plot attributes of a network that has "
               << "already been cleared." << endl;
        EXCEPTION0(ImproperUseException);
    }
hrchilds's avatar
hrchilds committed
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
    if (networkCache[id]->GetID() != id)
    {
        debug1 << "Internal error: network at position[" << id
            << "] does not have " << "the same id ("
            << networkCache[id]->GetID() << ")" << endl;
        EXCEPTION0(ImproperUseException);
    }

    networkCache[id]->GetPlot()->SetAtts(atts);
}

// ****************************************************************************
//  Method: NetworkManager::GetOutput
//
//  Purpose:
//      Get the output of the network and close the network.
//      Also, flush LRU items from the cache
//
//  Programmer: Jeremy Meredith
//  Creation:   September 29, 2000
//
//  Modifications:
//    Jeremy Meredith, Thu Nov  2 14:10:51 PST 2000
//    Added another sanity check.
//
//    Hank Childs, Wed Jan 17 11:05:20 PST 2001
//    Added plots.
//
//    Jeremy Meredith, Thu Mar  1 13:41:13 PST 2001
//    Disable the section of code which allows re-using of entire networks.
//    This allows us to continue caching networks, but avoids the temporary
//    problem where different networks' plots might use different values
//    but we have not checked to see if the plots are equivalent.
//    Also....  I changed the way the networks are hooked up to allow for
//    zero-length pipelines.
//
//    Hank Childs, Wed Mar  7 11:33:51 PST 2001
//    Timestep now an argument to the database instead of set as a global.
//
//    Kathleen Bonnell, Tue May  1 16:57:02 PDT 2001 
//    Added try-catch block so that exceptions could be rethrown. 
//
//    Hank Childs, Tue Jun 12 14:55:38 PDT 2001
//    Incorporate data specifications.
//
//    Jeremy Meredith, Tue Jul  3 15:18:55 PDT 2001
//    Modified to better handle interruption of pipeline execution.
//
//    Brad Whitlock, Mon Oct 22 18:33:37 PST 2001
//    Changed the exception keywords to macros.
//
//    Jeremy Meredith, Fri Nov  9 10:29:17 PST 2001
//    Moved code to finish network matching into EndNetwork.
//    Added sending of window attributes into plot->Execute.
//
//    Kathleen Bonnell, Tue Nov 20 12:35:54 PST 2001
//    Set flag in dataSpec based on value of requireOriginalCells. 
//
//    Hank Childs, Tue Jun 18 16:49:01 PDT 2002
//    Set MayRequireZones before Executing.
//
//    Mark C. Miller, 10Nov02
//    Made changes to support scalable rendering
//
//    Brad Whitlock, Thu Jan 16 11:07:10 PDT 2003
//    I replaced the return in the TRY/CATCH block with CATCH_RETURN2 so that
//    fake exceptions work again.
//
// ****************************************************************************
avtDataObjectWriter_p
hrchilds's avatar
hrchilds committed
1208
NetworkManager::GetOutput(bool respondWithNullData, bool calledForRender)
hrchilds's avatar
hrchilds committed
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
{

    // Is the network complete?
    if (*(workingNet->GetPlot()) == NULL)
    {
        debug1 << "Never told what type of plot to use." << endl;
        EXCEPTION0(ImproperUseException);
    }

    TRY
    {
hrchilds's avatar
hrchilds committed
1220 1221
        bool clearNetwork = true;

hrchilds's avatar
hrchilds committed
1222 1223
        // Hook up the network
        avtDataObject_p output = workingNet->GetOutput();
hrchilds's avatar
hrchilds committed
1224 1225
        int netId = GetCurrentNetworkId();

hrchilds's avatar
hrchilds committed
1226 1227 1228 1229 1230 1231 1232 1233

        workingNet->GetPipelineSpec()->GetDataSpecification()->
            SetMayRequireZones(requireOriginalCells); 

        avtDataObjectWriter_p writer = workingNet->GetWriter(output,
                                          workingNet->GetPipelineSpec(),
                                          &windowAttributes);

hrchilds's avatar
hrchilds committed
1234 1235
        int  scalableThreshold = windowAttributes.GetRenderAtts().GetScalableThreshold();

hrchilds's avatar
hrchilds committed
1236 1237 1238
        // compute this network's cell count if we haven't already 
        if (globalCellCounts[netId] == -1)
        {
hrchilds's avatar
hrchilds committed
1239
           bool polysOnly = true;
hrchilds's avatar
hrchilds committed
1240 1241
           if (scalableThreshold == 0)
               polysOnly = false;
hrchilds's avatar
hrchilds committed
1242
           int localCellCount = writer->GetInput()->GetNumberOfCells(polysOnly);
hrchilds's avatar
hrchilds committed
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
           int totalCellCount;
#ifdef PARALLEL
           MPI_Allreduce(&localCellCount, &totalCellCount, 1, MPI_INT, MPI_SUM,
              MPI_COMM_WORLD);
#else
           totalCellCount = localCellCount; 
#endif
           globalCellCounts[netId] = totalCellCount;
        }

hrchilds's avatar
hrchilds committed
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
        if (respondWithNullData)
        {
           // get the data object at the input to the writer
           avtDataObject_p writerInput = writer->GetInput();
           avtDataset_p origDataset;
           CopyTo(origDataset, writerInput);

           // make a copy of the dataset without the data
           avtDataset_p dummyDataset = new avtDataset(origDataset, true); 
           avtDataObject_p dummyDob;
           CopyTo(dummyDob, dummyDataset);
           writer = dummyDob->InstantiateWriter();
           writer->SetInput(dummyDob);
        }
hrchilds's avatar
hrchilds committed
1267 1268
        else if (!calledForRender)
        {
hrchilds's avatar
hrchilds committed
1269 1270

#ifdef PARALLEL
hrchilds's avatar
hrchilds committed
1271

hrchilds's avatar
hrchilds committed
1272
           if ((PAR_Size() > 1) && (GetTotalGlobalCellCounts() > scalableThreshold))
hrchilds's avatar
hrchilds committed
1273
           {
hrchilds's avatar
hrchilds committed
1274 1275 1276 1277 1278 1279 1280 1281 1282
               debug5 << "Cell count has exceeded SR threshold. Sending the "
                         "AVT_NULL_DATASET_MSG data object message" << endl;

               avtNullData_p nullData = new avtNullData(NULL,AVT_NULL_DATASET_MSG);
               avtDataObject_p dummyDob;
               CopyTo(dummyDob, nullData);
               writer = dummyDob->InstantiateWriter();
               writer->SetInput(dummyDob);
               clearNetwork = false;
hrchilds's avatar
hrchilds committed
1283
           }
hrchilds's avatar
hrchilds committed
1284
#endif
hrchilds's avatar
hrchilds committed
1285

hrchilds's avatar
hrchilds committed
1286
        }
hrchilds's avatar
hrchilds committed
1287 1288 1289

        // Zero out the workingNet.  Remember that we've already pushed it
        // onto the cache in EndNetwork.
hrchilds's avatar
hrchilds committed
1290 1291 1292 1293 1294
        if (clearNetwork)
        {
           workingNet = NULL;
           workingNetnodeList.clear();
        }
hrchilds's avatar
hrchilds committed
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319

        // return it
        CATCH_RETURN2(1, writer);
    }
    CATCHALL(...)
    {
        // Zero out the workingNet to ensure that the exception doesn't
        // cause a crash next time we try to build a network.  Remember
        // that we've already pushed it onto the cache in EndNetwork.
        workingNet = NULL;
        workingNetnodeList.clear();

        // rethrow
        RETHROW;
    }
    ENDTRY
}

// ****************************************************************************
//  Method:  NetworkManager::Render
//
//  Purpose: do a software scalable render
//
//  Programmer:  Mark C. Miller 
//  Creation:    08Apr03 
hrchilds's avatar
hrchilds committed
1320 1321 1322 1323 1324 1325 1326 1327
//
//  Modifications:
//
//    Hank Childs, Thu Mar 18 16:06:56 PST 2004
//    Do some bookkeeping for which plots are stored in the vis window already.
//    This allows us to not have to remove the plots and re-add them for each
//    render.
//
hrchilds's avatar
hrchilds committed
1328 1329 1330
//    Mark C. Miller, Tue Mar 30 10:58:01 PST 2004
//    Added code to set image compositor's background color
//
hrchilds's avatar
hrchilds committed
1331 1332
// ****************************************************************************
avtDataObjectWriter_p
hrchilds's avatar
hrchilds committed
1333
NetworkManager::Render(intVector plotIds, bool getZBuffer, bool do3DAnnotsOnly)
hrchilds's avatar
hrchilds committed
1334 1335 1336 1337 1338
{
    DataNetwork *origWorkingNet = workingNet;

    TRY
    {
hrchilds's avatar
hrchilds committed
1339
       int t1 = visitTimer->StartTimer();
hrchilds's avatar
hrchilds committed
1340
       avtDataObjectWriter_p writer;
hrchilds's avatar
hrchilds committed
1341
       int  scalableThreshold = windowAttributes.GetRenderAtts().GetScalableThreshold();
hrchilds's avatar
hrchilds committed
1342

hrchilds's avatar
hrchilds committed
1343 1344 1345
       // scalable threshold test (the 0.5 is to add some hysteresus to avoid 
       // the misfortune of oscillating switching of modes around the threshold)
       if (GetTotalGlobalCellCounts() < 0.5 * scalableThreshold)
hrchilds's avatar
hrchilds committed
1346
       {
hrchilds's avatar
hrchilds committed
1347 1348
           debug5 << "Cell count has fallen below SR threshold. Sending the "
                     "AVT_NULL_IMAGE_MSG data object to viewer" << endl;
hrchilds's avatar
hrchilds committed
1349 1350 1351 1352 1353 1354 1355

          avtNullData_p nullData = new avtNullData(NULL,AVT_NULL_IMAGE_MSG);
          avtDataObject_p dummyDob;
          CopyTo(dummyDob, nullData);
          writer = dummyDob->InstantiateWriter();
          writer->SetInput(dummyDob);

hrchilds's avatar
hrchilds committed
1356
       }
hrchilds's avatar
hrchilds committed
1357 1358 1359 1360 1361
       else
       {
          // put all the plot objects into the VisWindow
          viswin->SetScalableRendering(false);

hrchilds's avatar
hrchilds committed
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
          //
          // Determine if the plots currently in the window are the same as
          // those we were asked to render.
          //
          bool needToSetUpWindowContents = false;
          if (plotIds.size() != plotsCurrentlyInWindow.size())
              needToSetUpWindowContents = true;
          else
          {
              for (int p = 0 ; p < plotIds.size() ; p++)
                  if (plotIds[p] != plotsCurrentlyInWindow[p])
                      needToSetUpWindowContents = true;
          }
hrchilds's avatar
hrchilds committed
1375

hrchilds's avatar
hrchilds committed
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 13