Perl-OpenMP/p5-OpenMP-Simple

Auto insert `getenv` for specified envars via Inline::OpenMP

Closed this issue · 1 comments

The second order of business is define via Inline::OpenMP the '%ENV` to re-read whenever a defined method is called:

  • OMP_NUM_THREADS

Example code that I want to DWIM:

use OpenMP::Environment ();
use Inline::C (
    OpenMP   => 'DATA',
    with           => q{Alien::OpenMP},
    'with-env'    => [qw/OMP_NUM_THREADS/],
);

my $oenv = OpenMP::Environment->new;

for my $i (qw/1 2 4 8 16 32 64 128/) {
    $oenv->omp_num_threads($i);
    test;
}
__DATA__
__OpenMP__
#include <stdio.h>

void test(void) {
  #pragma omp parallel
  {
    if (0 == omp_get_thread_num())
      printf("'%d' should be '%d'\n", omp_get_num_threads(), num_threads); 
  }
}

The above achieves 2 things,

  1. #include <omp.h> is not explicitly required, in fact it's dynamic based on what compilers are supported by Alien::OpenMP
  2. %ENV keys specified with with-env are reread each time the defined function is called (via insertion)

this works via C macro in the latest master