File: array.rst

package info (click to toggle)
pycuda 2016.1.2%2Bgit20161024-1
  • links: PTS, VCS
  • area: contrib
  • in suites: stretch
  • size: 1,560 kB
  • ctags: 2,268
  • sloc: python: 11,951; cpp: 9,839; makefile: 139; sh: 1
file content (1184 lines) | stat: -rw-r--r-- 41,331 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
GPU Arrays
==========

.. module:: pycuda.gpuarray

Vector Types
------------

.. class :: vec

    All of CUDA's supported vector types, such as `float3` and `long4` are
    available as :mod:`numpy` data types within this class. These
    :class:`numpy.dtype` instances have field names of `x`, `y`, `z`, and `w`
    just like their CUDA counterparts. They will work both for parameter passing
    to kernels as well as for passing data back and forth between kernels and
    Python code. For each type, a `make_type` function is also provided (e.g.
    `make_float3(x,y,z)`).

The :class:`GPUArray` Array Class
---------------------------------

.. class:: GPUArray(shape, dtype, *, allocator=None, order="C")

    A :class:`numpy.ndarray` work-alike that stores its data and performs its
    computations on the compute device.  *shape* and *dtype* work exactly as in
    :mod:`numpy`.  Arithmetic methods in :class:`GPUArray` support the
    broadcasting of scalars. (e.g. `array+5`) If the

    *allocator* is a callable that, upon being called with an argument of the number
    of bytes to be allocated, returns an object that can be cast to an
    :class:`int` representing the address of the newly allocated memory.
    Observe that both :func:`pycuda.driver.mem_alloc` and
    :meth:`pycuda.tools.DeviceMemoryPool.alloc` are a model of this interface.

    All arguments beyond *allocator* should be considered keyword-only.

    .. attribute :: gpudata

        The :class:`pycuda.driver.DeviceAllocation` instance created for the memory that backs
        this :class:`GPUArray`.

    .. attribute :: shape

        The tuple of lengths of each dimension in the array.

    .. attribute :: dtype

        The :class:`numpy.dtype` of the items in the GPU array.

    .. attribute :: size

        The number of meaningful entries in the array. Can also be computed by
        multiplying up the numbers in :attr:`shape`.

    .. attribute :: mem_size

        The total number of entries, including padding, that are present in
        the array. Padding may arise for example because of pitch adjustment by
        :func:`pycuda.driver.mem_alloc_pitch`.

    .. attribute :: nbytes

        The size of the entire array in bytes. Computed as :attr:`size` times
        ``dtype.itemsize``.

    .. attribute :: strides

        Tuple of bytes to step in each dimension when traversing an array.

    .. attribute :: flags

        Return an object with attributes `c_contiguous`, `f_contiguous` and `forc`,
        which may be used to query contiguity properties in analogy to
        :attr:`numpy.ndarray.flags`.

    .. attribute :: ptr

        Return an :class:`int` reflecting the address in device memory where
        this array resides.

        .. versionadded: 2011.1

    .. method :: __len__()

        Returns the size of the leading dimension of *self*.

      .. warning ::

        This method existed in version 0.93 and below, but it returned the value
        of :attr:`size` instead of its current value. The change was made in order
        to match :mod:`numpy`.

    .. method :: reshape(shape)

        Returns an array containing the same data with a new shape.

    .. method :: ravel()

        Returns flattened array containing the same data.

    .. method :: view(dtype=None)

        Returns view of array with the same data. If *dtype* is different from
        current dtype, the actual bytes of memory will be reinterpreted.

    .. method :: squeeze(dtype=None)

        Returns a view of the array with dimensions of length 1 removed.

        .. versionadded: 2015.1.4

    .. method :: set(ary)

        Transfer the contents the :class:`numpy.ndarray` object *ary*
        onto the device.

        *ary* must have the same dtype and size (not necessarily shape) as *self*.

    .. method :: set_async(ary, stream=None)

        Asynchronously transfer the contents the :class:`numpy.ndarray` object *ary*
        onto the device, optionally sequenced on *stream*.

        *ary* must have the same dtype and size (not necessarily shape) as *self*.

    .. method :: get(ary=None, pagelocked=False)

        Transfer the contents of *self* into *ary* or a newly allocated
        :mod:`numpy.ndarray`. If *ary* is given, it must have the same
        shape and dtype. If it is not given,
        a *pagelocked* specifies whether the new array is allocated
        page-locked.

        .. versionchanged:: 2015.2

            *ary* with different shape was deprecated.

    .. method :: get_async(stream=None, ary=None)

        Transfer the contents of *self* into *ary* or a newly allocated
        :mod:`numpy.ndarray`. If *ary* is given, it must have the right
        size (not necessarily shape) and dtype. If it is not given,
        a *page-locked* array is newly allocated.

    .. method :: copy()

        .. versionadded :: 2013.1

    .. method :: mul_add(self, selffac, other, otherfac, add_timer=None, stream=None):

        Return `selffac*self + otherfac*other`. *add_timer*, if given,
        is invoked with the result from
        :meth:`pycuda.driver.Function.prepared_timed_call`.

    .. method :: __add__(other)
    .. method :: __sub__(other)
    .. method :: __iadd__(other)
    .. method :: __isub__(other)
    .. method :: __neg__(other)
    .. method :: __mul__(other)
    .. method :: __div__(other)
    .. method :: __rdiv__(other)
    .. method :: __pow__(other)

    .. method :: __abs__()

        Return a :class:`GPUArray` containing the absolute value of each
        element of *self*.

    .. UNDOC reverse()

    .. method :: fill(scalar, stream=None)

        Fill the array with *scalar*.

    .. method :: astype(dtype, stream=None)

        Return *self*, cast to *dtype*.

    .. attribute :: real

        Return the real part of *self*, or *self* if it is real.

        .. versionadded:: 0.94

    .. attribute :: imag

        Return the imaginary part of *self*, or *zeros_like(self)* if it is real.

        .. versionadded: 0.94

    .. method :: conj()

        Return the complex conjugate of *self*, or *self* if it is real.

        .. versionadded: 0.94

    .. method:: bind_to_texref(texref, allow_offset=False)

        Bind *self* to the :class:`pycuda.driver.TextureReference` *texref*.

        Due to alignment requirements, the effective texture bind address may be
        different from the requested one by an offset. This method returns this
        offset in units of *self*'s data type.  If *allow_offset* is ``False``, a
        nonzero value of this offset will cause an exception to be raised.

        .. note::

            It is recommended to use :meth:`bind_to_texref_ext` instead of
            this method.

    .. method:: bind_to_texref_ext(texref, channels=1, allow_double_hack=False, allow_offset=False)

        Bind *self* to the :class:`pycuda.driver.TextureReference` *texref*.
        In addition, set the texture reference's format to match :attr:`dtype`
        and its channel count to *channels*. This routine also sets the
        texture reference's :data:`pycuda.driver.TRSF_READ_AS_INTEGER` flag,
        if necessary.

        Due to alignment requirements, the effective texture bind address may be
        different from the requested one by an offset. This method returns this
        offset in units of *self*'s data type.  If *allow_offset* is ``False``, a
        nonzero value of this offset will cause an exception to be raised.

        .. versionadded:: 0.93

        .. highlight:: c

        As of this writing, CUDA textures do not natively support double-precision
        floating point data. To remedy this deficiency, PyCUDA contains a workaround,
        which can be enabled by passing *True* for allow_double_hack. In this case,
        use the following code for texture access in your kernel code::

            #include <pycuda-helpers.hpp>

            texture<fp_tex_double, 1, cudaReadModeElementType> my_tex;

            __global__ void f()
            {
              ...
              fp_tex1Dfetch(my_tex, threadIdx.x);
              ...
            }

        .. highlight:: python

        (This workaround was added in version 0.94.)

