[Python] Why (x / y)[i] faster than x[i] / y[i]?

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I'm new to CuPy and CUDA/GPU computing. Can someone explain why (x / y) faster than x / y?

    When taking advantage of GPU accelerated computations, are there any guidelines that would allow me to quickly determine which operation is faster? Which to avoid benchmarking every operation.

    # In VSCode Jupyter Notebook

    import cupy as cp
    from cupyx.profiler import benchmark

    x = cp.arange(1_000_000)
    y = (cp.arange(1_000_000) + 1) / 2
    i = cp.random.randint(2, size=1_000_000) == 0
    x, y, i

    # Output:
    (array([ 0, 1, 2, ..., 999997, 999998, 999999], shape=(1000000,), dtype=int32),
    array([5.000000e-01, 1.000000e+00, 1.500000e+00, ..., 4.999990e+05,
    4.999995e+05, 5.000000e+05], shape=(1000000,), dtype=float64),
    array([ True, False, True, ..., True, False, True], shape=(1000000,), dtype=bool))


    def test1(x, y, i):
    return (x / y)

    def test2(x, y, i):
    return x / y

    print(benchmark(test1, (x, y, i)))
    print(benchmark(test2, (x, y, i)))

    # Output:
    test1: CPU: 175.164 us +/- 61.250 (min: 125.200 / max: 765.100) us GPU-0: 186.001 us +/- 67.314 (min: 134.144 / max: 837.568) us
    test2: CPU: 342.364 us +/- 130.840 (min: 223.000 / max: 1277.600) us GPU-0: 368.133 us +/- 136.911 (min: 225.504 / max: 1297.408) us

    Continue reading...

Compartilhe esta Página