Updates will be applied April 15th at 12pm EDT (UTC-0400). GitLab could be a little slow between 12 - 12:45pm EDT.

vtkPVDataDeliveryManager.cxx 25.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*=========================================================================

  Program:   ParaView
  Module:    $RCSfile$

  Copyright (c) Kitware, Inc.
  All rights reserved.
  See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
15
#include "vtkPVDataDeliveryManager.h"
16 17

#include "vtkAlgorithmOutput.h"
18
#include "vtkDataObject.h"
19 20
#include "vtkExtentTranslator.h"
#include "vtkKdTreeManager.h"
21
#include "vtkMPIMoveData.h"
22 23 24 25 26
#include "vtkMultiProcessController.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkOrderedCompositeDistributor.h"
#include "vtkPKdTree.h"
27
#include "vtkPVDataRepresentation.h"
28
#include "vtkPVRenderView.h"
29
#include "vtkPVStreamingMacros.h"
30 31
#include "vtkPVTrivialProducer.h"
#include "vtkSmartPointer.h"
32
#include "vtkStreamingDemandDrivenPipeline.h"
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
33
#include "vtkTimerLog.h"
34
#include "vtkWeakPointer.h"
35 36

#include <assert.h>
37 38 39 40 41 42 43 44 45
#include <map>
#include <queue>
#include <utility>

//*****************************************************************************
class vtkPVDataDeliveryManager::vtkInternals
{
public:
  class vtkPriorityQueueItem
Kitware Robot's avatar
Kitware Robot committed
46
  {
47 48 49 50 51 52 53
  public:
    unsigned int RepresentationId;
    unsigned int BlockId;
    unsigned int Level;
    unsigned int Index;
    double Priority;

Kitware Robot's avatar
Kitware Robot committed
54 55 56 57 58 59
    vtkPriorityQueueItem()
      : RepresentationId(0)
      , BlockId(0)
      , Level(0)
      , Index(0)
      , Priority(0)
60 61 62
    {
    }

Kitware Robot's avatar
Kitware Robot committed
63 64
    bool operator<(const vtkPriorityQueueItem& other) const
    {
65
      return this->Priority < other.Priority;
Kitware Robot's avatar
Kitware Robot committed
66 67
    }
  };
68 69 70 71

