fork download
  1. #include <stdio.h>
  2. #include <omp.h>
  3.  
  4. #define N 4 // Matrix size
  5.  
  6. int main() {
  7. int A[N][N], sum = 0;
  8.  
  9. // Initialize matrix
  10. for (int i = 0; i < N; i++)
  11. for (int j = 0; j < N; j++)
  12. A[i][j] = i + j + 1;
  13.  
  14. // Parallel sum using OpenMP
  15. #pragma omp parallel for reduction(+:sum)
  16. for (int i = 0; i < N; i++)
  17. for (int j = 0; j < N; j++)
  18. sum += A[i][j];
  19.  
  20. printf("Sum of Matrix: %d\n", sum);
  21. return 0;
  22. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Sum of Matrix: 64