#include <stdio.h>
#include <omp.h>
#define ARRAY_SIZE 1000
int main() {
int array[ARRAY_SIZE];
long long sum = 0;
// Initialize the array with some values
for (int i = 0; i < ARRAY_SIZE; i++) {
array[i] = i + 1; // Filling the array with numbers 1 to ARRAY_SIZE
}
// Parallel region to calculate the sum
#pragma omp parallel
{
long long local_sum = 0;
// Parallel for loop with reduction to avoid race conditions
#pragma omp for reduction(+:sum)
for (int i = 0; i < ARRAY_SIZE; i++) {
local_sum += array[i];
}
// Update the total sum
sum += local_sum;
}
printf("The sum of the array is: %lld\n", sum);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxvbXAuaD4KCiNkZWZpbmUgQVJSQVlfU0laRSAxMDAwCgppbnQgbWFpbigpIHsKICAgIGludCBhcnJheVtBUlJBWV9TSVpFXTsKICAgIGxvbmcgbG9uZyBzdW0gPSAwOwoKICAgIC8vIEluaXRpYWxpemUgdGhlIGFycmF5IHdpdGggc29tZSB2YWx1ZXMKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgQVJSQVlfU0laRTsgaSsrKSB7CiAgICAgICAgYXJyYXlbaV0gPSBpICsgMTsgLy8gRmlsbGluZyB0aGUgYXJyYXkgd2l0aCBudW1iZXJzIDEgdG8gQVJSQVlfU0laRQogICAgfQoKICAgIC8vIFBhcmFsbGVsIHJlZ2lvbiB0byBjYWxjdWxhdGUgdGhlIHN1bQogICAgI3ByYWdtYSBvbXAgcGFyYWxsZWwKICAgIHsKICAgICAgICBsb25nIGxvbmcgbG9jYWxfc3VtID0gMDsKCiAgICAgICAgLy8gUGFyYWxsZWwgZm9yIGxvb3Agd2l0aCByZWR1Y3Rpb24gdG8gYXZvaWQgcmFjZSBjb25kaXRpb25zCiAgICAgICAgI3ByYWdtYSBvbXAgZm9yIHJlZHVjdGlvbigrOnN1bSkKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IEFSUkFZX1NJWkU7IGkrKykgewogICAgICAgICAgICBsb2NhbF9zdW0gKz0gYXJyYXlbaV07CiAgICAgICAgfQoKICAgICAgICAvLyBVcGRhdGUgdGhlIHRvdGFsIHN1bQogICAgICAgIHN1bSArPSBsb2NhbF9zdW07CiAgICB9CgogICAgcHJpbnRmKCJUaGUgc3VtIG9mIHRoZSBhcnJheSBpczogJWxsZFxuIiwgc3VtKTsKICAgIHJldHVybiAwOwp9Cg==