Logo 
Search:

C Programming Articles

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

Program to calculate average of the elements of an array and then the average deviation using barrier

Posted By: Rainhard Fischer     Category: C Programming     Views: 2485

Program to calculate average of the elements of an array and then the average deviation using barrier.

Code for Program to calculate average of the elements of an array and then the average deviation using barrier in C Programming

# include <stdio.h>
# include <math.h>
# include "/usr/include/sys/types.h"
# include "/usr/include/sys/shm.h"
# include "/usr/include/sys/ipc.h"
# include "/usr/include/sys/sem.h"
# include "forkjoin.h"
# include "sharedlib.h"
# include "spinlock.h"
# include "barrier.h"int main()
{
    int arr[100];
    int *bararr;        // Barrier Arrayint arrSize;        //Size of arrayint iCount;        //Counter Variableint id;            //Process IDint nProc=3;        //number of Processesfloat sum=0;
    
    float *deviation,*avg;  // Shared Variablesint *lock;        // Shared Variable for Spinlockint shmidavg,shmidlock,shmiddeviation,shmidbararr;    // Shmid for Shared Variables
    
    printf("Enter the Size of an Array :");
    scanf("%d",&arrSize);

    for(iCount=0;iCount<arrSize;iCount++)
    {
        printf("Enter arr[%d] :",iCount);
        scanf("%d",&arr[iCount]);
    }

    /* Allocate Shared memory */
avg=(float*)sshared(sizeof(float),&shmidavg); deviation=(float*)sshared(sizeof(float),&shmiddeviation); lock=(int*)sshared(sizeof(int),&shmidlock); bararr=(int*)sshared(sizeof(int)*4,&shmidbararr); spin_lock_init(lock); // Spin Lock Initialization barrier_init(bararr,nProc); // Barrier Initialization *avg=0; id=process_fork(nProc); // Forking Processes/* Partial Sum using Loop Spliting */
for(iCount=id;iCount<arrSize;iCount=iCount+nProc) { sum = sum + arr[iCount]; } spin_lock(lock); /* Critical Region */
*avg = *avg + ( sum /arrSize); // Calculate Average from Partial Sums spin_unlock(lock); /*
---------------------------------------------------------------------------
Barrier Should be here, So that all Processes have to wait until Final
Average is Calculated.
---------------------------------------------------------------------------
*/
barrier(bararr); // Barrier Called sum=0; /* Calculate Sum(Xi - Mean) Using Loop Spliting */
for(iCount=id;iCount<arrSize;iCount=iCount+nProc) { sum = sum + pow(((float)arr[iCount] - *avg),2); } spin_lock(lock); /* Critical Region */
*deviation = *deviation + (sum / arrSize-1); // Calculate Final Deviation spin_unlock(lock); process_join(nProc,id); // Joining the Process *deviation=sqrt(*deviation); printf("Deviation : %f\n",*deviation); /* Cleaning the Shared Region */
cleanup_memory(&shmidavg); cleanup_memory(&shmidlock); cleanup_memory(&shmiddeviation); cleanup_memory(&shmidbararr); return 0; } /* Output

[divyen@localhost pp-tw3]$ cc -o Prog03 -lm Prog03.c
[divyen@localhost pp-tw3]$ ./Prog03
Enter the Size of an Array :5
Enter arr[0] :10
Enter arr[1] :20
Enter arr[2] :30
Enter arr[3] :40
Enter arr[4] :50
Deviation : 14.035668

*/
  
Share: 



Rainhard Fischer
Rainhard Fischer author of Program to calculate average of the elements of an array and then the average deviation using barrier is from Frankfurt, Germany.
 
View All Articles

 
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!