Home > Programming > Parallel OpenMP Search algorithm

Parallel OpenMP Search algorithm


Here is the parallel version of the linear search algorithm using OpenMP

//Author: Chittampally Vasanth Raja
//The following program is parallized version of search algorithm
#include<stdio.h>
#include<omp.h>
#define MAXTHREADS 10
#define ARRAYSIZE 44
int main(void)
{
	int a[]={3,5,1,2,34,67,90,43,53,3,4,26,34,35,54,67,87,21,34,56,33,45,12,34,5,6,7,8,123,45,32,455,666,444,333,222,11,22,44,55,333,222},i,j,found=0,key=222;
	double start_time,run_time;
	for(j=1;j<=5;j++)
        {
	    	omp_set_num_threads(j);
		found=0;
start_time = omp_get_wtime();
		#pragma omp parallel private(i)
		{

			int start,noofsteps;
			#pragma omp single
				printf("num of threads in action: %d\n",j);

			if(found==0)
			{

				start=(omp_get_thread_num())*(ARRAYSIZE/omp_get_num_threads());
				noofsteps=start+(ARRAYSIZE/omp_get_num_threads());
				if(ARRAYSIZE%j!=0)
					noofsteps+=(ARRAYSIZE%j);
				for(i=start;i<noofsteps;i++)				
if(key==a[i]) {
						printf("Key has been found in %d thread at %d position\n",omp_get_thread_num(),i+1);
						found=1;
						break;
					}

			}
		}
		run_time = omp_get_wtime() - start_time;
	  printf("\n %f seconds %d threds \n ",run_time,j);
	}

return 0;
}
Advertisements
  1. hruthikroshan
    October 14, 2014 at 2:44 pm

    could you please provide an explanation for linear search parallelized code ASAP

  2. Chris Jacob a.k.a Sachin
    February 26, 2015 at 6:41 am

    [alard@localhost ~]$ vi a.c
    [alard@localhost ~]$ gcc a.c
    /tmp/cc2kaRA6.o: In function `main’:
    a.c:(.text+0x46): undefined reference to `omp_set_num_threads’
    a.c:(.text+0x52): undefined reference to `omp_get_wtime’
    a.c:(.text+0x88): undefined reference to `omp_get_thread_num’
    a.c:(.text+0x8f): undefined reference to `omp_get_num_threads’
    a.c:(.text+0xa4): undefined reference to `omp_get_num_threads’
    a.c:(.text+0xf9): undefined reference to `omp_get_thread_num’
    a.c:(.text+0x126): undefined reference to `omp_get_wtime’
    collect2: error: ld returned 1 exit status
    [alard@localhost ~]$

  3. February 26, 2015 at 6:57 am

    Hi Jacob,

    while compiling the above program you need to use option -openmp.
    Please check the following link.

    https://www.dartmouth.edu/~rc/classes/intro_openmp/compile_run.html

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: