0

I'm using Ubuntu 16.04 as Windows subsystem and gcc 5.4 version with bash. My windows version is windows 10 home, and ram is 16GB. My cpu is i7-7700HQ. I am studying computer programming at my university. These days, I am interested in parallel programming, so I've coded many codes, but there are some problems.

int i;
int sum = 0;
clock_t S, E;

S = clock();    

#pragma omp parallel for reduction(+:sum)
    for(i = 0 ; i < M ; i++){
        sum++;
    }

E = clock();

printf("\n%d :: time : %lf\n", sum, (double)(E-S)/1000);

return 0;

If I compile this code with commamd, "gcc -openmp test.c -o test" and "time ./test", it shows 100000000 :: time : 203.125000

real 0m0.311s user 0m0.203s sys 0m0.016s.

However,

int i;
int sum = 0;
clock_t S, E;

S = clock();


for(i = 0 ; i < M ; i++){
    sum++;
}

E = clock();

printf("\n%d :: time : %lf\n", sum, (double)(E-S)/1000);

return 0;

if is compile this code with command, "gcc -openmp test2.c -o test2" and "time ./test2", it shows 100000000 :: time : 171.875000

real 0m0.295s user 0m0.172s sys 0m0.016s.

If I compile those codes again and again, it sometimes takes same time, but openmp is never faster.

And I edited those codes with vim.

And I tried to compile with command, "gcc -fopenmp test.c" and "time ./penmp". It takes much more time than command, "gcc -openmp test.c."

And if I compile those same codes with visual studio 2017 community, openmp is much more faster.

Please let me know how I can reduce time with openmp.

  • This is probably a candidate for most frequent failure to check previous explanations. You are measuring by clock() the total of cpu time spent by all threads, so it is quite a success if the time doesn't increase with number of threads, meaning that you are seeing linear scaling. You might hope that "real" time would decrease, if that time were significant to start with. – tim18 Jul 16 '18 at 02:26
  • Doesn't openmp work at same time? I also compiled those code without clock(), and result was similar... I thought if I use openmp, all threads work at same time, so time would be reduced. – Youngsu Chon Jul 16 '18 at 04:29
  • I'll try it, thank you so much! – Youngsu Chon Jul 17 '18 at 02:58

1 Answers1

0

You should use omp_get_wtime() instead to measure the wall-clock:

double  dif;
double start = omp_get_wtime( ); //start the timer
 //beginning of computation
 ..
//end of computation
double end = omp_get_wtime();// end the timer
dif = end - start // stores the difference in dif
printf("the time of dif is %f", dif);
supercheval
  • 357
  • 3
  • 8
  • Oh, I got it. The time, showing with omp_get_wtime(), and Real time are much faster than before, but User time, showing when I command "gcc -fopenmp test.c", doesn't change. I wonder what is different between user time and the time with omp_get_wtime(). – Youngsu Chon Jul 17 '18 at 05:27
  • I did it!! Thank you so much. – Youngsu Chon Jul 17 '18 at 07:10
  • Good to hear that. If an answer fixed your issue, please mark it as accepted by clicking on the checkmark – supercheval Jul 26 '18 at 21:38