#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define SIZE 1000000
int main() {
int *arr
= (int*)malloc(SIZE
* sizeof(int)); int sum_parallel_for = 0;
int sum_sections = 0;
// Initialize array with random values
for (int i = 0; i < SIZE; ++i) {
}
// Work sharing with parallel for
#pragma omp parallel for reduction(+:sum_parallel_for)
for (int i = 0; i < SIZE; ++i) {
sum_parallel_for += arr[i];
}
// Work sharing with sections
#pragma omp parallel sections
{
#pragma omp section
{
for (int i = 0; i < SIZE / 2; ++i) {
sum_sections += arr[i];
}
}
#pragma omp section
{
for (int i = SIZE / 2; i < SIZE; ++i) {
sum_sections += arr[i];
}
}
}
printf("Sum using parallel for: %d\n", sum_parallel_for
); printf("Sum using sections: %d\n", sum_sections
);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG9tcC5oPgoKI2RlZmluZSBTSVpFIDEwMDAwMDAKCmludCBtYWluKCkgewogICAgaW50ICphcnIgPSAoaW50KiltYWxsb2MoU0laRSAqIHNpemVvZihpbnQpKTsKICAgIGludCBzdW1fcGFyYWxsZWxfZm9yID0gMDsKICAgIGludCBzdW1fc2VjdGlvbnMgPSAwOwoKICAgIC8vIEluaXRpYWxpemUgYXJyYXkgd2l0aCByYW5kb20gdmFsdWVzCiAgICBmb3IgKGludCBpID0gMDsgaSA8IFNJWkU7ICsraSkgewogICAgICAgIGFycltpXSA9IHJhbmQoKSAlIDEwMDsgCiAgICB9CgogICAgLy8gV29yayBzaGFyaW5nIHdpdGggcGFyYWxsZWwgZm9yCiAgICAjcHJhZ21hIG9tcCBwYXJhbGxlbCBmb3IgcmVkdWN0aW9uKCs6c3VtX3BhcmFsbGVsX2ZvcikKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgU0laRTsgKytpKSB7CiAgICAgICAgc3VtX3BhcmFsbGVsX2ZvciArPSBhcnJbaV07CiAgICB9CgogICAgLy8gV29yayBzaGFyaW5nIHdpdGggc2VjdGlvbnMKICAgICNwcmFnbWEgb21wIHBhcmFsbGVsIHNlY3Rpb25zCiAgICB7CiAgICAgICAgI3ByYWdtYSBvbXAgc2VjdGlvbgogICAgICAgIHsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBTSVpFIC8gMjsgKytpKSB7CiAgICAgICAgICAgICAgICBzdW1fc2VjdGlvbnMgKz0gYXJyW2ldOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgICNwcmFnbWEgb21wIHNlY3Rpb24KICAgICAgICB7CiAgICAgICAgICAgIGZvciAoaW50IGkgPSBTSVpFIC8gMjsgaSA8IFNJWkU7ICsraSkgewogICAgICAgICAgICAgICAgc3VtX3NlY3Rpb25zICs9IGFycltpXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICBwcmludGYoIlN1bSB1c2luZyBwYXJhbGxlbCBmb3I6ICVkXG4iLCBzdW1fcGFyYWxsZWxfZm9yKTsKICAgIHByaW50ZigiU3VtIHVzaW5nIHNlY3Rpb25zOiAlZFxuIiwgc3VtX3NlY3Rpb25zKTsKCiAgICBmcmVlKGFycik7CgogICAgcmV0dXJuIDA7Cn0=