Loading...
You are not logged-in Login/Register





  • Posts   Search Threads
  • inttelJuly 7, 2009 2:59 PM PDT   
    openmp slower than single threaded

    a program for the sole purpose of trying to demonstrate the advantage of using 4 cores simultaneously is below.

    however, it runs for 90 seconds on a 4 core xeon (3ghz) versus 2 seconds on a single core machine.

    any hints greatly appreciated.

    Tom




    #include <stdio.h>
    #include <stdlib.h>
    #include <omp.h>
    #include <time.h>

    #define N 1000
    #define CHUNKSIZE 25

    main () {
    time_t sec1;
    time_t sec2;
    sec1 = time(NULL);
    printf("start n");

    int i, chunk;
    float a[N];
    float b[N];
    float c[N];
    int j;
    float k;


    for (i=0; i < N; i++)
    a[i] = b[i] = i * 1.0;
    chunk = CHUNKSIZE;

    #pragma omp parallel for private(i,j,k) schedule(static,chunk)
    for (i=0; i < N; i++) {
    for (j = 0; j<200000; j++) {
    k = rand();
    }
    // c[i] = a[i] + b[i];
    }


    sec2 = time(NULL) - sec1;
    printf("%ld seconds", sec2);
    return 0;

    }



    compiled using 'gcc -O3 -fopenmp workshare2.c -o workshare2' on gcc 4.3.2 on opensuse64 11.1



    Robert Reed (Intel)July 7, 2009 4:03 PM PDT
    Rate
     
    Re: openmp slower than single threaded

    Quoting - inttel
    a program for the sole purpose of trying to demonstrate the advantage of using 4 cores simultaneously is below.
    however, it runs for 90 seconds on a 4 core xeon (3ghz) versus 2 seconds on a single core machine.
    any hints greatly appreciated.

    [code section excised for sanity]

    compiled using 'gcc -O3 -fopenmp workshare2.c -o workshare2' on gcc 4.3.2 on opensuse64 11.1

    The core of your problem is probably here:

     

    #pragma omp parallel for private(i,j,k) schedule (static,chunk) 
       for (i=0; i < N; i++) {
          for (j = 0; j<200000; j++) {
             k = rand(); 
          } 
          // c[i] = a[i] + b[i]; 
       }

    Though rand() not required to be reentrant and therefore not required to be thread safe (see http://www.opengroup.org/onlinepubs/000095399/functions/rand.html), the fact is that some implementations provide thread safety by putting a lock in the function, which probably means that all those parallel invocations of rand() from the various threads are being serialized.  That could go a long way to explaining the slowdown you report.

    For future reference, you might consider timing just the code you're testing for parallel performance, rather than including the serial initialization section as part of the timed section as is done in this example.



    Dmitriy VyukovJuly 9, 2009 6:49 AM PDT
    Rate
     
    Re: openmp slower than single threaded

    the fact is that some implementations provide thread safety by putting a lock in the function


    Sane implementations (Microsoft Visual C++) provide thread-safety by placing all the data to thread-local storage (TLS). This is a bit sub-optimal, but provides perfect scaling.
    You may consider using well-designed self-contained random generator (like the one in boost), so that you will be able to create generator per thread on the stack.





    ---------------------------------------------
    All about lock-free algorithms, multicore, scalability, parallel computing and related topics:
    www.1024cores.net

    Dmitriy VyukovJuly 9, 2009 6:53 AM PDT
    Rate
     
    Re: openmp slower than single threaded

    There is another possible problem. 25 rand() calls per task can be too small to outweigh parallelization overheads. Work per task must be some 10'000 machine cycles with current tools.



    ---------------------------------------------
    All about lock-free algorithms, multicore, scalability, parallel computing and related topics:
    www.1024cores.net

    Andrey KarpovJuly 10, 2009 4:13 AM PDT
    Rate
     
    Re: openmp slower than single threaded

    Additional about rand():
    Use of rand() in OpenMP parallel sections



    TudorJuly 14, 2009 8:38 AM PDT
    Rate
     
    Re: openmp slower than single threaded

    Ok, I'm a noob at openmp but maybe specifying chunksize = 25 creates too many threads that choke your 4 cores. Try to only create a maximum of 2 * number of cores threads.


    TimP (Intel)July 14, 2009 9:07 AM PDT
    Rate
     
    Re: openmp slower than single threaded

    Quoting - Tudor Serban
    Ok, I'm a noob at openmp but maybe specifying chunksize = 25 creates too many threads that choke your 4 cores. Try to only create a maximum of 2 * number of cores threads.
    No, setting chunk size doesn't affect the number of threads.  However, you touch on a good point.  Normally, with balanced work among chunks, the largest possible chunk size will be superior, at least when using static scheduling with affinity set.
    In this case, it's not at all clear what the original poster was getting at.  It's certainly not a normal usage of openmp.  Maybe he wanted to see whether OpenMP inhibits the compiler from eliminating redundant loops.


    jimdempseyatthecoveJuly 14, 2009 12:12 PM PDT
    Rate
     
    Re: openmp slower than single threaded


    Normally, with balanced work among chunks, the largest possible chunk size will be superior

    Only when all cores/HW threads are dedicated to running your app. When anthing else is running on the system then smaller chunk size may be superior. Similar situation with nested levels and/or when using NOWAIT.

    Jim Dempsey


    Blog: The Parallel Void
    www.quickthreadprogramming.com

Forum jump:  

Intel Software Network Forums Statistics

16,369 users have contributed to 46,341 threads and 163,954 posts to date.

In the past 24 hours, we have 18 new thread(s) 102 new posts(s), and 67 new user(s).

In the past 3 days, the most popular thread for everyone has been Formula for the intersection of straight lines The most posts were made to Take a look at John Burkhard&# The post with the most views is \"-check none\" generates error

Please welcome our newest member bikerepair8


For more complete information about compiler optimizations, see our Optimization Notice.