Logo 
Search:

C Programming Articles

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

Overcome forward dependency using block scheduling copy arr[i+1] to arr[i]

Posted By: Giselle Miller     Category: C Programming     Views: 2039

Overcome forward dependency using block scheduling (Copy arr[i+1] to arr[i] )

Code for Overcome forward dependency using block scheduling copy arr[i+1] to arr[i] in C Programming

# 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()
{
    // Note that Array Starts from Base 1    int arrSize=9;
    int *arr;
    int arrold[9];            // For Holding boundary Cellsint id;                // Process Identificationint nProc=3;            // Number of Processesint *bararr;            // Barrier Array        int shmidbararr,shmidarr;    // Shmid For Shared Variablesint blocksize = arrSize/nProc;    // Calculating Block Sizeint index,iCount;
    
    arr=(int*)sshared(sizeof(int)*10,&shmidarr);
        
    printf("Original Array ...\n");
    for(iCount=1;iCount<=arrSize+1;iCount++)
    {
        arr[iCount]=iCount;        // Assignning Array Values
        printf("arr[%d] : %d\n",iCount,arr[iCount]);
    }
    bararr=(int*)sshared(sizeof(int)* 4,&shmidbararr);
    
    
    barrier_init(bararr,nProc); // Initialize Barrier Array
    
    id=process_fork(nProc); //Forking Process
    
    arrold[id]=arr[blocksize * (id+1) + 1]; // Copying Boundary Values
    
    barrier(bararr);    // Calling Barrier
    
    index=id * blocksize;    // Calculating Start Index for Each Blockfor(iCount=1;iCount<blocksize;iCount++)
    {
        arr[index+iCount]=arr[index + iCount + 1];
    }

    arr[blocksize*(id+1)] = arrold[id];    //Assigning Boundary Values
    
    process_join(nProc,id);     // Joining Process
    
    printf("After Copy ...\n");
    for(iCount=1;iCount<=arrSize;iCount++)
    {
        printf("arr[%d] = %d\n",iCount,arr[iCount]);
    }
    cleanup_memory(&shmidbararr);
    cleanup_memory(&shmidarr);
    return 0;
}
/* Output
[divyen@localhost pp-tw3]$ cc -o Prog04 Prog04.c
[divyen@localhost pp-tw3]$ ./Prog04
Original Array ...
arr[1] : 1
arr[2] : 2
arr[3] : 3
arr[4] : 4
arr[5] : 5
arr[6] : 6
arr[7] : 7
arr[8] : 8
arr[9] : 9
arr[10] : 10
After Copy ...
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
arr[5] = 6
arr[6] = 7
arr[7] = 8
arr[8] = 9
arr[9] = 10

*/
  
Share: 



Giselle Miller
Giselle Miller author of Overcome forward dependency using block scheduling copy arr[i+1] to arr[i] 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!