Concurrency and Parallelism – Day 1


Concurrency means doing things asynchronously – i.e. not waiting for one thing to finish to start other. Parallelism means doing things in parallel i.e. doing multiple operations at same instance of time. Don’t worry this post is not about Quantum theory of space and time, we are talking about software design concepts which have been around for quite some time now and people have written some very good articles about these.
I would be writing a series of posts around the two concepts because I want to write the content that helps in answering questions like:
• Can Concurrency help in a particular situation?
• I want to do a lot of things in parallel can I do them?
• What is required for Concurrency?
• What is required for Parallelism?
Why do I write another post about the subject if there are a lot of them – because they lack practical examples and they discuss more about the theory (and they lack very good diagrams). So without wasting any time and space let’s quickly jump over to the business.

Concurrency is a property of your program/code/software which means that you have optimized your code to not wait for operations which are non-deterministic in terms of time (simply put you don’t know how much time they are going to take to finish) and you have additional things to do along with your long running tasks. Let me make it very clear here that these additional tasks would not happen in parallel but the system (operating system) is going to make use of a technique known as time-slicing to allocate some of the processor time to both/all your asynchronous tasks. Concurrency helps you in making your programs better in terms of usability, user productivity and user experience. Before presenting any examples I would like to explain that Concurrency can help you with optimal usage of resources but this is not always the case and I am going to present examples where the resources (processor) are being optimized.

Let’s start with a basic example:

Problem:

You have a single core, single processor machine and you want to write a word processing program which also does spelling and grammar check.

Approach 1:

You might write a program that takes the user input from the user checks its spelling and whether it fits correctly (grammatically) in the input so far and if it’s okay then allows the user to enter next word. This would not be a very good approach from user productivity point of view because the user cannot do anything else while you are validating the inputs.

Approach 1

Approach 2:

So you decide enough is enough let’s make it better and you modify the program to take all the user input in a single go and then check it in a single go. Well you have managed to get the user to do a lot of work continuously but they still need to wait while your code validates everything in the end. The user calls and says thanks for the changes now I can sit and enjoy while the software does the input validation and if my boss confronts me about sitting idle I can show them that things are happening and while they are happening I am supposed to wait.

Approach 2

The Solution:

So the solution to your problem lies in sharing (it is always good to share). What would you share? Share the processor. In a single core single processor environment a multi-tasking operating system has a feature known as time slicing which refers to switching execution of code at fast intervals to give an illusion of parallel execution of multiple code segments. How do you segregate code segments? There are constructs known as Threads which take code segments and execute them in their lifetime. The processor time is shared between these threads and the operating system does this switching at fast intervals. In our example we can have two threads, one which takes the user input and the other one which validates it. The operating system would do the time slicing between the two and your program would not require the use to wait because the program flow is not blocking the user input for the validation task to be finished. This is a program design property where you have designed your program in a way that the flow is not blocked between two related but independent tasks (user input and validation are related but are independent tasks).

Approach 3

We will discuss more about concurrency in the next post.

Until then…

Pradeep

