1 /* implementation of parallel merge sort */
10 void printlist(char * message, int * ptr) {
19 int rank(int elem, int * list) {
23 if (elem == -1) { return -1; }
25 /*printf ("elem_list: %i %i\n", elem, *list);*/
35 /* merge(a,n,b,m,c): merges a of size n and b of size m into c */
36 void merge(int ti, int * a, int n, int * b, int m, int * c) {
40 /*printf ("sorting a:%i (%i) and b:%i (%i)\n", *a, n, *b, m);*/
44 //printf ("%i modifying %i (%i+%i) (c[%i]/0x%08x -> c[%i]/0x%08x)\n", ti, sum, n, m, 0, (unsigned int) &c, sum-1, ((unsigned int) &c)+sum);
47 if (*a < 0 && *b < 0) {
48 printf ("%i Calculation failed somehow...\n", ti);
73 int main ( int argc, char ** argv) {
77 int a_len, b_len, b_len_end, b_len_begin;
78 int a[] = {0,3,4,14,61,62,70,72,74,99,-1};
79 int b[] = {1,2,7,34,35,68,77,78,79,84,-1};
82 printf ("----------------------------------\n");
84 c = (int *) malloc((LISTSIZE * 2 + 1) * (sizeof (int)));
87 printlist("0 Sorted List A:", a);
88 printlist("0 Sorted List B:", b);
91 #pragma omp parallel for shared(a,b,c,n,p,a_len) private(i,b_len_begin,b_len_end,b_len)
92 for (i = 0; i < p; i++) {
93 b_len_begin = rank(a[i*a_len], b);
94 b_len_end = rank(a[(i+1)*a_len], b);
95 if (b_len_begin < 0) {
96 printf ("Insert to end of list!\n");
100 printf ("Reached end of list!\n");
103 b_len = b_len_end - b_len_begin;
104 printf ("%i a_len: %i, b_len: %i (begin:%i ([%i]) -> end:%i [%i])\n", i+1, a_len, b_len, b_len_begin, b[b_len_begin], b_len_end, b[b_len_end]);
113 &c[i*a_len+b_len_begin]);
116 printlist("Sorted List:", c);