Algorithm.h 39.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//============================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt for details.
//  This software is distributed WITHOUT ANY WARRANTY; without even
//  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//  PURPOSE.  See the above copyright notice for more information.
//
//  Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
//  Copyright 2014 UT-Battelle, LLC.
//  Copyright 2014 Los Alamos National Security.
//
//  Under the terms of Contract DE-NA0003525 with NTESS,
//  the U.S. Government retains certain rights in this software.
//
//  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
//  Laboratory (LANL), the U.S. Government retains certain rights in
//  this software.
//============================================================================
#ifndef vtk_m_cont_Algorithm_h
#define vtk_m_cont_Algorithm_h

#include <vtkm/Types.h>

25
#include <vtkm/cont/ExecutionObjectBase.h>
26 27 28 29 30 31 32 33 34
#include <vtkm/cont/TryExecute.h>
#include <vtkm/cont/internal/ArrayManagerExecution.h>
#include <vtkm/cont/internal/DeviceAdapterTag.h>

namespace vtkm
{
namespace cont
{

35
namespace detail
36
{
37 38 39
template <typename Device, typename T>
inline auto DoPrepareArgForExec(T&& object, std::true_type)
  -> decltype(std::declval<T>().PrepareForExecution(Device()))
40
{
41
  VTKM_IS_EXECUTION_OBJECT(T);
42 43
  return object.PrepareForExecution(Device{});
}
44

45 46
template <typename Device, typename T>
inline T&& DoPrepareArgForExec(T&& object, std::false_type)
47
{
48 49
  static_assert(!vtkm::cont::internal::IsExecutionObjectBase<T>::value,
                "Internal error: failed to detect execution object.");
50 51
  return std::forward<T>(object);
}
52 53

template <typename Device, typename T>
54 55 56
auto PrepareArgForExec(T&& object)
  -> decltype(DoPrepareArgForExec<Device>(std::forward<T>(object),
                                          vtkm::cont::internal::IsExecutionObjectBase<T>{}))
57
{
58 59
  return DoPrepareArgForExec<Device>(std::forward<T>(object),
                                     vtkm::cont::internal::IsExecutionObjectBase<T>{});
60 61
}

62 63
struct CopyFunctor
{
64 65
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args) const
66 67
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
68 69
    vtkm::cont::DeviceAdapterAlgorithm<Device>::Copy(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
70 71 72 73 74 75 76
    return true;
  }
};

struct CopyIfFunctor
{

77 78
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args) const
79 80
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
81 82
    vtkm::cont::DeviceAdapterAlgorithm<Device>::CopyIf(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
83 84 85 86 87 88 89 90
    return true;
  }
};

struct CopySubRangeFunctor
{
  bool valid;

91 92 93 94 95
  CopySubRangeFunctor()
    : valid(false)
  {
  }

96 97
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args)
98 99
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
100 101
    valid = vtkm::cont::DeviceAdapterAlgorithm<Device>::CopySubRange(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
102 103 104 105 106 107 108
    return true;
  }
};

struct LowerBoundsFunctor
{

109 110
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args) const
111 112
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
113 114
    vtkm::cont::DeviceAdapterAlgorithm<Device>::LowerBounds(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
115 116 117 118
    return true;
  }
};

119
template <typename U>
120 121 122
struct ReduceFunctor
{
  U result;
123 124

  ReduceFunctor()
125
    : result(vtkm::TypeTraits<U>::ZeroInitialization())
126 127 128
  {
  }

129 130
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args)
131 132
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
133 134
    result = vtkm::cont::DeviceAdapterAlgorithm<Device>::Reduce(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
135 136 137 138 139 140
    return true;
  }
};

struct ReduceByKeyFunctor
{
141 142
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args) const
143 144
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
145 146
    vtkm::cont::DeviceAdapterAlgorithm<Device>::ReduceByKey(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
147 148 149 150
    return true;
  }
};

151
template <typename U>
152
struct ScanInclusiveResultFunctor
153
{
154 155
  U result;

156
  ScanInclusiveResultFunctor()
157
    : result(vtkm::TypeTraits<U>::ZeroInitialization())
158 159 160
  {
  }

161 162
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args)
163 164
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
165 166
    result = vtkm::cont::DeviceAdapterAlgorithm<Device>::ScanInclusive(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
    return true;
  }
};

template <typename T>
struct StreamingScanExclusiveFunctor
{
  T result;
  StreamingScanExclusiveFunctor()
    : result(T(0))
  {
  }

  template <typename Device, class CIn, class COut>
  VTKM_CONT bool operator()(Device,
                            const vtkm::Id numBlocks,
                            const vtkm::cont::ArrayHandle<T, CIn>& input,
                            vtkm::cont::ArrayHandle<T, COut>& output)
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
    result =
      vtkm::cont::DeviceAdapterAlgorithm<Device>::StreamingScanExclusive(numBlocks, input, output);
    return true;
  }
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238

  template <typename Device, class CIn, class COut, class BinaryFunctor>
  VTKM_CONT bool operator()(Device,
                            const vtkm::Id numBlocks,
                            const vtkm::cont::ArrayHandle<T, CIn>& input,
                            vtkm::cont::ArrayHandle<T, COut>& output,
                            BinaryFunctor binary_functor,
                            const T& initialValue)
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
    result = vtkm::cont::DeviceAdapterAlgorithm<Device>::StreamingScanExclusive(
      numBlocks, input, output, binary_functor, initialValue);
    return true;
  }
};