  typedef std::priority_queue<vtkPriorityQueueItem> PriorityQueueType;
  PriorityQueueType PriorityQueue;

72
  class vtkOrderedCompositingInfo
Kitware Robot's avatar
Kitware Robot committed
73
  {
74 75 76 77 78
  public:
    vtkSmartPointer<vtkExtentTranslator> Translator;
    double Origin[3];
    double Spacing[3];
    int WholeExtent[6];
Kitware Robot's avatar
Kitware Robot committed
79
  };
80

81
  class vtkItem
Kitware Robot's avatar
Kitware Robot committed
82
  {
83
    vtkSmartPointer<vtkPVTrivialProducer> Producer;
84 85

    // Data object produced by the representation.
86
    vtkWeakPointer<vtkDataObject> DataObject;
87 88 89 90 91 92 93 94

    // Data object available after delivery to the "rendering" node.
    vtkSmartPointer<vtkDataObject> DeliveredDataObject;

    // Data object after re-distributing when using ordered compositing, for
    // example.
    vtkSmartPointer<vtkDataObject> RedistributedDataObject;

95 96 97
    // Data object for a streamed piece.
    vtkSmartPointer<vtkDataObject> StreamedPiece;

98 99
    unsigned long TimeStamp;
    unsigned long ActualMemorySize;
Kitware Robot's avatar
Kitware Robot committed
100

101
  public:
102
    vtkOrderedCompositingInfo OrderedCompositingInfo;
103

104
    vtkWeakPointer<vtkPVDataRepresentation> Representation;
105 106 107
    bool CloneDataToAllNodes;
    bool DeliverToClientAndRenderingProcesses;
    bool GatherBeforeDeliveringToClient;
108 109 110
    bool Redistributable;
    bool Streamable;

Kitware Robot's avatar
Kitware Robot committed
111 112 113 114 115 116 117 118 119 120 121
    vtkItem()
      : Producer(vtkSmartPointer<vtkPVTrivialProducer>::New())
      , TimeStamp(0)
      , ActualMemorySize(0)
      , CloneDataToAllNodes(false)
      , DeliverToClientAndRenderingProcesses(false)
      , GatherBeforeDeliveringToClient(false)
      , Redistributable(false)
      , Streamable(false)
    {
    }
122 123

    void SetDataObject(vtkDataObject* data)
Kitware Robot's avatar
Kitware Robot committed
124
    {
125
      this->DataObject = data;
Kitware Robot's avatar
Kitware Robot committed
126
      this->ActualMemorySize = data ? data->GetActualMemorySize() : 0;
127

Kitware Robot's avatar
Kitware Robot committed
128 129
      vtkTimeStamp ts;
      ts.Modified();
130
      this->TimeStamp = ts;
Kitware Robot's avatar
Kitware Robot committed
131
    }
132

Kitware Robot's avatar
Kitware Robot committed
133
    void SetActualMemorySize(unsigned long size) { this->ActualMemorySize = size; }
134

Kitware Robot's avatar
Kitware Robot committed
135
    void SetDeliveredDataObject(vtkDataObject* data) { this->DeliveredDataObject = data; }
136

Kitware Robot's avatar
Kitware Robot committed
137
    void SetRedistributedDataObject(vtkDataObject* data) { this->RedistributedDataObject = data; }
138

Kitware Robot's avatar
Kitware Robot committed
139
    vtkDataObject* GetDeliveredDataObject() { return this->DeliveredDataObject.GetPointer(); }
140 141

    vtkDataObject* GetRedistributedDataObject()
Kitware Robot's avatar
Kitware Robot committed
142
    {
143
      return this->RedistributedDataObject.GetPointer();
Kitware Robot's avatar
Kitware Robot committed
144
    }
145 146

    vtkPVTrivialProducer* GetProducer(bool use_redistributed_data)
Kitware Robot's avatar
Kitware Robot committed
147
    {
148
      if (use_redistributed_data && this->Redistributable)
Kitware Robot's avatar
Kitware Robot committed
149
      {
150
        this->Producer->SetOutput(this->RedistributedDataObject);
Kitware Robot's avatar
Kitware Robot committed
151
      }
152
      else
Kitware Robot's avatar
Kitware Robot committed
153
      {
154
        this->Producer->SetOutput(this->DeliveredDataObject);
155
      }
Kitware Robot's avatar
Kitware Robot committed
156 157
      return this->Producer.GetPointer();
    }
158

Kitware Robot's avatar
Kitware Robot committed
159 160
    vtkDataObject* GetDataObject() const { return this->DataObject.GetPointer(); }
    unsigned long GetTimeStamp() const { return this->TimeStamp; }
161

162
    unsigned long GetVisibleDataSize()
Kitware Robot's avatar
Kitware Robot committed
163
    {
164
      if (this->Representation && this->Representation->GetVisibility())
Kitware Robot's avatar
Kitware Robot committed
165
      {
166 167
        return this->ActualMemorySize;
      }
Kitware Robot's avatar
Kitware Robot committed
168 169
      return 0;
    }
170

Kitware Robot's avatar
Kitware Robot committed
171 172 173
    void SetNextStreamedPiece(vtkDataObject* data) { this->StreamedPiece = data; }
    vtkDataObject* GetStreamedPiece() { return this->StreamedPiece; }
  };
174

175 176 177
  // First is repr unique id, second is the input port.
  typedef std::pair<unsigned int, int> ReprPortType;
  typedef std::map<ReprPortType, std::pair<vtkItem, vtkItem> > ItemsMapType;
178

179
  vtkItem* GetItem(unsigned int index, bool use_second, int port = 0)
Kitware Robot's avatar
Kitware Robot committed
180
  {
181 182 183
    ReprPortType key(index, port);
    ItemsMapType::iterator items = this->ItemsMap.find(key);
    if (items != this->ItemsMap.end())
Kitware Robot's avatar
Kitware Robot committed
184
    {
185
      return use_second ? &(items->second.second) : &(items->second.first);
186
    }
Kitware Robot's avatar
Kitware Robot committed
187 188
    return NULL;
  }
189

190
  vtkItem* GetItem(vtkPVDataRepresentation* repr, bool use_second, int port = 0)
Kitware Robot's avatar
Kitware Robot committed
191
  {
192
    return this->GetItem(repr->GetUniqueIdentifier(), use_second, port);
Kitware Robot's avatar
Kitware Robot committed
193
  }
194 195

