def benchmark_distance_2d():
    def distance_2d(n):
        x, y = np.meshgrid(scale[0] * (np.arange(n) - origin[0]),
                           scale[1] * (np.arange(n) - origin[1]),
                           sparse=True)
        return np.sqrt(x**2 + y**2)

    def distance_2d_fortran(n):
        out = np.empty((n, n))
        du.distance_2d_r8(out.T, origin, scale)
        return out

    def distance2_2d(n):
        x, y = np.meshgrid(scale[0] * (np.arange(n) - origin[0]),
                           scale[1] * (np.arange(n) - origin[1]),
                           sparse=True)
        return x**2 + y**2

    def distance2_2d_fortran(n):
        out = np.empty((n, n))
        du.distance2_2d_r8(out.T, origin, scale)
        return out

    origin = [0.2, 0.1]
    scale = [1.3, 1.7]
    ns = [2**_ for _ in range(1, 14)]
    assert_same(distance_2d(4), distance_2d_fortran(4))
    assert_same(distance2_2d(4), distance2_2d_fortran(4))

    print()
    print('BENCHMARK DISTANCE_2D')
    print('=====================')
    b = benchmark([distance_2d, distance_2d_fortran], ns, verbose=False)
    title = '{:^9}{:^12}{:^12}'.format('N', 'Python', 'Fortran')
    print(title)
    print(len(title) * '-')
    for n, t1, t2 in zip(ns, b['time'][0], b['time'][1]):
        print('{:<9}{:12.9f}{:12.9f}'.format(n, t1, t2))

    print()
    print('BENCHMARK DISTANCE2_2D')
    print('======================')
    b = benchmark([distance2_2d, distance2_2d_fortran], ns, verbose=False)
    title = '{:^9}{:^12}{:^12}'.format('N', 'Python', 'Fortran')
    print(title)
    print(len(title) * '-')
    for n, t1, t2 in zip(ns, b['time'][0], b['time'][1]):
        print('{:<9}{:12.9f}{:12.9f}'.format(n, t1, t2))
def benchmark_create_grid_squares():
    from pysimulators.geometry import create_grid_squares

    def create_grid_squares_slow(shape, spacing, filling_factor=1,
                                 center=(0, 0), angle=0):
        x = np.arange(shape[1], dtype=float) * spacing
        x -= x.mean()
        y = (np.arange(shape[0], dtype=float) * spacing)[::-1]
        y -= y.mean()
        grid_x, grid_y = np.meshgrid(x, y)
        nodes = np.asarray([grid_x.T, grid_y.T]).T.copy()
        d = np.sqrt(filling_factor * spacing**2) / 2
        offset = np.array([[d, d], [-d, d], [-d, -d], [d, -d]])
        corners = nodes[..., None, :] + offset
        pysimulators._flib.geometry.rotate_2d_inplace(corners.T, angle)
        corners += center
        return corners

    ns = list(range(11))
    ids = ['{0}x{0}'.format(2**n) for n in ns]
    shapes = [2 * (2**n,) for n in ns]
    spacing = 0.4
    b = benchmark([create_grid_squares_slow, create_grid_squares], shapes,
                  spacing, verbose=False)
    header = '{:^9}{:^12}{:^12}'.format('shape', 'Python', 'Fortran')
    print(header)
    print(len(header) * '-')
    for n, t1, t2 in zip(ids, b['time'][0], b['time'][1]):
        print('{:<9}{:12.9f}{:12.9f}'.format(n, t1, t2))
