Multi-thread is slower than single thread

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 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

Multi-thread is slower than single thread

Postby yongjun.li » Sun Jul 19, 2015 8:13 pm

Hi everyone,

I am new to OpenMP. The attached is my first OpenMP code. It is little bit better when I run it with two thread. But is was slower than single thread when I run with 4 threads.
I compile the code with

g++ -g -static -p -fopenmp myfirst_OpenMP.cpp -lm -o myfirst_OpenMP

and run it with

echo "Run with 1 thread."
export OMP_NUM_THREADS=1
./myfirst_OpenMP
#
echo "Run with 2 threads."
export OMP_NUM_THREADS=2
./myfirst_OpenMP
#
echo "Run with 4 threads."
export OMP_NUM_THREADS=4
./myfirst_OpenMP

Could anyone have a look and help me find what is wrong with it?

Many thanks,

Yongjun


Code: Select all
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <iostream>
# include <omp.h>


using namespace std;


void function(string * geno,double *x,int n);
void doubleVector(double* m, int n);

int main(int argc,char* argv[]) {
    printf ( "  Number of processors available = %d\n", omp_get_num_procs ( ) );
    printf ( "  Number of threads =              %d\n", omp_get_max_threads ( ) );

    double *f;
    int nmks=50000;
    f=new double [nmks];
    for (int i=0; i<nmks; i++) {
        f[i] = (double)rand() / (double)RAND_MAX ;
    }

    for (int i=0; i<1;i++){
        string * genome;
        genome = new string [nmks];
        function(genome,f,nmks);
    }
    delete [] f;
}  //end of main program

void doubleVector(double* m, int n){
    double rate;
    double time_begin;
    double time_elapsed;
    double time_stop;
    time_begin = omp_get_wtime();
    int i;
    srand(int(time(NULL)));

    for (i = 0; i < n; ++i)
        m[i] = (double)rand() / (double)RAND_MAX ;


}

void function(string * geno,double *x,int n) {

    double rate;
    double time_begin;
    double time_elapsed;
    double time_stop;
    int i;
    double **freq;
    freq = new double * [n];
    for(i=0; i<n; i++) {
        freq[i] = new double [4];
    }


    double *rn ;
    rn = new double [n];
    doubleVector(rn,n);
    /*for(i=0; i<n; i++) {
        rn[i] = (double)rand() / (double)RAND_MAX ;

    }  */
     for(i=0; i<n; i++) {
        freq[i][0] = x[i] * x[i];
        freq[i][1] = freq[i][0] + x[i] * (1.0-x[i]);
        freq[i][2] = freq[i][1] + (1.0-x[i]) * x[i];
        freq[i][3] = freq[i][2] + (1.0-x[i]) * (1.0-x[i]) ;
    }


    time_begin = omp_get_wtime();
    # pragma omp parallel shared (geno,rn,n,freq) private (i)

    # pragma omp for
    for(i=0; i<n; i++) {
        if (rn[i] < freq[i][0])      {
            geno[i] = "AA";
        } else if (rn[i] < freq[i][1])  {
            geno[i] = "AB";
        } else if (rn[i] < freq[i][2]){
            geno[i] = "BA";
        } else{
            geno[i] = "BB";
        }
    }

    time_stop = omp_get_wtime();
    time_elapsed = time_stop - time_begin;
    printf ( "  Elapsed time = %f\n", time_elapsed );

    delete [] rn;
    for (int i=0; i<n; i++)
      delete [] freq[i];
    delete [] freq;
}
yongjun.li
 
Posts: 1
Joined: Wed Jul 08, 2015 5:03 pm

Return to Using OpenMP - The Book and Examples

Who is online

Users browsing this forum: No registered users and 1 guest