test_examples.py 21.2 KB
Newer Older
dugupeiwen's avatar
dugupeiwen committed
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
# Contents in this file are referenced from the sphinx-generated docs.
# "magictoken" is used for markers as beginning and ending of example text.

import sys
import unittest
from numba.tests.support import captured_stdout
from numba.core.config import IS_WIN32


class MatplotlibBlocker:
    '''Blocks the import of matplotlib, so that doc examples that attempt to
    plot the output don't result in plots popping up and blocking testing.'''

    def find_spec(self, fullname, path, target=None):
        if fullname == 'matplotlib':
            msg = 'Blocked import of matplotlib for test suite run'
            raise ImportError(msg)


class DocsExamplesTest(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._mpl_blocker = MatplotlibBlocker()

    def setUp(self):
        sys.meta_path.insert(0, self._mpl_blocker)

    def tearDown(self):
        sys.meta_path.remove(self._mpl_blocker)

    def test_mandelbrot(self):
        with captured_stdout():
            # magictoken.ex_mandelbrot.begin
            from timeit import default_timer as timer
            try:
                from matplotlib.pylab import imshow, show
                have_mpl = True
            except ImportError:
                have_mpl = False
            import numpy as np
            from numba import jit

            @jit(nopython=True)
            def mandel(x, y, max_iters):
                """
                Given the real and imaginary parts of a complex number,
                determine if it is a candidate for membership in the Mandelbrot
                set given a fixed number of iterations.
                """
                i = 0
                c = complex(x,y)
                z = 0.0j
                for i in range(max_iters):
                    z = z * z + c
                    if (z.real * z.real + z.imag * z.imag) >= 4:
                        return i

                return 255

            @jit(nopython=True)
            def create_fractal(min_x, max_x, min_y, max_y, image, iters):
                height = image.shape[0]
                width = image.shape[1]

                pixel_size_x = (max_x - min_x) / width
                pixel_size_y = (max_y - min_y) / height
                for x in range(width):
                    real = min_x + x * pixel_size_x
                    for y in range(height):
                        imag = min_y + y * pixel_size_y
                        color = mandel(real, imag, iters)
                        image[y, x] = color

                return image

            image = np.zeros((500 * 2, 750 * 2), dtype=np.uint8)
            s = timer()
            create_fractal(-2.0, 1.0, -1.0, 1.0, image, 20)
            e = timer()
            print(e - s)
            if have_mpl:
                imshow(image)
                show()
            # magictoken.ex_mandelbrot.end

    def test_moving_average(self):
        with captured_stdout():
            # magictoken.ex_moving_average.begin
            import numpy as np

            from numba import guvectorize

            @guvectorize(['void(float64[:], intp[:], float64[:])'],
                         '(n),()->(n)')
            def move_mean(a, window_arr, out):
                window_width = window_arr[0]
                asum = 0.0
                count = 0
                for i in range(window_width):
                    asum += a[i]
                    count += 1
                    out[i] = asum / count
                for i in range(window_width, len(a)):
                    asum += a[i] - a[i - window_width]
                    out[i] = asum / count

            arr = np.arange(20, dtype=np.float64).reshape(2, 10)
            print(arr)
            print(move_mean(arr, 3))
            # magictoken.ex_moving_average.end

    def test_nogil(self):
        with captured_stdout():
            # magictoken.ex_no_gil.begin
            import math
            import threading
            from timeit import repeat

            import numpy as np
            from numba import jit

            nthreads = 4
            size = 10**6

            def func_np(a, b):
                """
                Control function using Numpy.
                """
                return np.exp(2.1 * a + 3.2 * b)

            @jit('void(double[:], double[:], double[:])', nopython=True,
                 nogil=True)
            def inner_func_nb(result, a, b):
                """
                Function under test.
                """
                for i in range(len(result)):
                    result[i] = math.exp(2.1 * a[i] + 3.2 * b[i])

            def timefunc(correct, s, func, *args, **kwargs):
                """
                Benchmark *func* and print out its runtime.
                """
                print(s.ljust(20), end=" ")
                # Make sure the function is compiled before the benchmark is
                # started
                res = func(*args, **kwargs)
                if correct is not None:
                    assert np.allclose(res, correct), (res, correct)
                # time it
                print('{:>5.0f} ms'.format(min(repeat(
                    lambda: func(*args, **kwargs), number=5, repeat=2)) * 1000))
                return res

            def make_singlethread(inner_func):
                """
                Run the given function inside a single thread.
                """
                def func(*args):
                    length = len(args[0])
                    result = np.empty(length, dtype=np.float64)
                    inner_func(result, *args)
                    return result
                return func

            def make_multithread(inner_func, numthreads):
                """
                Run the given function inside *numthreads* threads, splitting
                its arguments into equal-sized chunks.
                """
                def func_mt(*args):
                    length = len(args[0])
                    result = np.empty(length, dtype=np.float64)
                    args = (result,) + args
                    chunklen = (length + numthreads - 1) // numthreads
                    # Create argument tuples for each input chunk
                    chunks = [[arg[i * chunklen:(i + 1) * chunklen] for arg in
                               args] for i in range(numthreads)]
                    # Spawn one thread per chunk
                    threads = [threading.Thread(target=inner_func, args=chunk)
                               for chunk in chunks]
                    for thread in threads:
                        thread.start()
                    for thread in threads:
                        thread.join()
                    return result
                return func_mt

            func_nb = make_singlethread(inner_func_nb)
            func_nb_mt = make_multithread(inner_func_nb, nthreads)

            a = np.random.rand(size)
            b = np.random.rand(size)

            correct = timefunc(None, "numpy (1 thread)", func_np, a, b)
            timefunc(correct, "numba (1 thread)", func_nb, a, b)
            timefunc(correct, "numba (%d threads)" % nthreads, func_nb_mt, a, b)
            # magictoken.ex_no_gil.end

    def test_vectorize_one_signature(self):
        with captured_stdout():
            # magictoken.ex_vectorize_one_signature.begin
            from numba import vectorize, float64

            @vectorize([float64(float64, float64)])
            def f(x, y):
                return x + y
            # magictoken.ex_vectorize_one_signature.end

    def test_vectorize_multiple_signatures(self):
        with captured_stdout():
            # magictoken.ex_vectorize_multiple_signatures.begin
            from numba import vectorize, int32, int64, float32, float64
            import numpy as np

            @vectorize([int32(int32, int32),
                        int64(int64, int64),
                        float32(float32, float32),
                        float64(float64, float64)])
            def f(x, y):
                return x + y
            # magictoken.ex_vectorize_multiple_signatures.end

            # magictoken.ex_vectorize_return_call_one.begin
            a = np.arange(6)
            result = f(a, a)
            # result == array([ 0,  2,  4,  6,  8, 10])
            # magictoken.ex_vectorize_return_call_one.end

            self.assertIsInstance(result, np.ndarray)
            correct = np.array([0, 2, 4, 6, 8, 10])
            np.testing.assert_array_equal(result, correct)

            # magictoken.ex_vectorize_return_call_two.begin
            a = np.linspace(0, 1, 6)
            result = f(a, a)
            # Now, result == array([0. , 0.4, 0.8, 1.2, 1.6, 2. ])
            # magictoken.ex_vectorize_return_call_two.end

            self.assertIsInstance(result, np.ndarray)
            correct = np.array([0., 0.4, 0.8, 1.2, 1.6, 2. ])
            np.testing.assert_allclose(result, correct)

            # magictoken.ex_vectorize_return_call_three.begin
            a = np.arange(12).reshape(3, 4)
            # a == array([[ 0,  1,  2,  3],
            #             [ 4,  5,  6,  7],
            #             [ 8,  9, 10, 11]])

            result1 = f.reduce(a, axis=0)
            # result1 == array([12, 15, 18, 21])

            result2 = f.reduce(a, axis=1)
            # result2 == array([ 6, 22, 38])

            result3 = f.accumulate(a)
            # result3 == array([[ 0,  1,  2,  3],
            #                   [ 4,  6,  8, 10],
            #                   [12, 15, 18, 21]])

            result4 = f.accumulate(a, axis=1)
            # result3 == array([[ 0,  1,  3,  6],
            #                   [ 4,  9, 15, 22],
            #                   [ 8, 17, 27, 38]])
            # magictoken.ex_vectorize_return_call_three.end

            self.assertIsInstance(result1, np.ndarray)
            correct = np.array([12, 15, 18, 21])
            np.testing.assert_array_equal(result1, correct)

            self.assertIsInstance(result2, np.ndarray)
            correct = np.array([6, 22, 38])
            np.testing.assert_array_equal(result2, correct)

            self.assertIsInstance(result3, np.ndarray)
            correct = np.array([
                [0, 1, 2, 3],
                [4, 6, 8, 10],
                [12, 15, 18, 21]
            ])
            np.testing.assert_array_equal(result3, correct)

            self.assertIsInstance(result4, np.ndarray)
            correct = np.array([
                [0, 1, 3, 6],
                [4, 9, 15, 22],
                [8, 17, 27, 38]
            ])
            np.testing.assert_array_equal(result4, correct)

    def test_guvectorize(self):
        with captured_stdout():
            # magictoken.ex_guvectorize.begin
            from numba import guvectorize, int64
            import numpy as np

            @guvectorize([(int64[:], int64, int64[:])], '(n),()->(n)')
            def g(x, y, res):
                for i in range(x.shape[0]):
                    res[i] = x[i] + y
            # magictoken.ex_guvectorize.end

            # magictoken.ex_guvectorize_call_one.begin
            a = np.arange(5)
            result = g(a, 2)
            # result == array([2, 3, 4, 5, 6])
            # magictoken.ex_guvectorize_call_one.end

            self.assertIsInstance(result, np.ndarray)
            correct = np.array([2, 3, 4, 5, 6])
            np.testing.assert_array_equal(result, correct)

            # magictoken.ex_guvectorize_call_two.begin
            a = np.arange(6).reshape(2, 3)
            # a == array([[0, 1, 2],
            #             [3, 4, 5]])

            result1 = g(a, 10)
            # result1 == array([[10, 11, 12],
            #                   [13, 14, 15]])

            result2 = g(a, np.array([10, 20]))
            g(a, np.array([10, 20]))
            # result2 == array([[10, 11, 12],
            #                   [23, 24, 25]])
            # magictoken.ex_guvectorize_call_two.end

            self.assertIsInstance(result1, np.ndarray)
            correct = np.array([[10, 11, 12], [13, 14, 15]])
            np.testing.assert_array_equal(result1, correct)

            self.assertIsInstance(result2, np.ndarray)
            correct = np.array([[10, 11, 12], [23, 24, 25]])
            np.testing.assert_array_equal(result2, correct)

    def test_guvectorize_scalar_return(self):
        with captured_stdout():
            # magictoken.ex_guvectorize_scalar_return.begin
            from numba import guvectorize, int64
            import numpy as np

            @guvectorize([(int64[:], int64, int64[:])], '(n),()->()')
            def g(x, y, res):
                acc = 0
                for i in range(x.shape[0]):
                    acc += x[i] + y
                res[0] = acc
            # magictoken.ex_guvectorize_scalar_return.end

            # magictoken.ex_guvectorize_scalar_return_call.begin
            a = np.arange(5)
            result = g(a, 2)
            # At this point, result == 20.
            # magictoken.ex_guvectorize_scalar_return_call.end

            self.assertIsInstance(result, np.integer)
            self.assertEqual(result, 20)

    def test_guvectorize_overwrite(self):
        with captured_stdout():
            # magictoken.ex_guvectorize_overwrite.begin
            from numba import guvectorize, float64
            import numpy as np

            @guvectorize([(float64[:], float64[:])], '()->()')
            def init_values(invals, outvals):
                invals[0] = 6.5
                outvals[0] = 4.2
            # magictoken.ex_guvectorize_overwrite.end

            # magictoken.ex_guvectorize_overwrite_call_one.begin
            invals = np.zeros(shape=(3, 3), dtype=np.float64)
            # invals == array([[6.5, 6.5, 6.5],
            #                  [6.5, 6.5, 6.5],
            #                  [6.5, 6.5, 6.5]])

            outvals = init_values(invals)
            # outvals == array([[4.2, 4.2, 4.2],
            #                   [4.2, 4.2, 4.2],
            #                   [4.2, 4.2, 4.2]])
            # magictoken.ex_guvectorize_overwrite_call_one.end

            self.assertIsInstance(invals, np.ndarray)
            correct = np.array([
                [6.5, 6.5, 6.5],
                [6.5, 6.5, 6.5],
                [6.5, 6.5, 6.5]])
            np.testing.assert_array_equal(invals, correct)

            self.assertIsInstance(outvals, np.ndarray)
            correct = np.array([
                [4.2, 4.2, 4.2],
                [4.2, 4.2, 4.2],
                [4.2, 4.2, 4.2]])
            np.testing.assert_array_equal(outvals, correct)

            # magictoken.ex_guvectorize_overwrite_call_two.begin
            invals = np.zeros(shape=(3, 3), dtype=np.float32)
            # invals == array([[0., 0., 0.],
            #                  [0., 0., 0.],
            #                  [0., 0., 0.]], dtype=float32)
            outvals = init_values(invals)
            # outvals == array([[4.2, 4.2, 4.2],
            #                   [4.2, 4.2, 4.2],
            #                   [4.2, 4.2, 4.2]])
            print(invals)
            # invals == array([[0., 0., 0.],
            #                  [0., 0., 0.],
            #                  [0., 0., 0.]], dtype=float32)
            # magictoken.ex_guvectorize_overwrite_call_two.end

            self.assertIsInstance(invals, np.ndarray)
            correct = np.array([
                [0., 0., 0.],
                [0., 0., 0.],
                [0., 0., 0.]], dtype=np.float32)
            np.testing.assert_array_equal(invals, correct)

            self.assertIsInstance(outvals, np.ndarray)
            correct = np.array([
                [4.2, 4.2, 4.2],
                [4.2, 4.2, 4.2],
                [4.2, 4.2, 4.2]])
            np.testing.assert_array_equal(outvals, correct)

            # magictoken.ex_guvectorize_overwrite_call_three.begin
            @guvectorize(
                [(float64[:], float64[:])],
                '()->()',
                writable_args=('invals',)
            )
            def init_values(invals, outvals):
                invals[0] = 6.5
                outvals[0] = 4.2

            invals = np.zeros(shape=(3, 3), dtype=np.float32)
            # invals == array([[0., 0., 0.],
            #                  [0., 0., 0.],
            #                  [0., 0., 0.]], dtype=float32)
            outvals = init_values(invals)
            # outvals == array([[4.2, 4.2, 4.2],
            #                   [4.2, 4.2, 4.2],
            #                   [4.2, 4.2, 4.2]])
            print(invals)
            # invals == array([[6.5, 6.5, 6.5],
            #                  [6.5, 6.5, 6.5],
            #                  [6.5, 6.5, 6.5]], dtype=float32)
            # magictoken.ex_guvectorize_overwrite_call_three.end

            self.assertIsInstance(invals, np.ndarray)
            correct = np.array([
                [6.5, 6.5, 6.5],
                [6.5, 6.5, 6.5],
                [6.5, 6.5, 6.5]])
            np.testing.assert_array_equal(invals, correct)

            self.assertIsInstance(outvals, np.ndarray)
            correct = np.array([
                [4.2, 4.2, 4.2],
                [4.2, 4.2, 4.2],
                [4.2, 4.2, 4.2]])
            np.testing.assert_array_equal(outvals, correct)

    def test_vectorize_dynamic(self):
        with captured_stdout():
            # magictoken.ex_vectorize_dynamic.begin
            from numba import vectorize

            @vectorize
            def f(x, y):
                return x * y
            # magictoken.ex_vectorize_dynamic.end

            # magictoken.ex_vectorize_dynamic_call_one.begin
            result = f(3,4)
            # result == 12

            print(f.types)
            # ['ll->l']
            # magictoken.ex_vectorize_dynamic_call_one.end

            self.assertEqual(result, 12)
            if IS_WIN32:
                correct = ['ll->q']
            else:
                correct = ['ll->l']
            self.assertEqual(f.types, correct)

            # magictoken.ex_vectorize_dynamic_call_two.begin
            result = f(1.,2.)
            # result == 2.0

            print(f.types)
            # ['ll->l', 'dd->d']
            # magictoken.ex_vectorize_dynamic_call_two.end

            self.assertEqual(result, 2.0)
            if IS_WIN32:
                correct = ['ll->q', 'dd->d']
            else:
                correct = ['ll->l', 'dd->d']
            self.assertEqual(f.types, correct)

            # magictoken.ex_vectorize_dynamic_call_three.begin
            result = f(1,2.)
            # result == 2.0

            print(f.types)
            # ['ll->l', 'dd->d']
            # magictoken.ex_vectorize_dynamic_call_three.end

            self.assertEqual(result, 2.0)
            if IS_WIN32:
                correct = ['ll->q', 'dd->d']
            else:
                correct = ['ll->l', 'dd->d']
            self.assertEqual(f.types, correct)

            # magictoken.ex_vectorize_dynamic_call_four.begin
            @vectorize
            def g(a, b):
                return a / b

            print(g(2.,3.))
            # 0.66666666666666663

            print(g(2,3))
            # 0.66666666666666663

            print(g.types)
            # ['dd->d']
            # magictoken.ex_vectorize_dynamic_call_four.end

            correct = ['dd->d']
            self.assertEqual(g.types, correct)

    def test_guvectorize_dynamic(self):
        with captured_stdout():
            # magictoken.ex_guvectorize_dynamic.begin
            from numba import guvectorize
            import numpy as np

            @guvectorize('(n),()->(n)')
            def g(x, y, res):
                for i in range(x.shape[0]):
                    res[i] = x[i] + y
            # magictoken.ex_guvectorize_dynamic.end

            # magictoken.ex_guvectorize_dynamic_call_one.begin
            x = np.arange(5, dtype=np.int64)
            y = 10
            res = np.zeros_like(x)
            g(x, y, res)
            # res == array([10, 11, 12, 13, 14])
            print(g.types)
            # ['ll->l']
            # magictoken.ex_guvectorize_dynamic_call_one.end

            correct = np.array([10, 11, 12, 13, 14])
            np.testing.assert_array_equal(res, correct)
            if IS_WIN32:
                correct = ['qq->q']
            else:
                correct = ['ll->l']
            self.assertEqual(g.types, correct)

            # magictoken.ex_guvectorize_dynamic_call_two.begin
            x = np.arange(5, dtype=np.double)
            y = 2.2
            res = np.zeros_like(x)
            g(x, y, res)
            # res == array([2.2, 3.2, 4.2, 5.2, 6.2])
            # magictoken.ex_guvectorize_dynamic_call_two.end

            # magictoken.ex_guvectorize_dynamic_call_three.begin
            print(g.types) # shorthand for g.ufunc.types
            # ['ll->l', 'dd->d']
            # magictoken.ex_guvectorize_dynamic_call_three.end

            if IS_WIN32:
                correct = ['qq->q', 'dd->d']
            else:
                correct = ['ll->l', 'dd->d']
            self.assertEqual(g.types, correct)

            # magictoken.ex_guvectorize_dynamic_call_four.begin
            x = np.arange(5, dtype=np.int64)
            y = 2.2
            res = np.zeros_like(x)
            g(x, y, res)
            print(res)
            # res == array([2, 3, 4, 5, 6])
            # magictoken.ex_guvectorize_dynamic_call_four.end

            correct = np.array([2, 3, 4, 5, 6])
            np.testing.assert_array_equal(res, correct)


if __name__ == '__main__':
    unittest.main()