示例#1
0
    def test_abs2(self):
        for dtype in (np.float64, np.complex128):  # np.complex64 np.float32
            with self.subTest(dtype=dtype):
                nvec = 10000
                xa, stda = generate_random_xr(dtype, nvec=nvec, max_bin_exp=75)
                # Adjust the mantissa to be sure to trigger a renorm for some
                # (around 30 %) cases
                xa = np.asarray(xa)
                exp = np.copy(xa["exp"])
                xa["mantissa"] *= 2.**(2 * exp)
                xa["exp"] = -exp
                xa = xa.view(Xrange_array)
                res = Xrange_array.empty(xa.shape, dtype=dtype)
                expected = np.abs(stda)**2

                numba_test_abs(xa, res)
                # Numba timing without compilation
                t_numba = -time.time()
                numba_test_abs2(xa, res)
                t_numba += time.time()

                _matching(res,
                          expected,
                          almost=True,
                          dtype=np.float64,
                          ktol=2.)
示例#2
0
    def test_sub(self):
        for (dtypea, dtypeb
             ) in crossed_dtypes:  #, np.complex128]: # np.complex64 np.float32
            with self.subTest(dtypea=dtypea, dtypeb=dtypeb):
                nvec = 10000
                xa, stda = generate_random_xr(dtypea,
                                              nvec=nvec)  # , max_bin_exp=250)
                xb, stdb = generate_random_xr(dtypeb, nvec=nvec, seed=800)
                res = Xrange_array.empty(xa.shape,
                                         dtype=np.result_type(dtypea, dtypeb))
                expected = stda - stdb
                numba_test_sub(xa, xb, res)
                # Numba timing without compilation
                t_numba = -time.time()
                numba_test_sub(xa, xb, res)
                t_numba += time.time()
                # numpy timing
                t_np = -time.time()
                res_np = xa - xb
                t_np += time.time()

                _matching(res, expected)
                _matching(res_np, expected)

                print("t_numba", t_numba)
                print("t_numpy", t_np, t_numba / t_np)
                expr = (t_numba < t_np)
                self.assertTrue(expr, msg="Numba speed below numpy")

                # Test substract a scalar
                numba_test_sub(xa, stdb, res)
                _matching(res, expected)
                numba_test_sub(stda, xb, res)
                _matching(res, expected)
示例#3
0
    def test_div(self):
        for (dtypea, dtypeb
             ) in crossed_dtypes:  #, np.complex128]: # np.complex64 np.float32
            with self.subTest(dtypea=dtypea, dtypeb=dtypeb):
                nvec = 100000
                xa, stda = generate_random_xr(dtypea,
                                              nvec=nvec,
                                              max_bin_exp=75)
                # Adjust the mantissa to be sure to trigger a renorm for some
                # (around 30 %) cases
                xa = np.asarray(xa)
                exp = np.copy(xa["exp"])
                xa["mantissa"] *= 2.**(2 * exp)
                xa["exp"] = -exp
                xa = xa.view(Xrange_array)
                xb, stdb = generate_random_xr(dtypeb, nvec=nvec, seed=7800)
                res = Xrange_array.empty(xa.shape,
                                         dtype=np.result_type(dtypea, dtypeb))
                expected = stda / stdb

                numba_test_div(xa, xb, res)
                # Numba timing without compilation
                t_numba = -time.time()
                numba_test_div(xa, xb, res)
                t_numba += time.time()
                # numpy timing
                t_np = -time.time()
                res_np = xa / xb
                t_np += time.time()

                _matching(res,
                          expected,
                          almost=True,
                          dtype=np.float64,
                          ktol=2.)
                _matching(res_np,
                          expected,
                          almost=True,
                          dtype=np.float64,
                          ktol=2.)

                print("t_numba", t_numba)
                print("t_numpy", t_np, t_numba / t_np)
                expr = (t_numba < t_np)
                self.assertTrue(expr, msg="Numba speed below numpy")

                # Test divide by a scalar
                numba_test_div(xa, stdb, res)
                _matching(res,
                          expected,
                          almost=True,
                          dtype=np.float64,
                          ktol=2.)
                numba_test_div(stda, xb, res)
                _matching(res,
                          expected,
                          almost=True,
                          dtype=np.float64,
                          ktol=2.)
