Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Parallel Processing ProgramsRSS Feeds

PROGRAM TO EVALUATE CORRELATION OF TWO SETS OF DATA USING BARRIER

Posted By: Abel Fischer     Category: C Programming     Views: 2391

PROGRAM TO EVALUATE CORRELATION OF TWO SETS OF DATA USING BARRIER.

Code for PROGRAM TO EVALUATE CORRELATION OF TWO SETS OF DATA USING BARRIER in C Programming

#include<stdio.h>
#include "headfork.h"
#include "headshr.h"
#include "headbar.h"

main()
{
    int x[10],y[10],n,id,i,nproc;
    int sid1,sid2,sid3,sid4,sid5,sid6,sid7,*lock,*bar;
    float *dx,*dy,*meanx,*meany,*dxdy,sumx,sumy,cr;

    bar=(int *)shared(sizeof(int)*2,&sid1);
    dx=(float *)shared(sizeof(float)*2,&sid2);
    dy=(float *)shared(sizeof(float)*2,&sid3);
    meanx=(float *)shared(sizeof(float)*2,&sid4);
    meany=(float *)shared(sizeof(float)*2,&sid5);
    dxdy=(float *)shared(sizeof(float)*2,&sid6);
    lock=(int *)shared(sizeof(int)*2,&sid7);

    printf("Enter the limit : ");
    scanf("%d",&n);

    printf("Enter the elements of X : ");
    for(i=0;i<n;i++)
    {
        printf("\nx[%d] : ",i);
        scanf("%d",&x[i]);
    }

    printf("\nEnter the elements of Y : ");
        for(i=0;i<n;i++)
        {
             printf("\ny[%d] : ",i);
             scanf("%d",&y[i]);
        }

    printf("Enter the no.of proc :");
    scanf("%d",&nproc);

    bar=bar_init(nproc);
    lock_init(lock);
    cr=0.0;
    *meanx=0.0;
    *meany=0.0;
    
    id=p_fork(nproc);
    
    sumx=0.0;
    sumy=0.0;
    
    for(i=id;i<n;i+=nproc)
    {
        sumx+=x[i];
        sumy+=y[i];
    }

    locksem(lock);    
    *meanx+=sumx/(float)n;
    *meany+=sumy/(float)n;
    unlock(lock);
    
    barrier(bar);

    for(i=id;i<n;i+=nproc)
    {
        dx[i]=x[i] - *meanx;
        dy[i]=y[i] - *meany;
    }

    for(i=id;i<n;i+=nproc)
    {
        dxdy[i]=*(dx+i) * *(dy+i);
    }

    p_join(nproc,id);
    
    for(i=0;i<n;i++)
        cr += *(dxdy+i);
    printf("\nThe Correlation is : %f",cr);
        
}


 
OUTPUT

knoppix@ttyp0[pp]$ cc correl_bar.c
knoppix@ttyp0[pp]$ ./a.out
Enter the limit : 5
Enter the elements of X :
x[0] : 11
x[1] : 12
x[2] : 13
x[3] : 14
x[4] : 15

Enter the elements of Y :
y[0] : 1
y[1] : 2
y[2] : 3
y[3] : 4
y[4] : 5
Enter the no.of proc :3

The Correlation is : 10.000001
  
Share: 



Abel Fischer
Abel Fischer author of PROGRAM TO EVALUATE CORRELATION OF TWO SETS OF DATA USING BARRIER is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!