fork download
  1. #include <stdio.h>
  2. #include <omp.h>
  3.  
  4. #define N 1000000
  5.  
  6. int dotProduct(int *a, int *b, int n) {
  7. int dot = 0;
  8. #pragma omp parallel for reduction(+:dot)
  9. for (int i = 0; i < n; i++) {
  10. dot += a[i] * b[i];
  11. }
  12. return dot;
  13. }
  14.  
  15. int main() {
  16. int a[N], b[N];
  17.  
  18. // Initialize arrays a and b
  19. for (int i = 0; i < N; i++) {
  20. a[i] = i;
  21. b[i] = 2 * i;
  22. }
  23.  
  24. // Compute dot product
  25. int result = dotProduct(a, b, N);
  26.  
  27. // Display the result
  28. printf("Dot product: %d\n", result);
  29.  
  30. return 0;
  31. }
  32.  
Success #stdin #stdout 0.01s 9300KB
stdin
Standard input is empty
stdout
Dot product: 1168289984