ArrayHandleTransform.h 26 KB
Newer Older
1
//============================================================================
Kenneth Moreland's avatar
Kenneth Moreland committed
2 3 4 5 6 7 8
//  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.
9
//============================================================================
Kenneth Moreland's avatar
Kenneth Moreland committed
10 11 12 13
#ifndef vtk_m_cont_ArrayHandleTransform_h
#define vtk_m_cont_ArrayHandleTransform_h

#include <vtkm/cont/ArrayHandle.h>
David C. Lonie's avatar
David C. Lonie committed
14 15
#include <vtkm/cont/ErrorBadType.h>
#include <vtkm/cont/ErrorInternal.h>
16
#include <vtkm/cont/ExecutionAndControlObjectBase.h>
17
#include <vtkm/cont/RuntimeDeviceTracker.h>
Kenneth Moreland's avatar
Kenneth Moreland committed
18

19 20
#include <vtkm/cont/serial/internal/DeviceAdapterRuntimeDetectorSerial.h>

21 22 23 24 25 26
namespace vtkm
{
namespace cont
{
namespace internal
{
27 28

/// Tag used in place of an inverse functor.
29 30 31
struct NullFunctorType
{
};
32 33 34 35
}
}
} // namespace vtkm::cont::internal

36 37 38 39 40 41
namespace vtkm
{
namespace exec
{
namespace internal
{
42

43
using NullFunctorType = vtkm::cont::internal::NullFunctorType;
44

Kenneth Moreland's avatar
Kenneth Moreland committed
45 46
/// \brief An array portal that transforms a value from another portal.
///
47 48 49
template <typename ValueType_,
          typename PortalType_,
          typename FunctorType_,
50
          typename InverseFunctorType_ = NullFunctorType>
51
class VTKM_ALWAYS_EXPORT ArrayPortalTransform;
52

53 54 55
template <typename ValueType_, typename PortalType_, typename FunctorType_>
class VTKM_ALWAYS_EXPORT
  ArrayPortalTransform<ValueType_, PortalType_, FunctorType_, NullFunctorType>
Kenneth Moreland's avatar
Kenneth Moreland committed
56 57
{
public:
58 59 60
  using PortalType = PortalType_;
  using ValueType = ValueType_;
  using FunctorType = FunctorType_;
Kenneth Moreland's avatar
Kenneth Moreland committed
61

62
  VTKM_SUPPRESS_EXEC_WARNINGS
63
  VTKM_EXEC_CONT
64 65 66 67 68 69
  ArrayPortalTransform(const PortalType& portal = PortalType(),
                       const FunctorType& functor = FunctorType())
    : Portal(portal)
    , Functor(functor)
  {
  }
Kenneth Moreland's avatar
Kenneth Moreland committed
70

71
  /// Copy constructor for any other ArrayPortalTransform with an iterator
72 73
  /// type that can be copied to this iterator type. This allows us to do any
  /// type casting that the iterators do (like the non-const to const cast).
Kenneth Moreland's avatar
Kenneth Moreland committed
74
  ///
75
  VTKM_SUPPRESS_EXEC_WARNINGS
76 77 78 79 80 81
  template <class OtherV, class OtherP, class OtherF>
  VTKM_EXEC_CONT ArrayPortalTransform(const ArrayPortalTransform<OtherV, OtherP, OtherF>& src)
    : Portal(src.GetPortal())
    , Functor(src.GetFunctor())
  {
  }
Kenneth Moreland's avatar
Kenneth Moreland committed
82

83
  VTKM_SUPPRESS_EXEC_WARNINGS
84
  VTKM_EXEC_CONT
85
  vtkm::Id GetNumberOfValues() const { return this->Portal.GetNumberOfValues(); }
Kenneth Moreland's avatar
Kenneth Moreland committed
86

87
  VTKM_SUPPRESS_EXEC_WARNINGS
88
  VTKM_EXEC_CONT
89
  ValueType Get(vtkm::Id index) const { return this->Functor(this->Portal.Get(index)); }
Kenneth Moreland's avatar
Kenneth Moreland committed
90

91 92
  VTKM_SUPPRESS_EXEC_WARNINGS
  VTKM_EXEC_CONT
93
  void Set(vtkm::Id vtkmNotUsed(index), const ValueType& vtkmNotUsed(value)) const
94
  {
95
#if !(defined(VTKM_MSVC) && defined(VTKM_CUDA))
96 97
    VTKM_ASSERT(false &&
                "Cannot write to read-only transform array. (No inverse transform given.)");
98
#endif
99 100
  }

101
  VTKM_SUPPRESS_EXEC_WARNINGS
102
  VTKM_EXEC_CONT
103
  const PortalType& GetPortal() const { return this->Portal; }
Kenneth Moreland's avatar
Kenneth Moreland committed
104

105
  VTKM_SUPPRESS_EXEC_WARNINGS
106
  VTKM_EXEC_CONT
107
  const FunctorType& GetFunctor() const { return this->Functor; }
Kenneth Moreland's avatar
Kenneth Moreland committed
108

109
protected:
Kenneth Moreland's avatar
Kenneth Moreland committed
110 111 112 113
  PortalType Portal;
  FunctorType Functor;
};

114 115 116
template <typename ValueType_,
          typename PortalType_,
          typename FunctorType_,
117 118 119
          typename InverseFunctorType_>
class VTKM_ALWAYS_EXPORT ArrayPortalTransform
  : public ArrayPortalTransform<ValueType_, PortalType_, FunctorType_, NullFunctorType>
120 121
{
public:
122 123 124 125 126
  using Superclass = ArrayPortalTransform<ValueType_, PortalType_, FunctorType_, NullFunctorType>;
  using PortalType = PortalType_;
  using ValueType = ValueType_;
  using FunctorType = FunctorType_;
  using InverseFunctorType = InverseFunctorType_;
127

128
  VTKM_SUPPRESS_EXEC_WARNINGS
129
  VTKM_EXEC_CONT
130 131 132 133 134 135 136
  ArrayPortalTransform(const PortalType& portal = PortalType(),
                       const FunctorType& functor = FunctorType(),
                       const InverseFunctorType& inverseFunctor = InverseFunctorType())
    : Superclass(portal, functor)
    , InverseFunctor(inverseFunctor)
  {
  }
137

138
  VTKM_SUPPRESS_EXEC_WARNINGS
139 140 141 142 143 144 145
  template <class OtherV, class OtherP, class OtherF, class OtherInvF>
  VTKM_EXEC_CONT ArrayPortalTransform(
    const ArrayPortalTransform<OtherV, OtherP, OtherF, OtherInvF>& src)
    : Superclass(src)
    , InverseFunctor(src.GetInverseFunctor())
  {
  }
146

147
  VTKM_SUPPRESS_EXEC_WARNINGS
148
  VTKM_EXEC_CONT
149 150 151
  void Set(vtkm::Id index, const ValueType& value) const
  {
    return this->Portal.Set(index, this->InverseFunctor(value));
152 153
  }

154
  VTKM_SUPPRESS_EXEC_WARNINGS
155
  VTKM_EXEC_CONT
156
  const InverseFunctorType& GetInverseFunctor() const { return this->InverseFunctor; }
157 158 159 160

private:
  InverseFunctorType InverseFunctor;
};
161 162 163 164
}
}
} // namespace vtkm::exec::internal

