Logo 
Search:

C Programming Articles

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

Program of histogram using self-scheduling and locking the whole bin

Posted By: Huette Miller     Category: C Programming     Views: 1380

Program of histogram using self-scheduling and locking the whole bin.

Code for Program of histogram using self-scheduling and locking the whole bin 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 *index;                // Shared Counterint NoOfBins=5;                // Number of binsint binsize;                // Size of each binint *histogram;
    int *lock1,*lock2;            // Lock1 for Index// Lock2 For Binint shmidindex,shmidlock1,shmidlock2,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;

    index=(int*) sshared(sizeof(int),&shmidindex);
    lock1=(int*)sshared(sizeof(int),&shmidlock1);
    lock2=(int*)sshared(sizeof(int),&shmidlock2);
    histogram=(int*) sshared(sizeof(int)*NoOfBins,&shmidhist);
    
    printf("\n Bin Size : %d",binsize);
    printf("\n No. Of Bins : %d\n",NoOfBins);
    spin_lock_init(lock1);
    spin_lock_init(lock2);
    *index=0;
    for(iCount=0;iCount<NoOfBins;iCount++)
    {
        histogram[iCount]=0;
    }
    id=process_fork(NoOfBins);
        
    while(1)
    {
        spin_lock(lock1);
            i=*index;
            *index=*index+1;
        spin_unlock(lock1);
        
        if(i>=arrSize)
        {
            break;
        }

        bin=abs((arr[i] - minarr)/binsize);
        if(bin>=NoOfBins)
        {
            bin=NoOfBins-1;
        }
        printf("Number %d is  : %d\t Bin : %d\n",i,arr[i],bin);
        
        /* Locking Whole Bin */
spin_lock(lock2); histogram[bin]++; spin_unlock(lock2); } printf("No of Items in Bin(%d) : %d\n",id,histogram[id]); process_join(NoOfBins,id); cleanup_memory(&shmidindex); cleanup_memory(&shmidlock1); cleanup_memory(&shmidlock2); cleanup_memory(&shmidhist); return 0; } /* Output

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

Bin Size : 9
No. Of Bins : 5
Number 0 is : 33 Bin : 3
Number 1 is : 36 Bin : 3
Number 2 is : 27 Bin : 2
Number 3 is : 15 Bin : 1
Number 4 is : 43 Bin : 4
Number 5 is : 35 Bin : 3
Number 6 is : 36 Bin : 3
Number 7 is : 42 Bin : 4
Number 8 is : 49 Bin : 4
Number 9 is : 21 Bin : 2
Number 10 is : 12 Bin : 1
Number 11 is : 27 Bin : 2
Number 12 is : 40 Bin : 4
Number 13 is : 9 Bin : 0
Number 14 is : 13 Bin : 1
Number 15 is : 26 Bin : 2
Number 16 is : 40 Bin : 4
Number 17 is : 26 Bin : 2
Number 18 is : 22 Bin : 2
Number 19 is : 36 Bin : 3
Number 20 is : 11 Bin : 1
Number 21 is : 18 Bin : 1
Number 22 is : 17 Bin : 1
Number 23 is : 29 Bin : 3
Number 24 is : 32 Bin : 3
Number 25 is : 30 Bin : 3
Number 26 is : 12 Bin : 1
Number 27 is : 23 Bin : 2
Number 28 is : 17 Bin : 1
Number 29 is : 35 Bin : 3
Number 30 is : 29 Bin : 3
Number 31 is : 2 Bin : 0
Number 32 is : 22 Bin : 2
Number 33 is : 8 Bin : 0
Number 34 is : 19 Bin : 1
Number 35 is : 17 Bin : 1
Number 36 is : 43 Bin : 4
Number 37 is : 6 Bin : 0
Number 38 is : 11 Bin : 1
Number 39 is : 42 Bin : 4
Number 40 is : 29 Bin : 3
Number 41 is : 23 Bin : 2
Number 42 is : 21 Bin : 2
Number 43 is : 19 Bin : 1
Number 44 is : 34 Bin : 3
Number 45 is : 37 Bin : 3
Number 46 is : 48 Bin : 4
Number 47 is : 24 Bin : 2
Number 48 is : 15 Bin : 1
Number 49 is : 20 Bin : 2
No of Items in Bin(1) : 13
No of Items in Bin(2) : 12
No of Items in Bin(3) : 13
No of Items in Bin(4) : 8
No of Items in Bin(0) : 4

*/
  
Share: 



Huette Miller
Huette Miller author of Program of histogram using self-scheduling and locking the whole bin 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!