fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <omp.h>
  4.  
  5. #define VECTOR_SIZE 1000000
  6.  
  7. int main() {
  8. std::vector<int> vec1(VECTOR_SIZE, 1);
  9. std::vector<int> vec2(VECTOR_SIZE, 2);
  10.  
  11. int dot_product = 0;
  12.  
  13. // Static scheduling
  14. #pragma omp parallel for reduction(+:dot_product) schedule(static)
  15. for (int i = 0; i < VECTOR_SIZE; ++i) {
  16. dot_product += vec1[i] * vec2[i];
  17. }
  18.  
  19. std::cout << "Dot product using static scheduling: " << dot_product << std::endl;
  20.  
  21. dot_product = 0;
  22.  
  23. // Dynamic scheduling
  24. #pragma omp parallel for reduction(+:dot_product) schedule(dynamic)
  25. for (int i = 0; i < VECTOR_SIZE; ++i) {
  26. dot_product += vec1[i] * vec2[i];
  27. }
  28.  
  29. std::cout << "Dot product using dynamic scheduling: " << dot_product << std::endl;
  30.  
  31. dot_product = 0;
  32.  
  33. // Guided scheduling
  34. #pragma omp parallel for reduction(+:dot_product) schedule(guided)
  35. for (int i = 0; i < VECTOR_SIZE; ++i) {
  36. dot_product += vec1[i] * vec2[i];
  37. }
  38.  
  39. std::cout << "Dot product using guided scheduling: " << dot_product << std::endl;
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0.01s 10876KB
stdin
Standard input is empty
stdout
Dot product using static scheduling: 2000000
Dot product using dynamic scheduling: 2000000
Dot product using guided scheduling: 2000000