Search code examples
copenmp

OpenMP 5.0 Memory allocation


I am trying to use an example from the OpenMP specification on page 64:

#include <stdio.h>
#include <omp.h>

int basic_default2(int n)
{
  const int success=1, failure=0;
  int retval;
  omp_memspace_t *my_mspace;
  omp_allocator_t *my_allocator;
  double *buffer;

  my_mspace = omp_init_memspace(&omp_default_memtraits);
  my_allocator = omp_init_allocator(my_mspace, &omp_default_alloctraits);
  buffer = omp_alloc(n * sizeof(*buffer), my_allocator);

  if ( buffer == NULL ){
    fprintf("Could not allocate space using default traits\n");
    retval = failure;
  }else{
    do_work(buffer, n);
    omp_free(buffer, my_allocator);
    retval = success;
  }

  omp_destroy_allocator(my_allocator);
  omp_destroy_mspace(my_mspace);
}

According to the OpenMp 5.0 requirements this should work. I am using GNU 9.1.0 compiler and the -fopenmp flag.

Unfortunately, the type omp_allocator_t is unknown by the compiler.


Solution

  • You are using a 2017 Technical Report, not the OpenMP 5.0 standard, and the interface has evolved since the TR (which does say "Expires January 4th, 2018" on the cover!)

    In the standard, the return type from omp_init_allocator is omp_allocator_handle_t.

    Note, too, that many of the other names have also changed, so your code needs other changes as well (for instance there is no omp_init_memspace call any longer).

    The allocation section of the standard is here.