def benchmark_create_grid_squares():
    from pysimulators.geometry import create_grid_squares

    def create_grid_squares_slow(shape,
                                 spacing,
                                 filling_factor=1,
                                 center=(0, 0),
                                 angle=0):
        x = np.arange(shape[1], dtype=float) * spacing
        x -= x.mean()
        y = (np.arange(shape[0], dtype=float) * spacing)[::-1]
        y -= y.mean()
        grid_x, grid_y = np.meshgrid(x, y)
        nodes = np.asarray([grid_x.T, grid_y.T]).T.copy()
        d = np.sqrt(filling_factor * spacing**2) / 2
        offset = np.array([[d, d], [-d, d], [-d, -d], [d, -d]])
        corners = nodes[..., None, :] + offset
        pysimulators._flib.geometry.rotate_2d_inplace(corners.T, angle)
        corners += center
        return corners

    ns = list(range(11))
    ids = ['{0}x{0}'.format(2**n) for n in ns]
    shapes = [2 * (2**n, ) for n in ns]
    spacing = 0.4
    b = benchmark([create_grid_squares_slow, create_grid_squares],
                  shapes,
                  spacing,
                  verbose=False)
    header = '{:^9}{:^12}{:^12}'.format('shape', 'Python', 'Fortran')
    print(header)
    print(len(header) * '-')
    for n, t1, t2 in zip(ids, b['time'][0], b['time'][1]):
        print('{:<9}{:12.9f}{:12.9f}'.format(n, t1, t2))
def benchmark_distance_2d():
    def distance_2d(n):
        x, y = np.meshgrid(scale[0] * (np.arange(n) - origin[0]),
                           scale[1] * (np.arange(n) - origin[1]), sparse=True)
        return np.sqrt(x**2 + y**2)

    def distance_2d_fortran(n):
        out = np.empty((n, n))
        du.distance_2d_r8(out.T, origin, scale)
        return out

    def distance2_2d(n):
        x, y = np.meshgrid(scale[0] * (np.arange(n) - origin[0]),
                           scale[1] * (np.arange(n) - origin[1]), sparse=True)
        return x**2 + y**2

    def distance2_2d_fortran(n):
        out = np.empty((n, n))
        du.distance2_2d_r8(out.T, origin, scale)
        return out

    origin = [0.2, 0.1]
    scale = [1.3, 1.7]
    ns = [2**_ for _ in range(1, 14)]
    assert_same(distance_2d(4), distance_2d_fortran(4))
    assert_same(distance2_2d(4), distance2_2d_fortran(4))

    print()
    print('BENCHMARK DISTANCE_2D')
    print('=====================')
    b = benchmark([distance_2d, distance_2d_fortran], ns, verbose=False)
    title = '{:^9}{:^12}{:^12}'.format('N', 'Python', 'Fortran')
    print(title)
    print(len(title)*'-')
    for n, t1, t2 in zip(ns, b['time'][0], b['time'][1]):
        print('{:<9}{:12.9f}{:12.9f}'.format(n, t1, t2))

    print()
    print('BENCHMARK DISTANCE2_2D')
    print('======================')
    b = benchmark([distance2_2d, distance2_2d_fortran], ns, verbose=False)
    title = '{:^9}{:^12}{:^12}'.format('N', 'Python', 'Fortran')
    print(title)
    print(len(title)*'-')
    for n, t1, t2 in zip(ns, b['time'][0], b['time'][1]):
        print('{:<9}{:12.9f}{:12.9f}'.format(n, t1, t2))
def benchmark_distance_1d():
    def distance_1d(n):
        return abs(scale * (np.arange(n) - origin))

    def distance_1d_fortran(n):
        out = np.empty(n)
        du.distance_1d_r8(out, origin, scale)
        return out

    def distance2_1d(n):
        return (scale * (np.arange(n) - origin))**2

    def distance2_1d_fortran(n):
        out = np.empty(n)
        du.distance2_1d_r8(out, origin, scale)
        return out

    origin = 0.2
    scale = 1.3
    ns = [2**_ for _ in range(1, 27)]
    assert_same(distance_1d(4), distance_1d_fortran(4))
    assert_same(distance2_1d(4), distance2_1d_fortran(4))

    print()
    print('BENCHMARK DISTANCE_1D')
    print('=====================')
    b = benchmark([distance_1d, distance_1d_fortran], ns, verbose=False)
    title = '{:^9}{:^12}{:^12}'.format('N', 'Python', 'Fortran')
    print(title)
    print(len(title) * '-')
    for n, t1, t2 in zip(ns, b['time'][0], b['time'][1]):
        print('{:<9}{:12.9f}{:12.9f}'.format(n, t1, t2))

    print()
    print('BENCHMARK DISTANCE2_1D')
    print('======================')
    b = benchmark([distance2_1d, distance2_1d_fortran], ns, verbose=False)
    title = '{:^9}{:^12}{:^12}'.format('N', 'Python', 'Fortran')
    print(title)
    print(len(title) * '-')
    for n, t1, t2 in zip(ns, b['time'][0], b['time'][1]):
        print('{:<9}{:12.9f}{:12.9f}'.format(n, t1, t2))
