Skip to main content

Posts

Showing posts with the label parallel

Leveraging CUDA for General Parallel Processing Application

  Photo by SevenStorm JUHASZIMRUS by pexel Differences Between CPU-based Multi-threading and Multi-processing CPU-based Multi-threading : - Concept: Uses multiple threads within a single process. - Shared Memory: Threads share the same memory space. - I/O Bound Tasks: Effective for tasks that spend a lot of time waiting for I/O operations. - Global Interpreter Lock (GIL): In Python, the GIL can be a limiting factor for CPU-bound tasks since it allows only one thread to execute Python bytecode at a time. CPU-based Multi-processing : - Concept: Uses multiple processes, each with its own memory space. - Separate Memory: Processes do not share memory, leading to more isolation. - CPU Bound Tasks: Effective for tasks that require significant CPU computation since each process can run on a different CPU core. - No GIL: Each process has its own Python interpreter and memory space, so the GIL is not an issue. CUDA with PyTorch : - Concept: Utilizes the GPU for parallel computation. - Massi...

The new feature in Python 3.13 allowing CPython to run without the Global Interpreter Lock

Understanding Free-threaded CPython and Parallel Execution The new feature in Python 3.13, allowing CPython to run without the Global Interpreter Lock (GIL), is significant for improving parallelism in Python programs. Here’s a detailed explanation along with a code example to illustrate how it works and the benefits it brings: Key Points 1. Disabling the GIL : CPython can be built with the `--disable-gil` option, allowing threads to run in parallel across multiple CPU cores. 2. Parallel Execution : This enables full utilization of multi-core processors, leading to potential performance improvements for multi-threaded programs. 3. Experimental Feature : This is still experimental and may have bugs and performance trade-offs in single-threaded contexts. 4. Optional GIL : The GIL can still be enabled or disabled at runtime using the `PYTHON_GIL` environment variable or the `-X gil` command-line option. 5. C-API Extensions : Extensions need to be adapted to work without the GIL. Demo Code...

Python Parallel Processing and Threading Comparison

If you want to maximize your CPU bound #python processing tasks you can think the following way. Given that your Python process is CPU-bound and you have almost unlimited CPU capacity, using `concurrent.futures.ProcessPoolExecutor` is likely to provide better performance than `concurrent.futures.ThreadPoolExecutor`. Here's why: 1. Parallelism: `ProcessPoolExecutor` utilizes separate processes, each running in its own Python interpreter, which allows them to run truly concurrently across multiple CPU cores. On the other hand, `ThreadPoolExecutor` uses #threads , which are subject to the Global Interpreter Lock (GIL) in Python, limiting true parallelism when it comes to CPU-bound tasks. 2. GIL Limitation: The GIL restricts the execution of Python bytecode to a single thread at a time, even in multi-threaded applications. While threads can be useful for I/O-bound tasks or tasks that release the GIL, they are less effective for CPU-bound tasks because they cannot run simultaneously du...