Logo 
Search:

C Programming Articles

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

Program of histogram by calculating the partial histograms and consolidating in the end to get the final histogram

Posted By: Owen Evans     Category: C Programming     Views: 1613

Program of histogram by calculating the partial histograms and consolidating in the end to get the final histogram.

Code for Program of histogram by calculating the partial histograms and consolidating in the end to get the final histogram in C Programming

# include <stdio.h>
# include </usr/include/sys/types.h>
# include </usr/include/sys/shm.h>
# include </usr/include/sys/sem.h>
# include </usr/include/sys/ipc.h>
# include <stdlib.h>                 // For rand() Function
# include "forkjoin.h"
# include "sharedlib.h"
# include "spinlock.h"
# define arrSize 50
int main()
{    
    int arr[arrSize];
    int iCount;                  // Counter Variableint NoOfBins=5;                // Number of binsint binsize;                // Size of each binint *histogram;
    int parhist[50];            // Partial Histogram for each Processint *lock;                // For Locking entire Binint shmidlock,shmidhist;
    int id;
    int bin; // Subscript of histogram arrayint minarr,maxarr;
    int i;
    // Initialize an Arrayfor(iCount=0;iCount<arrSize;iCount++)
    {
        arr[iCount]=rand()%arrSize;
    }
    
    minarr=arr[0];
    maxarr=arr[0];
    for(iCount=1;iCount<arrSize;iCount++)
    {
        if(minarr > arr[iCount])
        {
            minarr=arr[iCount];
        }
        if(maxarr < arr[iCount])
        {
            maxarr=arr[iCount];
        }
    }

    binsize=(maxarr-minarr) / NoOfBins;

    
    lock=(int*)sshared(sizeof(int)*NoOfBins,&shmidlock);
    histogram=(int*) sshared(sizeof(int)*NoOfBins,&shmidhist);
    
    printf("\n Bin Size : %d",binsize);
    printf("\n No. Of Bins : %d\n",NoOfBins);
    
    for(iCount=0;iCount<NoOfBins;iCount++)
    {
        histogram[iCount]=0;
        parhist[iCount]=0;
    }
    printf("\n");
    id=process_fork(NoOfBins);
    spin_lock_init(&lock[id]);    
    for(iCount=id;iCount<arrSize;iCount=iCount+NoOfBins)
    {
        bin=abs(arr[iCount] - minarr)/binsize;
        if(bin>=NoOfBins)
        {
            bin=NoOfBins - 1;
        }
        printf("Process( %d ):Number [%d] is %d\t Bin:%d\n",id,iCount,arr[iCount],bin);
        
        parhist[bin]++;    
    
    }
    
    for(iCount=0;iCount<NoOfBins;iCount++)
    {
        spin_lock(&lock[id]);
            histogram[iCount] = histogram[iCount] + parhist[iCount];
        spin_unlock(&lock[id]);
    }
    
    printf("\n");    
    process_join(NoOfBins,id);
    
    for(iCount=0;iCount<NoOfBins;iCount++)
    {
        printf("No Of Items in Bin(%d) is %d\n",iCount,histogram[iCount]);
    }
    cleanup_memory(&shmidlock);;
    cleanup_memory(&shmidhist);    
    return 0;
}

/* Output

[divyen@localhost pp-tw2]$ cc -o Prog08 Prog08.c
[divyen@localhost pp-tw2]$ ./Prog08

Bin Size : 9
No. Of Bins : 5

Process( 1 ):Number [1] is 36 Bin:3
Process( 1 ):Number [6] is 36 Bin:3
Process( 1 ):Number [11] is 27 Bin:2
Process( 1 ):Number [16] is 40 Bin:4
Process( 1 ):Number [21] is 18 Bin:1
Process( 1 ):Number [26] is 12 Bin:1
Process( 1 ):Number [31] is 2 Bin:0
Process( 1 ):Number [36] is 43 Bin:4
Process( 1 ):Number [41] is 23 Bin:2
Process( 1 ):Number [46] is 48 Bin:4

Process( 2 ):Number [2] is 27 Bin:2
Process( 2 ):Number [7] is 42 Bin:4
Process( 2 ):Number [12] is 40 Bin:4
Process( 2 ):Number [17] is 26 Bin:2
Process( 2 ):Number [22] is 17 Bin:1
Process( 2 ):Number [27] is 23 Bin:2
Process( 2 ):Number [32] is 22 Bin:2
Process( 2 ):Number [37] is 6 Bin:0
Process( 2 ):Number [42] is 21 Bin:2
Process( 2 ):Number [47] is 24 Bin:2

Process( 3 ):Number [3] is 15 Bin:1
Process( 3 ):Number [8] is 49 Bin:4
Process( 3 ):Number [13] is 9 Bin:0
Process( 3 ):Number [18] is 22 Bin:2
Process( 3 ):Number [23] is 29 Bin:3
Process( 3 ):Number [28] is 17 Bin:1
Process( 3 ):Number [33] is 8 Bin:0
Process( 3 ):Number [38] is 11 Bin:1
Process( 3 ):Number [43] is 19 Bin:1
Process( 3 ):Number [48] is 15 Bin:1

Process( 4 ):Number [4] is 43 Bin:4
Process( 4 ):Number [9] is 21 Bin:2
Process( 4 ):Number [14] is 13 Bin:1
Process( 4 ):Number [19] is 36 Bin:3
Process( 4 ):Number [24] is 32 Bin:3
Process( 4 ):Number [29] is 35 Bin:3
Process( 4 ):Number [34] is 19 Bin:1
Process( 4 ):Number [39] is 42 Bin:4
Process( 4 ):Number [44] is 34 Bin:3
Process( 4 ):Number [49] is 20 Bin:2

Process( 0 ):Number [0] is 33 Bin:3
Process( 0 ):Number [5] is 35 Bin:3
Process( 0 ):Number [10] is 12 Bin:1
Process( 0 ):Number [15] is 26 Bin:2
Process( 0 ):Number [20] is 11 Bin:1
Process( 0 ):Number [25] is 30 Bin:3
Process( 0 ):Number [30] is 29 Bin:3
Process( 0 ):Number [35] is 17 Bin:1
Process( 0 ):Number [40] is 29 Bin:3
Process( 0 ):Number [45] is 37 Bin:3

No Of Items in Bin(0) is 4
No Of Items in Bin(1) is 13
No Of Items in Bin(2) is 12
No Of Items in Bin(3) is 13
No Of Items in Bin(4) is 8

*/
  
Share: 



Owen Evans
Owen Evans author of Program of histogram by calculating the partial histograms and consolidating in the end to get the final histogram is from London, United Kingdom.
 
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!