Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Dual Core Programming Source Code

Options
  • 07-09-2006 3:44pm
    #1
    Closed Accounts Posts: 1,567 ✭✭✭


    anyone here experience programming for dual core processors got any links to source code examples or helful documentation on it?
    thanks.


Comments

  • Closed Accounts Posts: 4 Mr_lebowski


    As far as i know, if you multi-thread your app. the OS should automatically use the dual core effectively!


  • Registered Users Posts: 6,180 ✭✭✭Talisman


    As Mr_lebowski said you need to multi-thread the application and the OS will take care of it.

    Taking Advantage of Concurrent Programming for Windows

    Intel Guide to Developing Multithreaded Applications


  • Closed Accounts Posts: 1,567 ✭✭✭Martyr


    this is what i thought, but wasn't sure how much of a difference it would make, i guess you would need some mutex object to synchronise events & maybe this would slow everything down..

    thanks!!


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    You're right, mutexs are (relatively) slow. But unless you completely abuse Mutexes and spawn new threads all over the place, the performance gain from using more than 1 thread in your app will *far* outweigh the loss from having to synchronise.

    A nice rule of thumb is to not program for single core, or dual core, but program for N cores. You split the work in your application into as many threads as makes sense to have. If your application logically splits into 4 threads, then code it for 4 threads. That way you'll get an automatic doubling of speed when quad cores come out.

    Whatever language you code in, read up on multithreading and see what the recommended thing to do.


  • Registered Users Posts: 4,188 ✭✭✭pH


    The advice has always been to thread your application in whatever way makes sense, ignoring the number of actual cores. In practice, this has meant writing single threaded code for the majority of programmers - there wasn't any real incentive to write multi-threaded code.

    Multi-threaded code is hard. I do a lot of Java Swing work, and the I regularly see very badly written attempts at threading. It almost needs a different mindset, it's very easy for inexperienced coders to miss huge 'gotchas' all over the place.

    Mistakes caused by not understanding threading, or implementing it incorrectly can lead to extremely difficult to find bugs. For most practical purposes (i.e. commercial business software), unless the coder is a damned genius I recommend not getting involved in threading code (to try an get performance gains).


  • Advertisement
  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Well, all GUI's should be multithreaded. So if you're writing a GUI, you have no choice really. (unless your application NEVER does anything that takes more than a few dozen miliseconds to complete).

    I wouldn't recommend never multithreading applications for performance, but i would recommend that you don't just jump in with "Thread a = new Thread; thread b = new Thread..... Thread z = new Thread" and blast away with threads. That'll never work :p

    The only way to ever get good at multithreading is to just fire ahead and multithread an application. At the start of the summer i had never written a serious multithreaded app. Then i worked on a bittorrent library (think 100 simultaneous open connections, some tasks which can take a few minutes to finish, and messages flying to and from peers all over the place). It's in those circumstances where you *have* to get good at multithreading. Ok, so after 3 months i realised i fecked things up a bit and have to rewrite a huge chunk of the multithreading. But thats all part of the learning experience :p


  • Registered Users Posts: 4,188 ✭✭✭pH


    Ok, so after 3 months i realised i fecked things up a bit and have to rewrite a huge chunk of the multithreading. But thats all part of the learning experience
    Which is fair enough, it's great to learn and get better.

    I still stand by the basic point that if someone I consider an average/good coder comes to me to talk about performance increases by threading his/her code, unless I know they have good threading experience my advice would be to stay away, and look for other ways to improve performance.

    Let's take a VERY simple example :

    You have a big list of 'things' (let's say they're all in memory). These things could be customers, bank accounts or planets in a game it doesn't matter. Let's say there's 500,000 of them.

    There's a calculation you can perform on the object, it's not dependent on any other objects, and it doesn't change/update any object. It can be performed solely on the object in memory, but it is computationally expensive.
    For example it could be calculating the customer's 'value' or the economic output of the planet for the next 10 years.

    You need to find the max value by iterating through all objects, performing the calculation and storing the max found.

    What's the N-core solution for optimizing this?


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Create a ThreadPool (or just use your languages built in one if there is one). Then just keep queuing the calculations in your threadpool where they will get computed as soon as a free thread is available. At the end of each calculation you will synchronise on an object and then update your "Highest value found" if your new value is higher.

    Psuedo code for C#:
    private void DoSomethingForEachItem()
    {
        while(ThreadPool.GetAvailableThreads > 0 && ItemsLeftToCalculate > 0)
        {
                System.Threading.ThreadPool.QueueUserWorkItem(CalculateValue(NextItem));
        }
    }
    
    private void CalculateValue(MyItem item)
    {
          int result = item.PerformMyComplexCalculation();
          lock(highest_value_found)
          {
               if(highest_value_found < result)
                     result = highest_value_found;
          }
    }
    

    That'll scale well for any number of cores.


    EDIT: A basic threadpool can easily be created in any language with good threading support. It wouldn't be too difficult.


Advertisement