brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · caff254 Raw
105 lines · c
1// RUN: %libomp-compile-and-run2#include <stdio.h>3#include "omp_testsuite.h"4 5/*6 * This test will hang if the nowait is not working properly7 *8 * It relies on a thread skipping to the second sections construct to9 * release the threads in the first sections construct10 *11 * Also, since scheduling of sections is implementation defined, it is12 * necessary to have all four sections in the second sections construct13 * release the threads since we can't guarantee which section a single thread14 * will execute.15 */16volatile int release;17volatile int count;18 19void wait_for_release_then_increment(int rank)20{21  fprintf(stderr, "Thread nr %d enters first section"22    " and waits.\n", rank);23  while (release == 0);24  #pragma omp atomic25  count++;26}27 28void release_and_increment(int rank)29{30  fprintf(stderr, "Thread nr %d sets release to 1\n", rank);31  release = 1;32  #pragma omp flush(release)33  #pragma omp atomic34  count++;35}36 37int test_omp_sections_nowait()38{39  release = 0;40  count = 0;41 42  #pragma omp parallel num_threads(4)43  {44    int rank;45    rank = omp_get_thread_num ();46    #pragma omp sections nowait47    {48      #pragma omp section49      {50        wait_for_release_then_increment(rank);51      }52      #pragma omp section53      {54        wait_for_release_then_increment(rank);55      }56      #pragma omp section57      {58        wait_for_release_then_increment(rank);59      }60      #pragma omp section61      {62        fprintf(stderr, "Thread nr %d enters first sections and goes "63          "immediately to next sections construct to release.\n", rank);64        #pragma omp atomic65        count++;66      }67    }68    /* Begin of second sections environment */69    #pragma omp sections70    {71      #pragma omp section72      {73        release_and_increment(rank);74      }75      #pragma omp section76      {77        release_and_increment(rank);78      }79      #pragma omp section80      {81        release_and_increment(rank);82      }83      #pragma omp section84      {85        release_and_increment(rank);86      }87    }88  }89  // Check to make sure all eight sections were executed90  return (count==8);91}92 93int main()94{95  int i;96  int num_failed=0;97 98  for(i = 0; i < REPETITIONS; i++) {99    if(!test_omp_sections_nowait()) {100      num_failed++;101    }102  }103  return num_failed;104}105