1 /* implementation of parallel merge sort */
9 #define min( a, b ) ( ((a) < (b)) ? (a) : (b) )
12 #define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
17 void printlist(char * message, numtype * ptr, int len);
18 int rank(int elem, numtype * list, int len);
19 /* merge(a,n,b,m,c): merges a of size n and b of size m into c */
20 void merge(int ti, numtype * a, int n, numtype * b, int m, numtype * c);
22 int main ( int argc, char ** argv) {
23 int n = LISTSIZE, m = LISTSIZE;
26 int a_len, b_len, b_len_end, b_len_begin;
28 double startTime, endTime;
30 while ((opt = getopt(argc, argv, "t:")) != -1) {
36 fprintf(stderr, "Usage: %s [-t threads]\n", argv[0]);
41 //printf ("Number of processes that will be started: %i\n", p);
42 //printf ("----------------------------------\n");
44 c = (numtype *) malloc((LISTSIZE * 2 + 1) * (sizeof (numtype)));
47 //printlist("0 Sorted List A:", a);
48 //printlist("0 Sorted List B:", b);
50 startTime = omp_get_wtime();
53 #pragma omp parallel for shared(a,b,c,n,m,p,a_len) private(i,b_len_begin,b_len_end,b_len)
54 for (i = 0; i < p; i++) {
55 b_len_begin = rank(a[i*a_len], b, m);
56 b_len_end = rank(a[(i+1)*a_len], b, m);
57 if (b_len_begin < 0) {
58 printf ("Insert to end of list!\n");
62 //printf ("Reached end of list!\n");
65 b_len = b_len_end - b_len_begin;
66 //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]);
72 &c[i*a_len+b_len_begin]);
75 endTime = omp_get_wtime();
76 printf("took %f seconds.\n", endTime-startTime);
78 //printlist("Sorted List:", c);
83 void printlist(char * message, numtype * ptr, int len) {
92 int rank(numtype elem, numtype * list, int len) {
97 if (elem == -1) { return -1; }
99 /*printf ("elem_list: %i %i\n", elem, *list);*/
110 void merge(int ti, numtype * a, int n, numtype * b, int m, int * c) {
113 /*printf ("sorting a:%i (%i) and b:%i (%i)\n", *a, n, *b, m);*/
117 //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);
118 for (i = 0; i < sum; i++) {
120 if (n <= 0 && m <= 0) {
121 printf ("%i Calculation failed somehow...\n", ti);
138 // printf ("merge done, n=%d, m=%d\n", n, m);