def benchmark_distance_1d():
    def distance_1d(n):
        return abs(scale * (np.arange(n) - origin))

    def distance_1d_fortran(n):
        out = np.empty(n)
        du.distance_1d_r8(out, origin, scale)
        return out

    def distance2_1d(n):
        return (scale * (np.arange(n) - origin))**2

    def distance2_1d_fortran(n):
        out = np.empty(n)
        du.distance2_1d_r8(out, origin, scale)
        return out

    origin = 0.2
    scale = 1.3
    ns = [2**_ for _ in range(1, 27)]
    assert_same(distance_1d(4), distance_1d_fortran(4))
    assert_same(distance2_1d(4), distance2_1d_fortran(4))

    print()
    print('BENCHMARK DISTANCE_1D')
    print('=====================')
    b = benchmark([distance_1d, distance_1d_fortran], ns, verbose=False)
    title = '{:^9}{:^12}{:^12}'.format('N', 'Python', 'Fortran')
    print(title)
    print(len(title)*'-')
    for n, t1, t2 in zip(ns, b['time'][0], b['time'][1]):
        print('{:<9}{:12.9f}{:12.9f}'.format(n, t1, t2))

    print()
    print('BENCHMARK DISTANCE2_1D')
    print('======================')
    b = benchmark([distance2_1d, distance2_1d_fortran], ns, verbose=False)
    title = '{:^9}{:^12}{:^12}'.format('N', 'Python', 'Fortran')
    print(title)
    print(len(title)*'-')
    for n, t1, t2 in zip(ns, b['time'][0], b['time'][1]):
        print('{:<9}{:12.9f}{:12.9f}'.format(n, t1, t2))
def benchmark_rotate_2d():
    rotate_2d_fortran = pysimulators._flib.geometry.rotate_2d
    rotate_2d_fortran_inplace = pysimulators._flib.geometry.rotate_2d_inplace

    def rotate1(n):
        coords = x[:n]
        coords = np.asarray(coords)
        out = np.empty_like(coords)
        cosangle = np.cos(angle)
        sinangle = np.sin(angle)
        m = np.asarray([[cosangle, -sinangle],
                        [sinangle,  cosangle]])
        return np.dot(coords, m.T, out)

    def rotate2(n):
        coords = x[:n]
        out = np.empty_like(coords)
        rotate_2d_fortran(coords.T, out.T, angle_deg)
        return out

    def rotate2_inplace(n):
        coords = x[:n]
        rotate_2d_fortran_inplace(coords.T, angle_deg)
        return x

    ns = (10, 100, 1000, 10000, 100000, 1000000, 10000000)
    x = np.random.random_sample((ns[-1], 2))
    angle = np.radians(30)
    angle_deg = 30
    b1 = benchmark(rotate1, ns, verbose=False)
    b2 = benchmark(rotate2, ns, verbose=False)
    b3 = benchmark(rotate2_inplace, ns, verbose=False)

    print('{:^9}{:^12}{:^12}{:15}'.format('N', 'Dot', 'Fortran',
                                          'Fortran Inplace'))
    print(48*'-')
    for n, t1, t2, t3 in zip(ns, b1['time'], b2['time'], b3['time']):
        print('{:<9}{:12.9f}{:12.9f}{:12.9f}'.format(n, t1, t2, t3))