Constructing :class:`GPUArray` Instances
----------------------------------------

.. function:: to_gpu(ary, allocator=None)

    Return a :class:`GPUArray` that is an exact copy of the :class:`numpy.ndarray`
    instance *ary*.

    See :class:`GPUArray` for the meaning of *allocator*.

.. function:: to_gpu_async(ary, allocator=None, stream=None)

    Return a :class:`GPUArray` that is an exact copy of the :class:`numpy.ndarray`
    instance *ary*. The copy is done asynchronously, optionally sequenced into
    *stream*.

    See :class:`GPUArray` for the meaning of *allocator*.

.. function:: empty(shape, dtype, *, allocator=None, order="C")

    A synonym for the :class:`GPUArray` constructor.

.. function:: zeros(shape, dtype, *, allocator=None, order="C")

    Same as :func:`empty`, but the :class:`GPUArray` is zero-initialized before
    being returned.

.. function:: empty_like(other_ary)

    Make a new, uninitialized :class:`GPUArray` having the same properties
    as *other_ary*.

.. function:: zeros_like(other_ary)

    Make a new, zero-initialized :class:`GPUArray` having the same properties
    as *other_ary*.

.. function:: arange(start, stop, step, dtype=None, stream=None)

    Create a :class:`GPUArray` filled with numbers spaced `step` apart,
    starting from `start` and ending at `stop`.

    For floating point arguments, the length of the result is
    `ceil((stop - start)/step)`.  This rule may result in the last
    element of the result being greater than `stop`.

    *dtype*, if not specified, is taken as the largest common type
    of *start*, *stop* and *step*.

