extreme slowdown on simple code

General OpenMP discussion

extreme slowdown on simple code

Postby dhysom » Thu Jan 05, 2012 1:17 pm

All,

Below is a simple code that is giving a huge slowdown
when I use more than a single thread. Can someone
spot what I might have done wrong? I compile as
g++ -O2 -fopenmp -Wall test.cpp
and am running on a AMD 8-Core 6128 platform,
32 cores total per node.

timing statistics:
setenv OMP_NUM_THREADS 1
time a.out
nthreads: 1
1.478u 0.006s 0:01.49 98.6%

setenv OMP_NUM_THREADS 2
nthreads: 2
10.456u 10.526s 0:12.82 163.5%


#include <iostream>
#include <omp.h>

using namespace std;

int COUNT = 100000000;

int main(int argc, char *argv[]) {
int *v = new int[COUNT];
double total = 0;

#pragma omp parallel shared(v)
{
int my_count = 0;
int nthreads = omp_get_num_threads();
int tid = omp_get_thread_num();
if (tid == 0) { cout << "nthreads: " << nthreads << endl; }
double ct = 0;
#pragma omp for
for (int j=0; j<COUNT; j+= 20) {
for (int i=0; i<20; i++) {
++my_count;
ct += v[j]*rand()/1000000;
}
}
// #pragma omp critical
// total += ct;
if (tid == 0) cout << "COUNT: " << COUNT << " my count: " << my_count << endl;
}

cout << "total: " << total << endl;
}
dhysom
 
Posts: 1
Joined: Tue Jan 03, 2012 1:20 pm

Re: extreme slowdown on simple code

Postby shiv4k » Fri Jan 13, 2012 7:31 am

Is time command seizing cpu time or real time?
shiv4k
 
Posts: 21
Joined: Fri Apr 17, 2009 10:40 am

Re: extreme slowdown on simple code

Postby MarkB » Fri Feb 17, 2012 5:12 am

I expect the problem is that the rand() function is not thread safe: all the threads are likely trying to update some internal state in the random number generator.
Not only will you see terrible performance, but the random numbers may be garbage too....
MarkB
 
Posts: 480
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 7 guests