Could someone help me: Writing multithreaded C applications

Discussion in 'Mac Programming' started by Spanky Deluxe, Oct 29, 2006.

  1. Spanky Deluxe macrumors 601

    Spanky Deluxe

    Joined:
    Mar 17, 2005
    Location:
    London, UK
    #1
    I'm writing a scientific application to basically crunch some numbers. It basically does the same calculation on a long list of numbers going up in a small step size. I've never written a multithreaded application but I really see this kind of thing benefiting from multiple processors. My idea was that the program would detect how many cores are present and then tell each core to perform the calculation for a different step size. So say Cores 1 through to 4 crunch the numbers 1, 2, 3, 4 and then 5, 6, 7, 8. I'm not looking for anything fancy or deeply optimised, just something that will make use of the extra cores.
    How could I go about doing this, is there a way in C to detect the number of cores and is there anywhere an idiots guide to multiprocessor programming in C anywhere that someone could point me to?

    Cheers,

    Spanky
     
  2. mleary macrumors regular

    Joined:
    Sep 13, 2006
    #2
    I don't know anything about writing multithreaded programs in C, but it is pretty simple in Java if you want to consider that instead.
     
  3. Spanky Deluxe thread starter macrumors 601

    Spanky Deluxe

    Joined:
    Mar 17, 2005
    Location:
    London, UK
    #3
    My course uses mainly C or Matlab and i don't think my partner in the project would appreciate a new programming language to learn!
     
  4. gekko513 macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #4
    For basic thread handling, posix threads (pthreads) may be what you're looking for. A pthreads tutorial I found by doing a quick search

    C doesn't have anything built in to work with threads or to detect anything about its environment, so you're going to have to use some kind of library or framework whether it's general purpose or platform dependant.

    This is on Mac OS X, right? This link has some discussion on programming on Mac OS X that also includes a small section on threads.

    If you were using C++, many would probably recommend Boost
     
  5. weg macrumors 6502a

    weg

    Joined:
    Mar 29, 2004
    Location:
    nj
    #5
    If you have a compatible compiler (e.g., Intel's C compiler or Microsoft VC), then you should consider OpenMP instead of pthreads or some other thread library. (Well, technically you can combine both approaches, OpenMP offers you a finer granularity, e.g. loop parallelization).
     
  6. rtharper macrumors regular

    rtharper

    Joined:
    Sep 6, 2006
    Location:
    Oxford, UK
    #6
    In C, the issue is that threads are platform specific. For POSIX threads, the pthreads library works fine. There are also other thread libraries for Win32, etc. There are two basic ways to let threads communicate: shared storage and message passing. There are advantages and disadvantages for both. I've done work in Scientific Computation before, what kind of problem in particular are you looking to process?
     
  7. mrichmon macrumors 6502a

    Joined:
    Jun 17, 2003
    #7
    If you are not looking for anything fancy and you can cleanly partition your data sets across multiple cores then you can simply use the fork() system call.

    In terms of determining the number of cores available then one easy way is to get the information by invoking system_profiler and pulling the information you need from the output.

    These are not the prettiest of suggestions but they will achieve what you are asking about with the minimum of fuss assuming an easily partitioned problem and data set.
     
  8. Spanky Deluxe thread starter macrumors 601

    Spanky Deluxe

    Joined:
    Mar 17, 2005
    Location:
    London, UK
    #8
    Hmmm... that sounds good, I'll look into the fork command. I had a brief look into pthreads but it looked a bit complicated. I might go back to that later but I really am trying to keep things simple. Like I said above, the program is just number crunching and each iteration is independent of the last apart from the input number being slightly changed. The time per iteration is also constant. This is why all I need is something to tell the computer to do one iteration per core at the same time and I should hopefully get a nearly 4x speed increase for four cores instead of one.
     
  9. xynopsis macrumors newbie

    Joined:
    Oct 29, 2006
    #9
    Hi,

    I understand there is quite some confusion here on deciding which route to use when doing multi-threaded programming.

    Here are some points that are worth remembering:

    (1) POSIX threads or pthreads is the standard multithreading API on UNIX and UNIX-like systems. Since OSX is UNIX, you can take advantage of pthreads.

    (2) pthreads automatically takes advantage of the multiple cores in a CPU. The operating system hides the details on how many cores there is or which core is activated on a specific task. It is all handled automatically behind the scene. If there is > 1 cores, the tasks are distributed to the rest of the cores.

    (3) All a programmer has to do is find a way to split the tasks somehow. Pthreads automatically schedules each task to an available core if there is one and execute the task concurrently.

    Pthreads programming in C looks something like this roughly:

    #include <pthread.h>

    void* task(void* p)
    {
    int* task1 = (int*) p;
    do_something(task1);
    }

    main()
    {
    pthread_t thread1, thread2;

    int task1[] = { 1,2,3,4,5 };
    int task2[] = { 6,7,8,9,10 };

    /* Create independent threads each of which will execute function */

    pthread_create( &thread1, NULL, task, (void*) task1);
    pthread_create( &thread2, NULL, task, (void*) task2);

    exit(0);
    }

    (4) Finally fork() is very slow compared to native threads and it doesn't take advantage of multiple cores in your systen. Don't use it!
     
  10. Spanky Deluxe thread starter macrumors 601

    Spanky Deluxe

    Joined:
    Mar 17, 2005
    Location:
    London, UK
    #10
    Ok cool, I'll have a play with the phtread route once I get the basic program done. Although, if I use pthreads, does that mean I will lose all cross platform compatability? Ideally I'd like to be able to compile in both Windows and OS X.
     
  11. rtharper macrumors regular

    rtharper

    Joined:
    Sep 6, 2006
    Location:
    Oxford, UK
    #11
    You got it. pthreads should be supported by OS X, but not windows. If you really want cross platform, multi threaded apps you have to use Java. Unfortunately, Java isn't the most high performance.

    Either way, I wouldn't recommend using fork() for your work. fork() has the overhead of new processes, which can't use message passing or shared memory and generally have cumbersome communication.

    Once again I would ask what kind of number crunching are you looking to do? Most SMP apps doing intense calculation are written to be optimized for a specific platform to get the most out of it. Depending on the size of the problem Java can still be useful.
     
  12. Spanky Deluxe thread starter macrumors 601

    Spanky Deluxe

    Joined:
    Mar 17, 2005
    Location:
    London, UK
    #12
    Its just a routine to fit some given formulae to various sources of data - computed data and experimental/observed data. Basically it'll try a range of numbers by taking small incremental steps, look to see where the best fit was and then try a more fine tuned range of numbers over the area where the best fit was before and then repeat until the best fit is found. Kind of like homing in on the answer. While not particularly complicated in theory, its the kind of thing that the longer it runs the greater the accuracy so I'd like to make use of all the power in this Mac Pro!
    Whilst my supervisor uses a Mac, the rest of the deparment is Windows based although since its only her and another guy marking it I could easily put some kind of multi core optimisation in if its easy to remove.
    Since my plan would be to create four threads (or however many cores there are) to evaluate a quarter of the steps each it will basically be a matter of the program running the same calculations concurrently on four cores allocating a quarter of the range to be calculated per core. This should be able to be done at quite an early stage in the program and so should be easy to take out afterwards.
    While I don't *have* to optimise my code in such a way, marks wise etc, it could well give me better marks for efficient code and more importantly would be great experience for future more complex programs where power is more important.
     
  13. rtharper macrumors regular

    rtharper

    Joined:
    Sep 6, 2006
    Location:
    Oxford, UK
    #13
    If you don't go much beyond primitive math operations and don't use complex data structures, Java should work just fine for what you want to do. Creating threads and using shared storage or message passing is both fairly easy once you do some work. I've done primality testing, simulated annealing, and Gaussian elimination in java in parallel using threads and using "channels" to send objects between them. It is not the MOST efficient way to do things, but it is still fast for a multicore system... AND it will run on any platform and it is easy to specify the number of threads you want to break it up into.

    If you're really interested I can email you some source code or we can discuss the problem.

    Just shoot me an email at rtharper@syr.edu or IM me at tyresyas on AIM
     
  14. enigma51 macrumors newbie

    Joined:
    Nov 1, 2006
    #14
    Hi Spanky

    Remeber efficient code is not always code that just run fast or use all of the available resource it is very important that you have a non complex easy to read and maintain piece of code.

    There is multiple ways of doing a hello world in C but trust me printf("Hallo World"); does the trick.

    To get back to your question. I use a form of multi threading in C on a daily basis but it is always dictated by how much need there is for it. The biggest problem with Multi threads is that it has huge overheads and fork is no exception to this plus if you miss anything and don't close the thread you have a major resource leak. One of the things I like about c and c++ is that it does not have a big stick that beats you over the head when you have forgotten about something. I treat multi threads the same way I treat arrays and double pointers.

    I do suggest you do a google search on multi threating in c or even try http://www.cprogramming.com/ for some advice seeing you need to code it universal between wind blows and mac!
     
  15. weg macrumors 6502a

    weg

    Joined:
    Mar 29, 2004
    Location:
    nj
    #15
    OpenMP, which supports C as well as C++, is fully portable between Windows, Mac OS X and Linux.
     
  16. rtharper macrumors regular

    rtharper

    Joined:
    Sep 6, 2006
    Location:
    Oxford, UK
    #16
    That's certainly good to know, thanks! =)
     

Share This Page