source: proiecte/swift/trunk/test/qsort_omp_par.c @ 176

Last change on this file since 176 was 176, checked in by (none), 14 years ago
  • imported repo from "guagal"
File size: 1.4 KB
Line 
1/*
2 * swift_context.c
3 *
4 * (c) 2009 Ionut Rosoiu <ionut.rosoiu@gmail.com>
5 *
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <omp.h>
11#include "swift_compiler.h"
12
13extern void read_array(char *filename, int **arr, int *n);
14extern void free_array(int *arr);
15extern void check_array(int *arr, int n);
16
17int partition (int *data, int p, int r)
18{
19    int x = data[p];
20    int k = p;
21    int l = r+1;
22    int t;
23
24    while (1) {
25        do k++; while ((data[k] <= x) && (k < r));
26        do l--; while (data[l] > x);
27        while (k < l) {
28            t = data[k];  data[k] = data[l];  data[l] = t;
29            do k++; while (data[k] <= x);
30            do l--; while (data[l] > x);
31        }
32        t = data[p];  data[p] = data[l];  data[l] = t;
33        return l;
34    }
35}
36
37void quicksort_omp_par (int *data, int p, int r)
38{
39    if (p < r) {
40            int q = partition (data, p, r);
41            #pragma omp parallel sections firstprivate(data, p, q, r)
42            {
43                #pragma omp section
44                quicksort_omp_par(data, p, q-1);
45                #pragma omp section
46                quicksort_omp_par(data, q+1, r);
47            }
48    }
49}
50
51void quicksort(int *arr, int n)
52{
53        quicksort_omp_par(arr, 0, n);
54}
55
56int main(int argc, char **argv)
57{
58        int n, *a;
59
60        if (argc != 2) {
61                fprintf(stderr, "usage: %s <input_file>\n", argv[0]);
62                abort();
63        }
64
65        read_array(argv[1], &a, &n);
66
67        quicksort(a, n);
68
69        check_array(a, n);
70
71        free_array(a);
72
73        printf("done.\n");
74        return 0;
75}
Note: See TracBrowser for help on using the repository browser.