Salome HOME
Adding distribution support
[modules/paravis.git] / src / Plugins / MEDReader / plugin / MEDReaderIO / vtkFileSeriesGroupReader.cxx
1 #include "vtkFileSeriesGroupReader.h"
2
3 #include <vtkInformation.h>
4 #include <vtkInformationVector.h>
5 #include <vtkMultiBlockDataSet.h>
6 #include <vtkMultiProcessController.h>
7 #include <vtkObjectFactory.h>
8 #include <vtkSmartPointer.h>
9 #include <vtkStreamingDemandDrivenPipeline.h>
10
11 #include "vtkMEDReader.h"
12
13 #include <vector>
14 #include <string>
15
16 vtkStandardNewMacro(vtkFileSeriesGroupReader);
17
18 //=============================================================================
19 struct vtkFileSeriesGroupReaderInternals
20 {
21   std::vector<std::string> FileNames;
22 };
23
24 //=============================================================================
25 vtkFileSeriesGroupReader::vtkFileSeriesGroupReader()
26   : Internals(new vtkFileSeriesGroupReaderInternals())
27 {
28   this->SetNumberOfInputPorts(0);
29   this->SetNumberOfOutputPorts(1);
30 }
31
32 //-----------------------------------------------------------------------------
33 vtkFileSeriesGroupReader::~vtkFileSeriesGroupReader() = default;
34
35 //----------------------------------------------------------------------------
36 void vtkFileSeriesGroupReader::AddFileName(const char* name)
37 {
38   // Make sure the reader always has a filename set
39   this->ReaderSetFileName(name);
40
41   this->AddFileNameInternal(name);
42   this->Modified();
43 }
44
45 //----------------------------------------------------------------------------
46 void vtkFileSeriesGroupReader::RemoveAllFileNames()
47 {
48   this->RemoveAllFileNamesInternal();
49   this->Modified();
50 }
51
52 //----------------------------------------------------------------------------
53 void vtkFileSeriesGroupReader::RemoveAllFileNamesInternal()
54 {
55   this->Internals->FileNames.clear();
56 }
57
58 //----------------------------------------------------------------------------
59 void vtkFileSeriesGroupReader::AddFileNameInternal(const char* name)
60 {
61   this->Internals->FileNames.emplace_back(name);
62 }
63
64 //----------------------------------------------------------------------------
65 unsigned int vtkFileSeriesGroupReader::GetNumberOfFileNames()
66 {
67   return static_cast<unsigned int>(this->Internals->FileNames.size());
68 }
69
70 //----------------------------------------------------------------------------
71 const char* vtkFileSeriesGroupReader::GetFileName(unsigned int idx)
72 {
73   if (idx >= this->Internals->FileNames.size())
74   {
75     return nullptr;
76   }
77   return this->Internals->FileNames[idx].c_str();
78 }
79
80 //----------------------------------------------------------------------------
81 int vtkFileSeriesGroupReader::CanReadFile(const char* filename)
82 {
83   if (!this->Reader)
84   {
85     return 0;
86   }
87
88   return this->ReaderCanReadFile(filename);
89 }
90
91 //----------------------------------------------------------------------------
92 int vtkFileSeriesGroupReader::RequestInformation(
93   vtkInformation* request, vtkInformationVector** inputVector, vtkInformationVector* outputVector)
94 {
95   return this->Reader->ProcessRequest(request, inputVector, outputVector);
96 }
97
98 //----------------------------------------------------------------------------
99 int vtkFileSeriesGroupReader::RequestData(vtkInformation* vtkNotUsed(request),
100   vtkInformationVector** vtkNotUsed(inputVector), vtkInformationVector* outputVector)
101 {
102   auto output = vtkMultiBlockDataSet::GetData(outputVector, 0);
103   output->SetNumberOfBlocks(this->GetNumberOfFileNames());
104
105   vtkInformation* info = outputVector->GetInformationObject(0);
106   double time = info->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEP());
107
108   vtkMultiProcessController *vmpc(vtkMultiProcessController::GetGlobalController());
109   unsigned int iProc = vmpc ? vmpc->GetLocalProcessId() : 0;
110   unsigned int nProc = vmpc ? vmpc->GetNumberOfProcesses() : 1;
111   unsigned int nFiles = this->GetNumberOfFileNames() / nProc;
112   unsigned int offFile =  iProc * nFiles;
113   unsigned int supFiles = this->GetNumberOfFileNames() % nProc;
114   if (iProc == nProc - 1)
115   {
116     nFiles += supFiles;
117   }
118
119   for (unsigned int i = 0; i < nFiles; i++)
120   {
121     this->ReaderSetFileName(this->GetFileName(i + offFile));
122     vtkMEDReader::SafeDownCast(this->Reader)->ReloadInternals();
123     vtkMEDReader::SafeDownCast(this->Reader)->SetDistributeWithMPI(false);
124     this->Reader->UpdateInformation();
125     this->Reader->UpdateTimeStep(time);
126     vtkDataObject* outputReader = this->Reader->GetOutputDataObject(0);
127     vtkSmartPointer<vtkDataObject> outputLeaf = vtkSmartPointer<vtkDataObject>::Take(outputReader->NewInstance());
128     outputLeaf->DeepCopy(outputReader);
129     output->SetBlock(i, outputLeaf);
130   }
131
132   return 1;
133 }
134
135 //------------------------------------------------------------------------------
136 int vtkFileSeriesGroupReader::FillOutputPortInformation(
137   int vtkNotUsed(port), vtkInformation* info)
138 {
139   info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkMultiBlockDataSet");
140   return 1;
141 }
142
143 //-----------------------------------------------------------------------------
144 void vtkFileSeriesGroupReader::PrintSelf(ostream& os, vtkIndent indent)
145 {
146   this->Superclass::PrintSelf(os, indent);
147
148   os << indent << "MetaFileName: " << (this->_MetaFileName ? this->_MetaFileName : "(none)")
149      << endl;
150 }