NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
work_queue.h
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of the NVIDIA CORPORATION nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #pragma once
29 
30 #include <nvbio/basic/types.h>
31 #include <nvbio/basic/numbers.h>
32 #include <thrust/device_vector.h>
33 
34 namespace nvbio {
35 namespace cuda {
36 
161 
171 
172 // default tag
173 struct InplaceQueueTag {};
174 
175 // a simple class to keep track of utilization stats
176 struct WorkQueueStats;
177 struct WorkQueueStatsView;
178 
179 // return a device-side view of a stats object
181 
188 };
189 
194 {
199 
203  WorkQueueStatsView(uint64* _active_lanes, uint64* _issued_warps, uint64* _iterations) :
204  active_lanes(_active_lanes), issued_warps(_issued_warps), iterations(_iterations) {}
205 
209  void sample(const WorkQueueStatsEvent type);
210 
214  void sample_iterations(const uint32 i);
215 
219  bool valid() const { return active_lanes != NULL; }
220 
224 };
225 
234 {
235  template <typename WorkStreamT, typename WorkUnitT>
237  void move(
238  const WorkStreamT& stream,
239  const uint2 src_slot, WorkUnitT* src_unit,
240  const uint2 dst_slot, WorkUnitT* dst_unit) const
241  {
242  // call WorkUnitT's copy operator and call it a day
243  *dst_unit = *src_unit;
244  }
245 };
246 
253 template <
254  typename PolicyTag,
255  typename WorkUnitT,
257 struct WorkQueue
258 {
259  typedef WorkUnitT WorkUnit;
260 
264 
272  void set_capacity(const uint32 capacity) {}
273 
276  template <typename WorkStream>
277  void consume(const WorkStream stream, WorkQueueStats* stats = NULL) { consume( stream, DefaultMover(), stats ); }
278 
281  template <typename WorkStream, typename WorkMover>
282  void consume(const WorkStream stream, const WorkMover mover, WorkQueueStats* stats = NULL);
283 };
284 
289 {
291 
294  WorkQueueStats() : counters(7,0) {}
295 
298  void clear() { thrust::fill( counters.begin(), counters.end(), uint64(0u) ); }
299 
303  {
304  return View(
305  thrust::raw_pointer_cast( &counters.front() ),
306  thrust::raw_pointer_cast( &counters.front() ) + 2u,
307  thrust::raw_pointer_cast( &counters.front() ) + 4u );
308  }
309 
312  float utilization(const WorkQueueStatsEvent type) const { return counters[2 + type] ? float(counters[0 + type])/float(counters[2 + type]*cuda::Arch::WARP_SIZE) : 1.0f; }
313 
316  float avg_iterations() const { return counters[6] ? float(counters[4])/float(counters[6]) : 0.0f; }
317 
320  float max_iterations() const { return float(counters[5]); }
321 
322 private:
323  thrust::device_vector<uint64> counters;
324 };
325 
328 inline WorkQueueStatsView view(WorkQueueStats* stats) { return stats ? stats->view() : WorkQueueStatsView(); }
329 
331 
332 } // namespace cuda
333 } // namespace nvbio
334