Partage
  • Partager sur Facebook
  • Partager sur Twitter

Multithreading Python

    19 juillet 2019 à 12:01:25

    Bonjour, j'essaye d'appréhender le multithreading sur python avec Numba, et j'ai trouvé un exemple pas mal qui expliquait ça, mais dès que j'essaye de rajouter un paramètre autre que a et b dans la fonction ça ne marche plus...

    j'ai l'impression que je ne comprends pas tout dans le code, notamment la syntaxe  chunks = [[arg[i * chunklen:(i + 1) * chunklen] for arg in args]


    Que signifient les ":" dans le code, et l'asterix est-elle vraiment une multiplication?

    Code:

    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)
    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 we start the benchmark
        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)


    Merci d'avance pour votre aide

    • Partager sur Facebook
    • Partager sur Twitter
      19 juillet 2019 à 13:24:49

      C'est un extrait de liste ou de nparray, ex :

      >>> a = [7,8,9,4,5,6,1,2,3,0]
      >>> a[2:4]
      [9, 4]
      

      Mais en général je préfère mettre des espaces je trouve ça plus propre surtout si les indices sont des expressions :

      a[2 : 4]


      Le * est bien une multiplication ici :

      a[2 : 2 * 2]


      Le * sert à trop de trucs en python ça peut prêter à confusion :ninja:

      -
      Edité par thelinekioubeur 19 juillet 2019 à 13:29:06

      • Partager sur Facebook
      • Partager sur Twitter
        22 juillet 2019 à 9:44:09

        Ah d'accord merci beaucoup pour votre réponse, je connaissais le slicing mais je n'avais pas du tout vu que c'etait ça
        • Partager sur Facebook
        • Partager sur Twitter

        Multithreading Python

        × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
        × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
        • Editeur
        • Markdown