.. function:: take(a, indices, stream=None)

    Return the :class:`GPUArray` ``[a[indices[0]], ..., a[indices[n]]]``.
    For the moment, *a* must be a type that can be bound to a texture.

Conditionals
^^^^^^^^^^^^

.. function:: if_positive(criterion, then_, else_, out=None, stream=None)

    Return an array like *then_*, which, for the element at index *i*,
    contains *then_[i]* if *criterion[i]>0*, else *else_[i]*. (added in 0.94)

.. function:: maximum(a, b, out=None, stream=None)

    Return the elementwise maximum of *a* and *b*. (added in 0.94)

.. function:: minimum(a, b, out=None, stream=None)

    Return the elementwise minimum of *a* and *b*. (added in 0.94)

Reductions
^^^^^^^^^^

.. function:: sum(a, dtype=None, stream=None)

.. function:: subset_sum(subset, a, dtype=None, stream=None)

    .. versionadded:: 2013.1

.. function:: dot(a, b, dtype=None, stream=None)

.. function:: subset_dot(subset, a, b, dtype=None, stream=None)

.. function:: max(a, stream=None)

.. function:: min(a, stream=None)

.. function:: subset_max(subset, a, stream=None)

.. function:: subset_min(subset, a, stream=None)

Elementwise Functions on :class:`GPUArray` Instances
-----------------------------------------------------

.. module:: pycuda.cumath

The :mod:`pycuda.cumath` module contains elementwise
workalikes for the functions contained in :mod:`math`.

Rounding and Absolute Value
^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. function:: fabs(array, *, out=None, stream=None)
.. function:: ceil(array, *, out=None, stream=None)
.. function:: floor(array, *, out=None, stream=None)

Exponentials, Logarithms and Roots
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. function:: exp(array, *, out=None, stream=None)
.. function:: log(array, *, out=None, stream=None)
.. function:: log10(array, *, out=None, stream=None)
.. function:: sqrt(array, *, out=None, stream=None)

Trigonometric Functions
^^^^^^^^^^^^^^^^^^^^^^^

.. function:: sin(array, *, out=None, stream=None)
.. function:: cos(array, *, out=None, stream=None)
.. function:: tan(array, *, out=None, stream=None)
.. function:: asin(array, *, out=None, stream=None)
.. function:: acos(array, *, out=None, stream=None)
.. function:: atan(array, *, out=None, stream=None)

Hyperbolic Functions
^^^^^^^^^^^^^^^^^^^^

.. function:: sinh(array, *, out=None, stream=None)
.. function:: cosh(array, *, out=None, stream=None)
.. function:: tanh(array, *, out=None, stream=None)

Floating Point Decomposition and Assembly
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. function:: fmod(arg, mod, stream=None)

    Return the floating point remainder of the division `arg/mod`,
    for each element in `arg` and `mod`.

.. function:: frexp(arg, stream=None)

    Return a tuple `(significands, exponents)` such that
    `arg == significand * 2**exponent`.

.. function:: ldexp(significand, exponent, stream=None)

    Return a new array of floating point values composed from the
    entries of `significand` and `exponent`, paired together as
    `result = significand * 2**exponent`.

.. function:: modf(arg, stream=None)

    Return a tuple `(fracpart, intpart)` of arrays containing the
    integer and fractional parts of `arg`.

Generating Arrays of Random Numbers
-----------------------------------

.. module:: pycuda.curandom

.. function:: rand(shape, dtype=numpy.float32, stream=None)

    Return an array of `shape` filled with random values of `dtype`
    in the range [0,1).

    .. note::

        The use case for this function is "I need some random numbers.
        I don't care how good they are or how fast I get them." It uses
        a pretty terrible MD5-based generator and doesn't even attempt
        to cache generated code.

        If you're interested in a non-toy random number generator, use the
        CURAND-based functionality below.

.. warning::

    The following classes are using random number generators that run on the GPU.
    Each thread uses its own generator. Creation of those generators requires more
    resources than subsequent generation of random numbers. After experiments
    it looks like maximum number of active generators on Tesla devices
    (with compute capabilities 1.x) is 256. Fermi devices allow for creating
    1024 generators without any problems. If there are troubles with creating
    objects of class PseudoRandomNumberGenerator or QuasiRandomNumberGenerator
    decrease number of created generators
    (and therefore number of active threads).

A pseudorandom sequence of numbers satisfies most of the statistical properties
of a truly random sequence but is generated by a deterministic algorithm.  A
quasirandom sequence of n-dimensional points is generated by a deterministic
algorithm designed to fill an n-dimensional space evenly.

