1 /* implementation of parallel merge sort */
8 void printlist(char * message, int * ptr);
9 int rank(int elem, int * list);
10 /* merge(a,n,b,m,c): merges a of size n and b of size m into c */
11 void merge(int ti, int * a, int n, int * b, int m, int * c);
13 int main ( int argc, char ** argv) {
17 int a_len, b_len, b_len_end, b_len_begin;
19 double startTime, endTime;
21 while ((opt = getopt(argc, argv, "t:")) != -1) {
27 fprintf(stderr, "Usage: %s [-t threads]\n", argv[0]);
32 //printf ("Maximal number of threads: %i\n", omp_get_max_threads());
33 //printf ("----------------------------------\n");
35 c = (int *) malloc((LISTSIZE * 2 + 1) * (sizeof (int)));
38 //printlist("0 Sorted List A:", a);
39 //printlist("0 Sorted List B:", b);
41 startTime = omp_get_wtime();
44 #pragma omp parallel for shared(a,b,c,n,p,a_len) private(i,b_len_begin,b_len_end,b_len)
45 for (i = 0; i < p; i++) {
46 b_len_begin = rank(a[i*a_len], b);
47 b_len_end = rank(a[(i+1)*a_len], b);
48 if (b_len_begin < 0) {
49 printf ("Insert to end of list!\n");
53 //printf ("Reached end of list!\n");
56 b_len = b_len_end - b_len_begin;
57 //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]);
63 &c[i*a_len+b_len_begin]);
66 endTime = omp_get_wtime();
67 printf("took %f seconds.\n", endTime-startTime);
69 //printlist("Sorted List:", c);
74 void printlist(char * message, int * ptr) {
83 int rank(int elem, int * list) {
87 if (elem == -1) { return -1; }
89 /*printf ("elem_list: %i %i\n", elem, *list);*/
99 void merge(int ti, int * a, int n, int * b, int m, int * c) {
102 /*printf ("sorting a:%i (%i) and b:%i (%i)\n", *a, n, *b, m);*/
106 //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);
107 for (i = 0; i < sum; i++) {
109 if (*a < 0 && *b < 0) {
110 printf ("%i Calculation failed somehow...\n", ti);