Thread: Parallel merge sort

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Hong Kong
    Posts
    18

    Parallel merge sort

    Hey everyone! So I wanted to implement parallel merge sort in C using fork(), mmap(...) and waitpid() system calls only.....The program should create processes until the problem size is less than 1000...I need to sort an int array..
    The algorithm that i have in mind is...

    (Suppose i create 4 processes, 1 parent and 3 children)

    Use mmap() to share data b/w processes..
    Parent P divides the array into 2 parts. fork() creates a child C1. Assign sorting of 2nd half of array to C1 and first half to P. P calls fork() again and creates another child C2. C2 will sort the second half of P's array. Simultaneously C1 calls fork() and creates another child C3 and C3 will sort the second half of C1's array. Meanwhile C1 and P will wait for the sorting to complete (i.e. wait for C3 and C2 to terminate respectively). Then C1 will merge with C3 and P with C2 and P will wait for C1 and so on...until the array is sorted...

    What i need to know is how to call a fork() within a fork() in this particular case...
    e.g.
    For two 4 processes i.e. when n = 2 can i implement like this..If yes, then actually i want to make at most 32 processes i.e. when n=5....So I cannot possibly use so many if statements and i don't think i can use for loop here..

    Code:
    pid = fork();
    
      if(pid == 0) //child1 process
    {  
        if(n == 2){ 
    
        pid2 = fork();
        if (pid2 == 0) //child3 process
        {
          msort(numbers,(3*size)/4,size,buffer);
        }
        else if (pid2 > 0) //child 1 process -- parent of child3
        {
          msort(numbers,size/2,(3*size)/4,buffer);
          waitpid(pid2,NULL,0);
          merge(numbers,size/2,(3*size)/4,size,buffer);
        }
        }
       else{
        msort(numbers,size/2,size,buffer);
        }
    }
    
      else if (pid > 0) //parent process
    {
       if(n == 2){
        pid2 = fork();
        if (pid2 == 0) //child2 process
        {
          msort(numbers,0,size/4,buffer);
        }
        else if (pid2 > 0) //parent process
        {
          msort(numbers,size/4,size/2,buffer);
          waitpid(pid2,NULL,0);
          merge(numbers,0,size/4,size/2,buffer);
        }
        }
        else{
         msort(numbers,0,size/2,buffer);
         waitpid(pid,NULL,0);
         merge(numbers,0,size/2,size,buffer);
        }
    }
    Last edited by Salem; 09-25-2011 at 01:07 AM. Reason: removed pointless html

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    So you want a process tree which looks like this, where M is a merging parent, and S is a sorting child.
    Code:
         M
        / \
       M   S
      / \
     S   S
    Seems like an ideal case for recursion to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Location
    Hong Kong
    Posts
    18
    Yes but the recursive sorting should be done in parallel and not sequential.

    The child on the right should further have a child....The figure should be similar to a complete binary tree.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    So forget about sorting and merging for the moment, and focus on getting something to print out all the steps, like

    sorting from 0 to 999
    sorting from 1000 to 1999
    merging 0 to 1999

    and so on through all the steps you need.

    When that looks good, you can replace the relevant steps with fork() and wait(), and perform the actual sort and merge as needed.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Hong Kong
    Posts
    18
    Quote Originally Posted by Salem View Post
    So forget about sorting and merging for the moment, and focus on getting something to print out all the steps, like

    sorting from 0 to 999
    sorting from 1000 to 1999
    merging 0 to 1999

    and so on through all the steps you need.

    When that looks good, you can replace the relevant steps with fork() and wait(), and perform the actual sort and merge as needed.
    Yeah i tried that and everything looks good but i currently i am at the second level of the binary tree structure...I want to go to at most 5th level (meaning that there would be 32 processes working simultaneously)..Is there any way to do that without using so many nested ifs..

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    I dunno, did you use the recursive approach and get 32 leaves printed out?

    If it's printing out the list of sorts and merges OK, then just replace them with fork() calls and see what happens.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Merge sort fails to sort
    By Cresh07 in forum C++ Programming
    Replies: 3
    Last Post: 09-23-2009, 11:17 AM
  2. Quick Sort or Merge Sort???
    By swanley007 in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2005, 06:48 PM
  3. Quick sort VS Merge Sort
    By sachitha in forum Tech Board
    Replies: 7
    Last Post: 09-03-2004, 11:57 PM
  4. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 02:24 PM
  5. help with merge sort
    By maloy in forum C Programming
    Replies: 4
    Last Post: 03-04-2002, 06:22 PM