template <typename U>
struct StreamingReduceFunctor
{
  U result;
  StreamingReduceFunctor()
    : result(U(0))
  {
  }
  template <typename Device, typename T, class CIn>
  VTKM_CONT bool operator()(Device,
                            const vtkm::Id numBlocks,
                            const vtkm::cont::ArrayHandle<T, CIn>& input,
                            U initialValue)
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
    result =
      vtkm::cont::DeviceAdapterAlgorithm<Device>::StreamingReduce(numBlocks, input, initialValue);
    return true;
  }

  template <typename Device, typename T, class CIn, class BinaryFunctor>
  VTKM_CONT bool operator()(Device,
                            const vtkm::Id numBlocks,
                            const vtkm::cont::ArrayHandle<T, CIn>& input,
                            U InitialValue,
                            BinaryFunctor binaryFunctor)
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
    result = vtkm::cont::DeviceAdapterAlgorithm<Device>::StreamingReduce(
      numBlocks, input, InitialValue, binaryFunctor);
    return true;
  }
239 240 241 242
};

struct ScanInclusiveByKeyFunctor
{
243
  ScanInclusiveByKeyFunctor() {}
244

245 246
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args) const
247 248
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
249 250
    vtkm::cont::DeviceAdapterAlgorithm<Device>::ScanInclusiveByKey(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
251 252 253 254 255
    return true;
  }
};

template <typename T>
256
struct ScanExclusiveFunctor
257 258 259
{
  T result;

260 261 262 263 264
  ScanExclusiveFunctor()
    : result(T())
  {
  }

265 266
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args)
267 268
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
269 270
    result = vtkm::cont::DeviceAdapterAlgorithm<Device>::ScanExclusive(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
271 272 273 274 275 276
    return true;
  }
};

struct ScanExclusiveByKeyFunctor
{
277 278 279 280
  ScanExclusiveByKeyFunctor() {}

  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args) const
281 282
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
283 284
    vtkm::cont::DeviceAdapterAlgorithm<Device>::ScanExclusiveByKey(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
285 286 287 288 289 290
    return true;
  }
};

struct ScheduleFunctor
{
291 292
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args)
293 294
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
295 296
    vtkm::cont::DeviceAdapterAlgorithm<Device>::Schedule(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
297 298 299 300 301 302
    return true;
  }
};

struct SortFunctor
{
303 304
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args) const
305 306
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
307 308
    vtkm::cont::DeviceAdapterAlgorithm<Device>::Sort(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
309 310 311 312 313 314
    return true;
  }
};

struct SortByKeyFunctor
{
315 316
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args) const
317 318
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
319 320
    vtkm::cont::DeviceAdapterAlgorithm<Device>::SortByKey(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
    return true;
  }
};

struct SynchronizeFunctor
{
  template <typename Device>
  VTKM_CONT bool operator()(Device)
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
    vtkm::cont::DeviceAdapterAlgorithm<Device>::Synchronize();
    return true;
  }
};

336 337 338 339 340 341
struct TransformFunctor
{
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args) const
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
342 343
    vtkm::cont::DeviceAdapterAlgorithm<Device>::Transform(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
344 345 346 347
    return true;
  }
};

348 349
struct UniqueFunctor
{
350 351
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args) const
352 353
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
354 355
    vtkm::cont::DeviceAdapterAlgorithm<Device>::Unique(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
356 357 358 359 360 361
    return true;
  }
};

struct UpperBoundsFunctor
{
362 363
  template <typename Device, typename... Args>
  VTKM_CONT bool operator()(Device, Args&&... args) const
364 365
  {
    VTKM_IS_DEVICE_ADAPTER_TAG(Device);
366 367
    vtkm::cont::DeviceAdapterAlgorithm<Device>::UpperBounds(
      PrepareArgForExec<Device>(std::forward<Args>(args))...);
368 369 370
    return true;
  }
};
371
} // namespace detail
372 373 374 375

struct Algorithm
{

376 377 378 379 380 381 382
  template <typename T, typename U, class CIn, class COut>
  VTKM_CONT static void Copy(vtkm::cont::DeviceAdapterId devId,
                             const vtkm::cont::ArrayHandle<T, CIn>& input,
                             vtkm::cont::ArrayHandle<U, COut>& output)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::CopyFunctor(), input, output);
  }
383 384 385 386
  template <typename T, typename U, class CIn, class COut>
  VTKM_CONT static void Copy(const vtkm::cont::ArrayHandle<T, CIn>& input,
                             vtkm::cont::ArrayHandle<U, COut>& output)
  {
387
    Copy(vtkm::cont::DeviceAdapterTagAny(), input, output);
388 389
  }

390 391 392 393 394 395 396 397 398

  template <typename T, typename U, class CIn, class CStencil, class COut>
  VTKM_CONT static void CopyIf(vtkm::cont::DeviceAdapterId devId,
                               const vtkm::cont::ArrayHandle<T, CIn>& input,
                               const vtkm::cont::ArrayHandle<U, CStencil>& stencil,
                               vtkm::cont::ArrayHandle<T, COut>& output)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::CopyIfFunctor(), input, stencil, output);
  }
