Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/time.h>
  5. #include <unistd.h>
  6. #include <omp.h>
  7.  
  8.  
  9.  
  10. void qsort_parallel(unsigned *a,int l,int r){
  11.     if(r>l){
  12.  
  13.         int pivot=a[r],tmp;
  14.         int less=l-1,more;
  15.         for(more=l;more<=r;more++){
  16.             if(a[more]<=pivot){
  17.                 less++;
  18.                 tmp=a[less];
  19.                 a[less]=a[more];
  20.                 a[more]=tmp;
  21.             }
  22.         }
  23.             #pragma omp task
  24.             qsort_parallel(a, l,less-1);
  25.             #pragma omp task
  26.             qsort_parallel(a, less+1,r);
  27.             #pragma omp taskwait
  28.     }
  29. }
  30.  
  31. void readFileUnsigned(unsigned *input, char *filename, int size) {
  32.     FILE *finput;
  33.     if (finput = fopen(filename, "rb")) {
  34.         fread(input, size * sizeof(unsigned), 1 , finput);
  35.     } else {
  36.         printf("Error reading input file");
  37.         exit(1);
  38.     }
  39. }
  40. int main(int argc, char** argv)
  41. {
  42.     int size, omp_num_threads, iterations;
  43.     char * inputFile, *outputFile;
  44.     unsigned *data;
  45.  
  46.     if (argc == 5) {
  47.         size = atoi(argv[1]);
  48.         omp_num_threads = atoi(argv[2]);
  49.         inputFile = argv[3];
  50.         outputFile = argv[4];
  51.     } else {
  52.         fprintf(stderr, "Usage: %s <input size> <num_threads> <input file> <output file>\n", argv[0]);
  53.         exit(1);
  54.     }
  55.  
  56.     omp_set_num_threads(omp_num_threads);
  57.  
  58.     data = (unsigned *)malloc(size*sizeof(unsigned));
  59.  
  60.     readFileUnsigned(data, inputFile, size);
  61.  
  62.     #pragma omp parallel   
  63.     #pragma omp single
  64.     qsort_parallel(data, 0,size-1);
  65.  
  66.     FILE *fp;
  67.     if (fp = fopen(outputFile, "wb")) {
  68.         fwrite(data, size * sizeof(unsigned), 1 , fp);
  69.     } else {
  70.         printf("Error writing output file");
  71.         exit(1);
  72.     }
  73.     fclose(fp);
  74.  
  75.     printf("Done\n");
  76.  
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement