Is it necessary to set loop index as a private variable in OpenMP? -
i reading openmp tutorial, , come across following program:
#include <omp.h> #include <stdio.h> #include <stdlib.h> #define n 100 int main (int argc, char *argv[]) { int nthreads, tid, i; float a[n], b[n], c[n]; /* initializations */ (i=0; < n; i++) a[i] = b[i] = i; #pragma omp parallel shared(a,b,c,nthreads) private(i,tid) { tid = omp_get_thread_num(); if (tid == 0) { nthreads = omp_get_num_threads(); printf("number of threads = %d\n", nthreads); } printf("thread %d starting...\n",tid); #pragma omp (i=0; i<n; i++) { c[i] = a[i] + b[i]; printf("thread %d: c[%d]= %f\n",tid,i,c[i]); } } /* end of parallel section */ }
i little confused whether loop index i
must private
variable or not. since same tutorial:
all threads can modify , access variables (except loop index)
so seems threads can't control i
, right? btw, try remove i
private
variable, , result seems ok.
no it's not necessary specify loop index variable private
. openmp enforces that, , somewhere in many many pages standard states much.
further requirements of openmp standard forbid adjustments loop index variable inside loop. in effect openmp enforces on c (and c++) programs 1 of constraints built fortran. enables run-time schedule multiple iterations across threads when loop first encountered, without worrying distribution might invalidated during execution.
bear in mind threads allocated, @ loop initialisation, sets of values of i
distribute individual loop iterations according schedule specified (either programmer or implementation-defined default). allowing threads update 'local' value i
lead mad code
Comments
Post a Comment