399 400 401 402 403
  template <typename T, typename U, class CIn, class CStencil, class COut>
  VTKM_CONT static void CopyIf(const vtkm::cont::ArrayHandle<T, CIn>& input,
                               const vtkm::cont::ArrayHandle<U, CStencil>& stencil,
                               vtkm::cont::ArrayHandle<T, COut>& output)
  {
404
    CopyIf(vtkm::cont::DeviceAdapterTagAny(), input, stencil, output);
405 406
  }

407 408 409 410 411 412 413 414 415 416 417

  template <typename T, typename U, class CIn, class CStencil, class COut, class UnaryPredicate>
  VTKM_CONT static void CopyIf(vtkm::cont::DeviceAdapterId devId,
                               const vtkm::cont::ArrayHandle<T, CIn>& input,
                               const vtkm::cont::ArrayHandle<U, CStencil>& stencil,
                               vtkm::cont::ArrayHandle<T, COut>& output,
                               UnaryPredicate unary_predicate)
  {
    vtkm::cont::TryExecuteOnDevice(
      devId, detail::CopyIfFunctor(), input, stencil, output, unary_predicate);
  }
418 419 420 421 422 423
  template <typename T, typename U, class CIn, class CStencil, class COut, class UnaryPredicate>
  VTKM_CONT static void CopyIf(const vtkm::cont::ArrayHandle<T, CIn>& input,
                               const vtkm::cont::ArrayHandle<U, CStencil>& stencil,
                               vtkm::cont::ArrayHandle<T, COut>& output,
                               UnaryPredicate unary_predicate)
  {
424
    CopyIf(vtkm::cont::DeviceAdapterTagAny(), input, stencil, output, unary_predicate);
425 426
  }

427

428
  template <typename T, typename U, class CIn, class COut>
429 430
  VTKM_CONT static bool CopySubRange(vtkm::cont::DeviceAdapterId devId,
                                     const vtkm::cont::ArrayHandle<T, CIn>& input,
431 432 433 434 435
                                     vtkm::Id inputStartIndex,
                                     vtkm::Id numberOfElementsToCopy,
                                     vtkm::cont::ArrayHandle<U, COut>& output,
                                     vtkm::Id outputIndex = 0)
  {
436
    detail::CopySubRangeFunctor functor;
437 438
    vtkm::cont::TryExecuteOnDevice(
      devId, functor, input, inputStartIndex, numberOfElementsToCopy, output, outputIndex);
439 440
    return functor.valid;
  }
441 442 443 444 445 446 447
  template <typename T, typename U, class CIn, class COut>
  VTKM_CONT static bool CopySubRange(const vtkm::cont::ArrayHandle<T, CIn>& input,
                                     vtkm::Id inputStartIndex,
                                     vtkm::Id numberOfElementsToCopy,
                                     vtkm::cont::ArrayHandle<U, COut>& output,
                                     vtkm::Id outputIndex = 0)
  {
448
    return CopySubRange(vtkm::cont::DeviceAdapterTagAny(),
449 450 451 452 453 454 455
                        input,
                        inputStartIndex,
                        numberOfElementsToCopy,
                        output,
                        outputIndex);
  }

456

457 458 459 460 461 462 463 464
  template <typename T, class CIn, class CVal, class COut>
  VTKM_CONT static void LowerBounds(vtkm::cont::DeviceAdapterId devId,
                                    const vtkm::cont::ArrayHandle<T, CIn>& input,
                                    const vtkm::cont::ArrayHandle<T, CVal>& values,
                                    vtkm::cont::ArrayHandle<vtkm::Id, COut>& output)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::LowerBoundsFunctor(), input, values, output);
  }
465 466 467 468 469
  template <typename T, class CIn, class CVal, class COut>
  VTKM_CONT static void LowerBounds(const vtkm::cont::ArrayHandle<T, CIn>& input,
                                    const vtkm::cont::ArrayHandle<T, CVal>& values,
                                    vtkm::cont::ArrayHandle<vtkm::Id, COut>& output)
  {
470
    LowerBounds(vtkm::cont::DeviceAdapterTagAny(), input, values, output);
471 472
  }

473 474 475 476 477 478 479 480 481 482 483

  template <typename T, class CIn, class CVal, class COut, class BinaryCompare>
  VTKM_CONT static void LowerBounds(vtkm::cont::DeviceAdapterId devId,
                                    const vtkm::cont::ArrayHandle<T, CIn>& input,
                                    const vtkm::cont::ArrayHandle<T, CVal>& values,
                                    vtkm::cont::ArrayHandle<vtkm::Id, COut>& output,
                                    BinaryCompare binary_compare)
  {
    vtkm::cont::TryExecuteOnDevice(
      devId, detail::LowerBoundsFunctor(), input, values, output, binary_compare);
  }
484 485 486 487 488 489
  template <typename T, class CIn, class CVal, class COut, class BinaryCompare>
  VTKM_CONT static void LowerBounds(const vtkm::cont::ArrayHandle<T, CIn>& input,
                                    const vtkm::cont::ArrayHandle<T, CVal>& values,
                                    vtkm::cont::ArrayHandle<vtkm::Id, COut>& output,
                                    BinaryCompare binary_compare)
  {
490
    LowerBounds(vtkm::cont::DeviceAdapterTagAny(), input, values, output, binary_compare);
491 492
  }

