#include #ifdef _OPENMP #include #endif #define N 100 #define CHUNKSIZE 10 #define MAXTIDS 4 int main (int argc, char *argv[]) { int nthreads, tid, i, chunk, iter, count[MAXTIDS]; float a[N], b[N], c[N]; /* Some initializations */ for (i=0; i < N; i++) { a[i] = b[i] = i * 1.0; } for (i=0; i < MAXTIDS; i++) { count[i] = 0; } iter = N; chunk = CHUNKSIZE; printf("Parallel region starts\n"); #pragma omp parallel private(i,nthreads,tid) { #ifdef _OPENMP tid = omp_get_thread_num(); #else tid=0; #endif #pragma omp single nowait { #ifdef _OPENMP nthreads = omp_get_num_threads(); #else nthreads = 1; #endif printf("%d: Number of threads = %d\n", tid,nthreads); } #pragma omp for schedule(dynamic,chunk) for (i=0; i < iter; i++) { c[i] = a[i] + b[i]; count[tid]++; // printf("tid=%d i=%d c[i]=%f\n", tid,i,c[i]); } printf("Thread %d did %d loop iterations. Done.\n",tid,count[tid]); } /* end of parallel region */ printf("Completed.\n"); for (i=0; i < iter; i++) { if(c[i]!=i*2.0) { printf("Error for %d. Is %f should be %f\n",i,c[i],i*2.0); } } }