fork download
  1. #include<omp.h>
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<time.h>
  5.  
  6. time_t t1;
  7. unsigned long int RandomIntGen(){
  8. return ( rand()%( 1000000000 - 1 +1)) +1 ;
  9. }
  10.  
  11.  
  12. clock_t begin, end;
  13.  
  14.  
  15. int main()
  16. {
  17. //double pi,x;
  18. srand((unsigned) time (&t1));
  19.  
  20.  
  21. unsigned long long int i,Array_size = 10000000;
  22. int n=15;
  23.  
  24. //printf("Enter size?: \n");
  25. //printf("\n array size! : ");
  26. //scanf("%llu",&Array_size);
  27. printf(" thread count : ");
  28. //scanf("%d",&n);
  29. //fflush(stdout);
  30.  
  31. unsigned long int x[Array_size], MAX = 0, MIN= 100000001;
  32.  
  33. for( i=0; i< Array_size; i++)
  34. x[i] = RandomIntGen();
  35. //printf("thread count?: ");
  36. //fflush(stdout);
  37. begin = clock();
  38. #pragma omp parallel num_threads(n)
  39. {
  40.  
  41. #pragma omp parallel for /*shared(MAX, x)*/ reduction(max:MAX)
  42. for(i=0;i<Array_size;i++){
  43. MAX = MAX > x[i] ? MAX : x[i];
  44. }
  45.  
  46. // min is working fine
  47. #pragma omp parallel for /*shared(MIN, x)*/ reduction(min:MIN)
  48. for(i=0;i<Array_size;i++){
  49. MIN = MIN < x[i] ? MIN : x[i];
  50. }
  51.  
  52. }
  53.  
  54. end = clock();
  55.  
  56. //printf("\n array size! : %llu\n",Array_size);
  57. //printf(" thread count : %d\n",n);
  58. printf(" MAX: %lu\n MIN: %lu\n",MAX,MIN);
  59. double t = (end-begin)/ CLOCKS_PER_SEC ;
  60. printf(" time: %lf s\n",t);
  61. //printf(" time: sci:%e s\n",t);
  62.  
  63. return 0;
  64. }
  65.  
  66.  
Success #stdin #stdout 0.17s 79752KB
stdin
Standard input is empty
stdout
 thread count :  MAX: 999999853
 MIN: 177
 time: 0.000000 s