-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorklets.tex
2051 lines (1675 loc) · 98.1 KB
/
Worklets.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% -*- latex -*-
\chapter{Worklets}
\label{chap:Worklets}
\index{worklet|(}
The simplest way to implement an algorithm in VTK-m is to create a
\keyterm{worklet}. A worklet is fundamentally a functor that operates on an
element of data. Thus, it is a \textcode{class} or \textcode{struct} that
has an overloaded parenthesis operator (which must be declared
\textcode{const} for thread safety). However, worklets are also embedded
with a significant amount of metadata on how the data should be managed and
how the execution should be structured. This chapter explains the basic
mechanics of defining and using worklets.
\section{Worklet Types}
\label{sec:WorkletTypes}
\index{worklet types|(}
Different operations in visualization can have different data access
patterns, perform different execution flow, and require different
provisions. VTK-m manages these different accesses, execution, and
provisions by grouping visualization algorithms into common classes of
operation and supporting each class with its own worklet type.
Each worklet type has a generic superclass that worklets of that particular
type must inherit. This makes the type of the worklet easy to identify. The
following list describes each worklet type provided by VTK-m and the
superclass that supports it. Details on how to create worklets of each type
are given in Section~\ref{sec:WorkletTypeReference}. It is also possible
to create new worklet types in VTK-m. This is an advanced topic covered in
Chapter~\ref{chap:NewWorkletTypes}.
\begin{description}
\item[Field Map] \index{worklet types!field map} \index{field map worklet}
A worklet deriving \vtkmworklet{WorkletMapField} performs a basic mapping
operation that applies a function (the operator in the worklet) on all
the field values at a single point or cell and creates a new field value
at that same location. Although the intention is to operate on some
variable over a mesh, a \textidentifier{WorkletMapField} may actually be
applied to any array. Thus, a field map can be used as a basic
\index{map}map operation.
\item[Topology Map] \index{worklet types!topology map}
\index{topology map worklet} A worklet deriving
\vtkmworklet{WorkletMapTopology} or one of its sibling classes performs a
mapping operation that applies a function (the operator in the worklet)
on all elements of a particular type (such as points or cells) and
creates a new field for those elements. The basic operation is similar to
a field map except that in addition to access fields being mapped on, the
worklet operation also has access to incident fields.
There are multiple convenience classes available for the most common
types of topology mapping. \index{worklet types!point to cell}
\index{point to cell worklet} \vtkmworklet{WorkletMapPointToCell} calls
the worklet operation for each cell and makes every incident point
available. This type of map also has access to cell structures and can
interpolate point fields.
%% \index{worklet types!cell to point}
%% \index{cell to point worklet} \vtkmworklet{WorkletMapCellToPoint} calls
%% the worklet operation for each point and makes every incident cell
%% available.
\end{description}
\index{worklet types|)}
\section{Dispatchers}
\label{sec:Dispatchers}
\index{dispatcher|(}
Worklets, both those provided by VTK-m as listed in
Section~\ref{sec:ProvidedWorklets} and ones created by a user as described
in Section~\ref{sec:CreatingWorklets}, are instantiated in the control
environment and run in the execution environment. This means that the
control environment must have a means to \index{invoke}\keyterm{invoke}
worklets that start running in the execution environment.
This invocation is done through a set of
\index{dispatcher}\keyterm{dispatcher} objects. A dispatcher object is an
object in the control environment that has an instance of a worklet and can
invoke that worklet with a set of arguments. There are multiple types of
dispatcher objects, each corresponding to a type of worklet object. All
dispatcher objects have at least two template parameters: the worklet class
being invoked, which is always the first argument, and the device adapter
tag, which is always the last argument and will be set to the default
device adapter if not specified.
All dispatcher classes have a method named \textcode{Invoke} that launches
the worklet in the execution environment. The arguments to
\textcode{Invoke} must match those expected by the worklet, which is
specified by something called a \keyterm{control signature}. The expected
arguments for worklets provided by VTK-m are documented in
Section~\ref{sec:ProvidedWorklets}. Also, for any worklet, the
\textcode{Invoke} arguments can be gleaned from the control signature,
which is described in Section~\ref{sec:ControlSignature}.
The following is a list of the dispatchers defined in VTK-m. The
dispatcher classes correspond to the list of worklet types specified in
Section~\ref{sec:WorkletTypes}. Many examples of using these dispatchers
are provided in Section~\ref{sec:ProvidedWorklets}.
\begin{description}
\item[\vtkmworklet{DispatcherMapField}] The dispatcher used in conjunction
with a worklet that subclasses \vtkmworklet{WorkletMapField}. The
dispatcher class has two template arguments: the worklet type and the
device adapter (optional).
\item[\vtkmworklet{DispatcherMapTopology}] The dispatcher used in
conjunction with a worklet that subclasses
\vtkmworklet{WorkletMapTopology} or one of its sibling classes (such as
\vtkmworklet{WorkletMapPointToCell}). The dispatcher class has two
template arguments: the worklet type and the device adapter (optional).
%% \item[\daxcont{DispatcherMapCell}] The dispatcher used in conjunction with
%% a worklet that subclasses \dax{WorkletMapCell}. The class has two
%% template arguments: the worklet type and the device adapter (optional).
%% \item[\daxcont{DispatcherGenerateTopology}] The dispatcher used in
%% conjunction with a worklet that subclasses
%% \dax{WorkletGenerateTopology}. The class has three template arguments: the
%% worklet type, the type of array handle containing the count of the number
%% of cells being generated (optional), and the device adapter
%% (optional). The default type of the count array handle is
%% \daxcont{ArrayHandle}\textcode{<}\dax{Id}\textcode{>}. An instance of the
%% count array handle must be provided in the constructor of
%% \daxcont{DispatcherGenerateTopology}.
%% \item[\daxcont{DispatcherInterpolatedCell}] The dispatcher used in
%% conjunction with a worklet that subclasses
%% \dax{WorkletInterpolatedCell}. The class has three template arguments: the
%% worklet type, the type of array handle containing the count of the number
%% of cells being generated (optional), and the device adapter
%% (optional). The default type of the count array handle is
%% \daxcont{ArrayHandle}\textcode{<}\dax{Id}\textcode{>}. An instance of the
%% count array handle must be provided in the constructor of
%% \daxcont{DispatcherInterpolatedCell}.
%% \item[\daxcont{DispatcherGenerateKeysValues}] The dispatcher used in
%% conjunction with a worklet that subclasses
%% \dax{WorkletGenerateKeysValues}. The class has three template arguments:
%% the worklet type, the type of array handle containing the count of the
%% number of key-values being generated (optional), and the device adapter
%% (optional). The default type of the count array handle is
%% \daxcont{ArrayHandle}\textcode{<}\dax{Id}\textcode{>}. An instance of the
%% count array handle must be provided in the constructor of
%% \daxcont{DispatcherGenerateKeysValues}.
%% \item[\daxcont{DispatcherReduceKeysValues}] The dispatcher used in
%% conjunction with a worklet that subclasses
%% \dax{WorkletReduceKeysValues}. The class has three template arguments:
%% the worklet type, the type of array handle containing the keys
%% (optional), and the device adapter (optional). The default type of the
%% key array handle is
%% \daxcont{ArrayHandle}\textcode{<}\dax{Id}\textcode{>}. An instance of the
%% key array handle must be provided in the constructor of
%% \daxcont{DispatcherReduceKeysValues}.
\end{description}
\index{dispatcher|)}
\section{Provided Worklets}
\label{sec:ProvidedWorklets}
\VTKm comes with several worklet implementations.
These worklet implementations for the most part provide the underlying implementations of the filters described in Chapter~\ref{chap:ProvidedFilters}.
The easiest way to execute a filter is to run it from the associated filter class.
However, if your data is not in a \vtkmcont{DataSet} structure or you have knowledge of the specific data types used in the \textidentifier{DataSet}, it might be more efficient to run the worklet directly.
Note that many of the filters use multiple worklets under the covers to implement the full functionality.
The following example demonstrates using the simple \vtkmworklet{PointElevation} worklet directly.
\vtkmlisting{Using the provided \textidentifier{PointElevation} worklet.}{UsePointElevationWorklet.cxx}
\section{Creating Worklets}
\label{sec:CreatingWorklets}
\index{worklet!creating|(}
A worklet is created by implementing a \textcode{class} or
\textcode{struct} with the following features.
\begin{enumerate}
\item The class must contain a \controlsignature \textcode{typedef}, which
specifies what arguments are expected when invoking the class with a
dispatcher in the control environment.
\item The class must contain an \executionsignature \textcode{typedef},
which specifies how the data gets passed from the arguments in the
control environment to the worklet running in the execution environment.
\item The class must contain an \inputdomain \textcode{typedef}, which
identifies which input parameter defines the input domain of the data.
\item The class may define a scatter operation to override a 1:1 mapping
from input to output.
\item The class must contain an overload of the parenthesis operator, which
is the method that is executed in the execution environment.
\item The class must publicly inherit from a base worklet class that
specifies the type of operation being performed.
\end{enumerate}
Figure~\ref{fig:WorkletExampleAnnotated} demonstrates all of the required
components of a worklet.
%% \pagebreak
%% \begin{vtkmexample}{Example Code for Cutting/Pasting.}
%% class TriangulateCell : public vtkm::worklet::WorkletMapPointToCell
%% {
%% public:
%% typedef void ControlSignature(CellSetIn topology,
%% ExecObject tables,
%% FieldOutCell<> connectivityOut);
%% typedef void ExecutionSignature(CellShape, PointIndices, _2, _3, VisitIndex);
%% typedef _1 InputDomain;
%% typedef vtkm::worklet::ScatterCounting ScatterType;
%% VTKM_CONT
%% ScatterType GetScatter() const
%% {
%% return this->Scatter;
%% }
%% template<typename CellShapeTag,
%% typename ConnectivityInVec,
%% typename ConnectivityOutVec>
%% VTKM_EXEC
%% void operator()(
%% CellShapeTag shape,
%% const ConnectivityInVec &connectivityIn,
%% const internal::TriangulateTablesExecutionObject<DeviceAdapter> &tables,
%% ConnectivityOutVec &connectivityOut,
%% vtkm::IdComponent visitIndex) const
%% {
%% \end{vtkmexample}
%% \pagebreak
\begin{figure}[htb]
\includegraphics[width=\linewidth]{images/WorkletExampleAnnotated}
\caption{Annotated example of a worklet declaration.}
\label{fig:WorkletExampleAnnotated}
\end{figure}
\subsection{Control Signature}
\label{sec:ControlSignature}
\index{control signature|(}
\index{signature!control|(}
\index{worklet!control signature|(}
The control signature of a worklet is the \textcode{typedef} of a function
prototype named \controlsignature. The function prototype matches the
calling specification used with the dispatcher \textcode{Invoke} function.
\vtkmlisting{A \protect\controlsignature.}{ControlSignature.cxx}
The return type of the function prototype is always \textcode{void} because
the dispatcher \textcode{Invoke} functions do not return values. The
parameters of the function prototype are \index{signature tags}\keyterm{tags}
that identify the type of data that is expected to be passed to invoke.
\controlsignature tags are defined by the worklet type and the various tags
are documented more fully in Section~\ref{sec:WorkletTypeReference}.
By convention, \controlsignature tag names start with the base concept
(e.g. \textsignature{Field} or \textsignature{Topology}) followed by the
domain (e.g. \textsignature{Point} or \textsignature{Cell}) followed by
\textsignature{In} or \textsignature{Out}. For example,
\sigtag{FieldPointIn} would specify values for a field on the points of a
mesh that are used as input (read only). Although they should be there in
most cases, some tag names might leave out the domain or in/out parts if
they are obvious or ambiguous.
\subsubsection{Type List Tags}
\label{sec:TypeListTags}
\index{type list tags|(}
\index{control signature!type list tags|(}
Some tags are templated to have modifiers. For example,
\textsignature{Field} tags have a template argument that is set to a type
list tag defining what types of field data are supported. (See
Section~\ref{sec:TypeLists} for a description of type lists.) In fact, this
type list modifier is so common that the following convenience subtags used
with \textsignature{Field} tags are defined for all worklet types.
\begin{didyouknow}
Any type list will work as modifiers for \controlsignature tags. However,
these common type lists are provided for convenience and to make the
\controlsignature shorter and more readable.
\end{didyouknow}
\begin{description}
\label{TypeTagList}
\item[\sigtag{AllTypes}] All possible types.
\item[\sigtag{CommonTypes}] The most used types in visualization. This
includes signed integers and floats that are 32 or 64 bit. It also
includes 3 dimensional vectors of floats. The same as
\vtkm{TypeListTagCommon}.
\item[\sigtag{IdType}] Contains the single item \vtkm{Id}. The same as
\vtkm{TypeListTagId}.
\item[\sigtag{Id2Type}] Contains the single item \vtkm{Id2}. The same as
\vtkm{TypeListTagId2}.
\item[\sigtag{Id3Type}] Contains the single item \vtkm{Id3}. The same as
\vtkm{TypeListTagId3}.
\item[\sigtag{Index}] All types used to index arrays. Contains \vtkm{Id},
\vtkm{Id2}, and \vtkm{Id3}. The same as \vtkm{TypeListTagIndex}.
\item[\sigtag{FieldCommon}] A list containing all the types generally
used for fields. It is the combination of \sigtag{Scalar}, \sigtag{Vec2},
\sigtag{Vec3}, and \sigtag{Vec4}. The same as \vtkm{TypeListTagField}.
\item[\sigtag{Scalar}] Types used for scalar fields. Specifically, it
contains floating point numbers of different widths (i.e. \vtkm{Float32}
and \vtkm{Float64}). The same as \vtkm{TypeListTagFieldScalar}.
\item[\sigtag{ScalarAll}] All scalar types. It contains signed and unsigned
integers of widths from 8 to 64 bits. It also contains floats of 32 and
64 bit widths. The same as \vtkm{TypeListTagScalarAll}.
\item[\sigtag{Vec2}] Types for values of fields with 2 dimensional
vectors. All these vectors use floating point numbers. The same as
\vtkm{TypeListTagFieldVec2}.
\item[\sigtag{Vec3}] Types for values of fields with 3 dimensional
vectors. All these vectors use floating point numbers. The same as
\vtkm{TypeListTagFieldVec3}.
\item[\sigtag{Vec4}] Types for values of fields with 4 dimensional
vectors. All these vectors use floating point numbers. The same as
\vtkm{TypeListTagFieldVec4}.
\item[\sigtag{VecAll}] All \vtkm{Vec} classes with standard integers or
floating points as components and lengths between 2 and 4. The same as
\vtkm{TypeListTagVecAll}.
\item[\sigtag{VecCommon}] The most common vector types. It contains all
\vtkm{Vec} class of size 2 through 4 containing components of unsigned
bytes, signed 32-bit integers, signed 64-bit integers, 32-bit floats, or
64-bit floats. The same as \vtkm{TypeListTagVecCommon}.
\end{description}
\index{control signature!type list tags|)}
\index{type list tags|)}
\index{worklet!control signature|)}
\index{signature!control|)}
\index{control signature|)}
\subsection{Execution Signature}
\label{sec:ExecutionSignature}
\index{execution signature|(}
\index{signature!execution|(}
\index{worklet!execution signature|(}
Like the control signature, the execution signature of a worklet is the
\textcode{typedef} of a function prototype named \executionsignature. The
function prototype must match the parenthesis operator (described in
Section~\ref{sec:WorkletOperator}) in terms of arity and argument
semantics.
\vtkmlisting{An \protect\executionsignature.}{ExecutionSignature.cxx}
The arguments of the \executionsignature's function prototype are tags that
define where the data come from. The most common tags are an underscore
followed by a number, such as \sigtagnum{1}, \sigtagnum{2}, etc. These
numbers refer back to the corresponding argument in the
\controlsignature. For example, \sigtagnum{1} means data from the first
control signature argument, \sigtagnum{2} means data from the second
control signature argument, etc.
Unlike the control signature, the execution signature optionally can
declare a return type if the parenthesis operator returns a value. If this
is the case, the return value should be one of the numeric tags
(i.e. \sigtagnum{1}, \sigtagnum{2}, etc.) to refer to one of the data
structures of the control signature. If the parenthesis operator does not
return a value, then \executionsignature should declare the return type as
\textcode{void}.
In addition to the numeric tags, there are other execution signature tags
to represent other types of data. For example, the \sigtag{WorkIndex} tag
identifies the instance of the worklet invocation. Each call to the worklet
function will have a unique \sigtag{WorkIndex}. Other such tags exist and
are described in the following section on worklet types where appropriate.
\index{worklet!execution signature|)}
\index{signature!execution|)}
\index{execution signature|)}
\subsection{Input Domain}
\label{sec:InputDomain}
\index{input domain|(}
\index{worklet!input domain|(}
All worklets represent data parallel operations that are executed over
independent elements in some domain. The type of domain is inherent from
the worklet type, but the size of the domain is dependent on the data being
operated on. One of the arguments given to the dispatcher's
\textcode{Invoke} in the control environment must specify the domain.
A worklet identifies the argument specifying the domain with a
\textcode{typedef} named \inputdomain. The \inputdomain must be
\textcode{typedef}ed to one of the execution signature numeric tags
(i.e. \sigtagnum{1}, \sigtagnum{2}, etc.). By default, the \inputdomain
points to the first argument, but a worklet can override that to point to
any argument.
\vtkmlisting{An \protect\inputdomain declaration.}{InputDomain.cxx}
Different types of worklets can have different types of domain. For example
a simple field map worklet has a \sigtag{FieldIn} argument as its input
domain, and the size of the input domain is taken from the size of the
associated field array. Likewise, a worklet that maps topology has a
\sigtag{CellSetIn} argument as its input domain, and the size of the input
domain is taken from the cell set.
Specifying the \inputdomain is optional. If it is not specified, the first
argument is assumed to be the input domain.
\index{worklet!input domain|)}
\index{input domain|)}
\subsection{Worklet Operator}
\label{sec:WorkletOperator}
A worklet is fundamentally a functor that operates on an element of data.
Thus, the algorithm that the worklet represents is contained in or called
from the parenthesis operator method.
\vtkmlisting{An overloaded parenthesis operator of a worklet.}{WorkletOperator.cxx}
There are some constraints on the parenthesis operator. First, it must have
the same arity as the \executionsignature, and the types of the parameters
and return must be compatible. Second, because it runs in the execution
environment, it must be declared with the \vtkmexecmodifier (or
\vtkmexeccontmodifier) modifier. Third, the method must be declared
\textcode{const} to help preserve thread safety.
\section{Worklet Type Reference}
\label{sec:WorkletTypeReference}
\index{worklet types|(}
There are multiple worklet types provided by VTK-m, each designed to
support a particular type of operation. Section~\ref{sec:WorkletTypes} gave
a brief overview of each type of worklet. This section gives a much more
detailed reference for each of the worklet types including identifying the
generic superclass that a worklet instance should derive, listing the
signature tags and their meanings, and giving an example of the worklet in
use.
\newcommand{\commoncontrolsignaturetags}{
\item[\sigtag{WholeArrayIn}] This tag represents an array where all entries
can be read by every worklet invocation. A \sigtag{WholeArrayIn} argument
expects an \textidentifier{ArrayHandle} in the associated parameter of
the dispatcher's \textcode{Invoke}. An array portal capable of reading
from any place in the array is given to the worklet. Whole arrays are
discussed in detail in Section~\ref{sec:WholeArrays} starting on
page~\pageref{sec:WholeArrays}.
\sigtag{WholeArrayIn} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\item[\sigtag{WholeArrayOut}] This tag represents an array where any entry
can be written by any worklet invocation. A \sigtag{WholeArrayOut}
argument expects an \textidentifier{ArrayHandle} in the associated
parameter of the dispatcher's \textcode{Invoke}. An array portal capable
of writing to any place in the array is given to the worklet. Developers
should take care when using writable whole arrays as introducing race
conditions is possible. Whole arrays are discussed in detail in
Section~\ref{sec:WholeArrays} starting on page~\pageref{sec:WholeArrays}.
\sigtag{WholeArrayOut} has a single template parameter that specifies
what data types are acceptable for the array. The type tags are described
in Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\item[\sigtag{WholeArrayInOut}] This tag represents an array where any
entry can be read or written by any worklet invocation. A
\sigtag{WholeArrayInOut} argument expects an \textidentifier{ArrayHandle}
in the associated parameter of the dispatcher's \textcode{Invoke}. An
array portal capable of reading from or writing to any place in the array
is given to the worklet. Developers should take care when using writable
whole arrays as introducing race conditions is possible. Whole arrays are
discussed in detail in Section~\ref{sec:WholeArrays} starting on
page~\pageref{sec:WholeArrays}.
\sigtag{WholeArrayInOut} has a single template parameter that specifies
what data types are acceptable for the array. The type tags are described
in Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\item[\sigtag{AtomicArrayInOut}]
This tag represents an array where any entry can be read or written by any worklet invocation.
A \sigtag{AtomicArrayInOut} argument expects an \textidentifier{ArrayHandle} in the associated parameter of the dispatcher's \textcode{Invoke}.
A \vtkmexec{AtomicArray} object capable of performing atomic operations to the entries in the array is given to the worklet.
Atomic arrays can help avoid race conditions but can slow down the running of a parallel algorithm.
Atomic arrays are discussed in detail in Section~\ref{sec:AtomicArrays} starting on page~\pageref{sec:AtomicArrays}.
\item[\sigtag{ExecObject}] This tag represents an execution object that is
passed directly from the control environment to the worklet. A
\sigtag{ExecObject} argument expects a subclass of
\vtkmexec{ExecutionObjectBase}, and this same object is given to the
worklet. Execution objects are discussed in detail in
Section~\ref{sec:ExecutionObjects} starting on
page~\pageref{sec:ExecutionObjects}.
}
\newcommand{\numericexecutionsignaturetags}{
\item[\sigtagnum{1}, \sigtagnum{2},$\ldots$] These reference the
corresponding parameter in the \controlsignature.
}
\newcommand{\commonexecutionsignaturetags}{
\item[\sigtag{WorkIndex}]
This tag produces a \vtkm{Id} that uniquely identifies the invocation of the worklet.
\item[\sigtag{VisitIndex}]
This tag produces a \vtkm{IdComponent} that uniquely identifies when multiple worklet invocations operate on the same input item, which can happen when defining a worklet with scatter (as described in Section~\ref{sec:WorkletScatter}).
\item[\sigtag{InputIndex}]
This tag produces a \vtkm{Id} that identifies the index of the input element, which can differ from the \sigtag{WorkIndex} in a worklet with a scatter (as described in Section~\ref{sec:WorkletScatter}).
\item[\sigtag{OutputIndex}]
This tag produces a \vtkm{Id} that identifies the index of the output element. (This is generally the same as \sigtag{WorkIndex}.)
\item[\sigtag{ThreadIndices}]
This tag produces an internal object that manages indices and other metadata of the current thread. Thread indices objects are described in Section~\ref{sec:ThreadIndices}, but most users can get the information they need through other signature tags.
}
\subsection{Field Map}
\index{worklet types!field map|(}
\index{field map worklet|(}
\index{map field|(}
A worklet deriving \vtkmworklet{WorkletMapField} performs a basic mapping
operation that applies a function (the operator in the worklet) on all the
field values at a single point or cell and creates a new field value at
that same location. Although the intention is to operate on some variable
over the mesh, a \textidentifier{WorkletMapField} can actually be applied
to any array.
A \textidentifier{WorkletMapField} subclass is invoked with a
\vtkmworklet{DispatcherMapField}. This dispatcher has two template
arguments. The first argument is the type of the worklet subclass. The
second argument, which is optional, is a device adapter tag.
A field map worklet supports the following tags in the parameters of its
\controlsignature.
\begin{description}
\item[\sigtag{FieldIn}] This tag represents an input field. A
\sigtag{FieldIn} argument expects an \textidentifier{ArrayHandle} or a
\textidentifier{DynamicArrayHandle} in the associated parameter of the
dispatcher's \textcode{Invoke}. Each invocation of the worklet gets a
single value out of this array.
\sigtag{FieldIn} has a single template parameter that specifies what data
types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on
page~\pageref{TypeTagList}.
The worklet's \inputdomain can be set to a \sigtag{FieldIn} argument. In
this case, the input domain will be the size of the array.
\item[\sigtag{FieldOut}] This tag represents an output field. A
\sigtag{FieldOut} argument expects an \textidentifier{ArrayHandle} or a
\textidentifier{DynamicArrayHandle} in the associated parameter of the
dispatcher's \textcode{Invoke}. The array is resized before scheduling
begins, and each invocation of the worklet sets a single value in the
array.
\sigtag{FieldOut} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on
page~\pageref{TypeTagList}.
\item[\sigtag{FieldInOut}] This tag represents field that is both an input
and an output. A \sigtag{FieldInOut} argument expects an
\textidentifier{ArrayHandle} or a \textidentifier{DynamicArrayHandle} in
the associated parameter of the dispatcher's \textcode{Invoke}. Each
invocation of the worklet gets a single value out of this array, which is
replaced by the resulting value after the worklet completes.
\sigtag{FieldInOut} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on
page~\pageref{TypeTagList}.
The worklet's \inputdomain can be set to a \sigtag{FieldInOut} argument. In
this case, the input domain will be the size of the array.
\commoncontrolsignaturetags
\end{description}
A field map worklet supports the following tags in the parameters of its
\executionsignature.
\begin{description}
\numericexecutionsignaturetags
\commonexecutionsignaturetags
\end{description}
Field maps most commonly perform basic calculator arithmetic, as
demonstrated in the following example.
\vtkmlisting[ex:UseWorkletMapField]{Implementation and use of a field map worklet.}{UseWorkletMapField.cxx}
Although simple, the \textidentifier{WorkletMapField} worklet type can be
used (and abused) as a general parallel-for/scheduling mechanism. In
particular, the \sigtag{WorkIndex} execution signature tag can be used to
get a unique index, the \textsignature{WholeArray}* tags can be used to get
random access to arrays, and the \sigtag{ExecObject} control signature tag
can be used to pass execution objects directly to the worklet. Whole arrays
and execution objects are talked about in more detail in Sections
\ref{sec:WholeArrays} and \ref{sec:ExecutionObjects}, respectively, in more
detail, but here is a simple example that uses the random access of
\sigtag{WholeArrayOut} to make a worklet that copies an array
in reverse order.
\vtkmlisting[ex:WholeArray]{Leveraging field maps and field maps for general processing.}{RandomArrayAccess.cxx}
\index{map field|)}
\index{field map worklet|)}
\index{worklet types!field map|)}
\subsection{Topology Map}
\label{sec:TopologyMaps}
A topology map performs a mapping that it applies a function (the
operator in the worklet) on all the elements of a \textidentifier{DataSet}
of a particular type (i.e. point, edge, face, or cell). While operating on
the element, the worklet has access to data from all incident elements of
another type.
There are several versions of topology maps that differ in what type of
element being mapped from and what type of element being mapped to. The
subsequent sections describe these different variations of the topology
maps. Regardless of their names, they are all defined in
\vtkmheader{vtkm/worklet}{WorkletMapTopology.h} and are all invoked with
\vtkmworklet{DispatcherMapTopology}.
\subsubsection{Point to Cell Map}
\label{sec:WorkletMapPointToCell}
\index{worklet types!point to cell map|(}
\index{point to cell map worklet|(}
\index{map point to cell|(}
A worklet deriving \vtkmworklet{WorkletMapPointToCell} performs a mapping
operation that applies a function (the operator in the worklet) on all the
cells of a \textidentifier{DataSet}. While operating on the cell, the
worklet has access to fields associated both with the cell and with all
incident points. Additionally, the worklet can get information about the
structure of the cell and can perform operations like interpolation on it.
A \textidentifier{WorkletMapPointToCell} subclass is invoked with a
\vtkmworklet{DispatcherMapTopology}. This dispatcher has two template
arguments. The first argument is the type of the worklet subclass. The
second argument, which is optional, is a device adapter tag.
A point to cell map worklet supports the following tags in the parameters
of its \controlsignature.
\begin{description}
\item[\sigtag{CellSetIn}] This tag represents the cell set that defines
the collection of cells the map will operate on. A \sigtag{CellSetIn}
argument expects a \textidentifier{CellSet} subclass or a
\textidentifier{DynamicCellSet} in the associated parameter of the
dispatcher's \textcode{Invoke}. Each invocation of the worklet gets a
cell shape tag. (Cell shapes and the operations you can do with cells are
discussed in Chapter~\ref{chap:WorkingWithCells}.)
There must be exactly one \sigtag{CellSetIn} argument, and the worklet's
\inputdomain must be set to this argument.
\item[\sigtag{FieldInPoint}] This tag represents an input field that is
associated with the points. A \sigtag{FieldInPoint} argument expects an
\textidentifier{ArrayHandle} or a \textidentifier{DynamicArrayHandle} in
the associated parameter of the dispatcher's \textcode{Invoke}. The size
of the array must be exactly the number of points.
Each invocation of the worklet gets a Vec-like object containing the
field values for all the points incident with the cell being visited. The
order of the entries is consistent with the defined order of the vertices
for the visited cell's shape. If the field is a vector field, then the
provided object is a Vec of Vecs.
\sigtag{FieldInPoint} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\item[\sigtag{FieldInCell}] This tag represents an input field that is
associated with the cells. A \sigtag{FieldInCell} argument expects an
\textidentifier{ArrayHandle} or a \textidentifier{DynamicArrayHandle} in
the associated parameter of the dispatcher's \textcode{Invoke}. The size
of the array must be exactly the number of cells. Each invocation of the
worklet gets a single value out of this array.
\sigtag{FieldInCell} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\item[\sigtag{FieldOutCell}] This tag represents an output field, which is
necessarily associated with cells. A \sigtag{FieldOutCell} argument
expects an \textidentifier{ArrayHandle} or a
\textidentifier{DynamicArrayHandle} in the associated parameter of the
dispatcher's \textcode{Invoke}. The array is resized before scheduling
begins, and each invocation of the worklet sets a single value in the
array.
\sigtag{FieldOutCell} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\sigtag{FieldOut} is an alias for \sigtag{FieldOutCell} (since output
arrays can only be defined on cells).
\item[\sigtag{FieldInOutCell}] This tag represents field that is both an
input and an output, which is necessarily associated with cells. A
\sigtag{FieldInOutCell} argument expects an \textidentifier{ArrayHandle}
or a \textidentifier{DynamicArrayHandle} in the associated parameter of
the dispatcher's \textcode{Invoke}. Each invocation of the worklet gets a
single value out of this array, which is replaced by the resulting value
after the worklet completes.
\sigtag{FieldInOutCell} has a single template parameter that specifies
what data types are acceptable for the array. The type tags are described
in Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\sigtag{FieldInOut} is an alias for \sigtag{FieldInOutCell} (since output
arrays can only be defined on cells).
\commoncontrolsignaturetags
\end{description}
A field map worklet supports the following tags in the parameters of its
\executionsignature.
\begin{description}
\numericexecutionsignaturetags
\item[\sigtag{CellShape}] This tag produces a shape tag corresponding to
the shape of the visited cell. (Cell shapes and the operations you can do
with cells are discussed in
Chapter~\ref{chap:WorkingWithCells}.) This is the
same value that gets provided if you reference the
\textsignature{CellSetIn} parameter.
\item[\sigtag{PointCount}] This tag produces a \vtkm{IdComponent} equal to
the number of points incident on the cell being visited. The Vecs
provided from a \textsignature{FieldInPoint} parameter will be the same
size as \sigtag{PointCount}.
\item[\sigtag{PointIndices}] This tag produces a Vec-like object of
\vtkm{Id}s giving the indices for all incident points. Like values from a
\textsignature{FieldInPoint} parameter, the order of the entries is
consistent with the defined order of the vertices for the visited cell's
shape.
\commonexecutionsignaturetags
\end{description}
Point to cell field maps are a powerful construct that allow you to interpolate point fields throughout the space of the data set.
See Chapter~\ref{chap:WorkingWithCells} for a description on how to work with the cell information provided to the worklet.
The following example provides a simple demonstration that finds the geometric center of each cell by interpolating the point coordinates to the cell centers.
\vtkmlisting[ex:UseWorkletMapPointToCell]{Implementation and use of a map point to cell worklet.}{UseWorkletMapPointToCell.cxx}
\index{map point to cell|)}
\index{point to cell map worklet|)}
\index{worklet types!point to cell map|)}
\subsubsection{Cell To Point Map}
\label{sec:WorkletMapCellToPoint}
\index{worklet types!cell to point map|(}
\index{cell to point map worklet|(}
\index{map cell to point|(}
A worklet deriving \vtkmworklet{WorkletMapCellToPoint} performs a mapping
operation that applies a function (the operator in the worklet) on all the
points of a \textidentifier{DataSet}. While operating on the point, the
worklet has access to fields associated both with the point and with all
incident cells.
A \textidentifier{WorkletMapCellToPoint} subclass is invoked with a
\vtkmworklet{DispatcherMapTopology}. This dispatcher has two template
arguments. The first argument is the type of the worklet subclass. The
second argument, which is optional, is a device adapter tag.
A cell to point map worklet supports the following tags in the parameters
of its \controlsignature.
\begin{description}
\item[\sigtag{CellSetIn}] This tag represents the cell set that defines
the collection of points the map will operate on. A \sigtag{CellSetIn}
argument expects a \textidentifier{CellSet} subclass or a
\textidentifier{DynamicCellSet} in the associated parameter of the
dispatcher's \textcode{Invoke}.
There must be exactly one \sigtag{CellSetIn} argument, and the worklet's
\inputdomain must be set to this argument.
\item[\sigtag{FieldInCell}] This tag represents an input field that is
associated with the cells. A \sigtag{FieldInCell} argument expects an
\textidentifier{ArrayHandle} or a \textidentifier{DynamicArrayHandle} in
the associated parameter of the dispatcher's \textcode{Invoke}. The size
of the array must be exactly the number of cells.
Each invocation of the worklet gets a Vec-like object containing the
field values for all the cells incident with the point being visited. The
order of the entries is arbitrary but will be consistent with the values
of all other \sigtag{FieldInCell} arguments for the same worklet
invocation. If the field is a vector field, then the provided object is a
Vec of Vecs.
\sigtag{FieldInCell} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\item[\sigtag{FieldInPoint}] This tag represents an input field that is
associated with the points. A \sigtag{FieldInPoint} argument expects an
\textidentifier{ArrayHandle} or a \textidentifier{DynamicArrayHandle} in
the associated parameter of the dispatcher's \textcode{Invoke}. The size
of the array must be exactly the number of points. Each invocation of the
worklet gets a single value out of this array.
\sigtag{FieldInPoint} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\item[\sigtag{FieldOutPoint}] This tag represents an output field, which is
necessarily associated with points. A \sigtag{FieldOutPoint} argument
expects an \textidentifier{ArrayHandle} or a
\textidentifier{DynamicArrayHandle} in the associated parameter of the
dispatcher's \textcode{Invoke}. The array is resized before scheduling
begins, and each invocation of the worklet sets a single value in the
array.
\sigtag{FieldOutPoint} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\sigtag{FieldOut} is an alias for \sigtag{FieldOutPoint} (since output
arrays can only be defined on points).
\item[\sigtag{FieldInOutPoint}] This tag represents field that is both an
input and an output, which is necessarily associated with points. A
\sigtag{FieldInOutPoint} argument expects an \textidentifier{ArrayHandle}
or a \textidentifier{DynamicArrayHandle} in the associated parameter of
the dispatcher's \textcode{Invoke}. Each invocation of the worklet gets a
single value out of this array, which is replaced by the resulting value
after the worklet completes.
\sigtag{FieldInOutPoint} has a single template parameter that specifies
what data types are acceptable for the array. The type tags are described
in Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\sigtag{FieldInOut} is an alias for \sigtag{FieldInOutPoint} (since output
arrays can only be defined on points).
\commoncontrolsignaturetags
\end{description}
A field map worklet supports the following tags in the parameters of its
\executionsignature.
\begin{description}
\numericexecutionsignaturetags
\item[\sigtag{CellCount}] This tag produces a \vtkm{IdComponent} equal to
the number of cells incident on the point being visited. The Vecs
provided from a \textsignature{FieldInCell} parameter will be the same
size as \sigtag{CellCount}.
\item[\sigtag{CellIndices}] This tag produces a Vec-like object of
\vtkm{Id}s giving the indices for all incident cells. The
order of the entries is arbitrary but will be consistent with the values
of all other \textsignature{FieldInCell} arguments for the same worklet
invocation.
\commonexecutionsignaturetags
\end{description}
Cell to point field maps are typically used for converting fields
associated with cells to points so that they can be interpolated. The
following example does a simple averaging, but you can also implement other
strategies such as a volume weighted average.
\vtkmlisting{Implementation and use of a map cell to point worklet.}{UseWorkletMapCellToPoint.cxx}
\index{map cell to point|)}
\index{cell to point map worklet|)}
\index{worklet types!cell to point map|)}
\subsubsection{General Topology Maps}
\label{sec:WorkletMapTopology}
\index{worklet types!topology map|(}
\index{topology map worklet|(}
\index{map topology|(}
A worklet deriving \vtkmworklet{WorkletMapTopology} performs a mapping
operation that applies a function (the operator in the worklet) on all the
elements of a specified type from a \textidentifier{DataSet}. While
operating on each element, the worklet has access to fields associated both
with that element and with all incident elements of a different specified
type.
The \textidentifier{WorkletMapTopology} class is a template with two
template parameters. The first template parameter specifies the ``from''
topology element, and the second template parameter specifies the ``to''
topology element. The worklet is scheduled such that each instance is
associated with a particular ``to'' topology element and has access to
incident ``from'' topology elements.
\index{topology element tag|(}
\index{tag!topology element|(}
These from and to topology elements are specified with topology element
tags, which are defined in the \vtkmheader{vtkm}{TopologyElementTag.h}
header file. The available topology element tags are
\vtkm{TopologyElementTagCell}, \vtkm{TopologyElementTagPoint},
\vtkm{TopologyElementTagEdge}, and \vtkm{TopologyElementTagFace}, which
represent the cell, point, edge, and face elements, respectively.
\index{topology element tag|)}
\index{tag!topology element|)}
\textidentifier{WorkletMapTopology} is a generic form of a topology map,
and it can perform identically to the aforementioned forms of topology map
with the correct template parameters. For example,
\begin{quote}
\vtkmworklet{WorkletMapTopology}\textcode{<}%
\vtkm{TopologyElementTagPoint}\textcode{, }%
\vtkm{TopologyElementTagCell}\textcode{>}
\end{quote}
is equivalent to the \vtkmworklet{WorkletMapPointToCell} class except the
signature tags have different names. The names used in the specific
topology map superclasses (such as \textidentifier{WorkletMapPointToCell})
tend to be easier to read and are thus preferable. However, the generic
\textidentifier{WorkletMapTopology} is available for topology combinations
without a specific superclass or to support more general mappings in a
worklet.
The general topology map worklet supports the following tags in the
parameters of its \controlsignature, which are equivalent to tags in the
other topology maps but with different (more general) names.
\begin{description}
\item[\sigtag{CellSetIn}] This tag represents the cell set that defines
the collection of elements the map will operate on. A \sigtag{CellSetIn}
argument expects a \textidentifier{CellSet} subclass or a
\textidentifier{DynamicCellSet} in the associated parameter of the
dispatcher's \textcode{Invoke}. Each invocation of the worklet gets a
cell shape tag. (Cell shapes and the operations you can do with cells are
discussed in Chapter~\ref{chap:WorkingWithCells}.)
There must be exactly one \sigtag{CellSetIn} argument, and the worklet's
\inputdomain must be set to this argument.
\item[\sigtag{FieldInFrom}] This tag represents an input field that is
associated with the ``from'' elements. A \sigtag{FieldInFrom} argument
expects an \textidentifier{ArrayHandle} or a
\textidentifier{DynamicArrayHandle} in the associated parameter of the
dispatcher's \textcode{Invoke}. The size of the array must be exactly the
number of ``from'' elements.
Each invocation of the worklet gets a Vec-like object containing the
field values for all the ``from'' elements incident with the ``to''
element being visited. If the field is a vector field, then the provided
object is a Vec of Vecs.
\sigtag{FieldInFrom} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\item[\sigtag{FieldInTo}] This tag represents an input field that is
associated with the ``to'' element. A \sigtag{FieldInTo} argument expects
an \textidentifier{ArrayHandle} or a \textidentifier{DynamicArrayHandle}
in the associated parameter of the dispatcher's \textcode{Invoke}. The
size of the array must be exactly the number of cells. Each invocation of
the worklet gets a single value out of this array.
\sigtag{FieldInTo} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\item[\sigtag{FieldOut}] This tag represents an output field, which is
necessarily associated with ``to'' elements. A \sigtag{FieldOut} argument
expects an \textidentifier{ArrayHandle} or a
\textidentifier{DynamicArrayHandle} in the associated parameter of the
dispatcher's \textcode{Invoke}. The array is resized before scheduling
begins, and each invocation of the worklet sets a single value in the
array.
\sigtag{FieldOut} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\item[\sigtag{FieldInOut}] This tag represents field that is both an input
and an output, which is necessarily associated with ``to'' elements. A
\sigtag{FieldInOut} argument expects an \textidentifier{ArrayHandle} or a
\textidentifier{DynamicArrayHandle} in the associated parameter of the
dispatcher's \textcode{Invoke}. Each invocation of the worklet gets a
single value out of this array, which is replaced by the resulting value
after the worklet completes.
\sigtag{FieldInOut} has a single template parameter that specifies what
data types are acceptable for the array. The type tags are described in
Section~\ref{sec:TypeListTags} starting on page~\pageref{TypeTagList}.
\commoncontrolsignaturetags