493 494 495 496 497 498 499 500

  template <class CIn, class COut>
  VTKM_CONT static void LowerBounds(vtkm::cont::DeviceAdapterId devId,
                                    const vtkm::cont::ArrayHandle<vtkm::Id, CIn>& input,
                                    vtkm::cont::ArrayHandle<vtkm::Id, COut>& values_output)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::LowerBoundsFunctor(), input, values_output);
  }
501 502 503 504
  template <class CIn, class COut>
  VTKM_CONT static void LowerBounds(const vtkm::cont::ArrayHandle<vtkm::Id, CIn>& input,
                                    vtkm::cont::ArrayHandle<vtkm::Id, COut>& values_output)
  {
505
    LowerBounds(vtkm::cont::DeviceAdapterTagAny(), input, values_output);
506 507
  }

508

509
  template <typename T, typename U, class CIn>
510 511 512
  VTKM_CONT static U Reduce(vtkm::cont::DeviceAdapterId devId,
                            const vtkm::cont::ArrayHandle<T, CIn>& input,
                            U initialValue)
513
  {
514
    detail::ReduceFunctor<U> functor;
515
    vtkm::cont::TryExecuteOnDevice(devId, functor, input, initialValue);
516
    return functor.result;
517
  }
518 519 520
  template <typename T, typename U, class CIn>
  VTKM_CONT static U Reduce(const vtkm::cont::ArrayHandle<T, CIn>& input, U initialValue)
  {
521
    return Reduce(vtkm::cont::DeviceAdapterTagAny(), input, initialValue);
522 523
  }

524 525

  template <typename T, typename U, class CIn, class BinaryFunctor>
526 527
  VTKM_CONT static U Reduce(vtkm::cont::DeviceAdapterId devId,
                            const vtkm::cont::ArrayHandle<T, CIn>& input,
528 529 530
                            U initialValue,
                            BinaryFunctor binary_functor)
  {
531
    detail::ReduceFunctor<U> functor;
532
    vtkm::cont::TryExecuteOnDevice(devId, functor, input, initialValue, binary_functor);
533 534
    return functor.result;
  }
535 536 537 538 539
  template <typename T, typename U, class CIn, class BinaryFunctor>
  VTKM_CONT static U Reduce(const vtkm::cont::ArrayHandle<T, CIn>& input,
                            U initialValue,
                            BinaryFunctor binary_functor)
  {
540
    return Reduce(vtkm::cont::DeviceAdapterTagAny(), input, initialValue, binary_functor);
541 542
  }

543

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
  template <typename T,
            typename U,
            class CKeyIn,
            class CValIn,
            class CKeyOut,
            class CValOut,
            class BinaryFunctor>
  VTKM_CONT static void ReduceByKey(vtkm::cont::DeviceAdapterId devId,
                                    const vtkm::cont::ArrayHandle<T, CKeyIn>& keys,
                                    const vtkm::cont::ArrayHandle<U, CValIn>& values,
                                    vtkm::cont::ArrayHandle<T, CKeyOut>& keys_output,
                                    vtkm::cont::ArrayHandle<U, CValOut>& values_output,
                                    BinaryFunctor binary_functor)
  {
    vtkm::cont::TryExecuteOnDevice(devId,
                                   detail::ReduceByKeyFunctor(),
                                   keys,
                                   values,
                                   keys_output,
                                   values_output,
                                   binary_functor);
  }
566 567 568 569 570 571 572 573 574 575 576 577 578
  template <typename T,
            typename U,
            class CKeyIn,
            class CValIn,
            class CKeyOut,
            class CValOut,
            class BinaryFunctor>
  VTKM_CONT static void ReduceByKey(const vtkm::cont::ArrayHandle<T, CKeyIn>& keys,
                                    const vtkm::cont::ArrayHandle<U, CValIn>& values,
                                    vtkm::cont::ArrayHandle<T, CKeyOut>& keys_output,
                                    vtkm::cont::ArrayHandle<U, CValOut>& values_output,
                                    BinaryFunctor binary_functor)
  {
579
    ReduceByKey(
580
      vtkm::cont::DeviceAdapterTagAny(), keys, values, keys_output, values_output, binary_functor);
581 582
  }

583

584
  template <typename T, class CIn, class COut>
585 586
  VTKM_CONT static T ScanInclusive(vtkm::cont::DeviceAdapterId devId,
                                   const vtkm::cont::ArrayHandle<T, CIn>& input,
587 588
                                   vtkm::cont::ArrayHandle<T, COut>& output)
  {
589
    detail::ScanInclusiveResultFunctor<T> functor;
590
    vtkm::cont::TryExecuteOnDevice(devId, functor, input, output);
591 592
    return functor.result;
  }
593 594 595 596
  template <typename T, class CIn, class COut>
  VTKM_CONT static T ScanInclusive(const vtkm::cont::ArrayHandle<T, CIn>& input,
                                   vtkm::cont::ArrayHandle<T, COut>& output)
  {
597
    return ScanInclusive(vtkm::cont::DeviceAdapterTagAny(), input, output);
598 599
  }

600 601

  template <typename T, class CIn, class COut>