165 166 167 168
namespace vtkm
{
namespace cont
{
169

170 171
namespace internal
{
172

173
template <typename ProvidedFunctorType, typename FunctorIsExecContObject>
174 175
struct TransformFunctorManagerImpl;

176 177
template <typename ProvidedFunctorType>
struct TransformFunctorManagerImpl<ProvidedFunctorType, std::false_type>
178
{
179 180 181
  VTKM_STATIC_ASSERT_MSG(!vtkm::cont::internal::IsExecutionObjectBase<ProvidedFunctorType>::value,
                         "Must use an ExecutionAndControlObject instead of an ExecutionObject.");

182
  ProvidedFunctorType Functor;
183
  using FunctorType = ProvidedFunctorType;
184 185 186 187 188 189 190 191 192 193

  TransformFunctorManagerImpl() = default;

  VTKM_CONT
  TransformFunctorManagerImpl(const ProvidedFunctorType& functor)
    : Functor(functor)
  {
  }

  VTKM_CONT
194 195 196 197 198 199 200
  ProvidedFunctorType PrepareForControl() const { return this->Functor; }

  template <typename Device>
  VTKM_CONT ProvidedFunctorType PrepareForExecution(Device) const
  {
    return this->Functor;
  }
201 202
};

203 204
template <typename ProvidedFunctorType>
struct TransformFunctorManagerImpl<ProvidedFunctorType, std::true_type>
205
{
206 207
  VTKM_IS_EXECUTION_AND_CONTROL_OBJECT(ProvidedFunctorType);

208
  ProvidedFunctorType Functor;
209 210
  //  using FunctorType = decltype(std::declval<ProvidedFunctorType>().PrepareForControl());
  using FunctorType = decltype(Functor.PrepareForControl());
211 212 213 214 215 216 217 218 219 220

  TransformFunctorManagerImpl() = default;

  VTKM_CONT
  TransformFunctorManagerImpl(const ProvidedFunctorType& functor)
    : Functor(functor)
  {
  }

  VTKM_CONT
221 222 223 224 225 226 227 228 229 230 231
  auto PrepareForControl() const -> decltype(this->Functor.PrepareForControl())
  {
    return this->Functor.PrepareForControl();
  }

  template <typename Device>
  VTKM_CONT auto PrepareForExecution(Device device) const
    -> decltype(this->Functor.PrepareForExecution(device))
  {
    return this->Functor.PrepareForExecution(device);
  }
232 233
};

234
template <typename ProvidedFunctorType>
235 236 237
struct TransformFunctorManager
  : TransformFunctorManagerImpl<
      ProvidedFunctorType,
238
      typename vtkm::cont::internal::IsExecutionAndControlObjectBase<ProvidedFunctorType>::type>
239 240 241
{
  using Superclass = TransformFunctorManagerImpl<
    ProvidedFunctorType,
242
    typename vtkm::cont::internal::IsExecutionAndControlObjectBase<ProvidedFunctorType>::type>;
243 244 245 246
  using FunctorType = typename Superclass::FunctorType;

  VTKM_CONT TransformFunctorManager() = default;

247
  VTKM_CONT TransformFunctorManager(const TransformFunctorManager&) = default;
248 249 250 251 252 253 254 255 256 257

  VTKM_CONT TransformFunctorManager(const ProvidedFunctorType& functor)
    : Superclass(functor)
  {
  }

  template <typename ValueType>
  using TransformedValueType = decltype(std::declval<FunctorType>()(ValueType{}));
};

258
template <typename ArrayHandleType,
259
          typename FunctorType,
260 261 262
          typename InverseFunctorType = NullFunctorType>
struct VTKM_ALWAYS_EXPORT StorageTagTransform
{
263 264 265
  using FunctorManager = TransformFunctorManager<FunctorType>;
  using ValueType =
    typename FunctorManager::template TransformedValueType<typename ArrayHandleType::ValueType>;
266
};
Kenneth Moreland's avatar
Kenneth Moreland committed
267

268 269 270
template <typename ArrayHandleType, typename FunctorType>
class Storage<typename StorageTagTransform<ArrayHandleType, FunctorType>::ValueType,
              StorageTagTransform<ArrayHandleType, FunctorType>>
Kenneth Moreland's avatar
Kenneth Moreland committed
271
{
272 273
  using FunctorManager = TransformFunctorManager<FunctorType>;

Kenneth Moreland's avatar
Kenneth Moreland committed
274
public:
275
  using ValueType = typename StorageTagTransform<ArrayHandleType, FunctorType>::ValueType;
Kenneth Moreland's avatar
Kenneth Moreland committed
276

277 278 279 280
  // This is meant to be invalid. Because Transform arrays are read only, you
  // should only be able to use the const version.
  struct PortalType
  {
281 282
    using ValueType = void*;
    using IteratorType = void*;
283 284
  };

285 286 287
  using PortalConstType =
    vtkm::exec::internal::ArrayPortalTransform<ValueType,
                                               typename ArrayHandleType::PortalConstControl,
288
                                               typename FunctorManager::FunctorType>;
Kenneth Moreland's avatar
Kenneth Moreland committed
289

290
  VTKM_CONT
291 292 293 294
  Storage()
    : Valid(false)
  {
  }
Kenneth Moreland's avatar
Kenneth Moreland committed
295

296
  VTKM_CONT
297 298 299 300 301 302
  Storage(const ArrayHandleType& array, const FunctorType& functor = FunctorType())
    : Array(array)
    , Functor(functor)
    , Valid(true)
  {
  }
Kenneth Moreland's avatar
Kenneth Moreland committed
303

304
  VTKM_CONT
305 306
  PortalType GetPortal()
  {
307 308
    throw vtkm::cont::ErrorBadType(
      "ArrayHandleTransform is read only. Cannot get writable portal.");
Kenneth Moreland's avatar
Kenneth Moreland committed
309 310
  }

311
  VTKM_CONT
312 313
  PortalConstType GetPortalConst() const
  {
314
    VTKM_ASSERT(this->Valid);
315
    vtkm::cont::ScopedRuntimeDeviceTracker trackerScope;
316
    trackerScope.ForceDevice(vtkm::cont::DeviceAdapterTagSerial());
317
    return PortalConstType(this->Array.GetPortalConstControl(), this->Functor.PrepareForControl());
Kenneth Moreland's avatar
Kenneth Moreland committed
318 319
  }

320
  VTKM_CONT
321 322
  vtkm::Id GetNumberOfValues() const
  {
323
    VTKM_ASSERT(this->Valid);
Kenneth Moreland's avatar
Kenneth Moreland committed
324 325 326
    return this->Array.GetNumberOfValues();
  }

327
  VTKM_CONT
328 329 330
  void Allocate(vtkm::Id vtkmNotUsed(numberOfValues))
  {
    throw vtkm::cont::ErrorBadType("ArrayHandleTransform is read only. It cannot be allocated.");
Kenneth Moreland's avatar
Kenneth Moreland committed
331 332
  }

333
  VTKM_CONT
334 335 336
  void Shrink(vtkm::Id vtkmNotUsed(numberOfValues))
  {
    throw vtkm::cont::ErrorBadType("ArrayHandleTransform is read only. It cannot shrink.");
Kenneth Moreland's avatar
Kenneth Moreland committed
337 338
  }

339
  VTKM_CONT
340 341
  void ReleaseResources()
  {
Kenneth Moreland's avatar
Kenneth Moreland committed
342 343 344 345 346
    // This request is ignored since it is asking to release the resources
    // of the delegate array, which may be used elsewhere. Should the behavior
    // be different?
  }

347
  VTKM_CONT
348 349
  const ArrayHandleType& GetArray() const
  {
350
    VTKM_ASSERT(this->Valid);
Kenneth Moreland's avatar
Kenneth Moreland committed
351 352 353
    return this->Array;
  }

354
  VTKM_CONT
355
  const FunctorManager& GetFunctor() const { return this->Functor; }
356

Kenneth Moreland's avatar
Kenneth Moreland committed
357 358
private:
  ArrayHandleType Array;
359
  FunctorManager Functor;
Kenneth Moreland's avatar
Kenneth Moreland committed
360 361 362
  bool Valid;
};

363 364 365 366
template <typename ArrayHandleType, typename FunctorType, typename InverseFunctorType>
class Storage<
  typename StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::ValueType,
  StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>>
367
{
368 369 370
  using FunctorManager = TransformFunctorManager<FunctorType>;
  using InverseFunctorManager = TransformFunctorManager<InverseFunctorType>;

371
public:
372 373
  using ValueType =
    typename StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::ValueType;
374

375 376 377
  using PortalType =
    vtkm::exec::internal::ArrayPortalTransform<ValueType,
                                               typename ArrayHandleType::PortalControl,
378 379
                                               typename FunctorManager::FunctorType,
                                               typename InverseFunctorManager::FunctorType>;
380 381 382
  using PortalConstType =
    vtkm::exec::internal::ArrayPortalTransform<ValueType,
                                               typename ArrayHandleType::PortalConstControl,
383 384
                                               typename FunctorManager::FunctorType,
                                               typename InverseFunctorManager::FunctorType>;
385

386
  VTKM_CONT
387 388 389 390
  Storage()
    : Valid(false)
  {
  }
391

392
  VTKM_CONT
393 394
  Storage(const ArrayHandleType& array,
          const FunctorType& functor,
395 396 397 398 399 400 401
          const InverseFunctorType& inverseFunctor)
    : Array(array)
    , Functor(functor)
    , InverseFunctor(inverseFunctor)
    , Valid(true)
  {
  }
402

403
  VTKM_CONT
404 405
  PortalType GetPortal()
  {
406
    VTKM_ASSERT(this->Valid);
407
    vtkm::cont::ScopedRuntimeDeviceTracker trackerScope;
408
    trackerScope.ForceDevice(vtkm::cont::DeviceAdapterTagSerial());
409
    return PortalType(this->Array.GetPortalControl(),
410 411
                      this->Functor.PrepareForControl(),
                      this->InverseFunctor.PrepareForControl());
412 413
  }

414
  VTKM_CONT
415 416
  PortalConstType GetPortalConst() const
  {
417
    VTKM_ASSERT(this->Valid);
418
    vtkm::cont::ScopedRuntimeDeviceTracker trackerScope;
419
    trackerScope.ForceDevice(vtkm::cont::DeviceAdapterTagSerial());
420
    return PortalConstType(this->Array.GetPortalConstControl(),
421 422
                           this->Functor.PrepareForControl(),
                           this->InverseFunctor.PrepareForControl());
423 424
  }

425
  VTKM_CONT
426 427
  vtkm::Id GetNumberOfValues() const
  {
428
    VTKM_ASSERT(this->Valid);
429 430 431
    return this->Array.GetNumberOfValues();
  }

432
  VTKM_CONT
433 434
  void Allocate(vtkm::Id numberOfValues)
  {
435 436 437 438
    this->Array.Allocate(numberOfValues);
    this->Valid = true;
  }

439
  VTKM_CONT
440
  void Shrink(vtkm::Id numberOfValues) { this->Array.Shrink(numberOfValues); }
441

442
  VTKM_CONT
443 444
  void ReleaseResources()
  {
445 446 447 448
    this->Array.ReleaseResources();
    this->Valid = false;
  }

449
  VTKM_CONT
450 451
  const ArrayHandleType& GetArray() const
  {
452
    VTKM_ASSERT(this->Valid);
453 454 455
    return this->Array;
  }

456
  VTKM_CONT
457
  const FunctorManager& GetFunctor() const { return this->Functor; }
458

459
  VTKM_CONT
460
  const InverseFunctorManager& GetInverseFunctor() const { return this->InverseFunctor; }
461 462 463

private:
  ArrayHandleType Array;
464 465
  FunctorManager Functor;
  InverseFunctorManager InverseFunctor;
466 467 468
  bool Valid;
};

469 470 471 472
template <typename ArrayHandleType, typename FunctorType, typename Device>
class ArrayTransfer<typename StorageTagTransform<ArrayHandleType, FunctorType>::ValueType,
                    StorageTagTransform<ArrayHandleType, FunctorType>,
                    Device>
Kenneth Moreland's avatar
Kenneth Moreland committed
473
{
474
  using StorageTag = StorageTagTransform<ArrayHandleType, FunctorType>;
475
  using FunctorManager = TransformFunctorManager<FunctorType>;
Kenneth Moreland's avatar
Kenneth Moreland committed
476 477

public:
478 479
  using ValueType = typename StorageTagTransform<ArrayHandleType, FunctorType>::ValueType;
  using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
Kenneth Moreland's avatar
Kenneth Moreland committed
480

481 482
  using PortalControl = typename StorageType::PortalType;
  using PortalConstControl = typename StorageType::PortalConstType;
Kenneth Moreland's avatar
Kenneth Moreland committed
483

484
  //meant to be an invalid writeable execution portal
485 486
  using PortalExecution = typename StorageType::PortalType;
  using PortalConstExecution = vtkm::exec::internal::ArrayPortalTransform<
487 488
    ValueType,
    typename ArrayHandleType::template ExecutionTypes<Device>::PortalConst,
489
    typename FunctorManager::FunctorType>;
Kenneth Moreland's avatar
Kenneth Moreland committed
490

491
  VTKM_CONT
492 493 494 495 496
  ArrayTransfer(StorageType* storage)
    : Array(storage->GetArray())
    , Functor(storage->GetFunctor())
  {
  }
Kenneth Moreland's avatar
Kenneth Moreland committed
497

498
  VTKM_CONT
499
  vtkm::Id GetNumberOfValues() const { return this->Array.GetNumberOfValues(); }
Kenneth Moreland's avatar
Kenneth Moreland committed
500

501
  VTKM_CONT
502 503
  PortalConstExecution PrepareForInput(bool vtkmNotUsed(updateData))
  {
504 505
    return PortalConstExecution(this->Array.PrepareForInput(Device()),
                                this->Functor.PrepareForExecution(Device()));
Kenneth Moreland's avatar
Kenneth Moreland committed
506 507
  }

508
  VTKM_CONT
509 510 511 512
  PortalExecution PrepareForInPlace(bool& vtkmNotUsed(updateData))
  {
    throw vtkm::cont::ErrorBadType("ArrayHandleTransform read only. "
                                   "Cannot be used for in-place operations.");
Kenneth Moreland's avatar
Kenneth Moreland committed
513 514
  }

515
  VTKM_CONT
516 517 518
  PortalExecution PrepareForOutput(vtkm::Id vtkmNotUsed(numberOfValues))
  {
    throw vtkm::cont::ErrorBadType("ArrayHandleTransform read only. Cannot be used as output.");
Kenneth Moreland's avatar
Kenneth Moreland committed
519 520
  }

521
  VTKM_CONT
522 523
  void RetrieveOutputData(StorageType* vtkmNotUsed(storage)) const
  {
David C. Lonie's avatar
David C. Lonie committed
524
    throw vtkm::cont::ErrorInternal(
525
      "ArrayHandleTransform read only. "
luz.paz's avatar
luz.paz committed
526
      "There should be no occurrence of the ArrayHandle trying to pull "
527
      "data from the execution environment.");
Kenneth Moreland's avatar
Kenneth Moreland committed
528 529
  }

530
  VTKM_CONT
531 532 533
  void Shrink(vtkm::Id vtkmNotUsed(numberOfValues))
  {
    throw vtkm::cont::ErrorBadType("ArrayHandleTransform read only. Cannot shrink.");
Kenneth Moreland's avatar
Kenneth Moreland committed
534 535
  }

536
  VTKM_CONT
537
  void ReleaseResources() { this->Array.ReleaseResourcesExecution(); }
Kenneth Moreland's avatar
Kenneth Moreland committed
538 539 540

private:
  ArrayHandleType Array;
541
  FunctorManager Functor;
Kenneth Moreland's avatar
Kenneth Moreland committed
542 543
};

544
template <typename ArrayHandleType,
545 546
          typename FunctorType,
          typename InverseFunctorType,
547
          typename Device>
548 549 550 551
class ArrayTransfer<
  typename StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::ValueType,
  StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>,
  Device>
552
{
553
  using StorageTag = StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>;
554 555
  using FunctorManager = TransformFunctorManager<FunctorType>;
  using InverseFunctorManager = TransformFunctorManager<InverseFunctorType>;
556 557

public:
558 559
  using ValueType = typename StorageTagTransform<ArrayHandleType, FunctorType>::ValueType;
  using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
560

561 562
  using PortalControl = typename StorageType::PortalType;
  using PortalConstControl = typename StorageType::PortalConstType;
563

564
  using PortalExecution = vtkm::exec::internal::ArrayPortalTransform<
565 566
    ValueType,
    typename ArrayHandleType::template ExecutionTypes<Device>::Portal,
567 568
    typename FunctorManager::FunctorType,
    typename InverseFunctorManager::FunctorType>;
569
  using PortalConstExecution = vtkm::exec::internal::ArrayPortalTransform<
570 571
    ValueType,
    typename ArrayHandleType::template ExecutionTypes<Device>::PortalConst,
572 573
    typename FunctorManager::FunctorType,
    typename InverseFunctorManager::FunctorType>;
574

575
  VTKM_CONT
576 577 578 579 580 581
  ArrayTransfer(StorageType* storage)
    : Array(storage->GetArray())
    , Functor(storage->GetFunctor())
    , InverseFunctor(storage->GetInverseFunctor())
  {
  }
582

583
  VTKM_CONT
584
  vtkm::Id GetNumberOfValues() const { return this->Array.GetNumberOfValues(); }
585

586
  VTKM_CONT
587 588
  PortalConstExecution PrepareForInput(bool vtkmNotUsed(updateData))
  {
589
    return PortalConstExecution(this->Array.PrepareForInput(Device()),
590 591
                                this->Functor.PrepareForExecution(Device()),
                                this->InverseFunctor.PrepareForExecution(Device()));
592 593
  }

594
  VTKM_CONT
595 596
  PortalExecution PrepareForInPlace(bool& vtkmNotUsed(updateData))
  {
597
    return PortalExecution(this->Array.PrepareForInPlace(Device()),
598 599
                           this->Functor.PrepareForExecution(Device()),
                           this->InverseFunctor.PrepareForExecution(Device()));
600 601
  }

602
  VTKM_CONT
603 604
  PortalExecution PrepareForOutput(vtkm::Id numberOfValues)
  {
605
    return PortalExecution(this->Array.PrepareForOutput(numberOfValues, Device()),
606 607
                           this->Functor.PrepareForExecution(Device()),
                           this->InverseFunctor.PrepareForExecution(Device()));
608 609
  }

610
  VTKM_CONT
611 612
  void RetrieveOutputData(StorageType* vtkmNotUsed(storage)) const
  {
613 614 615 616
    // Implementation of this method should be unnecessary. The internal
    // array handle should automatically retrieve the output data as necessary.
  }

617
  VTKM_CONT
618
  void Shrink(vtkm::Id numberOfValues) { this->Array.Shrink(numberOfValues); }
619

620
  VTKM_CONT
621
  void ReleaseResources() { this->Array.ReleaseResourcesExecution(); }
622 623 624

private:
  ArrayHandleType Array;
625 626
  FunctorManager Functor;
  InverseFunctorManager InverseFunctor;
627 628
};

Kenneth Moreland's avatar
Kenneth Moreland committed
629 630 631 632 633 634 635 636 637 638 639 640 641
} // namespace internal

/// \brief Implicitly transform values of one array to another with a functor.
///
/// ArrayHandleTransforms is a specialization of ArrayHandle. It takes a
/// delegate array handle and makes a new handle that calls a given unary
/// functor with the element at a given index and returns the result of the
/// functor as the value of this array at that position. This transformation is
/// done on demand. That is, rather than make a new copy of the array with new
/// values, the transformation is done as values are read from the array. Thus,
/// the functor operator should work in both the control and execution
/// environments.
///
642
template <typename ArrayHandleType,
643
          typename FunctorType,
644
          typename InverseFunctorType = internal::NullFunctorType>
645 646
class ArrayHandleTransform;

647 648 649 650 651
template <typename ArrayHandleType, typename FunctorType>
class ArrayHandleTransform<ArrayHandleType, FunctorType, internal::NullFunctorType>
  : public vtkm::cont::ArrayHandle<
      typename internal::StorageTagTransform<ArrayHandleType, FunctorType>::ValueType,
      internal::StorageTagTransform<ArrayHandleType, FunctorType>>
Kenneth Moreland's avatar
Kenneth Moreland committed
652 653 654 655 656
{
  // If the following line gives a compile error, then the ArrayHandleType
  // template argument is not a valid ArrayHandle type.
  VTKM_IS_ARRAY_HANDLE(ArrayHandleType);

657
public:
658
  VTKM_ARRAY_HANDLE_SUBCLASS(
659
    ArrayHandleTransform,
660 661 662 663
    (ArrayHandleTransform<ArrayHandleType, FunctorType>),
    (vtkm::cont::ArrayHandle<
      typename internal::StorageTagTransform<ArrayHandleType, FunctorType>::ValueType,
      internal::StorageTagTransform<ArrayHandleType, FunctorType>>));
664 665

private:
666
  using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
Kenneth Moreland's avatar
Kenneth Moreland committed
667

668
public:
669
  VTKM_CONT
670 671 672 673
  ArrayHandleTransform(const ArrayHandleType& handle, const FunctorType& functor = FunctorType())
    : Superclass(StorageType(handle, functor))
  {
  }
Kenneth Moreland's avatar
Kenneth Moreland committed
674 675 676 677 678
};

/// make_ArrayHandleTransform is convenience function to generate an
/// ArrayHandleTransform.  It takes in an ArrayHandle and a functor
/// to apply to each element of the Handle.
679 680
template <typename HandleType, typename FunctorType>
VTKM_CONT vtkm::cont::ArrayHandleTransform<HandleType, FunctorType> make_ArrayHandleTransform(
681 682
  HandleType handle,
  FunctorType functor)
Kenneth Moreland's avatar
Kenneth Moreland committed
683
{
684
  return ArrayHandleTransform<HandleType, FunctorType>(handle, functor);
Kenneth Moreland's avatar
Kenneth Moreland committed
685 686
}

687 688
// ArrayHandleTransform with inverse functors enabled (no need to subclass from
// ArrayHandleTransform without inverse functors: nothing to inherit).
689
template <typename ArrayHandleType, typename FunctorType, typename InverseFunctorType>
690
class ArrayHandleTransform
691
  : public vtkm::cont::ArrayHandle<
692 693 694
      typename internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::
        ValueType,
      internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>>
695 696 697 698 699
{
  VTKM_IS_ARRAY_HANDLE(ArrayHandleType);

public:
  VTKM_ARRAY_HANDLE_SUBCLASS(
700
    ArrayHandleTransform,
701
    (ArrayHandleTransform<ArrayHandleType, FunctorType, InverseFunctorType>),
702
    (vtkm::cont::ArrayHandle<
703 704 705
      typename internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>::
        ValueType,
      internal::StorageTagTransform<ArrayHandleType, FunctorType, InverseFunctorType>>));
706 707

private:
708
  using StorageType = vtkm::cont::internal::Storage<ValueType, StorageTag>;
709

710
public:
711 712
  ArrayHandleTransform(const ArrayHandleType& handle,
                       const FunctorType& functor = FunctorType(),
713 714 715 716
                       const InverseFunctorType& inverseFunctor = InverseFunctorType())
    : Superclass(StorageType(handle, functor, inverseFunctor))
  {
  }
717 718
};

719 720
template <typename HandleType, typename FunctorType, typename InverseFunctorType>
VTKM_CONT vtkm::cont::ArrayHandleTransform<HandleType, FunctorType, InverseFunctorType>
721 722
make_ArrayHandleTransform(HandleType handle, FunctorType functor, InverseFunctorType inverseFunctor)
{
723
  return ArrayHandleTransform<HandleType, FunctorType, InverseFunctorType>(
724
    handle, functor, inverseFunctor);
725
}
Kenneth Moreland's avatar
Kenneth Moreland committed
726
}
727

Kenneth Moreland's avatar
Kenneth Moreland committed
728 729
} // namespace vtkm::cont

