This file is indexed.

/usr/share/doc/python-pyopencl-doc/html/algorithm.html is in python-pyopencl-doc 2016.1+git20161130-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Parallel Algorithms &#8212; PyOpenCL 2016.2 documentation</title>
    
    <link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '2016.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="top" title="PyOpenCL 2016.2 documentation" href="index.html" />
    <link rel="next" title="How-tos" href="howto.html" />
    <link rel="prev" title="Multi-dimensional arrays" href="array.html" />
   
  <link rel="stylesheet" href="_static/custom.css" type="text/css" />
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head>
  <body role="document">
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="parallel-algorithms">
<h1>Parallel Algorithms<a class="headerlink" href="#parallel-algorithms" title="Permalink to this headline"></a></h1>
<div class="section" id="module-pyopencl.elementwise">
<span id="element-wise-expression-evalution-map"></span><h2>Element-wise expression evalution (&#8220;map&#8221;)<a class="headerlink" href="#module-pyopencl.elementwise" title="Permalink to this headline"></a></h2>
<p>Evaluating involved expressions on <a class="reference internal" href="array.html#pyopencl.array.Array" title="pyopencl.array.Array"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.array.Array</span></code></a> instances by
using overloaded operators can be somewhat inefficient, because a new temporary
is created for each intermediate result. The functionality in the module
<a class="reference internal" href="#module-pyopencl.elementwise" title="pyopencl.elementwise"><code class="xref py py-mod docutils literal"><span class="pre">pyopencl.elementwise</span></code></a> contains tools to help generate kernels that
evaluate multi-stage expressions on one or several operands in a single pass.</p>
<dl class="class">
<dt id="pyopencl.elementwise.ElementwiseKernel">
<em class="property">class </em><code class="descclassname">pyopencl.elementwise.</code><code class="descname">ElementwiseKernel</code><span class="sig-paren">(</span><em>context</em>, <em>arguments</em>, <em>operation</em>, <em>name=&quot;kernel&quot;</em>, <em>preamble=&quot;&quot;</em>, <em>options=[]</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.elementwise.ElementwiseKernel" title="Permalink to this definition"></a></dt>
<dd><p>A kernel that takes a number of scalar or vector <em>arguments</em> and performs
an <em>operation</em> specified as a snippet of C on these arguments.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>arguments</strong> &#8211; a string formatted as a C argument list.</li>
<li><strong>operation</strong> &#8211; a snippet of C that carries out the desired &#8216;map&#8217;
operation.  The current index is available as the variable <em>i</em>.
<em>operation</em> may contain the statement <code class="docutils literal"><span class="pre">PYOPENCL_ELWISE_CONTINUE</span></code>,
which will terminate processing for the current element.</li>
<li><strong>name</strong> &#8211; the function name as which the kernel is compiled</li>
<li><strong>options</strong> &#8211; passed unmodified to <a class="reference internal" href="runtime_program.html#pyopencl.Program.build" title="pyopencl.Program.build"><code class="xref py py-meth docutils literal"><span class="pre">pyopencl.Program.build()</span></code></a>.</li>
<li><strong>preamble</strong> &#8211; a piece of C source code that gets inserted outside of the
function context in the elementwise operation&#8217;s kernel source code.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">Using a <cite>return</cite> statement in <em>operation</em> will lead to
incorrect results, as some elements may never get processed. Use
<code class="docutils literal"><span class="pre">PYOPENCL_ELWISE_CONTINUE</span></code> instead.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2013.1: </span>Added <code class="docutils literal"><span class="pre">PYOPENCL_ELWISE_CONTINUE</span></code>.</p>
</div>
<dl class="method">
<dt id="pyopencl.elementwise.ElementwiseKernel.__call__">
<code class="descname">__call__</code><span class="sig-paren">(</span><em>*args</em>, <em>wait_for=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.elementwise.ElementwiseKernel.__call__" title="Permalink to this definition"></a></dt>
<dd><p>Invoke the generated scalar kernel. The arguments may either be scalars or
<code class="xref py py-class docutils literal"><span class="pre">GPUArray</span></code> instances.</p>
<p>Returns a new <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a>. <em>wait_for</em>
may either be <em>None</em> or a list of <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> instances for
whose completion this command waits before starting exeuction.</p>
</dd></dl>

</dd></dl>

<p>Here&#8217;s a usage example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="o">..</span> <span class="n">literalinclude</span><span class="p">::</span> <span class="o">../</span><span class="n">examples</span><span class="o">/</span><span class="n">demo_elementwise</span><span class="o">.</span><span class="n">py</span>
</pre></div>
</div>
<p>(You can find this example as
<a class="reference download internal" href="_downloads/demo_elementwise.py" download=""><code class="xref download docutils literal"><span class="pre">examples/demo_elementwise.py</span></code></a>
in the PyOpenCL distribution.)</p>
</div>
<div class="section" id="module-pyopencl.reduction">
<span id="sums-and-counts-reduce"></span><span id="custom-reductions"></span><h2>Sums and counts (&#8220;reduce&#8221;)<a class="headerlink" href="#module-pyopencl.reduction" title="Permalink to this headline"></a></h2>
<dl class="class">
<dt id="pyopencl.reduction.ReductionKernel">
<em class="property">class </em><code class="descclassname">pyopencl.reduction.</code><code class="descname">ReductionKernel</code><span class="sig-paren">(</span><em>ctx</em>, <em>dtype_out</em>, <em>neutral</em>, <em>reduce_expr</em>, <em>map_expr=None</em>, <em>arguments=None</em>, <em>name=&quot;reduce_kernel&quot;</em>, <em>options=[]</em>, <em>preamble=&quot;&quot;</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.reduction.ReductionKernel" title="Permalink to this definition"></a></dt>
<dd><p>Generate a kernel that takes a number of scalar or vector <em>arguments</em>
(at least one vector argument), performs the <em>map_expr</em> on each entry of
the vector argument and then the <em>reduce_expr</em> on the outcome of that.
<em>neutral</em> serves as an initial value. <em>preamble</em> offers the possibility
to add preprocessor directives and other code (such as helper functions)
to be added before the actual reduction kernel code.</p>
<p>Vectors in <em>map_expr</em> should be indexed by the variable <em>i</em>. <em>reduce_expr</em>
uses the formal values &#8220;a&#8221; and &#8220;b&#8221; to indicate two operands of a binary
reduction operation. If you do not specify a <em>map_expr</em>, <code class="docutils literal"><span class="pre">in[i]</span></code> is
automatically assumed and treated as the only one input argument.</p>
<p><em>dtype_out</em> specifies the <a class="reference external" href="file:///usr/share/doc/python-numpy-doc/html/reference/generated/numpy.dtype.html#numpy.dtype" title="(in NumPy v1.11)"><code class="xref py py-class docutils literal"><span class="pre">numpy.dtype</span></code></a> in which the reduction is
performed and in which the result is returned. <em>neutral</em> is specified as
float or integer formatted as string. <em>reduce_expr</em> and <em>map_expr</em> are
specified as string formatted operations and <em>arguments</em> is specified as a
string formatted as a C argument list. <em>name</em> specifies the name as which
the kernel is compiled. <em>options</em> are passed unmodified to
<a class="reference internal" href="runtime_program.html#pyopencl.Program.build" title="pyopencl.Program.build"><code class="xref py py-meth docutils literal"><span class="pre">pyopencl.Program.build()</span></code></a>. <em>preamble</em> specifies a string of code that
is inserted before the actual kernels.</p>
<dl class="method">
<dt id="pyopencl.reduction.ReductionKernel.__call__">
<code class="descname">__call__</code><span class="sig-paren">(</span><em>*args</em>, <em>queue=None</em>, <em>wait_for=None</em>, <em>return_event=False</em>, <em>out=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.reduction.ReductionKernel.__call__" title="Permalink to this definition"></a></dt>
<dd><p><em>wait_for</em>
may either be <em>None</em> or a list of <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> instances for
whose completion this command waits before starting exeuction.</p>
<p>With <em>out</em> the resulting single-entry <a class="reference internal" href="array.html#pyopencl.array.Array" title="pyopencl.array.Array"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.array.Array</span></code></a> can
be specified. Because offsets are supported one can store results
anywhere (e.g. <code class="docutils literal"><span class="pre">out=a[3]</span></code>).</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Returns:</th><td class="field-body">the resulting scalar as a single-entry <a class="reference internal" href="array.html#pyopencl.array.Array" title="pyopencl.array.Array"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.array.Array</span></code></a>
if <em>return_event</em> is <em>False</em>, otherwise a tuple <code class="docutils literal"><span class="pre">(scalar_array,</span> <span class="pre">event)</span></code>.</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The returned <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> corresponds only to part of the
execution of the reduction. It is not suitable for profiling.</p>
</div>
</dd></dl>

<div class="versionadded">
<p><span class="versionmodified">New in version 2011.1.</span></p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2014.2: </span>Added <em>out</em> parameter.</p>
</div>
</dd></dl>

<p>Here&#8217;s a usage example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="o">=</span> <span class="n">pyopencl</span><span class="o">.</span><span class="n">array</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">queue</span><span class="p">,</span> <span class="mi">400</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">numpy</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">pyopencl</span><span class="o">.</span><span class="n">array</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">queue</span><span class="p">,</span> <span class="mi">400</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">numpy</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>

<span class="n">krnl</span> <span class="o">=</span> <span class="n">ReductionKernel</span><span class="p">(</span><span class="n">ctx</span><span class="p">,</span> <span class="n">numpy</span><span class="o">.</span><span class="n">float32</span><span class="p">,</span> <span class="n">neutral</span><span class="o">=</span><span class="s2">&quot;0&quot;</span><span class="p">,</span>
        <span class="n">reduce_expr</span><span class="o">=</span><span class="s2">&quot;a+b&quot;</span><span class="p">,</span> <span class="n">map_expr</span><span class="o">=</span><span class="s2">&quot;x[i]*y[i]&quot;</span><span class="p">,</span>
        <span class="n">arguments</span><span class="o">=</span><span class="s2">&quot;__global float *x, __global float *y&quot;</span><span class="p">)</span>

<span class="n">my_dot_prod</span> <span class="o">=</span> <span class="n">krnl</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="module-pyopencl.scan">
<span id="prefix-sums-scan"></span><span id="custom-scan"></span><h2>Prefix Sums (&#8220;scan&#8221;)<a class="headerlink" href="#module-pyopencl.scan" title="Permalink to this headline"></a></h2>
<p>A prefix sum is a running sum of an array, as provided by
e.g. <code class="xref py py-mod docutils literal"><span class="pre">numpy.cumsum</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">np</span><span class="o">.</span><span class="n">cumsum</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="go">array([ 1,  2,  3,  4,  5,  7,  9, 11, 13, 15])</span>
</pre></div>
</div>
<p>This is a very simple example of what a scan can do. It turns out that scans
are significantly more versatile. They are a basic building block of many
non-trivial parallel algorithms. Many of the operations enabled by scans seem
difficult to parallelize because of loop-carried dependencies.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.128.6230">Prefix sums and their applications</a>, by Guy Blelloch.</dt>
<dd>This article gives an overview of some surprising applications of scans.</dd>
<dt><a class="reference internal" href="#predefined-scans"><span class="std std-ref">Simple / Legacy Interface</span></a></dt>
<dd>These operations built into PyOpenCL are realized using <a class="reference internal" href="#pyopencl.scan.GenericScanKernel" title="pyopencl.scan.GenericScanKernel"><code class="xref py py-class docutils literal"><span class="pre">GenericScanKernel</span></code></a>.</dd>
</dl>
</div>
<div class="section" id="usage-example">
<h3>Usage Example<a class="headerlink" href="#usage-example" title="Permalink to this headline"></a></h3>
<p>This example illustrates the implementation of a simplified version of
<a class="reference internal" href="#pyopencl.algorithm.copy_if" title="pyopencl.algorithm.copy_if"><code class="xref py py-func docutils literal"><span class="pre">pyopencl.algorithm.copy_if()</span></code></a>,
which copies integers from an array into the (variable-size) output if they are
greater than 300:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">knl</span> <span class="o">=</span> <span class="n">GenericScanKernel</span><span class="p">(</span>
        <span class="n">ctx</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">,</span>
        <span class="n">arguments</span><span class="o">=</span><span class="s2">&quot;__global int *ary, __global int *out&quot;</span><span class="p">,</span>
        <span class="n">input_expr</span><span class="o">=</span><span class="s2">&quot;(ary[i] &gt; 300) ? 1 : 0&quot;</span><span class="p">,</span>
        <span class="n">scan_expr</span><span class="o">=</span><span class="s2">&quot;a+b&quot;</span><span class="p">,</span> <span class="n">neutral</span><span class="o">=</span><span class="s2">&quot;0&quot;</span><span class="p">,</span>
        <span class="n">output_statement</span><span class="o">=</span><span class="s2">&quot;&quot;&quot;</span>
<span class="s2">            if (prev_item != item) out[item-1] = ary[i];</span>
<span class="s2">            &quot;&quot;&quot;</span><span class="p">)</span>

<span class="n">out</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="n">knl</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">out</span><span class="p">)</span>

<span class="n">a_host</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
<span class="n">out_host</span> <span class="o">=</span> <span class="n">a_host</span><span class="p">[</span><span class="n">a_host</span> <span class="o">&gt;</span> <span class="mi">300</span><span class="p">]</span>

<span class="k">assert</span> <span class="p">(</span><span class="n">out_host</span> <span class="o">==</span> <span class="n">out</span><span class="o">.</span><span class="n">get</span><span class="p">()[:</span><span class="nb">len</span><span class="p">(</span><span class="n">out_host</span><span class="p">)])</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
</pre></div>
</div>
<p>The value being scanned over is a number of flags indicating whether each array
element is greater than 300. These flags are computed by <em>input_expr</em>. The
prefix sum over this array gives a running count of array items greater than
300. The <em>output_statement</em> the compares <cite>prev_item</cite> (the previous item&#8217;s scan
result, i.e. index) to <cite>item</cite> (the current item&#8217;s scan result, i.e.
index). If they differ, i.e. if the predicate was satisfied at this
position, then the item is stored in the output at the computed index.</p>
<p>This example does not make use of the following advanced features also available
in PyOpenCL:</p>
<ul class="simple">
<li>Segmented scans</li>
<li>Access to the previous item in <em>input_expr</em> (e.g. for comparisons)
See the <a class="reference external" href="https://github.com/pyopencl/pyopencl/blob/master/pyopencl/scan.py#L1353">implementation</a> of <code class="xref py py-func docutils literal"><span class="pre">unique()</span></code> for an example.</li>
</ul>
</div>
<div class="section" id="making-custom-scan-kernels">
<h3>Making Custom Scan Kernels<a class="headerlink" href="#making-custom-scan-kernels" title="Permalink to this headline"></a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2013.1.</span></p>
</div>
<dl class="class">
<dt id="pyopencl.scan.GenericScanKernel">
<em class="property">class </em><code class="descclassname">pyopencl.scan.</code><code class="descname">GenericScanKernel</code><span class="sig-paren">(</span><em>ctx</em>, <em>dtype</em>, <em>arguments</em>, <em>input_expr</em>, <em>scan_expr</em>, <em>neutral</em>, <em>output_statement</em>, <em>is_segment_start_expr=None</em>, <em>input_fetch_exprs=[]</em>, <em>index_dtype=&lt;class 'numpy.int32'&gt;</em>, <em>name_prefix='scan'</em>, <em>options=[]</em>, <em>preamble=''</em>, <em>devices=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.scan.GenericScanKernel" title="Permalink to this definition"></a></dt>
<dd><p>Generates and executes code that performs prefix sums (&#8220;scans&#8221;) on
arbitrary types, with many possible tweaks.</p>
<p>Usage example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pyopencl.scan</span> <span class="k">import</span> <span class="n">GenericScanKernel</span>
<span class="n">knl</span> <span class="o">=</span> <span class="n">GenericScanKernel</span><span class="p">(</span>
        <span class="n">context</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">,</span>
        <span class="n">arguments</span><span class="o">=</span><span class="s2">&quot;__global int *ary&quot;</span><span class="p">,</span>
        <span class="n">input_expr</span><span class="o">=</span><span class="s2">&quot;ary[i]&quot;</span><span class="p">,</span>
        <span class="n">scan_expr</span><span class="o">=</span><span class="s2">&quot;a+b&quot;</span><span class="p">,</span> <span class="n">neutral</span><span class="o">=</span><span class="s2">&quot;0&quot;</span><span class="p">,</span>
        <span class="n">output_statement</span><span class="o">=</span><span class="s2">&quot;ary[i+1] = item;&quot;</span><span class="p">)</span>

<span class="n">a</span> <span class="o">=</span> <span class="n">cl</span><span class="o">.</span><span class="n">array</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">queue</span><span class="p">,</span> <span class="mi">10000</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
<span class="n">scan_kernel</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">queue</span><span class="o">=</span><span class="n">queue</span><span class="p">)</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>ctx</strong> &#8211; a <a class="reference internal" href="runtime_platform.html#pyopencl.Context" title="pyopencl.Context"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Context</span></code></a> within which the code
for this scan kernel will be generated.</li>
<li><strong>dtype</strong> &#8211; the <a class="reference external" href="file:///usr/share/doc/python-numpy-doc/html/reference/generated/numpy.dtype.html#numpy.dtype" title="(in NumPy v1.11)"><code class="xref py py-class docutils literal"><span class="pre">numpy.dtype</span></code></a> with which the scan will
be performed. May be a structured type if that type was registered
through <a class="reference internal" href="array.html#pyopencl.tools.get_or_register_dtype" title="pyopencl.tools.get_or_register_dtype"><code class="xref py py-func docutils literal"><span class="pre">pyopencl.tools.get_or_register_dtype()</span></code></a>.</li>
<li><strong>arguments</strong> &#8211; A string of comma-separated C argument declarations.
If <em>arguments</em> is specified, then <em>input_expr</em> must also be
specified. All types used here must be known to PyOpenCL.
(see <a class="reference internal" href="array.html#pyopencl.tools.get_or_register_dtype" title="pyopencl.tools.get_or_register_dtype"><code class="xref py py-func docutils literal"><span class="pre">pyopencl.tools.get_or_register_dtype()</span></code></a>).</li>
<li><strong>scan_expr</strong> &#8211; <p>The associative, binary operation carrying out the scan,
represented as a C string. Its two arguments are available as <cite>a</cite>
and <cite>b</cite> when it is evaluated. <cite>b</cite> is guaranteed to be the
&#8216;element being updated&#8217;, and <cite>a</cite> is the increment. Thus,
if some data is supposed to just propagate along without being
modified by the scan, it should live in <cite>b</cite>.</p>
<p>This expression may call functions given in the <em>preamble</em>.</p>
<p>Another value available to this expression is <cite>across_seg_boundary</cite>,
a C <cite>bool</cite> indicating whether this scan update is crossing a
segment boundary, as defined by <cite>is_segment_start_expr</cite>.
The scan routine does not implement segmentation
semantics on its own. It relies on <cite>scan_expr</cite> to do this.
This value is available (but always <cite>false</cite>) even for a
non-segmented scan.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In early pre-releases of the segmented scan,
segmentation semantics were implemented <em>without</em>
relying on <cite>scan_expr</cite>.</p>
</div>
</li>
<li><strong>input_expr</strong> &#8211; <p>A C expression, encoded as a string, resulting
in the values to which the scan is applied. This may be used
to apply a mapping to values stored in <em>arguments</em> before being
scanned. The result of this expression must match <em>dtype</em>.
The index intended to be mapped is available as <cite>i</cite> in this
expression. This expression may also use the variables defined
by <em>input_fetch_expr</em>.</p>
<p>This expression may also call functions given in the <em>preamble</em>.</p>
</li>
<li><strong>output_statement</strong> &#8211; <p>a C statement that writes
the output of the scan. It has access to the scan result as <cite>item</cite>,
the preceding scan result item as <cite>prev_item</cite>, and the current index
as <cite>i</cite>. <cite>prev_item</cite> in a segmented scan will be the neutral element
at a segment boundary, not the immediately preceding item.</p>
<p>Using <em>prev_item</em> in output statement has a small run-time cost.
<cite>prev_item</cite> enables the construction of an exclusive scan.</p>
<p>For non-segmented scans, <em>output_statement</em> may also reference
<cite>last_item</cite>, which evaluates to the scan result of the last
array entry.</p>
</li>
<li><strong>is_segment_start_expr</strong> &#8211; <p>A C expression, encoded as a string,
resulting in a C <cite>bool</cite> value that determines whether a new
scan segments starts at index <em>i</em>.  If given, makes the scan a
segmented scan. Has access to the current index <cite>i</cite>, the result
of <em>input_expr</em> as a, and in addition may use <em>arguments</em> and
<em>input_fetch_expr</em> variables just like <em>input_expr</em>.</p>
<p>If it returns true, then previous sums will not spill over into the
item with index <em>i</em> or subsequent items.</p>
</li>
<li><strong>input_fetch_exprs</strong> &#8211; <p>a list of tuples <em>(NAME, ARG_NAME, OFFSET)</em>.
An entry here has the effect of doing the equivalent of the following
before input_expr:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">ARG_NAME_TYPE</span> <span class="n">NAME</span> <span class="o">=</span> <span class="n">ARG_NAME</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="n">OFFSET</span><span class="p">];</span>
</pre></div>
</div>
<p><cite>OFFSET</cite> is allowed to be 0 or -1, and <cite>ARG_NAME_TYPE</cite> is the type
of <cite>ARG_NAME</cite>.</p>
</li>
<li><strong>preamble</strong> &#8211; A snippet of C that is inserted into the compiled kernel
before the actual kernel function. May be used for, e.g. type definitions
or include statements.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>The first array in the argument list determines the size of the index
space over which the scan is carried out, and thus the values over
which the index <em>i</em> occurring in a number of code fragments in
arguments above will vary.</p>
<p>All code fragments further have access to N, the number of elements
being processed in the scan.</p>
<dl class="method">
<dt id="pyopencl.scan.GenericScanKernel.__call__">
<code class="descname">__call__</code><span class="sig-paren">(</span><em>*args</em>, <em>allocator=None</em>, <em>queue=None</em>, <em>size=None</em>, <em>wait_for=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.scan.GenericScanKernel.__call__" title="Permalink to this definition"></a></dt>
<dd><p><em>queue</em> and <em>allocator</em> default to the ones provided on the first
<a class="reference internal" href="array.html#pyopencl.array.Array" title="pyopencl.array.Array"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.array.Array</span></code></a> in <em>args</em>. <em>size</em> may specify the
length of the scan to be carried out. If not given, this length
is inferred from the first array argument passed.</p>
<p>Returns a new <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a>. <em>wait_for</em>
may either be <em>None</em> or a list of <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> instances for
whose completion this command waits before starting exeuction.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The returned <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> corresponds only to part of the
execution of the scan. It is not suitable for profiling.</p>
</div>
</dd></dl>

</dd></dl>

<div class="section" id="debugging-aids">
<h4>Debugging aids<a class="headerlink" href="#debugging-aids" title="Permalink to this headline"></a></h4>
<dl class="class">
<dt id="pyopencl.scan.GenericDebugScanKernel">
<em class="property">class </em><code class="descclassname">pyopencl.scan.</code><code class="descname">GenericDebugScanKernel</code><a class="headerlink" href="#pyopencl.scan.GenericDebugScanKernel" title="Permalink to this definition"></a></dt>
<dd><p>Performs the same function and has the same interface as
<a class="reference internal" href="#pyopencl.scan.GenericScanKernel" title="pyopencl.scan.GenericScanKernel"><code class="xref py py-class docutils literal"><span class="pre">GenericScanKernel</span></code></a>, but uses a dead-simple, sequential scan.  Works
best on CPU platforms, and helps isolate bugs in scans by removing the
potential for issues originating in parallel execution.</p>
</dd></dl>

</div>
</div>
<div class="section" id="simple-legacy-interface">
<span id="predefined-scans"></span><h3>Simple / Legacy Interface<a class="headerlink" href="#simple-legacy-interface" title="Permalink to this headline"></a></h3>
<dl class="class">
<dt id="pyopencl.scan.ExclusiveScanKernel">
<em class="property">class </em><code class="descclassname">pyopencl.scan.</code><code class="descname">ExclusiveScanKernel</code><span class="sig-paren">(</span><em>ctx</em>, <em>dtype</em>, <em>scan_expr</em>, <em>neutral</em>, <em>name_prefix=&quot;scan&quot;</em>, <em>options=[]</em>, <em>preamble=&quot;&quot;</em>, <em>devices=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.scan.ExclusiveScanKernel" title="Permalink to this definition"></a></dt>
<dd><p>Generates a kernel that can compute a <a class="reference external" href="https://secure.wikimedia.org/wikipedia/en/wiki/Prefix_sum">prefix sum</a>
using any associative operation given as <em>scan_expr</em>.
<em>scan_expr</em> uses the formal values &#8220;a&#8221; and &#8220;b&#8221; to indicate two operands of
an associative binary operation. <em>neutral</em> is the neutral element
of <em>scan_expr</em>, obeying <em>scan_expr(a, neutral) == a</em>.</p>
<p><em>dtype</em> specifies the type of the arrays being operated on.
<em>name_prefix</em> is used for kernel names to ensure recognizability
in profiles and logs. <em>options</em> is a list of compiler options to use
when building. <em>preamble</em> specifies a string of code that is
inserted before the actual kernels. <em>devices</em> may be used to restrict
the set of devices on which the kernel is meant to run. (defaults
to all devices in the context <em>ctx</em>.</p>
<dl class="method">
<dt id="pyopencl.scan.ExclusiveScanKernel.__call__">
<code class="descname">__call__</code><span class="sig-paren">(</span><em>self</em>, <em>input_ary</em>, <em>output_ary=None</em>, <em>allocator=None</em>, <em>queue=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.scan.ExclusiveScanKernel.__call__" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

</dd></dl>

<dl class="class">
<dt id="pyopencl.scan.InclusiveScanKernel">
<em class="property">class </em><code class="descclassname">pyopencl.scan.</code><code class="descname">InclusiveScanKernel</code><span class="sig-paren">(</span><em>dtype</em>, <em>scan_expr</em>, <em>neutral=None</em>, <em>name_prefix=&quot;scan&quot;</em>, <em>options=[]</em>, <em>preamble=&quot;&quot;</em>, <em>devices=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.scan.InclusiveScanKernel" title="Permalink to this definition"></a></dt>
<dd><p>Works like <a class="reference internal" href="#pyopencl.scan.ExclusiveScanKernel" title="pyopencl.scan.ExclusiveScanKernel"><code class="xref py py-class docutils literal"><span class="pre">ExclusiveScanKernel</span></code></a>.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2013.1: </span><em>neutral</em> is now always required.</p>
</div>
</dd></dl>

<p>For the array <cite>[1,2,3]</cite>, inclusive scan results in <cite>[1,3,6]</cite>, and exclusive
scan results in <cite>[0,1,3]</cite>.</p>
<p>Here&#8217;s a usage example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">knl</span> <span class="o">=</span> <span class="n">InclusiveScanKernel</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">,</span> <span class="s2">&quot;a+b&quot;</span><span class="p">)</span>

<span class="n">n</span> <span class="o">=</span> <span class="mi">2</span><span class="o">**</span><span class="mi">20</span><span class="o">-</span><span class="mi">2</span><span class="o">**</span><span class="mi">18</span><span class="o">+</span><span class="mi">5</span>
<span class="n">host_data</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randint</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="n">n</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)</span>
<span class="n">dev_data</span> <span class="o">=</span> <span class="n">cl_array</span><span class="o">.</span><span class="n">to_device</span><span class="p">(</span><span class="n">queue</span><span class="p">,</span> <span class="n">host_data</span><span class="p">)</span>

<span class="n">knl</span><span class="p">(</span><span class="n">dev_data</span><span class="p">)</span>
<span class="k">assert</span> <span class="p">(</span><span class="n">dev_data</span><span class="o">.</span><span class="n">get</span><span class="p">()</span> <span class="o">==</span> <span class="n">np</span><span class="o">.</span><span class="n">cumsum</span><span class="p">(</span><span class="n">host_data</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">))</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="module-pyopencl.algorithm">
<span id="predicated-copies-partition-unique"></span><h2>Predicated copies (&#8220;partition&#8221;, &#8220;unique&#8221;, ...)<a class="headerlink" href="#module-pyopencl.algorithm" title="Permalink to this headline"></a></h2>
<dl class="function">
<dt id="pyopencl.algorithm.copy_if">
<code class="descclassname">pyopencl.algorithm.</code><code class="descname">copy_if</code><span class="sig-paren">(</span><em>ary</em>, <em>predicate</em>, <em>extra_args=[]</em>, <em>preamble=''</em>, <em>queue=None</em>, <em>wait_for=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.algorithm.copy_if" title="Permalink to this definition"></a></dt>
<dd><p>Copy the elements of <em>ary</em> satisfying <em>predicate</em> to an output array.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>predicate</strong> &#8211; a C expression evaluating to a <cite>bool</cite>, represented as a string.
The value to test is available as <cite>ary[i]</cite>, and if the expression evaluates
to <cite>true</cite>, then this value ends up in the output.</li>
<li><strong>extra_args</strong> &#8211; a list of tuples <em>(name, value)</em> specifying
extra arguments to pass to the scan procedure. For version 2013.1,
<em>value</em> must be a of a <a class="reference external" href="file:///usr/share/doc/python-numpy-doc/html/reference/index.html#module-numpy" title="(in NumPy v1.11)"><code class="xref py py-mod docutils literal"><span class="pre">numpy</span></code></a> sized scalar type. As of version 2013.2,
<em>value</em> may also be a <a class="reference internal" href="array.html#pyopencl.array.Array" title="pyopencl.array.Array"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.array.Array</span></code></a>.</li>
<li><strong>preamble</strong> &#8211; A snippet of C that is inserted into the compiled kernel
before the actual kernel function. May be used for, e.g. type definitions
or include statements.</li>
<li><strong>wait_for</strong> &#8211; <em>wait_for</em>
may either be <em>None</em> or a list of <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> instances for
whose completion this command waits before starting exeuction.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">a tuple <em>(out, count, event)</em> where <em>out</em> is the output array, <em>count</em>
is an on-device scalar (fetch to host with <cite>count.get()</cite>) indicating
how many elements satisfied <em>predicate</em>, and <em>event</em> is a
<a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> for dependency management. <em>out</em> is allocated
to the same length as <em>ary</em>, but only the first <em>count</em> entries carry
meaning.</p>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 2013.1.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="pyopencl.algorithm.remove_if">
<code class="descclassname">pyopencl.algorithm.</code><code class="descname">remove_if</code><span class="sig-paren">(</span><em>ary</em>, <em>predicate</em>, <em>extra_args=[]</em>, <em>preamble=''</em>, <em>queue=None</em>, <em>wait_for=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.algorithm.remove_if" title="Permalink to this definition"></a></dt>
<dd><p>Copy the elements of <em>ary</em> not satisfying <em>predicate</em> to an output array.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>predicate</strong> &#8211; a C expression evaluating to a <cite>bool</cite>, represented as a string.
The value to test is available as <cite>ary[i]</cite>, and if the expression evaluates
to <cite>false</cite>, then this value ends up in the output.</li>
<li><strong>extra_args</strong> &#8211; a list of tuples <em>(name, value)</em> specifying
extra arguments to pass to the scan procedure. For version 2013.1,
<em>value</em> must be a of a <a class="reference external" href="file:///usr/share/doc/python-numpy-doc/html/reference/index.html#module-numpy" title="(in NumPy v1.11)"><code class="xref py py-mod docutils literal"><span class="pre">numpy</span></code></a> sized scalar type. As of version 2013.2,
<em>value</em> may also be a <a class="reference internal" href="array.html#pyopencl.array.Array" title="pyopencl.array.Array"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.array.Array</span></code></a>.</li>
<li><strong>preamble</strong> &#8211; A snippet of C that is inserted into the compiled kernel
before the actual kernel function. May be used for, e.g. type definitions
or include statements.</li>
<li><strong>wait_for</strong> &#8211; <em>wait_for</em>
may either be <em>None</em> or a list of <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> instances for
whose completion this command waits before starting exeuction.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">a tuple <em>(out, count, event)</em> where <em>out</em> is the output array, <em>count</em>
is an on-device scalar (fetch to host with <cite>count.get()</cite>) indicating
how many elements did not satisfy <em>predicate</em>, and <em>event</em> is a
<a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> for dependency management.</p>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 2013.1.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="pyopencl.algorithm.partition">
<code class="descclassname">pyopencl.algorithm.</code><code class="descname">partition</code><span class="sig-paren">(</span><em>ary</em>, <em>predicate</em>, <em>extra_args=[]</em>, <em>preamble=''</em>, <em>queue=None</em>, <em>wait_for=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.algorithm.partition" title="Permalink to this definition"></a></dt>
<dd><p>Copy the elements of <em>ary</em> into one of two arrays depending on whether
they satisfy <em>predicate</em>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>predicate</strong> &#8211; a C expression evaluating to a <cite>bool</cite>, represented as a string.
The value to test is available as <cite>ary[i]</cite>.</li>
<li><strong>extra_args</strong> &#8211; a list of tuples <em>(name, value)</em> specifying
extra arguments to pass to the scan procedure. For version 2013.1,
<em>value</em> must be a of a <a class="reference external" href="file:///usr/share/doc/python-numpy-doc/html/reference/index.html#module-numpy" title="(in NumPy v1.11)"><code class="xref py py-mod docutils literal"><span class="pre">numpy</span></code></a> sized scalar type. As of version 2013.2,
<em>value</em> may also be a <a class="reference internal" href="array.html#pyopencl.array.Array" title="pyopencl.array.Array"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.array.Array</span></code></a>.</li>
<li><strong>preamble</strong> &#8211; A snippet of C that is inserted into the compiled kernel
before the actual kernel function. May be used for, e.g. type definitions
or include statements.</li>
<li><strong>wait_for</strong> &#8211; <em>wait_for</em>
may either be <em>None</em> or a list of <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> instances for
whose completion this command waits before starting exeuction.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">a tuple <em>(out_true, out_false, count, event)</em> where <em>count</em>
is an on-device scalar (fetch to host with <cite>count.get()</cite>) indicating
how many elements satisfied the predicate, and <em>event</em> is a
<a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> for dependency management.</p>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 2013.1.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="pyopencl.algorithm.unique">
<code class="descclassname">pyopencl.algorithm.</code><code class="descname">unique</code><span class="sig-paren">(</span><em>ary</em>, <em>is_equal_expr='a == b'</em>, <em>extra_args=[]</em>, <em>preamble=''</em>, <em>queue=None</em>, <em>wait_for=None</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.algorithm.unique" title="Permalink to this definition"></a></dt>
<dd><p>Copy the elements of <em>ary</em> into the output if <em>is_equal_expr</em>, applied to the
array element and its predecessor, yields false.</p>
<p>Works like the UNIX command <strong class="program">uniq</strong>, with a potentially custom
comparison.  This operation is often used on sorted sequences.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>is_equal_expr</strong> &#8211; a C expression evaluating to a <cite>bool</cite>,
represented as a string.  The elements being compared are
available as <cite>a</cite> and <cite>b</cite>. If this expression yields <cite>false</cite>, the
two are considered distinct.</li>
<li><strong>extra_args</strong> &#8211; a list of tuples <em>(name, value)</em> specifying
extra arguments to pass to the scan procedure. For version 2013.1,
<em>value</em> must be a of a <a class="reference external" href="file:///usr/share/doc/python-numpy-doc/html/reference/index.html#module-numpy" title="(in NumPy v1.11)"><code class="xref py py-mod docutils literal"><span class="pre">numpy</span></code></a> sized scalar type. As of version 2013.2,
<em>value</em> may also be a <a class="reference internal" href="array.html#pyopencl.array.Array" title="pyopencl.array.Array"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.array.Array</span></code></a>.</li>
<li><strong>preamble</strong> &#8211; A snippet of C that is inserted into the compiled kernel
before the actual kernel function. May be used for, e.g. type definitions
or include statements.</li>
<li><strong>wait_for</strong> &#8211; <em>wait_for</em>
may either be <em>None</em> or a list of <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> instances for
whose completion this command waits before starting exeuction.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">a tuple <em>(out, count, event)</em> where <em>out</em> is the output array, <em>count</em>
is an on-device scalar (fetch to host with <cite>count.get()</cite>) indicating
how many elements satisfied the predicate, and <em>event</em> is a
<a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> for dependency management.</p>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 2013.1.</span></p>
</div>
</dd></dl>

</div>
<div class="section" id="sorting-radix-sort">
<h2>Sorting (radix sort)<a class="headerlink" href="#sorting-radix-sort" title="Permalink to this headline"></a></h2>
<dl class="class">
<dt id="pyopencl.algorithm.RadixSort">
<em class="property">class </em><code class="descclassname">pyopencl.algorithm.</code><code class="descname">RadixSort</code><span class="sig-paren">(</span><em>context</em>, <em>arguments</em>, <em>key_expr</em>, <em>sort_arg_names</em>, <em>bits_at_a_time=2</em>, <em>index_dtype=&lt;class 'numpy.int32'&gt;</em>, <em>key_dtype=&lt;class 'numpy.uint32'&gt;</em>, <em>scan_kernel=&lt;class 'pyopencl.scan.GenericScanKernel'&gt;</em>, <em>options=[]</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.algorithm.RadixSort" title="Permalink to this definition"></a></dt>
<dd><p>Provides a general <a class="reference external" href="https://en.wikipedia.org/wiki/Radix_sort">radix sort</a>
on the compute device.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.algorithm.BitonicSort</span></code></p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2013.1.</span></p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>arguments</strong> &#8211; A string of comma-separated C argument declarations.
If <em>arguments</em> is specified, then <em>input_expr</em> must also be
specified. All types used here must be known to PyOpenCL.
(see <a class="reference internal" href="array.html#pyopencl.tools.get_or_register_dtype" title="pyopencl.tools.get_or_register_dtype"><code class="xref py py-func docutils literal"><span class="pre">pyopencl.tools.get_or_register_dtype()</span></code></a>).</li>
<li><strong>key_expr</strong> &#8211; An integer-valued C expression returning the
key based on which the sort is performed. The array index
for which the key is to be computed is available as <cite>i</cite>.
The expression may refer to any of the <em>arguments</em>.</li>
<li><strong>sort_arg_names</strong> &#8211; A list of argument names whose corresponding
array arguments will be sorted according to <em>key_expr</em>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="pyopencl.algorithm.RadixSort.__call__">
<code class="descname">__call__</code><span class="sig-paren">(</span><em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.algorithm.RadixSort.__call__" title="Permalink to this definition"></a></dt>
<dd><p>Run the radix sort. In addition to <em>args</em> which must match the
<em>arguments</em> specification on the constructor, the following
keyword arguments are supported:</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>key_bits</strong> &#8211; specify how many bits (starting from least-significant)
there are in the key.</li>
<li><strong>allocator</strong> &#8211; See the <em>allocator</em> argument of <a class="reference internal" href="array.html#pyopencl.array.empty" title="pyopencl.array.empty"><code class="xref py py-func docutils literal"><span class="pre">pyopencl.array.empty()</span></code></a>.</li>
<li><strong>queue</strong> &#8211; A <a class="reference internal" href="runtime_queue.html#pyopencl.CommandQueue" title="pyopencl.CommandQueue"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.CommandQueue</span></code></a>, defaulting to the
one from the first argument array.</li>
<li><strong>wait_for</strong> &#8211; <em>wait_for</em>
may either be <em>None</em> or a list of <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> instances for
whose completion this command waits before starting exeuction.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">A tuple <code class="docutils literal"><span class="pre">(sorted,</span> <span class="pre">event)</span></code>. <em>sorted</em> consists of sorted
copies of the arrays named in <em>sorted_args</em>, in the order of that
list. <em>event</em> is a <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> for dependency management.</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="building-many-variable-size-lists">
<h2>Building many variable-size lists<a class="headerlink" href="#building-many-variable-size-lists" title="Permalink to this headline"></a></h2>
<dl class="class">
<dt id="pyopencl.algorithm.ListOfListsBuilder">
<em class="property">class </em><code class="descclassname">pyopencl.algorithm.</code><code class="descname">ListOfListsBuilder</code><span class="sig-paren">(</span><em>context</em>, <em>list_names_and_dtypes</em>, <em>generate_template</em>, <em>arg_decls</em>, <em>count_sharing=None</em>, <em>devices=None</em>, <em>name_prefix='plb_build_list'</em>, <em>options=[]</em>, <em>preamble=''</em>, <em>debug=False</em>, <em>complex_kernel=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.algorithm.ListOfListsBuilder" title="Permalink to this definition"></a></dt>
<dd><p>Generates and executes code to produce a large number of variable-size
lists, simply.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This functionality is provided as a preview. Its interface
is subject to change until this notice is removed.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 2013.1.</span></p>
</div>
<p>Here&#8217;s a usage example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pyopencl.algorithm</span> <span class="k">import</span> <span class="n">ListOfListsBuilder</span>
<span class="n">builder</span> <span class="o">=</span> <span class="n">ListOfListsBuilder</span><span class="p">(</span><span class="n">context</span><span class="p">,</span> <span class="p">[(</span><span class="s2">&quot;mylist&quot;</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">int32</span><span class="p">)],</span> <span class="s2">&quot;&quot;&quot;</span>
<span class="s2">        void generate(LIST_ARG_DECL USER_ARG_DECL index_type i)</span>
<span class="s2">        {</span>
<span class="s2">            int count = i % 4;</span>
<span class="s2">            for (int j = 0; j &lt; count; ++j)</span>
<span class="s2">            {</span>
<span class="s2">                APPEND_mylist(count);</span>
<span class="s2">            }</span>
<span class="s2">        }</span>
<span class="s2">        &quot;&quot;&quot;</span><span class="p">,</span> <span class="n">arg_decls</span><span class="o">=</span><span class="p">[])</span>

<span class="n">result</span><span class="p">,</span> <span class="n">event</span> <span class="o">=</span> <span class="n">builder</span><span class="p">(</span><span class="n">queue</span><span class="p">,</span> <span class="mi">2000</span><span class="p">)</span>

<span class="n">inf</span> <span class="o">=</span> <span class="n">result</span><span class="p">[</span><span class="s2">&quot;mylist&quot;</span><span class="p">]</span>
<span class="k">assert</span> <span class="n">inf</span><span class="o">.</span><span class="n">count</span> <span class="o">==</span> <span class="mi">3000</span>
<span class="k">assert</span> <span class="p">(</span><span class="n">inf</span><span class="o">.</span><span class="n">list</span><span class="o">.</span><span class="n">get</span><span class="p">()[</span><span class="o">-</span><span class="mi">6</span><span class="p">:]</span> <span class="o">==</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span><span class="o">.</span><span class="n">all</span><span class="p">()</span>
</pre></div>
</div>
<p>The function <cite>generate</cite> above is called once for each &#8220;input object&#8221;.
Each input object can then generate zero or more list entries.
The number of these input objects is given to <a class="reference internal" href="#pyopencl.algorithm.ListOfListsBuilder.__call__" title="pyopencl.algorithm.ListOfListsBuilder.__call__"><code class="xref py py-meth docutils literal"><span class="pre">__call__()</span></code></a> as <em>n_objects</em>.
List entries are generated by calls to <cite>APPEND_&lt;list name&gt;(value)</cite>.
Multiple lists may be generated at once.</p>
<dl class="method">
<dt id="pyopencl.algorithm.ListOfListsBuilder.__init__">
<code class="descname">__init__</code><span class="sig-paren">(</span><em>context</em>, <em>list_names_and_dtypes</em>, <em>generate_template</em>, <em>arg_decls</em>, <em>count_sharing=None</em>, <em>devices=None</em>, <em>name_prefix='plb_build_list'</em>, <em>options=[]</em>, <em>preamble=''</em>, <em>debug=False</em>, <em>complex_kernel=False</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.algorithm.ListOfListsBuilder.__init__" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>context</strong> &#8211; A <a class="reference internal" href="runtime_platform.html#pyopencl.Context" title="pyopencl.Context"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Context</span></code></a>.</li>
<li><strong>list_names_and_dtypes</strong> &#8211; a list of <cite>(name, dtype)</cite> tuples
indicating the lists to be built.</li>
<li><strong>generate_template</strong> &#8211; a snippet of C as described below</li>
<li><strong>arg_decls</strong> &#8211; A string of comma-separated C argument declarations.</li>
<li><strong>count_sharing</strong> &#8211; A mapping consisting of <cite>(child, mother)</cite>
indicating that <cite>mother</cite> and <cite>child</cite> will always have the
same number of indices, and the <cite>APPEND</cite> to <cite>mother</cite>
will always happen <em>before</em> the <cite>APPEND</cite> to the child.</li>
<li><strong>name_prefix</strong> &#8211; the name prefix to use for the compiled kernels</li>
<li><strong>options</strong> &#8211; OpenCL compilation options for kernels using
<em>generate_template</em>.</li>
<li><strong>complex_kernel</strong> &#8211; If <cite>True</cite>, prevents vectorization on CPUs.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p><em>generate_template</em> may use the following C macros/identifiers:</p>
<ul class="simple">
<li><cite>index_type</cite>: expands to C identifier for the index type used
for the calculation</li>
<li><cite>USER_ARG_DECL</cite>: expands to the C declarator for <cite>arg_decls</cite></li>
<li><cite>USER_ARGS</cite>: a list of C argument values corresponding to
<cite>user_arg_decl</cite></li>
<li><cite>LIST_ARG_DECL</cite>: expands to a C argument list representing the
data for the output lists. These are escaped prefixed with
<cite>&#8220;plg_&#8221;</cite> so as to not interfere with user-provided names.</li>
<li><cite>LIST_ARGS</cite>: a list of C argument values corresponding to
<cite>LIST_ARG_DECL</cite></li>
<li><cite>APPEND_name(entry)</cite>: inserts <cite>entry</cite> into the list <cite>name</cite>.
<em>entry</em> must be a valid C expression of the correct type.</li>
</ul>
<p>All argument-list related macros have a trailing comma included
if they are non-empty.</p>
<p><em>generate_template</em> must supply a function:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="kt">void</span> <span class="nf">generate</span><span class="p">(</span><span class="n">USER_ARG_DECL</span> <span class="n">LIST_ARG_DECL</span> <span class="n">index_type</span> <span class="n">i</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">APPEND_mylist</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Internally, the <cite>kernel_template</cite> is expanded (at least) twice. Once,
for a &#8216;counting&#8217; stage where the size of all the lists is determined,
and a second time, for a &#8216;generation&#8217; stage where the lists are
actually filled. A <cite>generate</cite> function that has side effects beyond
calling <cite>append</cite> is therefore ill-formed.</p>
</dd></dl>

<dl class="method">
<dt id="pyopencl.algorithm.ListOfListsBuilder.__call__">
<code class="descname">__call__</code><span class="sig-paren">(</span><em>queue</em>, <em>n_objects</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.algorithm.ListOfListsBuilder.__call__" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>args</strong> &#8211; arguments corresponding to arg_decls in the constructor.
<a class="reference internal" href="array.html#pyopencl.array.Array" title="pyopencl.array.Array"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.array.Array</span></code></a> are not allowed directly and should
be passed as their <a class="reference internal" href="array.html#pyopencl.array.Array.data" title="pyopencl.array.Array.data"><code class="xref py py-attr docutils literal"><span class="pre">pyopencl.array.Array.data</span></code></a> attribute instead.</li>
<li><strong>allocator</strong> &#8211; optionally, the allocator to use to allocate new
arrays.</li>
<li><strong>omit_lists</strong> &#8211; An iterable of list names that should <em>not</em> be built
with this invocation. The kernel code may <em>not</em> call <code class="docutils literal"><span class="pre">APPEND_name</span></code>
for these omitted lists. If it does, undefined behavior will result.
The returned <em>lists</em> dictionary will not contain an entry for names
in <em>omit_lists</em>.</li>
<li><strong>wait_for</strong> &#8211; <em>wait_for</em>
may either be <em>None</em> or a list of <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> instances for
whose completion this command waits before starting exeuction.</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last"><p>a tuple <code class="docutils literal"><span class="pre">(lists,</span> <span class="pre">event)</span></code>, where
<em>lists</em> a mapping from (built) list names to objects which
have attributes</p>
<ul>
<li><p class="first"><code class="docutils literal"><span class="pre">count</span></code> for the total number of entries in all lists combined</p>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">lists</span></code> for the array containing all lists.</p>
</li>
<li><p class="first"><code class="docutils literal"><span class="pre">starts</span></code> for the array of starting indices in <cite>lists</cite>.
<cite>starts</cite> is built so that it has n+1 entries, so that
the <em>i</em>&#8216;th entry is the start of the <em>i</em>&#8216;th list, and the
<em>i</em>&#8216;th entry is the index one past the <em>i</em>&#8216;th list&#8217;s end,
even for the last list.</p>
<p>This implies that all lists are contiguous.</p>
<p><em>event</em> is a <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> for dependency management.</p>
</li>
</ul>
</p>
</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2016.2: </span>Added omit_lists.</p>
</div>
</dd></dl>

<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>context</strong> &#8211; A <a class="reference internal" href="runtime_platform.html#pyopencl.Context" title="pyopencl.Context"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Context</span></code></a>.</li>
<li><strong>list_names_and_dtypes</strong> &#8211; a list of <cite>(name, dtype)</cite> tuples
indicating the lists to be built.</li>
<li><strong>generate_template</strong> &#8211; a snippet of C as described below</li>
<li><strong>arg_decls</strong> &#8211; A string of comma-separated C argument declarations.</li>
<li><strong>count_sharing</strong> &#8211; A mapping consisting of <cite>(child, mother)</cite>
indicating that <cite>mother</cite> and <cite>child</cite> will always have the
same number of indices, and the <cite>APPEND</cite> to <cite>mother</cite>
will always happen <em>before</em> the <cite>APPEND</cite> to the child.</li>
<li><strong>name_prefix</strong> &#8211; the name prefix to use for the compiled kernels</li>
<li><strong>options</strong> &#8211; OpenCL compilation options for kernels using
<em>generate_template</em>.</li>
<li><strong>complex_kernel</strong> &#8211; If <cite>True</cite>, prevents vectorization on CPUs.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p><em>generate_template</em> may use the following C macros/identifiers:</p>
<ul class="simple">
<li><cite>index_type</cite>: expands to C identifier for the index type used
for the calculation</li>
<li><cite>USER_ARG_DECL</cite>: expands to the C declarator for <cite>arg_decls</cite></li>
<li><cite>USER_ARGS</cite>: a list of C argument values corresponding to
<cite>user_arg_decl</cite></li>
<li><cite>LIST_ARG_DECL</cite>: expands to a C argument list representing the
data for the output lists. These are escaped prefixed with
<cite>&#8220;plg_&#8221;</cite> so as to not interfere with user-provided names.</li>
<li><cite>LIST_ARGS</cite>: a list of C argument values corresponding to
<cite>LIST_ARG_DECL</cite></li>
<li><cite>APPEND_name(entry)</cite>: inserts <cite>entry</cite> into the list <cite>name</cite>.
<em>entry</em> must be a valid C expression of the correct type.</li>
</ul>
<p>All argument-list related macros have a trailing comma included
if they are non-empty.</p>
<p><em>generate_template</em> must supply a function:</p>
<div class="highlight-c"><div class="highlight"><pre><span></span><span class="kt">void</span> <span class="nf">generate</span><span class="p">(</span><span class="n">USER_ARG_DECL</span> <span class="n">LIST_ARG_DECL</span> <span class="n">index_type</span> <span class="n">i</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">APPEND_mylist</span><span class="p">(</span><span class="mi">5</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Internally, the <cite>kernel_template</cite> is expanded (at least) twice. Once,
for a &#8216;counting&#8217; stage where the size of all the lists is determined,
and a second time, for a &#8216;generation&#8217; stage where the lists are
actually filled. A <cite>generate</cite> function that has side effects beyond
calling <cite>append</cite> is therefore ill-formed.</p>
</dd></dl>

</div>
<div class="section" id="module-pyopencl.bitonic_sort">
<span id="bitonic-sort"></span><h2>Bitonic Sort<a class="headerlink" href="#module-pyopencl.bitonic_sort" title="Permalink to this headline"></a></h2>
<dl class="class">
<dt id="pyopencl.bitonic_sort.BitonicSort">
<em class="property">class </em><code class="descclassname">pyopencl.bitonic_sort.</code><code class="descname">BitonicSort</code><span class="sig-paren">(</span><em>context</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.bitonic_sort.BitonicSort" title="Permalink to this definition"></a></dt>
<dd><p>Sort an array (or one axis of one) using a sorting network.</p>
<p>Will only work if the axis of the array to be sorted has a length
that is a power of 2.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 2015.2.</span></p>
</div>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last"><a class="reference internal" href="#pyopencl.algorithm.RadixSort" title="pyopencl.algorithm.RadixSort"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.algorithm.RadixSort</span></code></a></p>
</div>
<dl class="method">
<dt id="pyopencl.bitonic_sort.BitonicSort.__call__">
<code class="descname">__call__</code><span class="sig-paren">(</span><em>arr</em>, <em>idx=None</em>, <em>queue=None</em>, <em>wait_for=None</em>, <em>axis=0</em><span class="sig-paren">)</span><a class="headerlink" href="#pyopencl.bitonic_sort.BitonicSort.__call__" title="Permalink to this definition"></a></dt>
<dd><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first simple">
<li><strong>arr</strong> &#8211; the array to be sorted. Will be overwritten with the sorted array.</li>
<li><strong>idx</strong> &#8211; an array of indices to be tracked along with the sorting of <em>arr</em></li>
<li><strong>queue</strong> &#8211; a <a class="reference internal" href="runtime_queue.html#pyopencl.CommandQueue" title="pyopencl.CommandQueue"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.CommandQueue</span></code></a>, defaults to the array&#8217;s queue
if None</li>
<li><strong>wait_for</strong> &#8211; a list of <a class="reference internal" href="runtime_queue.html#pyopencl.Event" title="pyopencl.Event"><code class="xref py py-class docutils literal"><span class="pre">pyopencl.Event</span></code></a> instances or None</li>
<li><strong>axis</strong> &#8211; the axis of the array by which to sort</li>
</ul>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first last">a tuple (sorted_array, event)</p>
</td>
</tr>
</tbody>
</table>
</dd></dl>

</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="index.html">PyOpenCL</a></h1>








<h3>Navigation</h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="runtime.html">OpenCL Runtime: Basics</a></li>
<li class="toctree-l1"><a class="reference internal" href="runtime_const.html">OpenCL Runtime: Constants</a></li>
<li class="toctree-l1"><a class="reference internal" href="runtime_platform.html">OpenCL Runtime: Platforms, Devices and Contexts</a></li>
<li class="toctree-l1"><a class="reference internal" href="runtime_queue.html">OpenCL Runtime: Command Queues and Events</a></li>
<li class="toctree-l1"><a class="reference internal" href="runtime_memory.html">OpenCL Runtime: Memory</a></li>
<li class="toctree-l1"><a class="reference internal" href="runtime_program.html">OpenCL Runtime: Programs and Kernels</a></li>
<li class="toctree-l1"><a class="reference internal" href="runtime_gl.html">OpenCL Runtime: OpenGL Interoperability</a></li>
<li class="toctree-l1"><a class="reference internal" href="array.html">Multi-dimensional arrays</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Parallel Algorithms</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#module-pyopencl.elementwise">Element-wise expression evalution (&#8220;map&#8221;)</a></li>
<li class="toctree-l2"><a class="reference internal" href="#module-pyopencl.reduction">Sums and counts (&#8220;reduce&#8221;)</a></li>
<li class="toctree-l2"><a class="reference internal" href="#module-pyopencl.scan">Prefix Sums (&#8220;scan&#8221;)</a></li>
<li class="toctree-l2"><a class="reference internal" href="#module-pyopencl.algorithm">Predicated copies (&#8220;partition&#8221;, &#8220;unique&#8221;, ...)</a></li>
<li class="toctree-l2"><a class="reference internal" href="#sorting-radix-sort">Sorting (radix sort)</a></li>
<li class="toctree-l2"><a class="reference internal" href="#building-many-variable-size-lists">Building many variable-size lists</a></li>
<li class="toctree-l2"><a class="reference internal" href="#module-pyopencl.bitonic_sort">Bitonic Sort</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="howto.html">How-tos</a></li>
<li class="toctree-l1"><a class="reference internal" href="tools.html">Built-in Utilities</a></li>
<li class="toctree-l1"><a class="reference internal" href="misc.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="misc.html#tips">Tips</a></li>
<li class="toctree-l1"><a class="reference internal" href="misc.html#guidelines">Guidelines</a></li>
<li class="toctree-l1"><a class="reference internal" href="misc.html#user-visible-changes">User-visible Changes</a></li>
<li class="toctree-l1"><a class="reference internal" href="misc.html#license">License</a></li>
<li class="toctree-l1"><a class="reference internal" href="misc.html#frequently-asked-questions">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="misc.html#citing-pyopencl">Citing PyOpenCL</a></li>
<li class="toctree-l1"><a class="reference internal" href="misc.html#acknowledgments">Acknowledgments</a></li>
</ul>


<hr />
<ul>
    
    <li class="toctree-l1"><a href="https://github.com/pyopencl/pyopencl">🚀 Github</a></li>
    
    <li class="toctree-l1"><a href="https://pypi.python.org/pypi/pyopencl">💾 Download Releases</a></li>
    
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="index.html">Documentation overview</a><ul>
      <li>Previous: <a href="array.html" title="previous chapter">Multi-dimensional arrays</a></li>
      <li>Next: <a href="howto.html" title="next chapter">How-tos</a></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2016, Andreas Kloeckner.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.4.9</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a>
      
    </div>

    

    
  </body>
</html>