602 603
  VTKM_CONT static T StreamingScanExclusive(vtkm::cont::DeviceAdapterId devId,
                                            const vtkm::Id numBlocks,
604 605 606
                                            const vtkm::cont::ArrayHandle<T, CIn>& input,
                                            vtkm::cont::ArrayHandle<T, COut>& output)
  {
607
    detail::StreamingScanExclusiveFunctor<T> functor;
608
    vtkm::cont::TryExecuteOnDevice(devId, functor, numBlocks, input, output);
609 610
    return functor.result;
  }
611

612 613 614 615 616
  template <typename T, class CIn, class COut>
  VTKM_CONT static T StreamingScanExclusive(const vtkm::Id numBlocks,
                                            const vtkm::cont::ArrayHandle<T, CIn>& input,
                                            vtkm::cont::ArrayHandle<T, COut>& output)
  {
617
    return StreamingScanExclusive(vtkm::cont::DeviceAdapterTagAny(), numBlocks, input, output);
618 619
  }

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
  template <typename T, class CIn, class COut, class BinaryFunctor>
  VTKM_CONT static T StreamingScanExclusive(const vtkm::Id numBlocks,
                                            const vtkm::cont::ArrayHandle<T, CIn>& input,
                                            vtkm::cont::ArrayHandle<T, COut>& output,
                                            BinaryFunctor binary_functor,
                                            const T& initialValue)
  {
    detail::StreamingScanExclusiveFunctor<T> functor;
    vtkm::cont::TryExecuteOnDevice(vtkm::cont::DeviceAdapterTagAny(),
                                   functor,
                                   numBlocks,
                                   input,
                                   output,
                                   binary_functor,
                                   initialValue);
    return functor.result;
  }

  template <typename T, typename U, class CIn>
  VTKM_CONT static U StreamingReduce(const vtkm::Id numBlocks,
                                     const vtkm::cont::ArrayHandle<T, CIn>& input,
                                     U initialValue)
  {
    detail::StreamingReduceFunctor<U> functor;
    vtkm::cont::TryExecuteOnDevice(
      vtkm::cont::DeviceAdapterTagAny(), functor, numBlocks, input, initialValue);
    return functor.result;
  }

  template <typename T, typename U, class CIn, class BinaryFunctor>
  VTKM_CONT static U StreamingReduce(const vtkm::Id numBlocks,
                                     const vtkm::cont::ArrayHandle<T, CIn>& input,
                                     U initialValue,
                                     BinaryFunctor binaryFunctor)
  {
    detail::StreamingReduceFunctor<U> functor;
    vtkm::cont::TryExecuteOnDevice(
      vtkm::cont::DeviceAdapterTagAny(), functor, numBlocks, input, initialValue, binaryFunctor);
    return functor.result;
  }
660 661

  template <typename T, class CIn, class COut, class BinaryFunctor>
662 663
  VTKM_CONT static T ScanInclusive(vtkm::cont::DeviceAdapterId devId,
                                   const vtkm::cont::ArrayHandle<T, CIn>& input,
664 665 666
                                   vtkm::cont::ArrayHandle<T, COut>& output,
                                   BinaryFunctor binary_functor)
  {
667
    detail::ScanInclusiveResultFunctor<T> functor;
668
    vtkm::cont::TryExecuteOnDevice(devId, functor, input, output, binary_functor);
669 670
    return functor.result;
  }
671 672 673 674 675
  template <typename T, class CIn, class COut, class BinaryFunctor>
  VTKM_CONT static T ScanInclusive(const vtkm::cont::ArrayHandle<T, CIn>& input,
                                   vtkm::cont::ArrayHandle<T, COut>& output,
                                   BinaryFunctor binary_functor)
  {
676
    return ScanInclusive(vtkm::cont::DeviceAdapterTagAny(), input, output, binary_functor);
677 678
  }

679

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
  template <typename T,
            typename U,
            typename KIn,
            typename VIn,
            typename VOut,
            typename BinaryFunctor>
  VTKM_CONT static void ScanInclusiveByKey(vtkm::cont::DeviceAdapterId devId,
                                           const vtkm::cont::ArrayHandle<T, KIn>& keys,
                                           const vtkm::cont::ArrayHandle<U, VIn>& values,
                                           vtkm::cont::ArrayHandle<U, VOut>& values_output,
                                           BinaryFunctor binary_functor)
  {
    vtkm::cont::TryExecuteOnDevice(
      devId, detail::ScanInclusiveByKeyFunctor(), keys, values, values_output, binary_functor);
  }
695 696 697 698 699 700 701 702 703 704 705
  template <typename T,
            typename U,
            typename KIn,
            typename VIn,
            typename VOut,
            typename BinaryFunctor>
  VTKM_CONT static void ScanInclusiveByKey(const vtkm::cont::ArrayHandle<T, KIn>& keys,
                                           const vtkm::cont::ArrayHandle<U, VIn>& values,
                                           vtkm::cont::ArrayHandle<U, VOut>& values_output,
                                           BinaryFunctor binary_functor)
  {
706
    ScanInclusiveByKey(
707
      vtkm::cont::DeviceAdapterTagAny(), keys, values, values_output, binary_functor);
708 709
  }

