Python, Java, C # and Parallel Algorithms

I was wondering: was there any merit in trying to create parallel algorithms in Python? Let's say I want to explore a new parallel algorithm, and I have a choice of C, C # and Python, whether it will be “better” to test and compare these algorithms, or are they just “functionally equivalent” and, in addition, the constants associated with the interpreted / compiled / vm languages, will everything be the same? Thanks

+2
source share
3 answers

Python is not suitable for this because of the global interpreter (GIL) lock; it does not work this way. C is difficult to use in a multi-threaded environment, but there is an alternative - Cilk . C # is a pretty good choice for concurrent programming. You can use the parallel task library, parallel data structures, and PLINQ from the .NET Framework 4.

+4
source

C has many alternatives, but it is a low-level language. Intel Thread Building Blocks is a commercial parallel library based on Cilk that I hear well.

It depends on what you want to do for Python. Although you can get around the GIL using Cython, a pre-compiled version of Python, Cython slowly compiles and loses platform independence. In addition, Python is a scripting language, so you usually want to switch to a faster language before parallelizing. At the same time, using the multiprocessing module in Python (which allows you to bypass the GIL), you can write some simple parallel codes.

Java is probably the best choice. The synchronized , as well as decent primitives (such as a streaming non-blocking queue) make it relatively nice. Java is old, although you spend time writing more verbose code than Python.

I slowly learn parallelism in Haskell. It seems that this is not the easiest thing to get something effective, but if your task is quite parallel, then it works well. And, of course, Haskell is incredibly expressive (after you get to his syntactic mind - differently differently imho), and since it functions, you don't get strange corruption problems in the state; In the worst case, your code does not scale. There are many libraries, such as STM (Transactional Programming Memory), which seem to solve some problems much better (theoretically) than the primitives that real programmers use, such as locks.

0
source

I would definitely use Python because writing Python programs is quick and easy, and because it probably already includes all the tools you need. This will allow you to focus on the algorithm, not the syntax and declarations.

For benchmarking you can use cProfile or timeit .

What parallel algorithms do you want to test? There are many ways to parallel programming. For algorithms that need to scale for many processes, the easiest way is probably to use MPI . mpi4py is a very good implementation for Python and is tightly integrated with numpy if you need to work with arrays of numbers.

As already mentioned, multithreading can only use 1 processor due to GIL . But this restriction applies only to this case. You can use multiprocessing as many processors as you want.

Finally, Python has tools for functional programming . And coroutines can be useful for parallel programming.

0
source

All Articles