<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - omp cancel parallel not working as expected"
   href="https://bugs.llvm.org/show_bug.cgi?id=50129">50129</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>omp cancel parallel not working as expected
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>OpenMP
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>michael.p.rice@intel.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following test shows 'omp cancel' is not working as expected.  Run with:

OMP_CANCELLATION=true  ./a.out

It should quickly print:

Cancel happened as you would expect!

Clang will not cancel.

#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>

// Run with environment variable: OMP_CANCELLATION=true

int main(int argc, char **argv) {
  int* array;
  int nthreads, i, j;

  #pragma omp parallel
  #pragma omp single nowait
  nthreads = omp_get_num_threads();

  array = (int*)malloc(sizeof(int)*nthreads);
  for (i = 0; i < nthreads; ++i)
    array[i] = 0;

  assert(omp_get_cancellation() && "Need to set OMP_CANCELLATION=true");

  #pragma omp parallel
  {
    int tid = omp_get_thread_num();
    if (tid == 0) {
      // Master signals other threads that cancellation is in order
      // This thread will go to end of parallel right here
      // not at the cancellation point below
      #pragma omp cancel parallel
    }
    // Have worker threads sleep here for "long enough"
    usleep(1000000);
    #pragma omp barrier
    array[tid] = 1;
  }

  int errors = 0;
  for (i = 0; i < nthreads; ++i) {
    if (array[i] != 0) {
      printf("Thread %d made it through unexpectedly!\n", i);
      errors++;
    }
  }

  if (errors) {
    printf("%d errors occurred!\n", errors);
    exit(1);
  }

  printf("Cancel happened as you would expect!\n");

  return 0;
}</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>