Other posts

  • Object Extraction using Image Segmentation: A Comprehensive Tutorial with Detectron2 and Mask2Former

    Object Extraction using Image Segmentation: A Comprehensive Tutorial with Detectron2 and Mask2Former

    Discover how to perform object extraction using image segmentation with Detectron2 and Mask2Former in our step-by-step tutorial. Learn to set up the environment, configure the model, and visualize segmentation results, extracting objects from images with ease. Boost your computer vision skills and optimize your image processing projects with this comprehensive guide.

  • Building Your First Neural Network with TensorFlow – Deep Learning 2

    Building Your First Neural Network with TensorFlow – Deep Learning 2

    Neural networks are a fundamental concept in deep learning and are used for a wide range of applications such as image and speech recognition, natural language processing, and much more. In this article, we will walk you through the process of building your first neural network using TensorFlow, a popular open-source machine learning library. We'll…

  • Introduction to Deep Learning with TensorFlow – Deep Learning 1

    Introduction to Deep Learning with TensorFlow – Deep Learning 1

    In this article, we provide an introduction to deep learning with TensorFlow. We cover what deep learning is, what it can do, why TensorFlow is a great choice for deep learning, and an overview of TensorFlow itself. We also explore the different types of neural networks used in deep learning, and demonstrate how to build…

  • How To: Set Up PyTorch with GPU Support on Windows 11 – A Comprehensive Guide

    How To: Set Up PyTorch with GPU Support on Windows 11 – A Comprehensive Guide

    Introduction Hello tech enthusiasts! Pradeep here, your trusted source for all things related to machine learning, deep learning, and Python. As you know, I’ve previously covered setting up TensorFlow on Windows. Today, I’m excited to bring you a detailed guide on setting up another popular deep learning framework, PyTorch, with GPU support on Windows 11.…

  • Solving a Complex Logistics Optimization Problem using the Pulp Library in Python – Part 4

    Solving a Complex Logistics Optimization Problem using the Pulp Library in Python – Part 4

    In this article, we demonstrate how to solve a logistics optimization problem using the Pulp library in Python. By defining the variables, objective function, and constraints, and using the solve method to find the optimal solution, we are able to minimize the total cost of transportation while satisfying the constraints. This article concludes the multi-part…

  • Linear Programming in Python using PuLP – Part 3: Optimizing Investment Portfolios with Multi-Objective Optimization

    Linear Programming in Python using PuLP – Part 3: Optimizing Investment Portfolios with Multi-Objective Optimization

    In this article, we used the Pulp library in Python to solve a linear programming problem to find the optimal investment portfolio. We defined variables, added constraints, defined objectives, and solved the problem to find the optimal solution that balances the trade-off between maximizing returns and minimizing risk. The code was concise and easy to…

  • Linear Programming in Python using Pulp – Part 2

    Linear Programming in Python using Pulp – Part 2

    In this article, we delve deeper into linear programming and explore how to solve a multi-objective optimization problem using the Pulp library in Python. We present a problem in which a nutritionist must find the optimal meal plan for a patient suffering from anemia, balancing the intake of Vitamin B12 and fat. We demonstrate how…

  • Linear Programming in Python using PuLP – Part 1

    Linear Programming in Python using PuLP – Part 1

    Linear programming is an optimization technique used to find the best outcomes for a given problem. This technique relies on a set of constructs which are all expressed using a system of linear equations. It is important to understand that you should be able to express your objective as a linear equation dependent on an…

  • How To: Setup Tensorflow With GPU Support using Docker

    How To: Setup Tensorflow With GPU Support using Docker

    Previously I published a guide for setting up tensorflow in an anconda environment with GPU support. A lot of people liked it and I have been working with this environment myself for more than a year now. I am happy with the results however the process is a bit involved and requires quite a bit…

  • How To: Setup Tensorflow With GPU Support in Windows 11

    How To: Setup Tensorflow With GPU Support in Windows 11

    It's been just 2 days since Windows 11 came out and I am already setting up my system for the ultimate machine learning environment. Today we are going to setup a new anaconda environment with tensorflow 2.5 with GPU support using NVIDIA CUDA 11.4 and CUDNN 8.2.4 along with Python 3.8. This is going to…

  • Tools of The Trade – II

    Tools of The Trade – II

    In continuation of my previous post today I will talk about the website tanooja.com. I did this project on request of my wife because she wanted to pursue blogging and didn't want to go through the ordeal needed to write, publish and manage SEO using most of the prominent blogging platforms like WordPress, Joomla, Drupal…

  • Tools of The Trade – I

    Tools of The Trade – I

    In this post I will share a few tools and technologies that I am using to run a couple of blazing fast websites using latest modern tools and technologies. The caveat here is that I don't pay any infrastructure/hosting costs for any of these websites and they can scale infinitely in terms of supported users…

  • Building Lizzie – IV

    Building Lizzie – IV

    Another post about Lizzie. I started off with a Raspberry Pi 3 to build a personal assistant for my car and I have come a long way both in terms of the concept and the functionality. Most importantly I have formalized the application flow and also extended the scope from one device to almost all…

  • OBD-II with Raspberry Pi3

    OBD-II with Raspberry Pi3

    I am writing this article in response to a question posted on my YouTube channel. Here I would be talking about communicating to an OBD-II device (ELM327 chip with Bluetooth) hooked into your car’s OBD-II port. The OS I am using is Windows 10 IoT core. This information is important because it makes a difference…

  • Building Lizzie – III

    Building Lizzie – III

    As mentioned in previous article today I would be talking about OBD-II integration in Lizzie using a Bluetooth serial communication with an ELM327 adapter that fits on a OBD-II port in your car. OBD stands for On Board Diagnostics which is connected to the ECU (Engine Control Unit) and provides a ton of information (both…

  • Building Lizzie – II

    Building Lizzie – II

    In the previous post I described my experiments around building an intelligent artificial personal assistant – Lizzie. The pseudo intelligent agents available today around us (Siri, Cortana or Google Next) are all great feats of engineering given the fact that they reside on small devices like mobile phones and are able to do powerful things…

  • Building Lizzie – I

    Building Lizzie – I

    Recently I have been busy building a personal assistant that I would be fitting in my car. Currently I am in experimentation mode and I am experimenting with speech capabilities. I would start with a description of my journey so far. First let me show off a little bit with these videos that I created…

  • How To: Super Fast Tensorflow 2 Setup with GPU Support in VS Code

    I am publishing this article in response to a comment I received on How To: Setup Tensorflow With GPU Support using Docker article. Docker is all good and nice but all of us agree that we use IDEs (integrated dev environment – think VSCode) on our machines. So let’s say you want to get the…

%d bloggers like this: