fork download
  1. #include <omp.h>
  2. #include <stdio.h>
  3.  
  4. int RECURSIVE_MIN(int *A, int n) {
  5. // Base case
  6. if (n == 1) {
  7. return A[0];
  8. }
  9.  
  10. int lmin, rmin;
  11.  
  12. // Recursive calls in parallel using OpenMP tasks
  13. #pragma omp task shared(lmin) if (n > 1)
  14. {
  15. lmin = RECURSIVE_MIN(A, n / 2);
  16. }
  17.  
  18. #pragma omp task shared(rmin) if (n > 1)
  19. {
  20. rmin = RECURSIVE_MIN(A + n / 2, n - n / 2);
  21. }
  22.  
  23. // Wait for both tasks to complete
  24. #pragma omp taskwait
  25.  
  26. // Return the minimum of the two halves
  27. return (lmin < rmin) ? lmin : rmin;
  28. }
  29.  
  30. int main() {
  31. int A[] = {5, 3, 9, 1, 6, 8, 7, 4, 2};
  32. int n = sizeof(A) / sizeof(A[0]);
  33. int min_value;
  34.  
  35. // Initialize OpenMP parallel region
  36. #pragma omp parallel
  37. {
  38. #pragma omp single
  39. {
  40. min_value = RECURSIVE_MIN(A, n);
  41. }
  42. }
  43.  
  44. printf("The minimum value is: %d\n", min_value);
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
The minimum value is: 1