def benchmark_rotate_2d():
    rotate_2d_fortran = pysimulators._flib.geometry.rotate_2d
    rotate_2d_fortran_inplace = pysimulators._flib.geometry.rotate_2d_inplace

    def rotate1(n):
        coords = x[:n]
        coords = np.asarray(coords)
        out = np.empty_like(coords)
        cosangle = np.cos(angle)
        sinangle = np.sin(angle)
        m = np.asarray([[cosangle, -sinangle], [sinangle, cosangle]])
        return np.dot(coords, m.T, out)

    def rotate2(n):
        coords = x[:n]
        out = np.empty_like(coords)
        rotate_2d_fortran(coords.T, out.T, angle_deg)
        return out

    def rotate2_inplace(n):
        coords = x[:n]
        rotate_2d_fortran_inplace(coords.T, angle_deg)
        return x

    ns = (10, 100, 1000, 10000, 100000, 1000000, 10000000)
    x = np.random.random_sample((ns[-1], 2))
    angle = np.radians(30)
    angle_deg = 30
    b1 = benchmark(rotate1, ns, verbose=False)
    b2 = benchmark(rotate2, ns, verbose=False)
    b3 = benchmark(rotate2_inplace, ns, verbose=False)

    print('{:^9}{:^12}{:^12}{:15}'.format('N', 'Dot', 'Fortran',
                                          'Fortran Inplace'))
    print(48 * '-')
    for n, t1, t2, t3 in zip(ns, b1['time'], b2['time'], b3['time']):
        print('{:<9}{:12.9f}{:12.9f}{:12.9f}'.format(n, t1, t2, t3))
from __future__ import division
import numpy as np
from pybenchmarks import benchmark
from pysimulators import _flib as flib, LayoutGridSquares
from pysimulators.sparse import FSRMatrix

grid = LayoutGridSquares((256, 256), 3, filling_factor=.8, angle=10.,
                         origin=(128*3, 128*3))
ncolmax = 16
nx, ny = grid.shape * np.array(3.)

setup = """
from __main__ import grid, ncolmax, nx, ny, FSRMatrix, np, flib
matrix = FSRMatrix((len(grid), nx * ny), dtype=dtype,
                   dtype_index=itype, ncolmax=ncolmax)
data = matrix.data.ravel().view(np.int8)
func = 'matrix_polygon_integration_i{0}_r{1}'.format(itype.itemsize,
                                                     dtype.itemsize)
f = getattr(flib.projection, func)
"""

b = benchmark('f(grid.vertex.T, nx, ny, data, ncolmax)',
              dtype=(np.dtype(np.float32), np.dtype(np.float64)),
              itype=(np.dtype(np.int32), np.dtype(np.int64)),
              setup=setup, repeat=10)
Example #10
0
from __future__ import division
import numpy as np
from pybenchmarks import benchmark
from pysimulators import _flib as flib, LayoutGridSquares
from pysimulators.sparse import FSRMatrix

grid = LayoutGridSquares((256, 256),
                         3,
                         filling_factor=.8,
                         angle=10.,
                         origin=(128 * 3, 128 * 3))
ncolmax = 16
nx, ny = grid.shape * np.array(3.)

setup = """
from __main__ import grid, ncolmax, nx, ny, FSRMatrix, np, flib
matrix = FSRMatrix((len(grid), nx * ny), dtype=dtype,
                   dtype_index=itype, ncolmax=ncolmax)
data = matrix.data.ravel().view(np.int8)
func = 'matrix_polygon_integration_i{0}_r{1}'.format(itype.itemsize,
                                                     dtype.itemsize)
f = getattr(flib.projection, func)
"""

b = benchmark('f(grid.vertex.T, nx, ny, data, ncolmax)',
              dtype=(np.dtype(np.float32), np.dtype(np.float64)),
              itype=(np.dtype(np.int32), np.dtype(np.int64)),
              setup=setup,
              repeat=10)