1 /* implementation of parallel merge sort */
9 #define LISTSIZE 10000000
11 void printlist(char * message, int * ptr) {
20 int rank(int elem, int * list) {
25 if (elem == -1) { return -1; }
27 /*printf ("elem_list: %i %i\n", elem, *list);*/
37 /* merge(a,n,b,m,c): merges a of size n and b of size m into c */
38 void merge(int ti, int * a, int n, int * b, int m, int * c) {
41 // usleep (1000);//1ms
42 /*printf ("sorting a:%i (%i) and b:%i (%i)\n", *a, n, *b, m);*/
46 //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 for (i = 0; i < sum; i++) {
50 if (*a < 0 && *b < 0) {
51 printf ("%i Calculation failed somehow...\n", ti);
75 int main ( int argc, char ** argv) {
79 int a_len, b_len, b_len_end, b_len_begin;
81 double startTime, endTime;
83 while ((opt = getopt(argc, argv, "t:")) != -1) {
89 fprintf(stderr, "Usage: %s [-t threads]\n", argv[0]);
94 //printf ("Maximal number of threads: %i\n", omp_get_max_threads());
95 //printf ("----------------------------------\n");
97 c = (int *) malloc((LISTSIZE * 2 + 1) * (sizeof (int)));
100 //printlist("0 Sorted List A:", a);
101 //printlist("0 Sorted List B:", b);
103 startTime = omp_get_wtime();
106 #pragma omp parallel for shared(a,b,c,n,p,a_len) private(i,b_len_begin,b_len_end,b_len)
107 for (i = 0; i < p; i++) {
108 b_len_begin = rank(a[i*a_len], b);
109 b_len_end = rank(a[(i+1)*a_len], b);
110 if (b_len_begin < 0) {
111 printf ("Insert to end of list!\n");
115 //printf ("Reached end of list!\n");
118 b_len = b_len_end - b_len_begin;
119 //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]);
125 &c[i*a_len+b_len_begin]);
128 endTime = omp_get_wtime();
129 printf(" took %f seconds.\n", endTime-startTime);
131 //printlist("Sorted List:", c);