Forum Jump

Select Group :
Select Forum :
Sorted By :
Sort Order :
From The :
 
Thread Tools  Search this thread 
inttel
Total Points:
10
Registered User
July 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)
Total Points:
8,851
Status Points:
8,351
Brown Belt
July 7, 2009 4:03 PM PDT
Rate
 
#1
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 Vyukov
Total Points:
24,727
Status Points:
24,727
Black Belt
July 9, 2009 6:49 AM PDT
Rate
 
#2 Reply to #1

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.





Dmitriy Vyukov
Total Points:
24,727
Status Points:
24,727
Black Belt
July 9, 2009 6:53 AM PDT
Rate
 
#3
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.



Andrey Karpov
Total Points:
4,323
Status Points:
3,823
Brown Belt
July 10, 2009 4:13 AM PDT
Rate
 
#4 Reply to #1
Tudor
Total Points:
925
Status Points:
425
Brown Belt
July 14, 2009 8:38 AM PDT
Rate
 
#5
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.

--------
'Everything is proceeding as I have foreseen.'


tim18
Total Points:
66,397
Status Points:
66,397
Black Belt
July 14, 2009 9:07 AM PDT
Rate
 
#6 Reply to #5
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.


jimdempseyatthecove
Total Points:
34,847
Status Points:
34,847
Black Belt
July 14, 2009 12:12 PM PDT
Rate
 
#7 Reply to #6

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




Intel Software Network Forums Statistics

8286 users have contributed to 31232 threads and 99107 posts to date.
In the past 24 hours, we have 7 new thread(s) 27 new posts(s), and 36 new user(s).

In the past 3 days, the most popular thread for everyone has been comparison cilk++, openmp, pthreads first results The most posts were made to comparison cilk++, openmp, pthreads first results The post with the most views is Very amusing...  Escalated as

Please welcome our newest member titanius.anglesmith