Sujin Philip's avatar
Sujin Philip committed
730 731 732 733 734 735 736 737
//=============================================================================
// Specializations of serialization related classes
namespace vtkm
{
namespace cont
{

template <typename AH, typename Functor, typename InvFunctor>
738
struct SerializableTypeString<vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
Sujin Philip's avatar
Sujin Philip committed
739 740 741
{
  static VTKM_CONT const std::string& Get()
  {
742 743 744
    static std::string name = "AH_Transform<" + SerializableTypeString<AH>::Get() + "," +
      SerializableTypeString<Functor>::Get() + "," + SerializableTypeString<InvFunctor>::Get() +
      ">";
Sujin Philip's avatar
Sujin Philip committed
745 746 747 748 749
    return name;
  }
};

template <typename AH, typename Functor>
750
struct SerializableTypeString<vtkm::cont::ArrayHandleTransform<AH, Functor>>
Sujin Philip's avatar
Sujin Philip committed
751 752 753
{
  static VTKM_CONT const std::string& Get()
  {
754 755
    static std::string name = "AH_Transform<" + SerializableTypeString<AH>::Get() + "," +
      SerializableTypeString<Functor>::Get() + ">";
Sujin Philip's avatar
Sujin Philip committed
756 757 758 759 760
    return name;
  }
};

template <typename AH, typename Functor, typename InvFunctor>
761
struct SerializableTypeString<vtkm::cont::ArrayHandle<
Sujin Philip's avatar
Sujin Philip committed
762 763
  typename vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>::ValueType,
  vtkm::cont::internal::StorageTagTransform<AH, Functor, InvFunctor>>>
764
  : SerializableTypeString<vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
Sujin Philip's avatar
Sujin Philip committed
765 766 767 768 769
{
};
}
} // vtkm::cont

