Partial Sum Openmp

PHOTO EMBED

Wed Jan 21 2026 00:06:47 GMT+0000 (Coordinated Universal Time)

Saved by @luisjdominguezp

#include <stdio.h>
#include <omp.h>

int main(int argc, char** argv){
    int partial_Sum, total_Sum;

    #pragma omp parallel private(partial_Sum) shared(total_Sum)
    {
        partial_Sum = 0;
        total_Sum = 0;

        #pragma omp for
        for(int i = 1; i <= 1000; i++){
            partial_Sum += i;
        }

        //Create thread safe region.
        #pragma omp critical
        {
            //add each threads partial sum to the total sum
            total_Sum += partial_Sum;
        }
    }
    printf("Total Sum: %d\n", total_Sum);
    return 0;
}

content_copyCOPY

https://curc.readthedocs.io/en/latest/programming/OpenMP-C.html