Monday, August 13, 2012

Chaos in Parallel Programming

Finally felt like blogging again so here it is a new post, a new topic. This time it is more towards the technical side. For the past so many weeks I had this stupid doubt on threading concepts and threads. Most of the techies would understand what a thread is but those who haven't heard of the term at all its not a problem. You all could still give me some general comments which "might" clear my doubt. Basically a thread is a smallest unit of processing that can be scheduled by an operating system. Well! this is the definition but to explain in simple terms it is a process in your windows 7, mac. OK! so what is a process? It an unit of work or rather in simplest terms its one of your applications like Microsoft word or Tetris game. There are quite a few programming languages which would support threading. Well! I have heard most of my professors say threading is a logical process happening in the backend and there is no way to control it while it runs. My first questions Then what is the use of it? Most of the answers would be concurrent programming improves efficiency, makes your applications run faster blah blah blah. But still no one has answered my question(which is really simple) If you don't have the rights to control something then why use it? Now coming to the next doubt/question which is as stated by me "In Java you could start a thread saying .Start() or combine threads to run but applying the same parallel processing in c++ there are no inbuilt functions saying .start() instead you just use an API called openmp where you just specify #pragma parallel num_thread(n). Why use such API even? You dont even have an idea when the threading starts and stuff like that. Damn!! I am starting to realise one thing. "Never learn parallel programming" else such stupid doubts (which none can answer) arise. 
P.S: Non techies. Sorry!! I got too carried away with the post that I forgot to explain few alien terms. 

3 comments:

  1. excellent introduction with neat and simple explanation for threads and process.
    good work.
    To answer your doubt
    1.) "Why to use thread if we cant control" ??
    Ans:You can control thread and there are few terms namely
    a.) Barrier:Consider it like a meeting point where all threads come together and say hello hi..wait for other threads to come....and once all threads come they again continue their respective work.
    b.)CountdownLatch : using this you can control threads in terms of when they have to stop or start.
    both the above mentioned terms are used for thread synchronization and used when you want a group of threads to start or stop at the same time.

    c.) Using graceful termination technique you can stop a thread at any point.
    d.) using thread.sleep() you can force a thread to wait for a given amount of time.

    and many more...we do have control over threads but since they are notorious its very very very hard to gain control over them since it takes lot of time to figure out where to apply those logic.


    2.)openMp is based on the concept of fork and join...its a simple process where in you divide a task and then regroup or collate the results after each thread has done their respective tasks.

    ReplyDelete
  2. I didnt know you write! :)

    You don't always want to control it.For example, if you need some calculation to be done; you provide the input and get a result, that satisfies your need. And say youu need to display the result in a particular UI then while the result is being calculated, you can load the UI parallely.
    Just a basic example!

    ReplyDelete
  3. True! but in extreme cases where you want to compare your parallel and sequetial program run times it becomes really difficult. There are situations where the parallel code is slower than sequnetial one- Reason: threads assigned are not intelligent at all.

    ReplyDelete