710 711 712 713 714 715 716 717 718 719

  template <typename T, typename U, typename KIn, typename VIn, typename VOut>
  VTKM_CONT static void ScanInclusiveByKey(vtkm::cont::DeviceAdapterId devId,
                                           const vtkm::cont::ArrayHandle<T, KIn>& keys,
                                           const vtkm::cont::ArrayHandle<U, VIn>& values,
                                           vtkm::cont::ArrayHandle<U, VOut>& values_output)
  {
    vtkm::cont::TryExecuteOnDevice(
      devId, detail::ScanInclusiveByKeyFunctor(), keys, values, values_output);
  }
720 721 722 723 724
  template <typename T, typename U, typename KIn, typename VIn, typename VOut>
  VTKM_CONT static void ScanInclusiveByKey(const vtkm::cont::ArrayHandle<T, KIn>& keys,
                                           const vtkm::cont::ArrayHandle<U, VIn>& values,
                                           vtkm::cont::ArrayHandle<U, VOut>& values_output)
  {
725
    ScanInclusiveByKey(vtkm::cont::DeviceAdapterTagAny(), keys, values, values_output);
726 727
  }

728

729
  template <typename T, class CIn, class COut>
730 731
  VTKM_CONT static T ScanExclusive(vtkm::cont::DeviceAdapterId devId,
                                   const vtkm::cont::ArrayHandle<T, CIn>& input,
732 733
                                   vtkm::cont::ArrayHandle<T, COut>& output)
  {
734
    detail::ScanExclusiveFunctor<T> functor;
735
    vtkm::cont::TryExecuteOnDevice(devId, functor, input, output);
736
    return functor.result;
737
  }
738 739 740 741
  template <typename T, class CIn, class COut>
  VTKM_CONT static T ScanExclusive(const vtkm::cont::ArrayHandle<T, CIn>& input,
                                   vtkm::cont::ArrayHandle<T, COut>& output)
  {
742
    return ScanExclusive(vtkm::cont::DeviceAdapterTagAny(), input, output);
743 744
  }

745 746

  template <typename T, class CIn, class COut, class BinaryFunctor>
747 748
  VTKM_CONT static T ScanExclusive(vtkm::cont::DeviceAdapterId devId,
                                   const vtkm::cont::ArrayHandle<T, CIn>& input,
749 750 751 752
                                   vtkm::cont::ArrayHandle<T, COut>& output,
                                   BinaryFunctor binaryFunctor,
                                   const T& initialValue)
  {
753
    detail::ScanExclusiveFunctor<T> functor;
754
    vtkm::cont::TryExecuteOnDevice(devId, functor, input, output, binaryFunctor, initialValue);
755 756
    return functor.result;
  }
757 758 759 760 761 762 763
  template <typename T, class CIn, class COut, class BinaryFunctor>
  VTKM_CONT static T ScanExclusive(const vtkm::cont::ArrayHandle<T, CIn>& input,
                                   vtkm::cont::ArrayHandle<T, COut>& output,
                                   BinaryFunctor binaryFunctor,
                                   const T& initialValue)
  {
    return ScanExclusive(
764
      vtkm::cont::DeviceAdapterTagAny(), input, output, binaryFunctor, initialValue);
765
  }
766

767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783

  template <typename T, typename U, typename KIn, typename VIn, typename VOut, class BinaryFunctor>
  VTKM_CONT static void ScanExclusiveByKey(vtkm::cont::DeviceAdapterId devId,
                                           const vtkm::cont::ArrayHandle<T, KIn>& keys,
                                           const vtkm::cont::ArrayHandle<U, VIn>& values,
                                           vtkm::cont::ArrayHandle<U, VOut>& output,
                                           const U& initialValue,
                                           BinaryFunctor binaryFunctor)
  {
    vtkm::cont::TryExecuteOnDevice(devId,
                                   detail::ScanExclusiveByKeyFunctor(),
                                   keys,
                                   values,
                                   output,
                                   initialValue,
                                   binaryFunctor);
  }
784 785 786 787 788 789 790
  template <typename T, typename U, typename KIn, typename VIn, typename VOut, class BinaryFunctor>
  VTKM_CONT static void ScanExclusiveByKey(const vtkm::cont::ArrayHandle<T, KIn>& keys,
                                           const vtkm::cont::ArrayHandle<U, VIn>& values,
                                           vtkm::cont::ArrayHandle<U, VOut>& output,
                                           const U& initialValue,
                                           BinaryFunctor binaryFunctor)
  {
791
    ScanExclusiveByKey(
792
      vtkm::cont::DeviceAdapterTagAny(), keys, values, output, initialValue, binaryFunctor);
793 794
  }

795 796 797 798 799 800 801 802 803 804

  template <typename T, typename U, class KIn, typename VIn, typename VOut>
  VTKM_CONT static void ScanExclusiveByKey(vtkm::cont::DeviceAdapterId devId,
                                           const vtkm::cont::ArrayHandle<T, KIn>& keys,
                                           const vtkm::cont::ArrayHandle<U, VIn>& values,
                                           vtkm::cont::ArrayHandle<U, VOut>& output)
  {
    vtkm::cont::TryExecuteOnDevice(
      devId, detail::ScanExclusiveByKeyFunctor(), keys, values, output);
  }
805 806 807 808 809
  template <typename T, typename U, class KIn, typename VIn, typename VOut>
  VTKM_CONT static void ScanExclusiveByKey(const vtkm::cont::ArrayHandle<T, KIn>& keys,
                                           const vtkm::cont::ArrayHandle<U, VIn>& values,
                                           vtkm::cont::ArrayHandle<U, VOut>& output)
  {
810
    ScanExclusiveByKey(vtkm::cont::DeviceAdapterTagAny(), keys, values, output);
811 812
  }

813 814 815 816 817 818 819 820

  template <class Functor>
  VTKM_CONT static void Schedule(vtkm::cont::DeviceAdapterId devId,
                                 Functor functor,
                                 vtkm::Id numInstances)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::ScheduleFunctor(), functor, numInstances);
  }
821 822 823
  template <class Functor>
  VTKM_CONT static void Schedule(Functor functor, vtkm::Id numInstances)
  {
824
    Schedule(vtkm::cont::DeviceAdapterTagAny(), functor, numInstances);
825 826
  }

827 828 829 830 831 832 833 834

  template <class Functor>
  VTKM_CONT static void Schedule(vtkm::cont::DeviceAdapterId devId,
                                 Functor functor,
                                 vtkm::Id3 rangeMax)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::ScheduleFunctor(), functor, rangeMax);
  }
835 836 837
  template <class Functor>
  VTKM_CONT static void Schedule(Functor functor, vtkm::Id3 rangeMax)
  {
838
    Schedule(vtkm::cont::DeviceAdapterTagAny(), functor, rangeMax);
839 840
  }

841 842 843 844 845 846 847

  template <typename T, class Storage>
  VTKM_CONT static void Sort(vtkm::cont::DeviceAdapterId devId,
                             vtkm::cont::ArrayHandle<T, Storage>& values)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::SortFunctor(), values);
  }
848 849 850
  template <typename T, class Storage>
  VTKM_CONT static void Sort(vtkm::cont::ArrayHandle<T, Storage>& values)
  {
851
    Sort(vtkm::cont::DeviceAdapterTagAny(), values);
852 853
  }

854 855 856 857 858 859 860 861

  template <typename T, class Storage, class BinaryCompare>
  VTKM_CONT static void Sort(vtkm::cont::DeviceAdapterId devId,
                             vtkm::cont::ArrayHandle<T, Storage>& values,
                             BinaryCompare binary_compare)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::SortFunctor(), values, binary_compare);
  }
862 863 864 865
  template <typename T, class Storage, class BinaryCompare>
  VTKM_CONT static void Sort(vtkm::cont::ArrayHandle<T, Storage>& values,
                             BinaryCompare binary_compare)
  {
866
    Sort(vtkm::cont::DeviceAdapterTagAny(), values, binary_compare);
867 868
  }

869 870 871 872 873 874 875 876

  template <typename T, typename U, class StorageT, class StorageU>
  VTKM_CONT static void SortByKey(vtkm::cont::DeviceAdapterId devId,
                                  vtkm::cont::ArrayHandle<T, StorageT>& keys,
                                  vtkm::cont::ArrayHandle<U, StorageU>& values)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::SortByKeyFunctor(), keys, values);
  }
877 878 879 880
  template <typename T, typename U, class StorageT, class StorageU>
  VTKM_CONT static void SortByKey(vtkm::cont::ArrayHandle<T, StorageT>& keys,
                                  vtkm::cont::ArrayHandle<U, StorageU>& values)
  {
881
    SortByKey(vtkm::cont::DeviceAdapterTagAny(), keys, values);
882 883
  }

884 885 886 887 888 889 890 891
  template <typename T, typename U, class StorageT, class StorageU, class BinaryCompare>
  VTKM_CONT static void SortByKey(vtkm::cont::DeviceAdapterId devId,
                                  vtkm::cont::ArrayHandle<T, StorageT>& keys,
                                  vtkm::cont::ArrayHandle<U, StorageU>& values,
                                  BinaryCompare binary_compare)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::SortByKeyFunctor(), keys, values, binary_compare);
  }
892 893 894 895 896
  template <typename T, typename U, class StorageT, class StorageU, class BinaryCompare>
  VTKM_CONT static void SortByKey(vtkm::cont::ArrayHandle<T, StorageT>& keys,
                                  vtkm::cont::ArrayHandle<U, StorageU>& values,
                                  BinaryCompare binary_compare)
  {
897
    SortByKey(vtkm::cont::DeviceAdapterTagAny(), keys, values, binary_compare);
898 899 900
  }


901 902 903 904
  VTKM_CONT static void Synchronize(vtkm::cont::DeviceAdapterId devId)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::SynchronizeFunctor());
  }
905
  VTKM_CONT static void Synchronize() { Synchronize(vtkm::cont::DeviceAdapterTagAny()); }
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923


  template <typename T,
            typename U,
            typename V,
            typename StorageT,
            typename StorageU,
            typename StorageV,
            typename BinaryFunctor>
  VTKM_CONT static void Transform(vtkm::cont::DeviceAdapterId devId,
                                  const vtkm::cont::ArrayHandle<T, StorageT>& input1,
                                  const vtkm::cont::ArrayHandle<U, StorageU>& input2,
                                  vtkm::cont::ArrayHandle<V, StorageV>& output,
                                  BinaryFunctor binaryFunctor)
  {
    vtkm::cont::TryExecuteOnDevice(
      devId, detail::TransformFunctor(), input1, input2, output, binaryFunctor);
  }
