/* File: omp_loop_sum_fail.c * Purpose: Illustrate OpenMP rejection of a loop without "canonical shape" * * Compile: gcc -g -Wall -fopenmp -o omp_loop_sum_fail omp_loop_sum_fail.c * Run: Won't compile */ #include #include #include #define MAX 100000 int thread_count; int f(int i); long long Loop_sum(int A[], int n); int main(int argc, char* argv[]) { int i, n; int* A; long long sum; if (argc != 2) { fprintf(stderr, "usage: %s \n", argv[0]); exit(0); } thread_count = strtol(argv[1], NULL, 10); printf("How many elements?\n"); scanf("%d", &n); A = malloc(n*sizeof(int)); srandom(1); for (i = 0; i < n; i++) A[i] = random() % MAX; sum = Loop_sum(A, n); printf("loop sum = %lld\n", sum); return 0; } /* main */ long long Loop_sum(int A[], int n) { int i; long long sum = 0; int my_rank; /* This is going to cause a compile-time error */ # pragma omp parallel for num_threads(thread_count) \ reduction(+: sum) default(none) private(i, my_rank) \ shared(n, A) for (i = 0; i < n; i = f(i)) { my_rank = omp_get_thread_num(); printf("Thread %d > i = %d\n", my_rank, i); sum += A[i]; } return sum; } /* Loop_sum */ int f(int i) { return i + 1; } /* f */