示例#4
0
    def test_sqrt(self):
        for dtype in (np.float64, np.complex128):  # np.complex64 np.float32
            with self.subTest(dtype=dtype):
                nvec = 10000
                xa, stda = generate_random_xr(dtype, nvec=nvec, max_bin_exp=75)
                # sqrt not defined for negative reals
                if dtype == np.float64:
                    xa = np.abs(xa)
                    stda = np.abs(stda)
                # Adjust the mantissa to be sure to trigger a renorm for some
                # (around 30 %) cases
                xa = np.asarray(xa)
                exp = np.copy(xa["exp"])
                xa["mantissa"] *= 2.**(2 * exp)
                xa["exp"] = -exp
                xa = xa.view(Xrange_array)
                res = Xrange_array.empty(xa.shape, dtype=dtype)
                expected = np.sqrt(stda)

                numba_test_sqrt(xa, res)
                # Numba timing without compilation
                t_numba = -time.time()
                numba_test_sqrt(xa, res)
                t_numba += time.time()
                # numpy timing
                t_np = -time.time()
                res_np = np.sqrt(xa)
                t_np += time.time()

                _matching(res,
                          expected,
                          almost=True,
                          dtype=np.float64,
                          ktol=2.)
                _matching(res_np,
                          expected,
                          almost=True,
                          dtype=np.float64,
                          ktol=2.)

                print("t_numba", t_numba)
                print("t_numpy", t_np, t_numba / t_np)
                expr = (t_numba < t_np)
                self.assertTrue(expr, msg="Numba speed below numpy")
示例#5
0
    def test_expr(self):
        for (dtypea, dtypeb
             ) in crossed_dtypes:  #, np.complex128]: # np.complex64 np.float32

            dtype_res = np.result_type(dtypea, dtypeb)
            nvec = 10000
            xa, stda = generate_random_xr(dtypea,
                                          nvec=nvec)  # , max_bin_exp=250)
            xb, stdb = generate_random_xr(dtypeb, nvec=nvec, seed=800)
            res = Xrange_array.empty(xa.shape, dtype=dtype_res)

            def get_numba_expr(case):
                if case == 0:

                    def numba_expr(xa, xb, out):
                        n, = xa.shape
                        for i in range(n):
                            out[i] = xa[i] * xb[i] * xa[i]
                elif case == 1:

                    def numba_expr(xa, xb, out):
                        n, = xa.shape
                        for i in range(n):
                            out[i] = xa[i] * xb[i] + xa[i] - 7.8
                elif case == 2:

                    def numba_expr(xa, xb, out):
                        n, = xa.shape
                        for i in range(n):
                            out[i] = (xb[i] * 2.) * (xa[i] + xa[i] * xb[i]) + (
                                xa[i] * xb[i] - 7.8 + xb[i])
                elif case == 3:

                    def numba_expr(xa, xb, out):
                        n, = xa.shape
                        for i in range(n):
                            out[i] = (
                                (xb[i] * 2.) *
                                (xa[i] + np.abs(xa[i] * xb[i]) + 1.) +
                                (xa[i] * np.sqrt(np.abs(xb[i]) + 7.8) + xb[i]))
                else:
                    raise ValueError(case)
                return numba.njit(numba_expr)

            def get_std_expr(case):
                if case == 0:

                    def std_expr(xa, xb):
                        return xa * xb * xa
                elif case == 1:

                    def std_expr(xa, xb):
                        return xa * xb + xa - 7.8
                elif case == 2:

                    def std_expr(xa, xb):
                        return (xb * 2.) * (xa + xa * xb) + (xa * xb - 7.8 +
                                                             xb)
                elif case == 3:

                    def std_expr(xa, xb):
                        return ((xb * 2.) * (xa + np.abs(xa * xb) + 1.) +
                                (xa * np.sqrt(np.abs(xb) + 7.8) + xb))
                else:
                    raise ValueError(case)
                return std_expr

            n_case = 4
            for case in range(n_case):

                with self.subTest(dtypea=dtypea, dtypeb=dtypeb, expr=case):
                    expected = get_std_expr(case)(stda, stdb)

                    # numpy timing
                    t_np = -time.time()
                    res_np = get_std_expr(case)(xa, xb)
                    t_np += time.time()

                    numba_expr = get_numba_expr(case)
                    numba_expr(xa, xb, res)
                    # Numba timing without compilation
                    t_numba = -time.time()
                    numba_expr(xa, xb, res)
                    t_numba += time.time()

                    _matching(res,
                              expected,
                              almost=True,
                              dtype=np.float64,
                              ktol=2.)
                    _matching(res_np,
                              expected,
                              almost=True,
                              dtype=np.float64,
                              ktol=2.)

                    print("t_numba", t_numba)
                    print("t_numpy", t_np, t_numba / t_np)
                    expr = (t_numba < t_np)
                    self.assertTrue(expr, msg="Numba speed below numpy")