Execution in multiprocessors

Asked

Viewed 265 times

0

I am researching about parallel execution in Python and I was left with a question that I have not found clear answer.

Let’s say I want to multiply two matrices and add in a task list where each task is a row of matrix A and a column of matrix B and ask that n threads remove from this list a task to multiply.

I have 4 colors on the machine, using the module threading.Thread from Python, if I create 4 theads for the execution of the task list, these threads one shall be carried out in each core? I mean, parallel? Or for that I would need to use the module multiprocessing that divides the tasks into 1 process for each core?

If only with multiprocessing I can distribute the tasks in the 4 colors, how do I set, for example, 2 threads in each process, that is to say each core would receive 2 threads for processing my task?

  • 2

    Something I already recommend you research is about GIL (Global Interpreter Lock), in Python, which briefly says that the interpreter can only be used by one thread at a time.

  • Have you tried anything? Add your question what you have tried

  • I haven’t developed yet I’m trying to understand how to use the 4 processors of the machine with python, I don’t know if by creating 4 threads automatically each one will run on each processor.

  • @Andrésalgueiro The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

  • In fact, in pure Python, only one thread runs at a time, no matter how many nuclei you have. Numpy is no longer pure Python - and in fact, it already parallelizes this type of operation on several cores alone. For didactic purposes yes, there is a lot to talk about. I have some answers already about multiprocessing

  • These two answers have a lot of detail of what you need to understand now - both how Python works with multi-threading and multi-processing, how to do it. https://answall.com/questions/146417/fibonacci-com-execu%C3%A7%C3%a3o-parallel-threads/146458#146458 https://answall.com/questions/197174/python-c%C3%b3digo-serial-vs-multiprocessing-vs-threading/197678#197678

Show 1 more comment

1 answer

1

To thread exists to allow concurrent processing. There are no guarantees that the execution will be done on different processors. It is guaranteed that if you have only the thread main will only run on a processor. That being said, it is likely to use all available processors if you have at least one thread for each of them. In this case everything will run in parallel.

That’s actually the best way to create a thread. I don’t like its use when all you really want is execution asynchrony.

In general you have no control over where it will run, at most you can establish affinity and priority, but I don’t know if you have an API that can handle it. Has for the process.

I don’t know how to deal with threads in Python. I remember that I had some problem with the GIL, but I don’t understand all the consequences of this to give more details.

Some people prefer to do this on another level, in C for example. There must be a reason.

Whether you can properly parallelize is another matter.

See this to help you make a decision: It’s always guaranteed that a multi-threaded application runs faster than using a single thread?

  • Nice guy you referred me to. But I still don’t know if creating 4 threads in python will cause each of the 4 processors to receive a thread to run

  • 1

    No - the Python VM runs bytecode on only one kernel at a time - the so-called "Global Interpreter Lock" - only when the execution calls a function in native code, which releases GIL, more than one kernel can be used. In Python, threads are just a very simple way to solve parallelism when the problem is I/O latency/hold events. Numpy, however, already uses more than one core in its computations without having to do anything else.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.