1 /* implementation of parallel merge sort */
8 void printlist(char * message, int * ptr) {
17 int rank(int elem, int * list) {
22 if (elem == -1) { return -1; }
24 /*printf ("elem_list: %i %i\n", elem, *list);*/
34 /* merge(a,n,b,m,c): merges a of size n and b of size m into c */
35 void merge(int ti, int * a, int n, int * b, int m, int * c) {
38 // usleep (1000);//1ms
39 /*printf ("sorting a:%i (%i) and b:%i (%i)\n", *a, n, *b, m);*/
43 //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);
44 for (i = 0; i < sum; i++) {
47 if (*a < 0 && *b < 0) {
48 printf ("%i Calculation failed somehow...\n", ti);
72 int main ( int argc, char ** argv) {
76 int a_len, b_len, b_len_end, b_len_begin;
78 double startTime, endTime;
80 while ((opt = getopt(argc, argv, "t:")) != -1) {
86 fprintf(stderr, "Usage: %s [-t threads]\n", argv[0]);
91 //printf ("Maximal number of threads: %i\n", omp_get_max_threads());
92 //printf ("----------------------------------\n");
94 c = (int *) malloc((LISTSIZE * 2 + 1) * (sizeof (int)));
97 //printlist("0 Sorted List A:", a);
98 //printlist("0 Sorted List B:", b);
100 startTime = omp_get_wtime();
103 #pragma omp parallel for shared(a,b,c,n,p,a_len) private(i,b_len_begin,b_len_end,b_len)
104 for (i = 0; i < p; i++) {
105 b_len_begin = rank(a[i*a_len], b);
106 b_len_end = rank(a[(i+1)*a_len], b);
107 if (b_len_begin < 0) {
108 printf ("Insert to end of list!\n");
112 //printf ("Reached end of list!\n");
115 b_len = b_len_end - b_len_begin;
116 //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]);
122 &c[i*a_len+b_len_begin]);
125 endTime = omp_get_wtime();
126 printf("took %f seconds.\n", endTime-startTime);
128 //printlist("Sorted List:", c);