  unsigned long GetVisibleDataSize(bool use_second_if_available)
Kitware Robot's avatar
Kitware Robot committed
196
  {
197 198 199
    unsigned long size = 0;
    ItemsMapType::iterator iter;
    for (iter = this->ItemsMap.begin(); iter != this->ItemsMap.end(); ++iter)
Kitware Robot's avatar
Kitware Robot committed
200
    {
201
      if (use_second_if_available && iter->second.second.GetDataObject())
Kitware Robot's avatar
Kitware Robot committed
202
      {
203
        size += iter->second.second.GetVisibleDataSize();
Kitware Robot's avatar
Kitware Robot committed
204
      }
205
      else
Kitware Robot's avatar
Kitware Robot committed
206
      {
207 208 209
        size += iter->second.first.GetVisibleDataSize();
      }
    }
Kitware Robot's avatar
Kitware Robot committed
210 211
    return size;
  }
212

213 214
  ItemsMapType ItemsMap;
};
215

216 217 218
//*****************************************************************************

vtkStandardNewMacro(vtkPVDataDeliveryManager);
219
//----------------------------------------------------------------------------
220
vtkPVDataDeliveryManager::vtkPVDataDeliveryManager()
221 222 223 224 225
  : Internals(new vtkInternals())
{
}

//----------------------------------------------------------------------------
226
vtkPVDataDeliveryManager::~vtkPVDataDeliveryManager()
227 228 229 230 231
{
  delete this->Internals;
  this->Internals = 0;
}

232
//----------------------------------------------------------------------------
233
void vtkPVDataDeliveryManager::SetRenderView(vtkPVRenderView* view)
234
{
235
  this->RenderView = view;
236 237
}

238
//----------------------------------------------------------------------------
239
vtkPVRenderView* vtkPVDataDeliveryManager::GetRenderView()
240
{
241
  return this->RenderView;
242 243
}

Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
244
//----------------------------------------------------------------------------
245
unsigned long vtkPVDataDeliveryManager::GetVisibleDataSize(bool low_res)
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
246 247 248
{
  return this->Internals->GetVisibleDataSize(low_res);
}
249 250

//----------------------------------------------------------------------------
251
void vtkPVDataDeliveryManager::RegisterRepresentation(vtkPVDataRepresentation* repr)
252
{
Kitware Robot's avatar
Kitware Robot committed
253
  assert("A representation must have a valid UniqueIdentifier" && repr->GetUniqueIdentifier());
254

255
  int numPorts = repr->GetNumberOfInputPorts();
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
256

257 258 259 260 261 262 263 264 265 266 267 268
  for (int port = 0; port < numPorts; ++port)
  {
    vtkInternals::ReprPortType key(repr->GetUniqueIdentifier(), port);

    vtkInternals::vtkItem item;
    item.Representation = repr;
    this->Internals->ItemsMap[key].first = item;

    vtkInternals::vtkItem item2;
    item2.Representation = repr;
    this->Internals->ItemsMap[key].second = item2;
  }
269 270 271
}

//----------------------------------------------------------------------------
Kitware Robot's avatar
Kitware Robot committed
272
void vtkPVDataDeliveryManager::UnRegisterRepresentation(vtkPVDataRepresentation* repr)
273
{
274 275 276 277 278 279 280
  int numPorts = repr->GetNumberOfInputPorts();

  for (int port = 0; port < numPorts; ++port)
  {
    vtkInternals::ReprPortType key(repr->GetUniqueIdentifier(), port);
    this->Internals->ItemsMap.erase(key);
  }
281 282 283
}

//----------------------------------------------------------------------------
Kitware Robot's avatar
Kitware Robot committed
284
vtkPVDataRepresentation* vtkPVDataDeliveryManager::GetRepresentation(unsigned int index)
285 286
{
  vtkInternals::vtkItem* item = this->Internals->GetItem(index, false);
Kitware Robot's avatar
Kitware Robot committed
287
  return item ? item->Representation : NULL;
288 289
}

290
//----------------------------------------------------------------------------
291
void vtkPVDataDeliveryManager::SetDeliverToAllProcesses(
292
  vtkPVDataRepresentation* repr, bool mode, bool low_res, int port)
