Use this forum to discuss the book: Using OpenMP - Portable Shared Memory Parallel Programming, by Barbara Chapman, Gabriele Jost and Ruud van der Pas Read the http://openmp.org/forum/viewtopic.php?f=8&t=465 for book info and to download the examples. Post your feedback about the book and examples to this forum
Post Reply


Post by anisurr »

i am working on a core 2 duo machine and actually trying to find speedup of some program using openmp. can anybody suggest a sample code. Am using RedHat Linuc and gcc complier.

Posts: 32
Joined: Wed Dec 12, 2007 9:36 am

Re: speedup.

Post by anv »

Hi ANis!

Here is simple example:

Code: Select all

#include <stdlib.h>
#include <stdio.h>
#include <omp.h>
#define N 100000000
int main()
    int i, *a;
    long long sum = 0;
    double t;
    a = (int*)malloc(N*sizeof(int));
    t = omp_get_wtime();
    #pragma omp parallel for
    for( i=0; i<N; ++i)
    { a[i] = i; }
    #pragma omp parallel for reduction(+:sum)
    for( i=0; i<N; ++i)
    { sum += a[i]; }
    printf( "sum =%lld, t = %f\n", sum, omp_get_wtime()-t );
    return 0;
and usage:

Code: Select all

$ gcc -fopenmp 1.c

$ OMP_NUM_THREADS=1 ./a.out
sum =4999999950000000, t = 0.982977

$ OMP_NUM_THREADS=2 ./a.out
sum =4999999950000000, t = 0.696786

$ OMP_NUM_THREADS=4 ./a.out
sum =4999999950000000, t = 0.280423
Of cause you may get no speedup on dual core machine from more than 2 threads (I used quad core), but 2 threads should run faster than 1.



Re: speedup.

Post by ruud »

Hi ANis,

You could use one of the examples from the "Using OpenMP" book for this. Especially the matrix times vector program should be very suitable for your purpose. You can download all examples from http://openmp.org/forum/viewforum.php?f=8

It would be best to insert calls to omp_get_wtime() around the call to "mxv" to time the performance of this algorithm. Something like this:

t_start = omp_get_wtime();
(void) mxv(m, n, a, b, c);
t_end = omp_get_wtime() - t_start;

printf("Execution time is %f\n",t_end);

You need to have a sufficiently large matrix to get reliable timings. The size depends on your system and how you compile, so you may need to try out a couple of sizes. Usually a 1000x1000 matrix or bigger should be okay though.

Kind regards, Ruud

Post Reply