924 925 926 927 928 929 930 931 932 933 934 935
  template <typename T,
            typename U,
            typename V,
            typename StorageT,
            typename StorageU,
            typename StorageV,
            typename BinaryFunctor>
  VTKM_CONT static void Transform(const vtkm::cont::ArrayHandle<T, StorageT>& input1,
                                  const vtkm::cont::ArrayHandle<U, StorageU>& input2,
                                  vtkm::cont::ArrayHandle<V, StorageV>& output,
                                  BinaryFunctor binaryFunctor)
  {
936
    Transform(vtkm::cont::DeviceAdapterTagAny(), input1, input2, output, binaryFunctor);
937 938
  }

939 940 941 942 943 944 945

  template <typename T, class Storage>
  VTKM_CONT static void Unique(vtkm::cont::DeviceAdapterId devId,
                               vtkm::cont::ArrayHandle<T, Storage>& values)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::UniqueFunctor(), values);
  }
946 947 948
  template <typename T, class Storage>
  VTKM_CONT static void Unique(vtkm::cont::ArrayHandle<T, Storage>& values)
  {
949
    Unique(vtkm::cont::DeviceAdapterTagAny(), values);
950 951
  }

952 953 954 955 956 957 958 959

  template <typename T, class Storage, class BinaryCompare>
  VTKM_CONT static void Unique(vtkm::cont::DeviceAdapterId devId,
                               vtkm::cont::ArrayHandle<T, Storage>& values,
                               BinaryCompare binary_compare)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::UniqueFunctor(), values, binary_compare);
  }
960 961 962 963
  template <typename T, class Storage, class BinaryCompare>
  VTKM_CONT static void Unique(vtkm::cont::ArrayHandle<T, Storage>& values,
                               BinaryCompare binary_compare)
  {
964
    Unique(vtkm::cont::DeviceAdapterTagAny(), values, binary_compare);
965 966
  }

967 968 969 970 971 972 973 974 975

  template <typename T, class CIn, class CVal, class COut>
  VTKM_CONT static void UpperBounds(vtkm::cont::DeviceAdapterId devId,
                                    const vtkm::cont::ArrayHandle<T, CIn>& input,
                                    const vtkm::cont::ArrayHandle<T, CVal>& values,
                                    vtkm::cont::ArrayHandle<vtkm::Id, COut>& output)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::UpperBoundsFunctor(), input, values, output);
  }
976 977 978 979 980
  template <typename T, class CIn, class CVal, class COut>
  VTKM_CONT static void UpperBounds(const vtkm::cont::ArrayHandle<T, CIn>& input,
                                    const vtkm::cont::ArrayHandle<T, CVal>& values,
                                    vtkm::cont::ArrayHandle<vtkm::Id, COut>& output)
  {
981
    UpperBounds(vtkm::cont::DeviceAdapterTagAny(), input, values, output);
982 983
  }

984 985 986 987 988 989 990 991 992 993 994

  template <typename T, class CIn, class CVal, class COut, class BinaryCompare>
  VTKM_CONT static void UpperBounds(vtkm::cont::DeviceAdapterId devId,
                                    const vtkm::cont::ArrayHandle<T, CIn>& input,
                                    const vtkm::cont::ArrayHandle<T, CVal>& values,
                                    vtkm::cont::ArrayHandle<vtkm::Id, COut>& output,
                                    BinaryCompare binary_compare)
  {
    vtkm::cont::TryExecuteOnDevice(
      devId, detail::UpperBoundsFunctor(), input, values, output, binary_compare);
  }
995 996 997 998 999 1000
  template <typename T, class CIn, class CVal, class COut, class BinaryCompare>
  VTKM_CONT static void UpperBounds(const vtkm::cont::ArrayHandle<T, CIn>& input,
                                    const vtkm::cont::ArrayHandle<T, CVal>& values,
                                    vtkm::cont::ArrayHandle<vtkm::Id, COut>& output,
                                    BinaryCompare binary_compare)
  {
1001
    UpperBounds(vtkm::cont::DeviceAdapterTagAny(), input, values, output, binary_compare);
1002 1003
  }

1004 1005 1006 1007 1008 1009 1010 1011

  template <class CIn, class COut>
  VTKM_CONT static void UpperBounds(vtkm::cont::DeviceAdapterId devId,
                                    const vtkm::cont::ArrayHandle<vtkm::Id, CIn>& input,
                                    vtkm::cont::ArrayHandle<vtkm::Id, COut>& values_output)
  {
    vtkm::cont::TryExecuteOnDevice(devId, detail::UpperBoundsFunctor(), input, values_output);
  }
1012 1013 1014 1015
  template <class CIn, class COut>
  VTKM_CONT static void UpperBounds(const vtkm::cont::ArrayHandle<vtkm::Id, CIn>& input,
                                    vtkm::cont::ArrayHandle<vtkm::Id, COut>& values_output)
  {
1016
    UpperBounds(vtkm::cont::DeviceAdapterTagAny(), input, values_output);
1017 1018 1019 1020 1021 1022
  }
};
}
} // namespace vtkm::cont

#endif //vtk_m_cont_Algorithm_h