293
{
294
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, low_res, port);
295
  if (item)
Kitware Robot's avatar
Kitware Robot committed
296
  {
297
    item->CloneDataToAllNodes = mode;
Kitware Robot's avatar
Kitware Robot committed
298
  }
299
  else
Kitware Robot's avatar
Kitware Robot committed
300
  {
301
    vtkErrorMacro("Invalid argument.");
Kitware Robot's avatar
Kitware Robot committed
302
  }
303 304 305 306
}

//----------------------------------------------------------------------------
void vtkPVDataDeliveryManager::SetDeliverToClientAndRenderingProcesses(
307 308
  vtkPVDataRepresentation* repr, bool deliver_to_client, bool gather_before_delivery, bool low_res,
  int port)
309
{
310
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, low_res, port);
311
  if (item)
Kitware Robot's avatar
Kitware Robot committed
312
  {
313 314
    item->DeliverToClientAndRenderingProcesses = deliver_to_client;
    item->GatherBeforeDeliveringToClient = gather_before_delivery;
Kitware Robot's avatar
Kitware Robot committed
315
  }
316
  else
Kitware Robot's avatar
Kitware Robot committed
317
  {
318
    vtkErrorMacro("Invalid argument.");
Kitware Robot's avatar
Kitware Robot committed
319
  }
320 321
}

322
//----------------------------------------------------------------------------
323
void vtkPVDataDeliveryManager::MarkAsRedistributable(
324
  vtkPVDataRepresentation* repr, bool value /*=true*/, int port)
325
{
326 327
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, false, port);
  vtkInternals::vtkItem* low_item = this->Internals->GetItem(repr, true, port);
328
  if (item)
Kitware Robot's avatar
Kitware Robot committed
329
  {
330 331
    item->Redistributable = value;
    low_item->Redistributable = value;
Kitware Robot's avatar
Kitware Robot committed
332
  }
333
  else
Kitware Robot's avatar
Kitware Robot committed
334
  {
335
    vtkErrorMacro("Invalid argument.");
Kitware Robot's avatar
Kitware Robot committed
336
  }
337
}
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
338

339
//----------------------------------------------------------------------------
340
void vtkPVDataDeliveryManager::SetStreamable(vtkPVDataRepresentation* repr, bool val, int port)
341
{
342 343
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, false, port);
  vtkInternals::vtkItem* low_item = this->Internals->GetItem(repr, true, port);
344
  if (item)
Kitware Robot's avatar
Kitware Robot committed
345
  {
346 347
    item->Streamable = val;
    low_item->Streamable = val;
Kitware Robot's avatar
Kitware Robot committed
348
  }
349
  else
Kitware Robot's avatar
Kitware Robot committed
350
  {
351
    vtkErrorMacro("Invalid argument.");
Kitware Robot's avatar
Kitware Robot committed
352
  }
353 354
}

355
//----------------------------------------------------------------------------
356 357
void vtkPVDataDeliveryManager::SetPiece(vtkPVDataRepresentation* repr, vtkDataObject* data,
  bool low_res, unsigned long trueSize, int port)
358
{
359
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, low_res, port);
360
  if (item)
Kitware Robot's avatar
Kitware Robot committed
361
  {
362
    unsigned long data_time = 0;
363
    if (data && (data->GetMTime() > data_time))
Kitware Robot's avatar
Kitware Robot committed
364
    {
365
      data_time = data->GetMTime();
Kitware Robot's avatar
Kitware Robot committed
366 367 368
    }
    if (data_time > item->GetTimeStamp() || item->GetDataObject() != data)
    {
369
      item->SetDataObject(data);
Kitware Robot's avatar
Kitware Robot committed
370
    }
371
    if (trueSize > 0)
Kitware Robot's avatar
Kitware Robot committed
372
    {
373
      item->SetActualMemorySize(trueSize);
374
    }
Kitware Robot's avatar
Kitware Robot committed
375
  }
376
  else
Kitware Robot's avatar
Kitware Robot committed
377
  {
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
378
    vtkErrorMacro("Invalid argument.");
Kitware Robot's avatar
Kitware Robot committed
379
  }
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
380 381 382
}

