VTK-m  2.2
TaskQueue.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_TaskQueue_h
11 #define vtk_m_filter_TaskQueue_h
12 
13 #include <queue>
14 
15 namespace vtkm
16 {
17 namespace filter
18 {
19 
20 template <typename T>
21 class TaskQueue
22 {
23 public:
24  TaskQueue() = default;
25 
26  //Add a task to the Queue.
27  void Push(T&& item)
28  {
29  std::unique_lock<std::mutex> lock(this->Lock);
30  this->Queue.push(item);
31  }
32 
33  bool HasTasks()
34  {
35  std::unique_lock<std::mutex> lock(this->Lock);
36  return !(this->Queue.empty());
37  }
38 
39  bool GetTask(T& item)
40  {
41  std::unique_lock<std::mutex> lock(this->Lock);
42  if (this->Queue.empty())
43  return false;
44 
45  item = this->Queue.front();
46  this->Queue.pop();
47  return true;
48  }
49 
50  T Pop()
51  {
52  T item;
53  std::unique_lock<std::mutex> lock(this->Lock);
54  if (!this->Queue.empty())
55  {
56  item = this->Queue.front();
57  this->Queue.pop();
58  }
59 
60  return item;
61  }
62 
63 protected:
65  {
66  std::unique_lock<std::mutex> lock(this->Lock);
67  return static_cast<vtkm::Id>(this->Queue.size());
68  }
69 
70 private:
71  std::mutex Lock;
72  std::queue<T> Queue;
73 
74  //don't want copies of this
75  TaskQueue(const TaskQueue& rhs) = delete;
76  TaskQueue& operator=(const TaskQueue& rhs) = delete;
77  TaskQueue(TaskQueue&& rhs) = delete;
78  TaskQueue& operator=(TaskQueue&& rhs) = delete;
79 };
80 
81 
82 class DataSetQueue : public TaskQueue<std::pair<vtkm::Id, vtkm::cont::DataSet>>
83 {
84 public:
86  {
87  vtkm::Id idx = 0;
88  for (auto ds : input)
89  this->Push(std::make_pair(idx++, std::move(ds)));
90  }
91 
93 
95  {
97  vtkm::Id num = this->Length();
98 
99  if (num > 0)
100  {
101  std::vector<vtkm::cont::DataSet> dataSets(static_cast<std::size_t>(num));
102 
103  //Insert them back in the same order.
104  std::pair<vtkm::Id, vtkm::cont::DataSet> task;
105  while (this->GetTask(task))
106  {
107  dataSets[static_cast<std::size_t>(task.first)] = std::move(task.second);
108  }
109 
110  pds.AppendPartitions(dataSets);
111  }
112 
113  return pds;
114  }
115 
116 private:
117 };
118 
119 }
120 }
121 
122 #endif
vtkm::filter::TaskQueue::Lock
std::mutex Lock
Definition: TaskQueue.h:71
vtkm::filter::TaskQueue::Length
vtkm::Id Length()
Definition: TaskQueue.h:64
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::filter::TaskQueue::TaskQueue
TaskQueue()=default
vtkm::filter::TaskQueue::Push
void Push(T &&item)
Definition: TaskQueue.h:27
vtkm::filter::TaskQueue::GetTask
bool GetTask(T &item)
Definition: TaskQueue.h:39
vtkm::filter::DataSetQueue::DataSetQueue
DataSetQueue(const vtkm::cont::PartitionedDataSet &input)
Definition: TaskQueue.h:85
vtkm::filter::DataSetQueue::Get
vtkm::cont::PartitionedDataSet Get()
Definition: TaskQueue.h:94
vtkm::filter::TaskQueue::HasTasks
bool HasTasks()
Definition: TaskQueue.h:33
vtkm::filter::DataSetQueue::DataSetQueue
DataSetQueue()
Definition: TaskQueue.h:92
vtkm::filter::TaskQueue::Pop
T Pop()
Definition: TaskQueue.h:50
vtkm::Id
vtkm::Int64 Id
Base type to use to index arrays.
Definition: Types.h:227
vtkm::filter::TaskQueue
Definition: TaskQueue.h:21
vtkm::filter::TaskQueue::Queue
std::queue< T > Queue
Definition: TaskQueue.h:72
vtkm::filter::DataSetQueue
Definition: TaskQueue.h:82
vtkm::filter::TaskQueue::operator=
TaskQueue & operator=(const TaskQueue &rhs)=delete
vtkm::cont::PartitionedDataSet::AppendPartitions
void AppendPartitions(const std::vector< vtkm::cont::DataSet > &partitions)
Append the DataSet vector partitions to the end of list of partitions.
vtkm::cont::PartitionedDataSet
Comprises a set of vtkm::cont::DataSet objects.
Definition: PartitionedDataSet.h:26