| 1 | // RUN: %libomp-compile-and-run |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include "omp_testsuite.h" |
| 5 | #include "omp_my_sleep.h" |
| 6 | |
| 7 | #define CFSMAX_SIZE 1000 |
| 8 | #define MAX_TIME 0.01 |
| 9 | |
| 10 | #ifdef SLEEPTIME |
| 11 | #undef SLEEPTIME |
| 12 | #define SLEEPTIME 0.0005 |
| 13 | #endif |
| 14 | |
| 15 | int test_omp_for_schedule_static() |
| 16 | { |
| 17 | int threads; |
| 18 | int i,lasttid; |
| 19 | int * tids; |
| 20 | int notout; |
| 21 | int maxiter; |
| 22 | int chunk_size; |
| 23 | int counter = 0; |
| 24 | int tmp_count=1; |
| 25 | int lastthreadsstarttid = -1; |
| 26 | int result = 1; |
| 27 | |
| 28 | chunk_size = 7; |
| 29 | tids = (int *) malloc (size: sizeof (int) * (CFSMAX_SIZE + 1)); |
| 30 | notout = 1; |
| 31 | maxiter = 0; |
| 32 | |
| 33 | #pragma omp parallel shared(tids,counter) |
| 34 | { /* begin of parallel*/ |
| 35 | #pragma omp single |
| 36 | { |
| 37 | threads = omp_get_num_threads (); |
| 38 | } /* end of single */ |
| 39 | } /* end of parallel */ |
| 40 | |
| 41 | if (threads < 2) { |
| 42 | omp_set_num_threads(2); |
| 43 | threads = 2; |
| 44 | } |
| 45 | fprintf (stderr,format: "Using an internal count of %d\nUsing a specified" |
| 46 | " chunksize of %d\n" , CFSMAX_SIZE, chunk_size); |
| 47 | tids[CFSMAX_SIZE] = -1; /* setting endflag */ |
| 48 | #pragma omp parallel shared(tids) |
| 49 | { /* begin of parallel */ |
| 50 | double count; |
| 51 | int tid; |
| 52 | int j; |
| 53 | |
| 54 | tid = omp_get_thread_num (); |
| 55 | |
| 56 | #pragma omp for nowait schedule(static,chunk_size) |
| 57 | for(j = 0; j < CFSMAX_SIZE; ++j) { |
| 58 | count = 0.; |
| 59 | #pragma omp flush(maxiter) |
| 60 | if (j > maxiter) { |
| 61 | #pragma omp critical |
| 62 | { |
| 63 | maxiter = j; |
| 64 | } |
| 65 | } |
| 66 | /*printf ("thread %d sleeping\n", tid);*/ |
| 67 | while (notout && (count < MAX_TIME) && (maxiter == j)) { |
| 68 | #pragma omp flush(maxiter,notout) |
| 69 | my_sleep (SLEEPTIME); |
| 70 | count += SLEEPTIME; |
| 71 | printf(format: "." ); |
| 72 | } |
| 73 | #ifdef VERBOSE |
| 74 | if (count > 0.) printf(" waited %lf s\n" , count); |
| 75 | #endif |
| 76 | /*printf ("thread %d awake\n", tid);*/ |
| 77 | tids[j] = tid; |
| 78 | #ifdef VERBOSE |
| 79 | printf("%d finished by %d\n" ,j,tid); |
| 80 | #endif |
| 81 | } /* end of for */ |
| 82 | notout = 0; |
| 83 | #pragma omp flush(maxiter,notout) |
| 84 | } /* end of parallel */ |
| 85 | |
| 86 | /**** analysing the data in array tids ****/ |
| 87 | |
| 88 | lasttid = tids[0]; |
| 89 | tmp_count = 0; |
| 90 | |
| 91 | for (i = 0; i < CFSMAX_SIZE + 1; ++i) { |
| 92 | /* If the work was done by the same thread increase tmp_count by one. */ |
| 93 | if (tids[i] == lasttid) { |
| 94 | tmp_count++; |
| 95 | #ifdef VERBOSE |
| 96 | fprintf (stderr, "%d: %d \n" , i, tids[i]); |
| 97 | #endif |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | /* Check if the next thread had has the right thread number. When finding |
| 102 | * threadnumber -1 the end should be reached. |
| 103 | */ |
| 104 | if (tids[i] == (lasttid + 1) % threads || tids[i] == -1) { |
| 105 | /* checking for the right chunk size */ |
| 106 | if (tmp_count == chunk_size) { |
| 107 | tmp_count = 1; |
| 108 | lasttid = tids[i]; |
| 109 | #ifdef VERBOSE |
| 110 | fprintf (stderr, "OK\n" ); |
| 111 | #endif |
| 112 | } else { |
| 113 | /* If the chunk size was wrong, check if the end was reached */ |
| 114 | if (tids[i] == -1) { |
| 115 | if (i == CFSMAX_SIZE) { |
| 116 | fprintf (stderr, format: "Last thread had chunk size %d\n" , |
| 117 | tmp_count); |
| 118 | break; |
| 119 | } else { |
| 120 | fprintf (stderr, format: "ERROR: Last thread (thread with" |
| 121 | " number -1) was found before the end.\n" ); |
| 122 | result = 0; |
| 123 | } |
| 124 | } else { |
| 125 | fprintf (stderr, format: "ERROR: chunk size was %d. (assigned" |
| 126 | " was %d)\n" , tmp_count, chunk_size); |
| 127 | result = 0; |
| 128 | } |
| 129 | } |
| 130 | } else { |
| 131 | fprintf(stderr, format: "ERROR: Found thread with number %d (should be" |
| 132 | " inbetween 0 and %d)." , tids[i], threads - 1); |
| 133 | result = 0; |
| 134 | } |
| 135 | #ifdef VERBOSE |
| 136 | fprintf (stderr, "%d: %d \n" , i, tids[i]); |
| 137 | #endif |
| 138 | } |
| 139 | |
| 140 | return result; |
| 141 | } |
| 142 | |
| 143 | int main() |
| 144 | { |
| 145 | int i; |
| 146 | int num_failed=0; |
| 147 | |
| 148 | for(i = 0; i < REPETITIONS; i++) { |
| 149 | if(!test_omp_for_schedule_static()) { |
| 150 | num_failed++; |
| 151 | } |
| 152 | } |
| 153 | return num_failed; |
| 154 | } |
| 155 | |