770
namespace mangled_diy_namespace
Sujin Philip's avatar
Sujin Philip committed
771 772 773 774 775 776 777 778 779 780 781 782 783
{

template <typename AH, typename Functor>
struct Serialization<vtkm::cont::ArrayHandleTransform<AH, Functor>>
{
private:
  using Type = vtkm::cont::ArrayHandleTransform<AH, Functor>;
  using BaseType = vtkm::cont::ArrayHandle<typename Type::ValueType, typename Type::StorageTag>;

public:
  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
  {
    auto storage = obj.GetStorage();
784 785
    vtkmdiy::save(bb, storage.GetArray());
    vtkmdiy::save(bb, storage.GetFunctor());
Sujin Philip's avatar
Sujin Philip committed
786 787 788 789 790
  }

  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
  {
    AH array;
791
    vtkmdiy::load(bb, array);
Sujin Philip's avatar
Sujin Philip committed
792
    Functor functor;
793
    vtkmdiy::load(bb, functor);
Sujin Philip's avatar
Sujin Philip committed
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
    obj = vtkm::cont::make_ArrayHandleTransform(array, functor);
  }
};

template <typename AH, typename Functor, typename InvFunctor>
struct Serialization<vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>>
{
private:
  using Type = vtkm::cont::ArrayHandleTransform<AH, Functor, InvFunctor>;
  using BaseType = vtkm::cont::ArrayHandle<typename Type::ValueType, typename Type::StorageTag>;

public:
  static VTKM_CONT void save(BinaryBuffer& bb, const BaseType& obj)
  {
    auto storage = obj.GetStorage();
809 810 811
    vtkmdiy::save(bb, storage.GetArray());
    vtkmdiy::save(bb, storage.GetFunctor());
    vtkmdiy::save(bb, storage.GetInverseFunctor());
Sujin Philip's avatar
Sujin Philip committed
812 813 814 815 816
  }

  static VTKM_CONT void load(BinaryBuffer& bb, BaseType& obj)
  {
    AH array;
817
    vtkmdiy::load(bb, array);
Sujin Philip's avatar
Sujin Philip committed
818
    Functor functor;