VTK-m  2.0
Filter.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_filter_Filter_h
11 #define vtk_m_filter_Filter_h
12 
13 #include <vtkm/cont/DataSet.h>
14 #include <vtkm/cont/Field.h>
15 #include <vtkm/cont/Invoker.h>
16 #include <vtkm/cont/Logging.h>
18 
20 #include <vtkm/filter/TaskQueue.h>
21 #include <vtkm/filter/vtkm_filter_core_export.h>
22 
23 namespace vtkm
24 {
25 namespace filter
26 {
218 class VTKM_FILTER_CORE_EXPORT Filter
219 {
220 public:
221  VTKM_CONT
222  virtual ~Filter();
223 
224  VTKM_CONT
225  virtual bool CanThread() const;
226 
227  VTKM_CONT
228  void SetThreadsPerCPU(vtkm::Id numThreads) { this->NumThreadsPerCPU = numThreads; }
229  VTKM_CONT
230  void SetThreadsPerGPU(vtkm::Id numThreads) { this->NumThreadsPerGPU = numThreads; }
231 
232  VTKM_CONT
233  vtkm::Id GetThreadsPerCPU() const { return this->NumThreadsPerCPU; }
234  VTKM_CONT
235  vtkm::Id GetThreadsPerGPU() const { return this->NumThreadsPerGPU; }
236 
237  VTKM_CONT
239  {
240  return this->CanThread() && this->RunFilterWithMultipleThreads;
241  }
242 
243  VTKM_CONT
245  {
246  if (this->CanThread())
247  this->RunFilterWithMultipleThreads = val;
248  else
249  {
250  std::string msg =
251  "Multi threaded filter not supported for " + std::string(typeid(*this).name());
253  }
254  }
255 
266  VTKM_CONT void SetFieldsToPass(const vtkm::filter::FieldSelection& fieldsToPass);
267  VTKM_CONT void SetFieldsToPass(vtkm::filter::FieldSelection&& fieldsToPass);
268 
269  VTKM_DEPRECATED(2.0)
270  VTKM_CONT void SetFieldsToPass(const vtkm::filter::FieldSelection& fieldsToPass,
271  vtkm::filter::FieldSelection::Mode mode);
272 
273  VTKM_CONT void SetFieldsToPass(
274  std::initializer_list<std::string> fields,
275  vtkm::filter::FieldSelection::Mode mode = vtkm::filter::FieldSelection::Mode::Select);
276 
277  VTKM_CONT void SetFieldsToPass(
278  std::initializer_list<std::pair<std::string, vtkm::cont::Field::Association>> fields,
279  vtkm::filter::FieldSelection::Mode mode = vtkm::filter::FieldSelection::Mode::Select);
280 
281 
282  VTKM_CONT void SetFieldsToPass(
283  const std::string& fieldname,
284  vtkm::cont::Field::Association association,
285  vtkm::filter::FieldSelection::Mode mode = vtkm::filter::FieldSelection::Mode::Select);
286 
287  VTKM_CONT void SetFieldsToPass(const std::string& fieldname,
288  vtkm::filter::FieldSelection::Mode mode)
289  {
290  this->SetFieldsToPass(fieldname, vtkm::cont::Field::Association::Any, mode);
291  }
292 
293  VTKM_CONT
294  const vtkm::filter::FieldSelection& GetFieldsToPass() const { return this->FieldsToPass; }
295  VTKM_CONT
296  vtkm::filter::FieldSelection& GetFieldsToPass() { return this->FieldsToPass; }
298 
310  VTKM_CONT void SetPassCoordinateSystems(bool flag) { this->PassCoordinateSystems = flag; }
311  VTKM_CONT bool GetPassCoordinateSystems() const { return this->PassCoordinateSystems; }
313 
318  VTKM_CONT vtkm::cont::DataSet Execute(const vtkm::cont::DataSet& input);
320 
327 
328  // FIXME: Is this actually materialize? Are there different kinds of Invoker?
332  void SetInvoker(vtkm::cont::Invoker inv) { this->Invoke = inv; }
333 
334 protected:
336 
347  VTKM_CONT vtkm::cont::DataSet CreateResult(const vtkm::cont::DataSet& inDataSet) const;
348 
349 
362  const vtkm::cont::PartitionedDataSet& input,
363  const vtkm::cont::PartitionedDataSet& resultPartitions) const;
364 
379  template <typename FieldMapper>
381  const vtkm::cont::PartitionedDataSet& input,
382  const vtkm::cont::PartitionedDataSet& resultPartitions,
383  FieldMapper&& fieldMapper) const
384  {
385  vtkm::cont::PartitionedDataSet output(resultPartitions.GetPartitions());
386  this->MapFieldsOntoOutput(input, this->GetFieldsToPass(), output, fieldMapper);
387  return output;
388  }
389 
408  template <typename FieldMapper>
410  const vtkm::cont::UnknownCellSet& resultCellSet,
411  FieldMapper&& fieldMapper) const
412  {
413  vtkm::cont::DataSet outDataSet;
414  outDataSet.SetCellSet(resultCellSet);
415  this->MapFieldsOntoOutput(inDataSet, this->GetFieldsToPass(), outDataSet, fieldMapper);
416  return outDataSet;
417  }
418 
440  template <typename FieldMapper>
442  const vtkm::cont::DataSet& inDataSet,
443  const vtkm::cont::UnknownCellSet& resultCellSet,
444  const vtkm::cont::CoordinateSystem& resultCoordSystem,
445  FieldMapper&& fieldMapper) const
446  {
447  vtkm::cont::DataSet outDataSet;
448  outDataSet.SetCellSet(resultCellSet);
449  vtkm::filter::FieldSelection fieldSelection = this->GetFieldsToPass();
450  if (this->GetPassCoordinateSystems() || fieldSelection.HasField(resultCoordSystem))
451  {
452  outDataSet.AddCoordinateSystem(resultCoordSystem);
453  fieldSelection.AddField(resultCoordSystem, vtkm::filter::FieldSelection::Mode::Exclude);
454  }
455  this->MapFieldsOntoOutput(inDataSet, fieldSelection, outDataSet, fieldMapper);
456  return outDataSet;
457  }
458 
459  template <typename FieldMapper>
461  const vtkm::cont::DataSet& inDataSet,
462  const vtkm::cont::UnknownCellSet& resultCellSet,
463  const std::string& coordsName,
464  const vtkm::cont::UnknownArrayHandle& coordsData,
465  FieldMapper&& fieldMapper) const
466  {
467  return this->CreateResultCoordinateSystem(
468  inDataSet,
469  resultCellSet,
470  vtkm::cont::CoordinateSystem{ coordsName, coordsData },
471  fieldMapper);
472  }
474 
475  VTKM_CONT virtual vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& inData) = 0;
476  VTKM_CONT virtual vtkm::cont::PartitionedDataSet DoExecutePartitions(
477  const vtkm::cont::PartitionedDataSet& inData);
478 
479 private:
480  template <typename FieldMapper>
482  const vtkm::filter::FieldSelection& fieldSelection,
483  vtkm::cont::DataSet& output,
484  FieldMapper&& fieldMapper) const
485  {
486  // Basic field mapping
487  for (vtkm::IdComponent cc = 0; cc < input.GetNumberOfFields(); ++cc)
488  {
489  auto field = input.GetField(cc);
490  if (fieldSelection.IsFieldSelected(field))
491  {
492  fieldMapper(output, field);
493  }
494  }
495 
496  // Check if the ghost levels have been copied. If so, set so on the output.
497  if (input.HasGhostCellField())
498  {
499  const std::string& ghostFieldName = input.GetGhostCellFieldName();
500  if (output.HasCellField(ghostFieldName) && (output.GetGhostCellFieldName() != ghostFieldName))
501  {
502  output.SetGhostCellFieldName(ghostFieldName);
503  }
504  }
505 
506  for (vtkm::IdComponent csIndex = 0; csIndex < input.GetNumberOfCoordinateSystems(); ++csIndex)
507  {
508  auto coords = input.GetCoordinateSystem(csIndex);
509  if (!output.HasCoordinateSystem(coords.GetName()))
510  {
511  if (!output.HasPointField(coords.GetName()) && this->GetPassCoordinateSystems())
512  {
513  fieldMapper(output, coords);
514  }
515  if (output.HasPointField(coords.GetName()))
516  {
517  output.AddCoordinateSystem(coords.GetName());
518  }
519  }
520  }
521  }
522 
523  template <typename FieldMapper>
525  const vtkm::filter::FieldSelection& fieldSelection,
527  FieldMapper&& fieldMapper) const
528  {
529  for (vtkm::IdComponent cc = 0; cc < input.GetNumberOfFields(); ++cc)
530  {
531  auto field = input.GetField(cc);
532  if (fieldSelection.IsFieldSelected(field))
533  {
534  fieldMapper(output, field);
535  }
536  }
537  }
538 
539  VTKM_CONT
540  virtual vtkm::Id DetermineNumberOfThreads(const vtkm::cont::PartitionedDataSet& input);
541 
542 
544  bool PassCoordinateSystems = true;
545  bool RunFilterWithMultipleThreads = false;
546  vtkm::Id NumThreadsPerCPU = 4;
547  vtkm::Id NumThreadsPerGPU = 8;
548 };
549 }
550 } // namespace vtkm::filter
551 
552 #endif
vtkm::cont::DataSet::HasPointField
VTKM_CONT bool HasPointField(const std::string &name) const
Definition: DataSet.h:101
vtkm::filter::Filter::CreateResultCoordinateSystem
VTKM_CONT vtkm::cont::DataSet CreateResultCoordinateSystem(const vtkm::cont::DataSet &inDataSet, const vtkm::cont::UnknownCellSet &resultCellSet, const vtkm::cont::CoordinateSystem &resultCoordSystem, FieldMapper &&fieldMapper) const
Create the output data set for DoExecute.
Definition: Filter.h:441
vtkm::filter::FieldSelection::Mode::All
@ All
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::cont::PartitionedDataSet::GetPartitions
const VTKM_CONT std::vector< vtkm::cont::DataSet > & GetPartitions() const
Get an STL vector of all DataSet objects stored in PartitionedDataSet.
vtkm::filter::FieldSelection::AddField
VTKM_CONT void AddField(const vtkm::cont::Field &inputField)
Definition: FieldSelection.h:116
vtkm::cont::PartitionedDataSet::GetField
const VTKM_CONT vtkm::cont::Field & GetField(vtkm::Id index) const
Definition: PartitionedDataSet.h:139
vtkm::filter::FieldSelection
A FieldSelection stores information about fields to map for input dataset to output when a filter is ...
Definition: FieldSelection.h:31
vtkm::filter::Filter::MapFieldsOntoOutput
VTKM_CONT void MapFieldsOntoOutput(const vtkm::cont::PartitionedDataSet &input, const vtkm::filter::FieldSelection &fieldSelection, vtkm::cont::PartitionedDataSet &output, FieldMapper &&fieldMapper) const
Definition: Filter.h:524
vtkm::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::filter::Filter::GetRunMultiThreadedFilter
VTKM_CONT bool GetRunMultiThreadedFilter() const
Definition: Filter.h:238
vtkm::cont::UnknownArrayHandle
An ArrayHandle of an unknown value type and storage.
Definition: UnknownArrayHandle.h:406
vtkm::filter::Filter::GetThreadsPerCPU
VTKM_CONT vtkm::Id GetThreadsPerCPU() const
Definition: Filter.h:233
vtkm::filter::Filter::Invoke
vtkm::cont::Invoker Invoke
Definition: Filter.h:335
vtkm::cont::DataSet
Definition: DataSet.h:34
vtkm::filter::Filter::GetFieldsToPass
VTKM_CONT vtkm::filter::FieldSelection & GetFieldsToPass()
Specify which fields get passed from input to output.
Definition: Filter.h:296
Invoker.h
vtkm::cont::UnknownCellSet
A CellSet of an unknown type.
Definition: UnknownCellSet.h:48
vtkm::filter::FieldSelection::HasField
VTKM_CONT bool HasField(const vtkm::cont::Field &inputField) const
Returns true if the input field has been added to this selection.
Definition: FieldSelection.h:161
vtkm::filter::Filter::CreateResult
VTKM_CONT vtkm::cont::DataSet CreateResult(const vtkm::cont::DataSet &inDataSet, const vtkm::cont::UnknownCellSet &resultCellSet, FieldMapper &&fieldMapper) const
Create the output data set for DoExecute.
Definition: Filter.h:409
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
vtkm::cont::CoordinateSystem
Definition: CoordinateSystem.h:25
vtkm::filter::Filter::GetThreadsPerGPU
VTKM_CONT vtkm::Id GetThreadsPerGPU() const
Definition: Filter.h:235
vtkm::cont::LogLevel::Info
@ Info
Information messages (detected hardware, etc) and temporary debugging output.
vtkm::cont::DataSet::HasCellField
VTKM_CONT bool HasCellField(const std::string &name) const
Definition: DataSet.h:89
vtkm::filter::Filter::CreateResultCoordinateSystem
VTKM_CONT vtkm::cont::DataSet CreateResultCoordinateSystem(const vtkm::cont::DataSet &inDataSet, const vtkm::cont::UnknownCellSet &resultCellSet, const std::string &coordsName, const vtkm::cont::UnknownArrayHandle &coordsData, FieldMapper &&fieldMapper) const
Create the output data set for DoExecute.
Definition: Filter.h:460
vtkm::filter::Filter::GetFieldsToPass
const VTKM_CONT vtkm::filter::FieldSelection & GetFieldsToPass() const
Specify which fields get passed from input to output.
Definition: Filter.h:294
FieldSelection.h
vtkm::cont::DataSet::GetCoordinateSystem
VTKM_CONT vtkm::cont::CoordinateSystem GetCoordinateSystem(vtkm::Id index=0) const
vtkm::cont::DataSet::GetNumberOfCoordinateSystems
VTKM_CONT vtkm::IdComponent GetNumberOfCoordinateSystems() const
Definition: DataSet.h:371
vtkm::cont::Invoker
Allows launching any worklet without a dispatcher.
Definition: Invoker.h:41
vtkm::filter::Filter
base class for all filters.
Definition: Filter.h:218
vtkm::cont::Field::Association::Any
@ Any
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
TaskQueue.h
vtkm::cont::DataSet::HasCoordinateSystem
VTKM_CONT bool HasCoordinateSystem(const std::string &name) const
Definition: DataSet.h:333
VTKM_LOG_S
#define VTKM_LOG_S(level,...)
Writes a message using stream syntax to the indicated log level.
Definition: Logging.h:261
vtkm::filter::FieldSelection::Mode::Exclude
@ Exclude
vtkm::cont::DataSet::SetGhostCellFieldName
VTKM_CONT void SetGhostCellFieldName(const std::string &name)
Sets the name of the field to use for cell ghost levels.
vtkm::filter::Filter::GetPassCoordinateSystems
VTKM_CONT bool GetPassCoordinateSystems() const
Specify whether to always pass coordinate systems.
Definition: Filter.h:311
vtkm::filter::Filter::SetRunMultiThreadedFilter
VTKM_CONT void SetRunMultiThreadedFilter(bool val)
Definition: Filter.h:244
vtkm::filter::Filter::MapFieldsOntoOutput
VTKM_CONT void MapFieldsOntoOutput(const vtkm::cont::DataSet &input, const vtkm::filter::FieldSelection &fieldSelection, vtkm::cont::DataSet &output, FieldMapper &&fieldMapper) const
Definition: Filter.h:481
PartitionedDataSet.h
Field.h
vtkm::filter::FieldSelection::IsFieldSelected
VTKM_CONT bool IsFieldSelected(const vtkm::cont::Field &inputField) const
Returns true if the input field should be mapped to the output dataset.
Definition: FieldSelection.h:104
vtkm::cont::DataSet::GetField
const VTKM_CONT vtkm::cont::Field & GetField(vtkm::Id index) const
Retrieves a field by index.
Definition: DataSet.h:75
vtkm::cont::DataSet::AddCoordinateSystem
VTKM_CONT vtkm::IdComponent AddCoordinateSystem(const vtkm::cont::CoordinateSystem &cs)
Adds the given CoordinateSystem to the DataSet.
vtkm::cont::DataSet::SetCellSet
VTKM_CONT void SetCellSet(const CellSetType &cellSet)
Definition: DataSet.h:355
vtkm::cont::DataSet::GetNumberOfFields
VTKM_CONT vtkm::IdComponent GetNumberOfFields() const
Definition: DataSet.h:368
Logging.h
Logging utilities.
vtkm::filter::Filter::SetPassCoordinateSystems
VTKM_CONT void SetPassCoordinateSystems(bool flag)
Specify whether to always pass coordinate systems.
Definition: Filter.h:310
vtkm::cont::PartitionedDataSet::GetNumberOfFields
VTKM_CONT vtkm::IdComponent GetNumberOfFields() const
Definition: PartitionedDataSet.h:91
vtkm::filter::Filter::CreateResult
VTKM_CONT vtkm::cont::PartitionedDataSet CreateResult(const vtkm::cont::PartitionedDataSet &input, const vtkm::cont::PartitionedDataSet &resultPartitions, FieldMapper &&fieldMapper) const
Create the output data set for DoExecute.
Definition: Filter.h:380
vtkm::filter::Filter::SetThreadsPerGPU
VTKM_CONT void SetThreadsPerGPU(vtkm::Id numThreads)
Definition: Filter.h:230
VTKM_DEPRECATED
#define VTKM_DEPRECATED(...)
Definition: Deprecated.h:145
vtkm::cont::DataSet::HasGhostCellField
VTKM_CONT bool HasGhostCellField() const
vtkm::filter::Filter::SetInvoker
void SetInvoker(vtkm::cont::Invoker inv)
Specify the vtkm::cont::Invoker to be used to execute worklets by this filter instance.
Definition: Filter.h:332
DataSet.h
vtkm::cont::DataSet::GetGhostCellFieldName
const VTKM_CONT std::string & GetGhostCellFieldName() const
vtkm::filter::Filter::SetThreadsPerCPU
VTKM_CONT void SetThreadsPerCPU(vtkm::Id numThreads)
Definition: Filter.h:228
vtkm::cont::PartitionedDataSet
Definition: PartitionedDataSet.h:25