fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <omp.h>
  4.  
  5. #define VECTOR_SIZE 1000000
  6.  
  7. int main() {
  8. // Initialize vectors
  9. std::vector<int> A(VECTOR_SIZE);
  10. std::vector<int> B(VECTOR_SIZE);
  11.  
  12. // Initialize vectors with random values
  13. for (int i = 0; i < VECTOR_SIZE; ++i) {
  14. A[i] = i;
  15. B[i] = VECTOR_SIZE - i;
  16. }
  17.  
  18. int dot_product = 0;
  19.  
  20. // Perform dot product in parallel
  21. #pragma omp parallel for reduction(+:dot_product)
  22. for (int i = 0; i < VECTOR_SIZE; ++i) {
  23. dot_product += A[i] * B[i];
  24. }
  25.  
  26. // Output the result
  27. std::cout << "Dot product result: " << dot_product << std::endl;
  28.  
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0.01s 10864KB
stdin
Standard input is empty
stdout
Dot product result: 1183719328