1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTemporalStreamTracer.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm 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.
=========================================================================*/
#include "vtkTemporalStreamTracer.h"
#include "vtkPTemporalStreamTracer.h"
#include "vtkCellArray.h"
#include "vtkCellData.h"
#include "vtkCompositeDataIterator.h"
#include "vtkCompositeDataPipeline.h"
#include "vtkDataSetAttributes.h"
#include "vtkDoubleArray.h"
#include "vtkExecutive.h"
#include "vtkGenericCell.h"
#include "vtkIdList.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkIntArray.h"
#include "vtkFloatArray.h"
#include "vtkDoubleArray.h"
#include "vtkCharArray.h"
#include "vtkMath.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkMultiProcessController.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPointSet.h"
#include "vtkPolyData.h"
#include "vtkPolyLine.h"
#include "vtkRungeKutta2.h"
#include "vtkRungeKutta4.h"
#include "vtkRungeKutta45.h"
#include "vtkSmartPointer.h"
#include "vtkTemporalInterpolatedVelocityField.h"
#include "vtkOutputWindow.h"
#include "vtkAbstractParticleWriter.h"
#include "vtkToolkits.h"
#include <cassert>
#include "vtkMPIController.h"
using namespace vtkTemporalStreamTracerNamespace;
//---------------------------------------------------------------------------
vtkStandardNewMacro(vtkPTemporalStreamTracer);
vtkCxxSetObjectMacro(vtkPTemporalStreamTracer, Controller, vtkMultiProcessController);
//---------------------------------------------------------------------------
vtkPTemporalStreamTracer::vtkPTemporalStreamTracer()
{
this->Controller = NULL;
this->SetController(vtkMultiProcessController::GetGlobalController());
}
//---------------------------------------------------------------------------
vtkPTemporalStreamTracer::~vtkPTemporalStreamTracer()
{
this->SetController(NULL);
this->SetParticleWriter(NULL);
}
//---------------------------------------------------------------------------
void vtkPTemporalStreamTracer::AssignSeedsToProcessors(
vtkDataSet *source, int sourceID, int ptId,
ParticleVector &LocalSeedPoints, int &LocalAssignedCount)
{
if(!this->Controller)
{
return Superclass::AssignSeedsToProcessors(source, sourceID, ptId,
LocalSeedPoints, LocalAssignedCount);
}
ParticleVector candidates;
//
// take points from the source object and create a particle list
//
int numSeeds = source->GetNumberOfPoints();
#ifndef NDEBUG
int numTested = numSeeds;
#endif
candidates.resize(numSeeds);
//
for (int i=0; i<numSeeds; i++) {
ParticleInformation &info = candidates[i];
memcpy(&(info.CurrentPosition.x[0]), source->GetPoint(i), sizeof(double)*3);
info.CurrentPosition.x[3] = this->CurrentTimeSteps[0];
info.LocationState = 0;
info.CachedCellId[0] =-1;
info.CachedCellId[1] =-1;
info.CachedDataSetId[0] = 0;
info.CachedDataSetId[1] = 0;
info.SourceID = sourceID;
info.InjectedPointId = i+ptId;
info.InjectedStepId = this->ReinjectionCounter;
info.TimeStepAge = 0;
info.UniqueParticleId =-1;
info.rotation = 0.0;
info.angularVel = 0.0;
info.time = 0.0;
info.age = 0.0;
info.speed = 0.0;
info.ErrorCode = 0;
}
//
// Gather all Seeds to all processors for classification
//
// TODO : can we just use the same array here for send and receive
ParticleVector allCandidates;
if (this->UpdateNumPieces>1) {
// Gather all seed particles to all processes
this->TransmitReceiveParticles(candidates, allCandidates, false);
#ifndef NDEBUG
numTested = static_cast<int>(allCandidates.size());
#endif
vtkDebugMacro(<< "Local Particles " << numSeeds << " TransmitReceive Total " << numTested);
// Test to see which ones belong to us
this->TestParticles(allCandidates, LocalSeedPoints, LocalAssignedCount);
}
else {
#ifndef NDEBUG
numTested = static_cast<int>(candidates.size());
#endif
this->TestParticles(candidates, LocalSeedPoints, LocalAssignedCount);
}
int TotalAssigned = 0;
this->Controller->Reduce(&LocalAssignedCount, &TotalAssigned, 1, vtkCommunicator::SUM_OP, 0);
// Assign unique identifiers taking into account uneven distribution
// across processes and seeds which were rejected
this->AssignUniqueIds(LocalSeedPoints);
//
vtkDebugMacro(<< "Tested " << numTested << " LocallyAssigned " << LocalAssignedCount);
if (this->UpdatePiece==0) {
vtkDebugMacro(<< "Total Assigned to all processes " << TotalAssigned);
}
}
//---------------------------------------------------------------------------
void vtkPTemporalStreamTracer::AssignUniqueIds(
vtkTemporalStreamTracerNamespace::ParticleVector &LocalSeedPoints)
{
if(!this->Controller)
{
return Superclass::AssignUniqueIds(LocalSeedPoints);
}
vtkIdType ParticleCountOffset = 0;
vtkIdType numParticles = LocalSeedPoints.size();
if (this->UpdateNumPieces>1) {
vtkMPICommunicator* com = vtkMPICommunicator::SafeDownCast(
this->Controller->GetCommunicator());
if (com == 0) {
vtkErrorMacro("MPICommunicator needed for this operation.");
return;
}
// everyone starts with the master index
com->Broadcast(&this->UniqueIdCounter, 1, 0);
// vtkErrorMacro("UniqueIdCounter " << this->UniqueIdCounter);
// setup arrays used by the AllGather call.
std::vector<vtkIdType> recvNumParticles(this->UpdateNumPieces, 0);
// Broadcast and receive count to/from all other processes.
com->AllGather(&numParticles, &recvNumParticles[0], 1);
// Each process is allocating a certain number.
// start our indices from sum[0,this->UpdatePiece](numparticles)
for (int i=0; i<this->UpdatePiece; ++i) {
ParticleCountOffset += recvNumParticles[i];
}
for (vtkIdType i=0; i<numParticles; i++) {
LocalSeedPoints[i].UniqueParticleId =
this->UniqueIdCounter + ParticleCountOffset + i;
}
for (int i=0; i<this->UpdateNumPieces; ++i) {
this->UniqueIdCounter += recvNumParticles[i];
}
}
else {
for (vtkIdType i=0; i<numParticles; i++) {
LocalSeedPoints[i].UniqueParticleId =
this->UniqueIdCounter + ParticleCountOffset + i;
}
this->UniqueIdCounter += numParticles;
}
}
//---------------------------------------------------------------------------
void vtkPTemporalStreamTracer::TransmitReceiveParticles(
ParticleVector &sending, ParticleVector &received, bool removeself)
{
vtkMPICommunicator* com = vtkMPICommunicator::SafeDownCast(
this->Controller->GetCommunicator());
if (com == 0) {
vtkErrorMacro("MPICommunicator needed for this operation.");
return;
}
//
// We must allocate buffers for all processor particles
//
vtkIdType OurParticles = sending.size();
vtkIdType TotalParticles = 0;
// setup arrays used by the AllGatherV call.
std::vector<vtkIdType> recvLengths(this->UpdateNumPieces, 0);
std::vector<vtkIdType> recvOffsets(this->UpdateNumPieces, 0);
// Broadcast and receive size to/from all other processes.
com->AllGather(&OurParticles, &recvLengths[0], 1);
// Compute the displacements.
const vtkIdType TypeSize = sizeof(ParticleInformation);
for (int i=0; i<this->UpdateNumPieces; ++i)
{
// << i << ": " << recvLengths[i] << " ";
recvOffsets[i] = TotalParticles*TypeSize;
TotalParticles += recvLengths[i];
recvLengths[i] *= TypeSize;
}
// << '\n';
// Allocate the space for all particles
received.resize(TotalParticles);
if (TotalParticles==0) return;
// Gather the data from all procs.
char *sendbuf = (char*) ((sending.size()>0) ? &(sending[0]) : NULL);
char *recvbuf = (char*) (&(received[0]));
com->AllGatherV(sendbuf, recvbuf,
OurParticles*TypeSize, &recvLengths[0], &recvOffsets[0]);
// Now all particles from all processors are in one big array
// remove any from ourself that we have already tested
if (removeself) {
std::vector<ParticleInformation>::iterator first =
received.begin() + recvOffsets[this->UpdatePiece]/TypeSize;
std::vector<ParticleInformation>::iterator last =
first + recvLengths[this->UpdatePiece]/TypeSize;
received.erase(first, last);
}
}
//---------------------------------------------------------------------------
int vtkPTemporalStreamTracer::RequestData(
vtkInformation *request,
vtkInformationVector **inputVector,
vtkInformationVector *outputVector)
{
int rvalue = this->Superclass::RequestData(request, inputVector, outputVector);
if(this->Controller)
{
this->Controller->Barrier();
}
return rvalue;
}
//---------------------------------------------------------------------------
void vtkPTemporalStreamTracer::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "Controller: " << this->Controller << endl;
}
//---------------------------------------------------------------------------
void vtkPTemporalStreamTracer::AddParticleToMPISendList(ParticleInformation &info)
{
double eps = (this->CurrentTimeSteps[1]-this->CurrentTimeSteps[0])/100;
if (info.CurrentPosition.x[3]<(this->CurrentTimeSteps[0]-eps) ||
info.CurrentPosition.x[3]>(this->CurrentTimeSteps[1]+eps)) {
vtkDebugMacro(<< "Unexpected time value in MPISendList - expected ("
<< this->CurrentTimeSteps[0] << "-" << this->CurrentTimeSteps[1] << ") got "
<< info.CurrentPosition.x[3]);
}
if (this->MPISendList.capacity()<(this->MPISendList.size()+1)) {
this->MPISendList.reserve(static_cast<int>(this->MPISendList.size()*1.5));
}
this->MPISendList.push_back(info);
}
|