1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestPStructuredGridConnectivity.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 #include "vtkCellData.h"
16 #include "vtkCellType.h"
17 #include "vtkDoubleArray.h"
18 #include "vtkExtentRCBPartitioner.h"
19 #include "vtkIdList.h"
20 #include "vtkIdTypeArray.h"
21 #include "vtkMPIController.h"
22 #include "vtkMPIUtilities.h"
23 #include "vtkMultiProcessController.h"
24 #include "vtkPUnstructuredGridConnectivity.h"
25 #include "vtkPUnstructuredGridGhostDataGenerator.h"
26 #include "vtkPointData.h"
27 #include "vtkPointData.h"
28 #include "vtkPoints.h"
29 #include "vtkStructuredData.h"
30 #include "vtkTimerLog.h"
31 #include "vtkUnstructuredGrid.h"
32 #include "vtkUnstructuredGridWriter.h"
33 
34 #include <sstream>
35 #include <string>
36 
37 //#define DEBUG
38 #include "UnstructuredGhostZonesCommon.h"
39 
40 //------------------------------------------------------------------------------
41 // Program main
TestPUnstructuredGridGhostDataGenerator(int argc,char * argv[])42 int TestPUnstructuredGridGhostDataGenerator(int argc, char* argv[])
43 {
44   int rc             = 0;
45   double ellapsed    = 0.0;
46   vtkTimerLog* timer = vtkTimerLog::New();
47 
48   // STEP 0: Initialize
49   vtkMPIController* cntrl = vtkMPIController::New();
50   cntrl->Initialize( &argc, &argv, 0 );
51   vtkMultiProcessController::SetGlobalController( cntrl );
52   global::Rank   = cntrl->GetLocalProcessId();
53   global::NRanks = cntrl->GetNumberOfProcesses();
54 
55   // STEP 1: Generate grid in parallel in each process
56   global::Grid = vtkUnstructuredGrid::New();
57   GenerateDataSet();
58 
59   // STEP 2: Setup ghost data generator
60   vtkPUnstructuredGridGhostDataGenerator* ghostGenerator =
61       vtkPUnstructuredGridGhostDataGenerator::New();
62   ghostGenerator->SetInputData(global::Grid);
63 
64   // STEP 3: Update ghost zones
65   std::ostringstream grdfname;   // input grid name at each iteration for I/O
66   std::ostringstream ghostfname; // ghosted grid name at each iteration for I/O
67   for(int i=0; i < 2; ++i)
68     {
69     grdfname.clear();
70     grdfname.str("");
71     grdfname << "INITIAL-T" << i;
72 
73     ghostfname.clear();
74     ghostfname.str("");
75     ghostfname << "GHOSTED-T" << i;
76 
77     // update grid in this iteration...
78     UpdateGrid(i);
79     global::Grid->Modified();
80 #ifdef DEBUG
81     WriteDataSet(global::Grid,grdfname.str().c_str());
82 #endif
83 
84     // update ghost zones in this iteration...
85     vtkMPIUtilities::Printf(cntrl,"[INFO]: iteration=%d\n",i);
86     vtkMPIUtilities::Printf(cntrl,"[INFO]: Update ghost zones...");
87     timer->StartTimer();
88 
89     ghostGenerator->Update();
90 
91     timer->StopTimer();
92     ellapsed = timer->GetElapsedTime();
93     vtkMPIUtilities::Printf(cntrl,"[DONE]\n");
94 
95     // get some performance statistics
96     double minGhostUpdateTime = 0.0;
97     double maxGhostUpdateTime = 0.0;
98     double avgGhostUpdateTime = 0.0;
99     cntrl->Reduce(&ellapsed,&minGhostUpdateTime,1,vtkCommunicator::MIN_OP,0);
100     cntrl->Reduce(&ellapsed,&maxGhostUpdateTime,1,vtkCommunicator::MAX_OP,0);
101     cntrl->Reduce(&ellapsed,&avgGhostUpdateTime,1,vtkCommunicator::SUM_OP,0);
102     avgGhostUpdateTime /= static_cast<double>(cntrl->GetNumberOfProcesses());
103     vtkMPIUtilities::Printf(
104           cntrl,"-- Ellapsed Time: min=%f, avg=%f, max=%f\n",
105           minGhostUpdateTime,avgGhostUpdateTime,maxGhostUpdateTime);
106 
107     vtkUnstructuredGrid* ghostGrid = vtkUnstructuredGrid::New();
108     ghostGrid->DeepCopy(ghostGenerator->GetOutput());
109 #ifdef DEBUG
110     assert("pre: ghost gird should not be NULL!" && (ghostGrid != NULL) );
111     WriteDataSet(ghostGrid,ghostfname.str().c_str());
112 #endif
113 
114     rc += CheckGrid(ghostGrid,i);
115     ghostGrid->Delete();
116     } // END for
117 
118   // STEP 5: Delete the ghost generator
119   timer->Delete();
120   ghostGenerator->Delete();
121   global::Grid->Delete();
122   cntrl->Finalize();
123   cntrl->Delete();
124   return( rc );
125 }
126