C Programming Blog

 
 
 

Image

# include <stdio.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 iCount;
    int nProc=10;
    int const n=19;
    float const r=1,t=1;        
    int id,k,**bar_ary,**shmid;    
    float *x,y[20],u[27],z[20],*sum;


    shmid=(int **)malloc(sizeof(int)* ((nProc-1)/2 +3) );

    for(iCount=0;iCount< ((nProc-1)/2+3); iCount++)
    {
        shmid[iCount]=(int *)malloc(sizeof(int));
    }

    sum=(float *)malloc(sizeof(float)*20);
    x=(float *)malloc(sizeof(float)*20);

    sum=(float *)sshared(sizeof(float)*20,shmid[0]);
    x=(float *)sshared(sizeof(float)*20,shmid[1]);

    bar_ary=(int**)malloc(sizeof(int)*((nProc-1)/2 +2));

    for(iCount=0; iCount <= ((nProc-1)/2); iCount++)
    {
        bar_ary[iCount]=(int *)malloc(sizeof(int)*5);
        bar_ary[iCount]=(int *)sshared(sizeof(int)*5,shmid[iCount+2]);
    }

    for(iCount=0;iCount<27;iCount++)
    {
        u[iCount]=iCount+3;
        if(iCount<20)
        {
            y[iCount]=iCount+1;
            z[iCount]=iCount+2;
            x[iCount]=0;
            sum[iCount]=0;
        }
    }

    for(iCount=0;iCount <= ((nProc-1)/2);iCount++)
    {
        barrier_init(bar_ary[1,iCount],2);
    }

    id=process_fork(nProc);

    for(k=1+(id/2); k<n; k+=nProc/2)
    {        
        if((id%2)==0)
        {
        sum[id]=u[k] + r * ( z[k] + r * y[k] ) + t * ( u[k+3] + r * u[k+2] );
        }
        else
        {
            sum[id]=t * r * r * u[k+1] + t * t * ( u[k+6] + r * ( u[k+5] + r * u[k+4] ));
        }

        barrier(bar_ary[1,id/2]);

        if((id%2) == 0)
        {
        x[k]=sum[id]+sum[id+1];
        }         
        barrier(bar_ary[1,id/2]);
    }

    process_join(nProc,id);

    printf(\" Results \\n\");
    for(iCount=0; iCount<n ;iCount++)
    {
        printf(\" x[%d]=%.2f \\n\",iCount,x[iCount]);
    }

    iCount=0;
    while(iCount <  ((nProc-1)/2 + 3))
    {
        cleanup_memory(shmid[iCount]);
        iCount++;
    }
    return 0;
}

 
 
Didn't find what you were looking for? Find more on Program of expression Splitting using multiple barriers