brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.2 KiB · d2845d5 Raw
146 lines · c
1// RUN: %libomp-compile && env OMP_MAX_TASK_PRIORITY='2' %libomp-run2 3// Test OMP 4.5 task priorities4// Higher priority task supposed to be executed before lower priority task.5 6#include <stdio.h>7#include <omp.h>8 9#include "omp_my_sleep.h"10// delay(n) - sleep n ms11#define delay(n) my_sleep(((double)n)/1000.0)12 13int main ( void ) {14  int passed;15  passed = (omp_get_max_task_priority() == 2);16  printf("Got %d max priority via env\n", omp_get_max_task_priority());17  if(!passed) {18    printf( "failed\n" );19    return 1;20  }21  printf("parallel 1 spawns 4 tasks for primary thread to execute\n");22  #pragma omp parallel num_threads(2)23  {24    int th = omp_get_thread_num();25    if (th == 0) // primary thread26    {27      #pragma omp task priority(1)28      { // middle priority29        int val, t = omp_get_thread_num();30        #pragma omp atomic capture31          val = passed++;32        printf("P1:    val = %d, thread gen %d, thread exe %d\n", val, th, t);33        delay(10); // sleep 10 ms34      }35      #pragma omp task priority(2)36      { // high priority37        int val, t = omp_get_thread_num();38        #pragma omp atomic capture39          val = passed++;40        printf("P2:    val = %d, thread gen %d, thread exe %d\n", val, th, t);41        delay(20); // sleep 20 ms42      }43      #pragma omp task priority(0)44      { // low priority specified explicitly45        int val, t = omp_get_thread_num();46        #pragma omp atomic capture47          val = passed++;48        printf("P0exp: val = %d, thread gen %d, thread exe %d\n", val, th, t);49        delay(1); // sleep 1 ms50      }51      #pragma omp task52      { // low priority by default53        int val, t = omp_get_thread_num();54        #pragma omp atomic capture55          val = passed++;56        printf("P0imp: val = %d, thread gen %d, thread exe %d\n", val, th, t);57        delay(1); // sleep 1 ms58      }59    } else {60      // wait for the primary thread to finish all tasks61      int wait = 0;62      do {63        delay(5);64        #pragma omp atomic read65          wait = passed;66      } while (wait < 5);67    }68  }69  printf("parallel 2 spawns 4 tasks for worker thread to execute\n");70  #pragma omp parallel num_threads(2)71  {72    int th = omp_get_thread_num();73    if (th == 0) // primary thread74    {75      #pragma omp task priority(1)76      { // middle priority77        int val, t = omp_get_thread_num();78        #pragma omp atomic capture79          val = passed++;80        printf("P1:    val = %d, thread gen %d, thread exe %d\n", val, th, t);81        delay(10); // sleep 10 ms82      }83      #pragma omp task priority(2)84      { // high priority85        int val, t = omp_get_thread_num();86        #pragma omp atomic capture87          val = passed++;88        printf("P2:    val = %d, thread gen %d, thread exe %d\n", val, th, t);89        delay(20); // sleep 20 ms90      }91      #pragma omp task priority(0)92      { // low priority specified explicitly93        int val, t = omp_get_thread_num();94        #pragma omp atomic capture95          val = passed++;96        printf("P0exp: val = %d, thread gen %d, thread exe %d\n", val, th, t);97        delay(1); // sleep 1 ms98      }99      #pragma omp task100      { // low priority by default101        int val, t = omp_get_thread_num();102        #pragma omp atomic capture103          val = passed++;104        printf("P0imp: val = %d, thread gen %d, thread exe %d\n", val, th, t);105        delay(1); // sleep 1 ms106      }107      // signal creation of all tasks: passed = 5 + 1 = 6108      #pragma omp atomic109        passed++;110      // wait for completion of all 4 tasks111      int wait = 0;112      do {113        delay(5);114        #pragma omp atomic read115          wait = passed;116      } while (wait < 10); // passed = 6 + 4 = 10117    } else {118      // wait for the primary thread to create all tasks119      int wait = 0;120      do {121        delay(5);122        #pragma omp atomic read123          wait = passed;124      } while (wait < 6);125      // go execute 4 tasks created by primary thread126    }127  }128  if (passed != 10) {129    printf("failed, passed = %d (should be 10)\n", passed);130    return 1;131  }132  printf("passed\n");133  return 0;134}135// CHECK: parallel 1136// CHECK-NEXT: P2137// CHECK-NEXT: P1138// CHECK-NEXT: P0139// CHECK-NEXT: P0140// CHECK-NEXT: parallel 2141// CHECK-NEXT: P2142// CHECK-NEXT: P1143// CHECK-NEXT: P0144// CHECK-NEXT: P0145// CHECK: passed146