document.write('
PROGRAM NAME
search.c
VERSION
1.0.0.0
DESCRIPTION
This program attempts the parallelize the linear search algorithm. It implements an array, that is shared by all the nodes. The server calculates the range and sends the range to the client nodes. The client nodes start searching within that range, and the node that gets the result returns it back to the server.

CHANGES INCORPORATED SINCE LAST VERSION
(none)


#include<stdlib.h>
#include<stdio.h>
#include<mpi.h>

#define MAX 2000000
long int a[MAX];              //Array in which elements would be randomly filled up.

int main(int argc,char *argv[])
{
    int j=0,k,rank,nproc;
    long int i,div,x;         //div: count of no. of divisions to be made in the array.
    long start,stop;          //start, stop: the place where a node
                              //should start and stop searching.
                       
    float starttime,stoptime;
    MPI_Status s;
   
    MPI_Init(&argc,&argv);                   //Initialize MPI environment.
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD,&nproc);
   
    printf("\n");
    for(i=0;i<MAX;i++)
    {
        a[i]=rand()%MAX;      //Populate the array.           
    }
    if(rank==0)              //Server-side code
    {
        div=MAX/(nproc-1);    //calcuate no. of divisions to be made.     
        printf("\nDivisions: %ld \n\n",div);
        printf("\n=>> %d \n\n",a[MAX-1]);
        printf("\nEnter the search element: ");
        scanf("%ld",&x);   

      /* Here, the server sends the divisions to the client, i.e the
         range in the array the each node should search.         
      */
        for(j=1;j<=nproc-1;j++)    
        {
            start=div*(j-1);
            stop=start+div-1;
            MPI_Send(&start,sizeof(long),MPI_LONG,j,j,MPI_COMM_WORLD);
            MPI_Send(&stop,sizeof(long),MPI_LONG,j,j,MPI_COMM_WORLD);
            MPI_Send(&x,sizeof(long),MPI_LONG,j,j,MPI_COMM_WORLD);           
        }

    }
    else    //Client-side
    {
       
        /*
            The array is shared by all the nodes.
            Each node recieves the the range from the server.
            Once the range is recieved, the client starts the searching
        */
       
        MPI_Recv(&start,sizeof(long),MPI_LONG,0,MPI_ANY_TAG,MPI_COMM_WORLD,&s);
        MPI_Recv(&stop,sizeof(long),MPI_LONG,0,MPI_ANY_TAG,MPI_COMM_WORLD,&s);   
        MPI_Recv(&x,sizeof(long),MPI_LONG,0,MPI_ANY_TAG,MPI_COMM_WORLD,&s);   

        printf("\nNODE %d: Start: %ld",rank,start);
        printf("\nNODE %d: Stop: %ld",rank,stop);
           
        starttime=MPI_Wtime();
        j=0;
        for(i=start;i<stop;i++)
        {
            if(x==a[i])
            {
                printf("\nElement FOUND at node: %d",rank);
                stoptime=MPI_Wtime();
                j=1;
            }

        }
        if(j==0)
        {
            stoptime=MPI_Wtime();
            printf("\nElement NOT FOUND at node: %d",rank);
        }
        printf("\nTotal Time required for node %d: %f",rank,(stoptime-starttime));
    }
    MPI_Finalize();
    printf("\n");
    return(0);
}


');