Quasirandom numbers are more expensive to generate.

.. function:: get_curand_version()

    Obtain the version of CURAND against which PyCUDA was compiled. Returns a
    3-tuple of integers as *(major, minor, revision)*.

.. function:: seed_getter_uniform(N)

    Return an :class:`GPUArray` filled with one random `int32` repeated `N`
    times which can be used as a seed for XORWOW generator.

.. function:: seed_getter_unique(N)

    Return an :class:`GPUArray` filled with `N` random `int32` which can
    be used as a seed for XORWOW generator.

.. class:: XORWOWRandomNumberGenerator(seed_getter=None, offset=0)

    :arg seed_getter: a function that, given an integer count, will yield an
      `int32` :class:`GPUArray` of seeds.
    :arg offset: Starting index into the XORWOW sequence, given seed.

    Provides pseudorandom numbers. Generates sequences with period
    at least :math:`2^190`.

    CUDA 3.2 and above.

    .. versionadded:: 2011.1

    .. method:: fill_uniform(data, stream=None)

        Fills in :class:`GPUArray` *data* with uniformly distributed
        pseudorandom values.

    .. method:: gen_uniform(shape, dtype, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with uniformly distributed pseudorandom values,
        and returns newly created object.

    .. method:: fill_normal(data, stream=None)

        Fills in :class:`GPUArray` *data* with normally distributed
        pseudorandom values.

    .. method:: gen_normal(shape, dtype, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with normally distributed pseudorandom values,
        and returns newly created object.

    .. method:: fill_log_normal(data, mean, stddev, stream=None)

        Fills in :class:`GPUArray` *data* with log-normally distributed
        pseudorandom values with mean *mean* and standard deviation *stddev*.

        CUDA 4.0 and above.

        .. versionadded:: 2012.2

    .. method:: gen_log_normal(shape, dtype, mean, stddev, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with log-normally distributed pseudorandom values
        with mean *mean* and standard deviation *stddev*, and returns
        newly created object.

        CUDA 4.0 and above.

        .. versionadded:: 2012.2

    .. method:: fill_poisson(data, lambda_value, stream=None)

        Fills in :class:`GPUArray` *data* with Poisson distributed
        pseudorandom values with lambda *lambda_value*. *data* must
        be of type 32-bit unsigned int.

        CUDA 5.0 and above.

        .. versionadded:: 2013.1

    .. method:: gen_poisson(shape, dtype, lambda_value, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with Poisson distributed pseudorandom values
        with lambda *lambda_value*, and returns newly created object.
        *dtype* must be 32-bit unsigned int.

        CUDA 5.0 and above.

        .. versionadded:: 2013.1

    .. method:: call_skip_ahead(i, stream=None)

        Forces all generators to skip i values. Is equivalent to generating
        i values and discarding results, but is much faster.

    .. method::  call_skip_ahead_array(i, stream=None)

        Accepts array i of integer values, telling each generator how many
        values to skip.

    .. method:: call_skip_ahead_sequence(i, stream=None)

        Forces all generators to skip i subsequences. Is equivalent to
        generating i * :math:`2^67` values and discarding results,
        but is much faster.

    .. method:: call_skip_ahead_sequence_array(i, stream=None)

        Accepts array i of integer values, telling each generator how many
        subsequences to skip.

.. class:: MRG32k3aRandomNumberGenerator(seed_getter=None, offset=0)

    :arg seed_getter: a function that, given an integer count, will yield an
      `int32` :class:`GPUArray` of seeds.
    :arg offset: Starting index into the XORWOW sequence, given seed.

    Provides pseudorandom numbers. Generates sequences with period
    at least :math:`2^190`.

    CUDA 4.1 and above.

    .. versionadded:: 2013.1

    .. method:: fill_uniform(data, stream=None)

        Fills in :class:`GPUArray` *data* with uniformly distributed
        pseudorandom values.

    .. method:: gen_uniform(shape, dtype, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with uniformly distributed pseudorandom values,
        and returns newly created object.

    .. method:: fill_normal(data, stream=None)

        Fills in :class:`GPUArray` *data* with normally distributed
        pseudorandom values.

    .. method:: gen_normal(shape, dtype, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with normally distributed pseudorandom values,
        and returns newly created object.

    .. method:: fill_log_normal(data, mean, stddev, stream=None)

        Fills in :class:`GPUArray` *data* with log-normally distributed
        pseudorandom values with mean *mean* and standard deviation *stddev*.

    .. method:: gen_log_normal(shape, dtype, mean, stddev, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with log-normally distributed pseudorandom values
        with mean *mean* and standard deviation *stddev*, and returns
        newly created object.

    .. method:: fill_poisson(data, lambda_value, stream=None)

        Fills in :class:`GPUArray` *data* with Poisson distributed
        pseudorandom values with lambda *lambda_value*. *data* must
        be of type 32-bit unsigned int.

        CUDA 5.0 and above.

        .. versionadded:: 2013.1

    .. method:: gen_poisson(shape, dtype, lambda_value, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with Poisson distributed pseudorandom values
        with lambda *lambda_value*, and returns newly created object.
        *dtype* must be 32-bit unsigned int.

        CUDA 5.0 and above.

        .. versionadded:: 2013.1

    .. method:: call_skip_ahead(i, stream=None)

        Forces all generators to skip i values. Is equivalent to generating
        i values and discarding results, but is much faster.

    .. method::  call_skip_ahead_array(i, stream=None)

        Accepts array i of integer values, telling each generator how many
        values to skip.

    .. method:: call_skip_ahead_sequence(i, stream=None)

        Forces all generators to skip i subsequences. Is equivalent to
        generating i * :math:`2^67` values and discarding results,
        but is much faster.

    .. method:: call_skip_ahead_sequence_array(i, stream=None)

        Accepts array i of integer values, telling each generator how many
        subsequences to skip.

.. function:: generate_direction_vectors(count, direction=direction_vector_set.VECTOR_32)

    Return an :class:`GPUArray` `count` filled with direction vectors
    used to initialize Sobol generators.

.. function:: generate_scramble_constants32(count)

    Return a :class:`GPUArray` filled with `count' 32-bit unsigned integer
    numbers used to initialize :class:`ScrambledSobol32RandomNumberGenerator`

.. function:: generate_scramble_constants64(count)

    Return a :class:`GPUArray` filled with `count' 64-bit unsigned integer
    numbers used to initialize :class:`ScrambledSobol64RandomNumberGenerator`

.. class:: Sobol32RandomNumberGenerator(dir_vector=None, offset=0)

    :arg dir_vector: a :class:`GPUArray` of 32-element `int32` vectors which
      are used to initialize quasirandom generator; it must contain one vector
      for each initialized generator
    :arg offset: Starting index into the Sobol32 sequence, given direction
      vector.

    Provides quasirandom numbers. Generates
    sequences with period of :math:`2^32`.

    CUDA 3.2 and above.

    .. versionadded:: 2011.1

    .. method:: fill_uniform(data, stream=None)

        Fills in :class:`GPUArray` *data* with uniformly distributed
        quasirandom values.

    .. method:: gen_uniform(shape, dtype, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with uniformly distributed pseudorandom values,
        and returns newly created object.

    .. method:: fill_normal(data, stream=None)

        Fills in :class:`GPUArray` *data* with normally distributed
        quasirandom values.

    .. method:: gen_normal(shape, dtype, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with normally distributed pseudorandom values,
        and returns newly created object.

    .. method:: fill_log_normal(data, mean, stddev, stream=None)

        Fills in :class:`GPUArray` *data* with log-normally distributed
        pseudorandom values with mean *mean* and standard deviation *stddev*.

        CUDA 4.0 and above.

        .. versionadded:: 2012.2

    .. method:: gen_log_normal(shape, dtype, mean, stddev, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with log-normally distributed pseudorandom values
        with mean *mean* and standard deviation *stddev*, and returns
        newly created object.

        CUDA 4.0 and above.

        .. versionadded:: 2012.2

    .. method:: fill_poisson(data, lambda_value, stream=None)

        Fills in :class:`GPUArray` *data* with Poisson distributed
        pseudorandom values with lambda *lambda_value*. *data* must
        be of type 32-bit unsigned int.

        CUDA 5.0 and above.

        .. versionadded:: 2013.1

    .. method:: gen_poisson(shape, dtype, lambda_value, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with Poisson distributed pseudorandom values
        with lambda *lambda_value*, and returns newly created object.
        *dtype* must be 32-bit unsigned int.

        CUDA 5.0 and above.

        .. versionadded:: 2013.1

    .. method:: call_skip_ahead(i, stream=None)

        Forces all generators to skip i values. Is equivalent to generating
        i values and discarding results, but is much faster.

    .. method:: call_skip_ahead_array(i, stream=None)

        Accepts array i of integer values, telling each generator how many
        values to skip.

.. class:: ScrambledSobol32RandomNumberGenerator(dir_vector=None, scramble_vector=None, offset=0)

    :arg dir_vector: a :class:`GPUArray` of 32-element `uint32` vectors which
      are used to initialize quasirandom generator; it must contain one vector
      for each initialized generator
    :arg scramble_vector: a :class:`GPUArray` of `uint32` elements which
      are used to initialize quasirandom generator; it must contain one number
      for each initialized generator
    :arg offset: Starting index into the Sobol32 sequence, given direction
      vector.

    Provides quasirandom numbers. Generates
    sequences with period of :math:`2^32`.

    CUDA 4.0 and above.

    .. versionadded:: 2011.1

    .. method:: fill_uniform(data, stream=None)

        Fills in :class:`GPUArray` *data* with uniformly distributed
        quasirandom values.

    .. method:: gen_uniform(shape, dtype, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with uniformly distributed pseudorandom values,
        and returns newly created object.

    .. method:: fill_normal(data, stream=None)

        Fills in :class:`GPUArray` *data* with normally distributed
        quasirandom values.

    .. method:: gen_normal(shape, dtype, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with normally distributed pseudorandom values,
        and returns newly created object.

    .. method:: fill_log_normal(data, mean, stddev, stream=None)

        Fills in :class:`GPUArray` *data* with log-normally distributed
        pseudorandom values with mean *mean* and standard deviation *stddev*.

        CUDA 4.0 and above.

        .. versionadded:: 2012.2

    .. method:: gen_log_normal(shape, dtype, mean, stddev, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with log-normally distributed pseudorandom values
        with mean *mean* and standard deviation *stddev*, and returns
        newly created object.

        CUDA 4.0 and above.

        .. versionadded:: 2012.2

    .. method:: fill_poisson(data, lambda_value, stream=None)

        Fills in :class:`GPUArray` *data* with Poisson distributed
        pseudorandom values with lambda *lambda_value*. *data* must
        be of type 32-bit unsigned int.

        CUDA 5.0 and above.

        .. versionadded:: 2013.1

    .. method:: gen_poisson(shape, dtype, lambda_value, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with Poisson distributed pseudorandom values
        with lambda *lambda_value*, and returns newly created object.
        *dtype* must be 32-bit unsigned int.

        CUDA 5.0 and above.

        .. versionadded:: 2013.1

    .. method:: call_skip_ahead(i, stream=None)

        Forces all generators to skip i values. Is equivalent to generating
        i values and discarding results, but is much faster.

    .. method:: call_skip_ahead_array(i, stream=None)

        Accepts array i of integer values, telling each generator how many
        values to skip.

.. class:: Sobol64RandomNumberGenerator(dir_vector=None, offset=0)

    :arg dir_vector: a :class:`GPUArray` of 64-element `uint64` vectors which
      are used to initialize quasirandom generator; it must contain one vector
      for each initialized generator
    :arg offset: Starting index into the Sobol64 sequence, given direction
      vector.

    Provides quasirandom numbers. Generates
    sequences with period of :math:`2^64`.

    CUDA 4.0 and above.

    .. versionadded:: 2011.1

    .. method:: fill_uniform(data, stream=None)

        Fills in :class:`GPUArray` *data* with uniformly distributed
        quasirandom values.

    .. method:: gen_uniform(shape, dtype, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with uniformly distributed pseudorandom values,
        and returns newly created object.

    .. method:: fill_normal(data, stream=None)

        Fills in :class:`GPUArray` *data* with normally distributed
        quasirandom values.

    .. method:: gen_normal(shape, dtype, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with normally distributed pseudorandom values,
        and returns newly created object.

    .. method:: fill_log_normal(data, mean, stddev, stream=None)

        Fills in :class:`GPUArray` *data* with log-normally distributed
        pseudorandom values with mean *mean* and standard deviation *stddev*.

        CUDA 4.0 and above.

        .. versionadded:: 2012.2

    .. method:: gen_log_normal(shape, dtype, mean, stddev, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with log-normally distributed pseudorandom values
        with mean *mean* and standard deviation *stddev*, and returns
        newly created object.

        CUDA 4.0 and above.

        .. versionadded:: 2012.2

    .. method:: fill_poisson(data, lambda_value, stream=None)

        Fills in :class:`GPUArray` *data* with Poisson distributed
        pseudorandom values with lambda *lambda_value*. *data* must
        be of type 32-bit unsigned int.

        CUDA 5.0 and above.

        .. versionadded:: 2013.1

    .. method:: gen_poisson(shape, dtype, lambda_value, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with Poisson distributed pseudorandom values
        with lambda *lambda_value*, and returns newly created object.
        *dtype* must be 32-bit unsigned int.

        CUDA 5.0 and above.

        .. versionadded:: 2013.1

    .. method:: call_skip_ahead(i, stream=None)

        Forces all generators to skip i values. Is equivalent to generating
        i values and discarding results, but is much faster.

    .. method:: call_skip_ahead_array(i, stream=None)

        Accepts array i of integer values, telling each generator how many
        values to skip.

.. class:: ScrambledSobol64RandomNumberGenerator(dir_vector=None, scramble_vector=None, offset=0)

    :arg dir_vector: a :class:`GPUArray` of 64-element `uint64` vectors which
      are used to initialize quasirandom generator; it must contain one vector
      for each initialized generator
    :arg scramble_vector: a :class:`GPUArray` of `uint64` vectors which
      are used to initialize quasirandom generator; it must contain one vector
      for each initialized generator
    :arg offset: Starting index into the ScrambledSobol64 sequence,
      given direction vector.

    Provides quasirandom numbers. Generates
    sequences with period of :math:`2^64`.

    CUDA 4.0 and above.

    .. versionadded:: 2011.1

    .. method:: fill_uniform(data, stream=None)

        Fills in :class:`GPUArray` *data* with uniformly distributed
        quasirandom values.

    .. method:: gen_uniform(shape, dtype, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with uniformly distributed pseudorandom values,
        and returns newly created object.

    .. method:: fill_normal(data, stream=None)

        Fills in :class:`GPUArray` *data* with normally distributed
        quasirandom values.

    .. method:: gen_normal(shape, dtype, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with normally distributed pseudorandom values,
        and returns newly created object.

    .. method:: fill_log_normal(data, mean, stddev, stream=None)

        Fills in :class:`GPUArray` *data* with log-normally distributed
        pseudorandom values with mean *mean* and standard deviation *stddev*.

        CUDA 4.0 and above.

        .. versionadded:: 2012.2

    .. method:: gen_log_normal(shape, dtype, mean, stddev, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with log-normally distributed pseudorandom values
        with mean *mean* and standard deviation *stddev*, and returns
        newly created object.

        CUDA 4.0 and above.

        .. versionadded:: 2012.2

    .. method:: fill_poisson(data, lambda_value, stream=None)

        Fills in :class:`GPUArray` *data* with Poisson distributed
        pseudorandom values with lambda *lambda_value*. *data* must
        be of type 32-bit unsigned int.

        CUDA 5.0 and above.

        .. versionadded:: 2013.1

    .. method:: gen_poisson(shape, dtype, lambda_value, stream=None)

        Creates object of :class:`GPUArray` with given *shape* and *dtype*,
        fills it in with Poisson distributed pseudorandom values
        with lambda *lambda_value*, and returns newly created object.
        *dtype* must be 32-bit unsigned int.

        CUDA 5.0 and above.

        .. versionadded:: 2013.1

    .. method:: call_skip_ahead(i, stream=None)

        Forces all generators to skip i values. Is equivalent to generating
        i values and discarding results, but is much faster.

    .. method:: call_skip_ahead_array(i, stream=None)

        Accepts array i of integer values, telling each generator how many
        values to skip.

Single-pass Custom Expression Evaluation
----------------------------------------

.. module:: pycuda.elementwise

Evaluating involved expressions on :class:`GPUArray` instances can be
somewhat inefficient, because a new temporary is created for each
intermediate result. The functionality in the module :mod:`pycuda.elementwise`
contains tools to help generate kernels that evaluate multi-stage expressions
on one or several operands in a single pass.

.. class:: ElementwiseKernel(arguments, operation, name="kernel", keep=False, options=[], preamble="")

    Generate a kernel that takes a number of scalar or vector *arguments*
    and performs the scalar *operation* on each entry of its arguments, if that
    argument is a vector.

    *arguments* is specified as a string formatted as a C argument list.
    *operation* is specified as a C assignment statement, without a semicolon.
    Vectors in *operation* should be indexed by the variable *i*.

    *name* specifies the name as which the kernel is compiled, *keep*
    and *options* are passed unmodified to :class:`pycuda.compiler.SourceModule`.

    *preamble* specifies some source code that is included before the
    elementwise kernel specification. You may use this to include other
    files and/or define functions that are used by *operation*.

    .. method:: __call__(*args, range=None, slice=None)

        Invoke the generated scalar kernel. The arguments may either be scalars or
        :class:`GPUArray` instances.

        If *range* is given, it must be a :class:`slice` object and specifies
        the range of indices *i* for which the *operation* is carried out.

        If *slice* is given, it must be a :class:`slice` object and specifies
        the range of indices *i* for which the *operation* is carried out,
        truncated to the container. Also, *slice* may contain negative indices
        to index relative to the end of the array.

        If *stream* is given, it must be a :class:`pycuda.driver.Stream` object,
        where the execution will be serialized.

Here's a usage example::

    import pycuda.gpuarray as gpuarray
    import pycuda.driver as cuda
    import pycuda.autoinit
    import numpy
    from pycuda.curandom import rand as curand

    a_gpu = curand((50,))
    b_gpu = curand((50,))

    from pycuda.elementwise import ElementwiseKernel
    lin_comb = ElementwiseKernel(
            "float a, float *x, float b, float *y, float *z",
            "z[i] = a*x[i] + b*y[i]",
            "linear_combination")

    c_gpu = gpuarray.empty_like(a_gpu)
    lin_comb(5, a_gpu, 6, b_gpu, c_gpu)

    import numpy.linalg as la
    assert la.norm((c_gpu - (5*a_gpu+6*b_gpu)).get()) < 1e-5

(You can find this example as :file:`examples/demo_elementwise.py` in the PyCuda
distribution.)

Custom Reductions
-----------------

.. module:: pycuda.reduction

.. class:: ReductionKernel(dtype_out, neutral, reduce_expr, map_expr=None, arguments=None, name="reduce_kernel", keep=False, options=[], preamble="", allocator=None)

    Generate a kernel that takes a number of scalar or vector *arguments*
    (at least one vector argument), performs the *map_expr* on each entry of
    the vector argument and then the *reduce_expr* on the outcome of that.
    *neutral* serves as an initial value. *preamble* offers the possibility
    to add preprocessor directives and other code (such as helper functions)
    to be added before the actual reduction kernel code.

    Vectors in *map_expr* should be indexed by the variable *i*. *reduce_expr*
    uses the formal values "a" and "b" to indicate two operands of a binary
    reduction operation. If you do not specify a *map_expr*, "in[i]" -- and
    therefore the presence of only one input argument -- is automatically
    assumed.

    *dtype_out* specifies the :class:`numpy.dtype` in which the reduction is
    performed and in which the result is returned. *neutral* is
    specified as float or integer formatted as string. *reduce_expr* and
    *map_expr* are specified as string formatted operations and *arguments*
    is specified as a string formatted as a C argument list. *name* specifies
    the name as which the kernel is compiled, *keep* and *options* are passed
    unmodified to :class:`pycuda.compiler.SourceModule`. *preamble* is specified
    as a string of code.

    .. method __call__(*args, stream=None)

Here's a usage example::

    a = gpuarray.arange(400, dtype=numpy.float32)
    b = gpuarray.arange(400, dtype=numpy.float32)

    krnl = ReductionKernel(numpy.float32, neutral="0",
            reduce_expr="a+b", map_expr="x[i]*y[i]",
            arguments="float *x, float *y")

    my_dot_prod = krnl(a, b).get()

Parallel Scan / Prefix Sum
--------------------------

.. module:: pycuda.scan

.. class:: ExclusiveScanKernel(dtype, scan_expr, neutral, name_prefix="scan", options=[], preamble="")

    Generates a kernel that can compute a `prefix sum <https://secure.wikimedia.org/wikipedia/en/wiki/Prefix_sum>`_
    using any associative operation given as *scan_expr*.
    *scan_expr* uses the formal values "a" and "b" to indicate two operands of
    an associative binary operation. *neutral* is the neutral element
    of *scan_expr*, obeying *scan_expr(a, neutral) == a*.

    *dtype* specifies the type of the arrays being operated on.
    *name_prefix* is used for kernel names to ensure recognizability
    in profiles and logs. *options* is a list of compiler options to use
    when building. *preamble* specifies a string of code that is
    inserted before the actual kernels.

    .. method:: __call__(self, input_ary, output_ary=None, allocator=None, queue=None)

.. class:: InclusiveScanKernel(dtype, scan_expr, neutral=None, name_prefix="scan", options=[], preamble="", devices=None)

    Works like :class:`ExclusiveScanKernel`. Unlike the exclusive case,
    *neutral* is not required.

Here's a usage example::

    knl = InclusiveScanKernel(np.int32, "a+b")

    n = 2**20-2**18+5
    host_data = np.random.randint(0, 10, n).astype(np.int32)
    dev_data = gpuarray.to_gpu(queue, host_data)

    knl(dev_data)
    assert (dev_data.get() == np.cumsum(host_data, axis=0)).all()

Custom data types in Reduction and Scan
---------------------------------------

If you would like to use your own (struct/union/whatever) data types in
scan and reduction, define those types in the *preamble* and let PyCUDA
know about them using this function:

.. function:: pycuda.tools.register_dtype(dtype, name)

    *dtype* is a :func:`numpy.dtype`.

    .. versionadded: 2011.2

GPGPU Algorithms
----------------

Bogdan Opanchuk's `reikna <http://pypi.python.org/pypi/reikna>`_ offers a
variety of GPU-based algorithms (FFT, RNG, matrix multiplication) designed to work with
:class:`pycuda.gpuarray.GPUArray` objects.