/********************************************* In this example we have NUM_THREADS threads that share N values. Each one taking a chunk of size CHUNKSIZE every time. C[treadID] is modifed only by one thread gcc -fopenmp -Wall -Werror example2OMP.c -o a.out bash-4.1$ ./a.out Thread 0: counter = 200 Thread 3: counter = 200 Thread 2: counter = 200 Thread 1: counter = 200 Thread 5: counter = 100 Thread 4: counter = 100 Average Value is 999.000000 ************************************************/ #include #include #define CHUNKSIZE 100 #define N 1000 #define NUM_THREADS 10 int main () { int i, chunk, counter=0; float a[N], b[N], c[NUM_THREADS], result=0; /* Some initializations */ for (i=0; i < N; i++) a[i] = b[i] = i * 1.0; chunk = CHUNKSIZE; omp_set_num_threads(NUM_THREADS); #pragma omp parallel shared(a,b,c,chunk) private(i,counter) { counter=0; #pragma omp for schedule(static,chunk) for (i=0; i < N; i++){ c[omp_get_thread_num ()] += a[i] + b[i]; counter++; } printf("Thread %d[%d]: counter = %d\n",omp_get_thread_num (),chunk,counter); } /* End of parallel section. All Thread join master thread and terminate */ /* Implicit barrier at the end of the parallel region */ for (i=0; i < NUM_THREADS; i++)result+=c[i]; printf("Average Value is %f\n",result/N); return 0; }