Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Fine-Grained and Coarse-Grained actions - Help me understand

  1. #1
    Join Date
    Nov 2009
    Thanked 0 Times in 0 Posts

    Default Fine-Grained and Coarse-Grained actions - Help me understand


    I have a question around fine-grained and coarse-grained atomic actions.

    Could someone please give me a very basic description on what each one is.

    As far as I understand, coarse-grained is built up of multiple fine-grained actions in that it being something like a synchronized method.

    But for fine-grained I am unsure on exactly what this means, i know it is an instruction which has either happened or hasn't in that it have either processed everything or nothing at all. An as far as my research goes it has led me to believe that the following is an example of a fine-grained atomic action:

    int x = 0;

    where as:

    int x = y+3;

    isnt due to the fact it must read and process data before doing a direct write to a memory location.

    Am i on the right track?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Eastern Florida
    Thanked 2,274 Times in 2,245 Posts

    Default Re: Fine-Grained and Coarse-Grained actions - Help me understand

    Never heard those terms used to describe programming. Where did you get them from?

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: Fine-Grained and Coarse-Grained actions - Help me understand

    I've heard these terms used in parallel programming. In that context, the granularity refers to the amount you're able to split the original job up into.

    For example, say you want to add up two vectors each with 1 million numbers. The coarsest possible grain size is to have everything in 1 job.

    // coursest grain size
    for(int i = 0; i < 1000000; ++i)
        result[i] = A[i] + B[i];

    The finest grain size would be to have each grain (or task) add up one element.
    // finest grain size
    int thread_id; // get a thread ID somehow
    result[thread_id] = A[thread_id] + B[thread_id];

    And it's possible to have various grain sizes between these (for example adding up 10 elements/task, 100 elements/task, etc. etc.).

    CPU's are designed to have "heavy weight" threads, i.e. they are capable of doing a lot of things at the cost of performance (speed). In the above example code, it would be silly to start a thread and have it add up two numbers then save the result. The amount of time it takes to start the thread and mange it exceeds the time it takes to add up a single element. The overhead is simply too great. However, if you end up with a trillion elements per job, the overhead becomes extremely small. If there are any idle processing units, then your program isn't utilizing the full computational potential of your computer and will run slower. The trick is to balance the right granularity size so that the overhead isn't unbearable, but also ensure that you are able to utilize as much of your computers resources effectively (there is also another issue of load balancing, but that's a slightly different issue).

    To contrast this, in the context of the GPU, it has lots and lots of very light-weight threads. These threads aren't able to do as much as a CPU thread, but their primary advantage is that it's very easy to create lots of them very quickly. It's not unheard of to have a GPGPU kernel create and run thousands or tens of thousands of threads. Additionally, each core on the GPU is slower than that of the CPU. This lends GPU programming to benefit the most from having lots of very fine grain tasks (there are other reasons for this, too but I won't go into them here). On the GPU, adding just one element up or a handful of elements per grain/task is quite common.
    Last edited by helloworld922; January 18th, 2012 at 09:36 PM.

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    mds1256 (January 26th, 2012)

  5. #4
    Join Date
    Nov 2009
    Thanked 0 Times in 0 Posts

    Default Re: Fine-Grained and Coarse-Grained actions - Help me understand

    a good explanation there ^^^^^^. Thank you very much

Similar Threads

  1. Help in putting actions to buttons
    By juliusmasa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 14th, 2011, 06:22 AM
  2. [SOLVED] need help with struts actions
    By arvindbis in forum Web Frameworks
    Replies: 0
    Last Post: October 4th, 2011, 05:40 AM
  3. [SOLVED] I am unable to read radio button actions in JSP
    By abhiM in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 17th, 2011, 01:41 AM
  4. Java - Button Actions on Form --Please help
    By Cami7 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 24th, 2011, 12:31 PM
  5. do actions in ComboBox
    By java_cs in forum AWT / Java Swing
    Replies: 2
    Last Post: October 1st, 2009, 10:53 AM