//----------------------------------------------------------------------------
383
vtkAlgorithmOutput* vtkPVDataDeliveryManager::GetProducer(
384
  vtkPVDataRepresentation* repr, bool low_res, int port)
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
385
{
386
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, low_res, port);
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
387
  if (!item)
Kitware Robot's avatar
Kitware Robot committed
388
  {
389
    vtkErrorMacro("Invalid arguments.");
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
390
    return NULL;
Kitware Robot's avatar
Kitware Robot committed
391
  }
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
392

Kitware Robot's avatar
Kitware Robot committed
393
  return item->GetProducer(this->RenderView->GetUseOrderedCompositing())->GetOutputPort(0);
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
394 395 396
}

//----------------------------------------------------------------------------
397 398
void vtkPVDataDeliveryManager::SetPiece(
  unsigned int id, vtkDataObject* data, bool low_res, int port)
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
399
{
400
  vtkInternals::vtkItem* item = this->Internals->GetItem(id, low_res, port);
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
401
  if (item)
Kitware Robot's avatar
Kitware Robot committed
402
  {
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
403
    item->SetDataObject(data);
Kitware Robot's avatar
Kitware Robot committed
404
  }
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
405
  else
Kitware Robot's avatar
Kitware Robot committed
406
  {
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
407
    vtkErrorMacro("Invalid argument.");
Kitware Robot's avatar
Kitware Robot committed
408
  }
409 410
}

411
//----------------------------------------------------------------------------
Kitware Robot's avatar
Kitware Robot committed
412 413
void vtkPVDataDeliveryManager::SetOrderedCompositingInformation(vtkPVDataRepresentation* repr,
  vtkExtentTranslator* translator, const int whole_extents[6], const double origin[3],
414
  const double spacing[3], int port)
415
{
416
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr, false, port);
417
  if (item)
Kitware Robot's avatar
Kitware Robot committed
418
  {
419 420
    vtkInternals::vtkOrderedCompositingInfo info;
    info.Translator = translator;
Kitware Robot's avatar
Kitware Robot committed
421 422 423
    memcpy(info.WholeExtent, whole_extents, sizeof(int) * 6);
    memcpy(info.Origin, origin, sizeof(double) * 3);
    memcpy(info.Spacing, spacing, sizeof(double) * 3);
424 425

    item->OrderedCompositingInfo = info;
Kitware Robot's avatar
Kitware Robot committed
426
  }
427
  else
Kitware Robot's avatar
Kitware Robot committed
428
  {
429
    vtkErrorMacro("Invalid argument.");
Kitware Robot's avatar
Kitware Robot committed
430
  }
431 432
}

433
//----------------------------------------------------------------------------
434
vtkAlgorithmOutput* vtkPVDataDeliveryManager::GetProducer(unsigned int id, bool low_res, int port)
435
{
436
  vtkInternals::vtkItem* item = this->Internals->GetItem(id, low_res, port);
437
  if (!item)
Kitware Robot's avatar
Kitware Robot committed
438
  {
439 440
    vtkErrorMacro("Invalid arguments.");
    return NULL;
Kitware Robot's avatar
Kitware Robot committed
441
  }
442

Kitware Robot's avatar
Kitware Robot committed
443
  return item->GetProducer(this->RenderView->GetUseOrderedCompositing())->GetOutputPort(0);
444 445
}

446
//----------------------------------------------------------------------------
447
bool vtkPVDataDeliveryManager::NeedsDelivery(
Kitware Robot's avatar
Kitware Robot committed
448
  unsigned long timestamp, std::vector<unsigned int>& keys_to_deliver, bool use_low)
449 450
{
  vtkInternals::ItemsMapType::iterator iter;
Kitware Robot's avatar
Kitware Robot committed
451 452 453 454
  for (iter = this->Internals->ItemsMap.begin(); iter != this->Internals->ItemsMap.end(); ++iter)
  {
    vtkInternals::vtkItem& item = use_low ? iter->second.second : iter->second.first;
    if (item.Representation && item.Representation->GetVisibility() &&
455
      item.GetTimeStamp() > timestamp)
Kitware Robot's avatar
Kitware Robot committed
456
    {
457
      keys_to_deliver.push_back(iter->first.first);
458
    }
Kitware Robot's avatar
Kitware Robot committed
459
  }
460 461 462 463
  return keys_to_deliver.size() > 0;
}

//----------------------------------------------------------------------------
Kitware Robot's avatar
Kitware Robot committed
464
void vtkPVDataDeliveryManager::Deliver(int use_lod, unsigned int size, unsigned int* values)
465 466

{
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
467 468 469 470 471 472 473 474 475 476 477 478
  // This method gets called on all processes with the list of representations
  // to "deliver". We check with the view what mode we're operating in and
  // decide where the data needs to be delivered.
  //
  // Representations can provide overrides, e.g. though the view says data is
  // merely "pass-through", some representation says we need to clone the data
  // everywhere. That makes it critical that this method is called on all
  // processes at the same time to avoid deadlocks and other complications.
  //
  // This method will be implemented in "view-specific" subclasses since how the
  // data is delivered is very view specific.

Kitware Robot's avatar
Kitware Robot committed
479
  vtkTimerLog::MarkStartEvent(use_lod ? "LowRes Data Migration" : "FullRes Data Migration");
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
480

Kitware Robot's avatar
Kitware Robot committed
481 482 483
  bool using_remote_rendering = use_lod
    ? this->RenderView->GetUseDistributedRenderingForInteractiveRender()
    : this->RenderView->GetUseDistributedRenderingForStillRender();
484
  int mode = this->RenderView->GetDataDistributionMode(using_remote_rendering);
Utkarsh Ayachit's avatar
Utkarsh Ayachit committed
485

Kitware Robot's avatar
Kitware Robot committed
486 487
  for (unsigned int cc = 0; cc < size; cc++)
  {
488 489 490 491 492
    // Get the repr so we can loop over input ports:
    // TODO Both this method and NeedsDelivery should be updated to use
    // port information so we don't copy unneccesarily.
    vtkPVDataRepresentation* repr = this->GetRepresentation(values[cc]);
    int numPorts = repr->GetNumberOfInputPorts();
493

494 495 496
    for (int port = 0; port < numPorts; ++port)
    {
      vtkInternals::vtkItem* item = this->Internals->GetItem(values[cc], use_lod != 0, port);
497

498
      vtkDataObject* data = item ? item->GetDataObject() : NULL;
499

500 501 502 503
      if (!data)
      {
        continue;
      }
504

505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
      //    if (data != NULL && data->IsA("vtkUniformGridAMR"))
      //      {
      //      // we are dealing with AMR datasets.
      //      // We assume for now we're not running in render-server mode. We can
      //      // ensure that at some point in future.
      //      // So we are either in pass-through or collect mode.

      //      // FIXME: check that the mode flags are "suitable" for AMR.
      //      }

      vtkNew<vtkMPIMoveData> dataMover;
      dataMover->InitializeForCommunicationForParaView();
      dataMover->SetOutputDataType(data ? data->GetDataObjectType() : VTK_POLY_DATA);
      dataMover->SetMoveMode(mode);
      if (item->CloneDataToAllNodes)
Kitware Robot's avatar
Kitware Robot committed
520
      {
521
        dataMover->SetMoveModeToClone();
Kitware Robot's avatar
Kitware Robot committed
522
      }
523
      else if (item->DeliverToClientAndRenderingProcesses)
Kitware Robot's avatar
Kitware Robot committed
524
      {
525 526 527 528 529 530 531 532 533 534
        if (mode == vtkMPIMoveData::PASS_THROUGH)
        {
          dataMover->SetMoveMode(vtkMPIMoveData::COLLECT_AND_PASS_THROUGH);
        }
        else
        {
          // nothing to do, since the data is going to be delivered to the client
          // anyways.
        }
        dataMover->SetSkipDataServerGatherToZero(item->GatherBeforeDeliveringToClient == false);
535
      }
536
      dataMover->SetInputData(data);
537

538 539 540 541 542 543 544 545 546 547
      if (dataMover->GetOutputGeneratedOnProcess())
      {
        // release old memory (not necessarily, but try).
        item->SetDeliveredDataObject(NULL);
      }
      dataMover->Update();
      if (item->GetDeliveredDataObject() == NULL)
      {
        item->SetDeliveredDataObject(dataMover->GetOutputDataObject(0));
      }
548
    }
Kitware Robot's avatar
Kitware Robot committed
549
  }
550

Kitware Robot's avatar
Kitware Robot committed
551
  vtkTimerLog::MarkEndEvent(use_lod ? "LowRes Data Migration" : "FullRes Data Migration");
552
}
553

554
//----------------------------------------------------------------------------
Kitware Robot's avatar
Kitware Robot committed
555
void vtkPVDataDeliveryManager::RedistributeDataForOrderedCompositing(bool use_lod)
556 557
{
  if (this->RenderView->GetUpdateTimeStamp() > this->RedistributionTimeStamp)
Kitware Robot's avatar
Kitware Robot committed
558
  {
559 560 561 562
    vtkTimerLog::MarkStartEvent("Regenerate Kd-Tree");
    // need to re-generate the kd-tree.
    this->RedistributionTimeStamp.Modified();

563
    vtkNew<vtkKdTreeManager> cutsGenerator;
564
    vtkInternals::ItemsMapType::iterator iter;
Kitware Robot's avatar
Kitware Robot committed
565 566 567 568
    for (iter = this->Internals->ItemsMap.begin(); iter != this->Internals->ItemsMap.end(); ++iter)
    {
      vtkInternals::vtkItem& item = iter->second.first;
      if (item.Representation && item.Representation->GetVisibility())
569
      {
570
        if (item.OrderedCompositingInfo.Translator)
Kitware Robot's avatar
Kitware Robot committed
571
        {
572 573
          // implies that the representation is providing us with means to
          // override how the ordered compositing happens.
Kitware Robot's avatar
Kitware Robot committed
574 575 576 577
          const vtkInternals::vtkOrderedCompositingInfo& info = item.OrderedCompositingInfo;
          cutsGenerator->SetStructuredDataInformation(
            info.Translator, info.WholeExtent, info.Origin, info.Spacing);
        }
578
        else if (item.Redistributable)
Kitware Robot's avatar
Kitware Robot committed
579
        {
580
          cutsGenerator->AddDataObject(item.GetDeliveredDataObject());
581 582
        }
      }
Kitware Robot's avatar
Kitware Robot committed
583
    }
584 585
    cutsGenerator->GenerateKdTree();
    this->KdTree = cutsGenerator->GetKdTree();
586

587
    vtkTimerLog::MarkEndEvent("Regenerate Kd-Tree");
Kitware Robot's avatar
Kitware Robot committed
588
  }
589 590

  if (this->KdTree == NULL)
Kitware Robot's avatar
Kitware Robot committed
591
  {
592
    return;
Kitware Robot's avatar
Kitware Robot committed
593
  }
594

595 596
  vtkTimerLog::MarkStartEvent("Redistributing Data for Ordered Compositing");
  vtkInternals::ItemsMapType::iterator iter;
Kitware Robot's avatar
Kitware Robot committed
597 598 599
  for (iter = this->Internals->ItemsMap.begin(); iter != this->Internals->ItemsMap.end(); ++iter)
  {
    vtkInternals::vtkItem& item = use_lod ? iter->second.second : iter->second.first;
600

Kitware Robot's avatar
Kitware Robot committed
601
    if (!item.Redistributable || item.Representation == NULL ||
602 603 604 605
      item.Representation->GetVisibility() == false ||

      // delivered object can be null in case we're updating lod and the
      // representation doeesn't have any LOD data.
606
      item.GetDeliveredDataObject() == NULL)
Kitware Robot's avatar
Kitware Robot committed
607
    {
608
      continue;
Kitware Robot's avatar
Kitware Robot committed
609
    }
610

611 612 613
    if (item.GetRedistributedDataObject() &&

      // input-data didn't change
Kitware Robot's avatar
Kitware Robot committed
614
      (item.GetDeliveredDataObject()->GetMTime() < item.GetRedistributedDataObject()->GetMTime()) &&
615 616

      // kd-tree didn't change
Kitware Robot's avatar
Kitware Robot committed
617 618
      (item.GetRedistributedDataObject()->GetMTime() > this->KdTree->GetMTime()))
    {
619 620
      // skip redistribution.
      continue;
Kitware Robot's avatar
Kitware Robot committed
621
    }
622

623 624
    // release old memory (not necessarily, but try).
    item.SetRedistributedDataObject(NULL);
625

626 627 628 629 630 631 632
    vtkNew<vtkOrderedCompositeDistributor> redistributor;
    redistributor->SetController(vtkMultiProcessController::GetGlobalController());
    redistributor->SetInputData(item.GetDeliveredDataObject());
    redistributor->SetPKdTree(this->KdTree);
    redistributor->SetPassThrough(0);
    redistributor->Update();
    item.SetRedistributedDataObject(redistributor->GetOutputDataObject(0));
Kitware Robot's avatar
Kitware Robot committed
633
  }
634
  vtkTimerLog::MarkEndEvent("Redistributing Data for Ordered Compositing");
635 636
}

637
//----------------------------------------------------------------------------
Kitware Robot's avatar
Kitware Robot committed
638
void vtkPVDataDeliveryManager::ClearRedistributedData(bool use_lod)
639 640 641 642 643 644
{
  // It seems like we should be able to set each item's RedistributedDataObject
  // to NULL in this loop but that doesn't work. For now we're leaving this as
  // is to make sure we don't break functionality but this should be revisited
  // later.
  vtkInternals::ItemsMapType::iterator iter;
Kitware Robot's avatar
Kitware Robot committed
645 646 647
  for (iter = this->Internals->ItemsMap.begin(); iter != this->Internals->ItemsMap.end(); ++iter)
  {
    vtkInternals::vtkItem& item = use_lod ? iter->second.second : iter->second.first;
648

Kitware Robot's avatar
Kitware Robot committed
649
    if (!item.Redistributable || item.Representation == NULL ||
650 651 652 653 654
      item.Representation->GetVisibility() == false ||

      // delivered object can be null in case we're updating lod and the
      // representation doeesn't have any LOD data.
      item.GetDeliveredDataObject() == NULL)
Kitware Robot's avatar
Kitware Robot committed
655
    {
656 657
      continue;
    }
Kitware Robot's avatar
Kitware Robot committed
658 659
    item.SetRedistributedDataObject(item.GetDeliveredDataObject());
  }
660 661
}

662
//----------------------------------------------------------------------------
663
vtkPKdTree* vtkPVDataDeliveryManager::GetKdTree()
664 665 666
{
  return this->KdTree;
}
667 668

//----------------------------------------------------------------------------
669
void vtkPVDataDeliveryManager::SetNextStreamedPiece(
670
  vtkPVDataRepresentation* repr, vtkDataObject* data, int port)
671
{
672 673
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr,
    /*low_res=*/false, port);
674
  if (item == NULL)
Kitware Robot's avatar
Kitware Robot committed
675
  {
676 677
    vtkErrorMacro("Invalid argument.");
    return;
Kitware Robot's avatar
Kitware Robot committed
678 679
  }

680 681 682 683
  // For now, I am going to keep things simple. Piece is delivered to the
  // representation separately. That's it.
  item->SetNextStreamedPiece(data);
}
684

685
//----------------------------------------------------------------------------
686 687
vtkDataObject* vtkPVDataDeliveryManager::GetCurrentStreamedPiece(
  vtkPVDataRepresentation* repr, int port)
688
{
689 690
  vtkInternals::vtkItem* item = this->Internals->GetItem(repr,
    /*low_res=*/false, port);
691
  if (item == NULL)
Kitware Robot's avatar
Kitware Robot committed
692
  {
693 694
    vtkErrorMacro("Invalid argument.");
    return NULL;
Kitware Robot's avatar
Kitware Robot committed
695
  }
696 697
  return item->GetStreamedPiece();
}
698

699 700 701 702 703
//----------------------------------------------------------------------------
void vtkPVDataDeliveryManager::ClearStreamedPieces()
{
  // I am not too sure if I want to do this. Right now I am thinking once a
  // piece is delivered, the delivery manager should no longer bother about it.
704
  vtkInternals::ItemsMapType::iterator iter;
Kitware Robot's avatar
Kitware Robot committed
705 706
  for (iter = this->Internals->ItemsMap.begin(); iter != this->Internals->ItemsMap.end(); ++iter)
  {
707
    vtkInternals::vtkItem& item = iter->second.first;
708
    item.SetNextStreamedPiece(NULL);
Kitware Robot's avatar
Kitware Robot committed
709
  }
710 711 712
}

//----------------------------------------------------------------------------
713 714
bool vtkPVDataDeliveryManager::GetRepresentationsReadyToStreamPieces(
  std::vector<unsigned int>& keys)
715
{