gcc/libgomp/testsuite/libgomp.c-c++-common/alloc-1.c
Jakub Jelinek 800bcc8c00 openmp: Add basic library allocator support.
This patch adds very basic allocator support (omp_{init,destroy}_allocator,
omp_{alloc,free}, omp_[sg]et_default_allocator).
The plan is to use memkind (likely dlopened) for high bandwidth memory, but
that part isn't implemented yet, probably mlock for pinned memory and see
what other options there are for other kinds of memory.
For offloading targets, we need to decide if we want to support the
dynamic allocators (and on which targets), or if e.g. all we do is at compile
time replace omp_alloc/omp_free calls with constexpr predefined allocators
with something special.

And allocate directive and allocator/uses_allocators clauses are future work
too.

2020-05-19  Jakub Jelinek  <jakub@redhat.com>

	* omp.h.in (omp_uintptr_t): New typedef.
	(__GOMP_UINTPTR_T_ENUM): Define.
	(omp_memspace_handle_t, omp_allocator_handle_t, omp_alloctrait_key_t,
	omp_alloctrait_value_t, omp_alloctrait_t): New typedefs.
	(__GOMP_DEFAULT_NULL_ALLOCATOR): Define.
	(omp_init_allocator, omp_destroy_allocator, omp_set_default_allocator,
	omp_get_default_allocator, omp_alloc, omp_free): Declare.
	* libgomp.h (struct gomp_team_state): Add def_allocator field.
	(gomp_def_allocator): Declare.
	* libgomp.map (OMP_5.0.1): Export omp_set_default_allocator,
	omp_get_default_allocator, omp_init_allocator, omp_destroy_allocator,
	omp_alloc and omp_free.
	* team.c (gomp_team_start): Copy over ts.def_allocator.
	* env.c (gomp_def_allocator): New variable.
	(parse_wait_policy): Adjust function comment.
	(parse_allocator): New function.
	(handle_omp_display_env): Print OMP_ALLOCATOR.
	(initialize_env): Call parse_allocator.
	* Makefile.am (libgomp_la_SOURCES): Add allocator.c.
	* allocator.c: New file.
	* icv.c (omp_set_default_allocator, omp_get_default_allocator): New
	functions.
	* testsuite/libgomp.c-c++-common/alloc-1.c: New test.
	* testsuite/libgomp.c-c++-common/alloc-2.c: New test.
	* testsuite/libgomp.c-c++-common/alloc-3.c: New test.
	* Makefile.in: Regenerated.
2020-05-19 10:11:01 +02:00

158 lines
4.3 KiB
C

#include <omp.h>
#include <stdint.h>
#include <stdlib.h>
const omp_alloctrait_t traits2[]
= { { omp_atk_alignment, 16 },
{ omp_atk_sync_hint, omp_atv_default },
{ omp_atk_access, omp_atv_default },
{ omp_atk_pool_size, 1024 },
{ omp_atk_fallback, omp_atv_default_mem_fb },
{ omp_atk_partition, omp_atv_environment } };
omp_alloctrait_t traits3[]
= { { omp_atk_sync_hint, omp_atv_uncontended },
{ omp_atk_alignment, 32 },
{ omp_atk_access, omp_atv_all },
{ omp_atk_pool_size, 512 },
{ omp_atk_fallback, omp_atv_allocator_fb },
{ omp_atk_fb_data, 0 },
{ omp_atk_partition, omp_atv_default } };
const omp_alloctrait_t traits4[]
= { { omp_atk_alignment, 128 },
{ omp_atk_pool_size, 1024 },
{ omp_atk_fallback, omp_atv_null_fb } };
int
main ()
{
int *volatile p = (int *) omp_alloc (3 * sizeof (int), omp_default_mem_alloc);
int *volatile q;
int *volatile r;
omp_alloctrait_t traits[3]
= { { omp_atk_alignment, 64 },
{ omp_atk_fallback, omp_atv_null_fb },
{ omp_atk_pool_size, 4096 } };
omp_allocator_handle_t a, a2;
if ((((uintptr_t) p) % __alignof (int)) != 0)
abort ();
p[0] = 1;
p[1] = 2;
p[2] = 3;
omp_free (p, omp_default_mem_alloc);
p = (int *) omp_alloc (2 * sizeof (int), omp_default_mem_alloc);
if ((((uintptr_t) p) % __alignof (int)) != 0)
abort ();
p[0] = 1;
p[1] = 2;
omp_free (p, omp_null_allocator);
omp_set_default_allocator (omp_default_mem_alloc);
p = (int *) omp_alloc (sizeof (int), omp_null_allocator);
if ((((uintptr_t) p) % __alignof (int)) != 0)
abort ();
p[0] = 3;
omp_free (p, omp_get_default_allocator ());
a = omp_init_allocator (omp_default_mem_space, 3, traits);
if (a == omp_null_allocator)
abort ();
p = (int *) omp_alloc (3072, a);
if ((((uintptr_t) p) % 64) != 0)
abort ();
p[0] = 1;
p[3071 / sizeof (int)] = 2;
if (omp_alloc (3072, a) != NULL)
abort ();
omp_free (p, a);
p = (int *) omp_alloc (3072, a);
p[0] = 3;
p[3071 / sizeof (int)] = 4;
omp_free (p, omp_null_allocator);
omp_set_default_allocator (a);
if (omp_get_default_allocator () != a)
abort ();
p = (int *) omp_alloc (3072, omp_null_allocator);
if (omp_alloc (3072, omp_null_allocator) != NULL)
abort ();
omp_free (p, a);
omp_destroy_allocator (a);
a = omp_init_allocator (omp_default_mem_space,
sizeof (traits2) / sizeof (traits2[0]),
traits2);
if (a == omp_null_allocator)
abort ();
if (traits3[5].key != omp_atk_fb_data)
abort ();
traits3[5].value = (uintptr_t) a;
a2 = omp_init_allocator (omp_default_mem_space,
sizeof (traits3) / sizeof (traits3[0]),
traits3);
if (a2 == omp_null_allocator)
abort ();
p = (int *) omp_alloc (420, a2);
if ((((uintptr_t) p) % 32) != 0)
abort ();
p[0] = 5;
p[419 / sizeof (int)] = 6;
q = (int *) omp_alloc (768, a2);
if ((((uintptr_t) q) % 16) != 0)
abort ();
q[0] = 7;
q[767 / sizeof (int)] = 8;
r = (int *) omp_alloc (512, a2);
if ((((uintptr_t) r) % __alignof (int)) != 0)
abort ();
r[0] = 9;
r[511 / sizeof (int)] = 10;
omp_free (p, omp_null_allocator);
omp_free (q, a2);
omp_free (r, omp_null_allocator);
omp_destroy_allocator (a2);
omp_destroy_allocator (a);
a = omp_init_allocator (omp_default_mem_space,
sizeof (traits4) / sizeof (traits4[0]),
traits4);
if (a == omp_null_allocator)
abort ();
if (traits3[5].key != omp_atk_fb_data)
abort ();
traits3[5].value = (uintptr_t) a;
a2 = omp_init_allocator (omp_default_mem_space,
sizeof (traits3) / sizeof (traits3[0]),
traits3);
if (a2 == omp_null_allocator)
abort ();
omp_set_default_allocator (a2);
#ifdef __cplusplus
p = static_cast <int *> (omp_alloc (420));
#else
p = (int *) omp_alloc (420, omp_null_allocator);
#endif
if ((((uintptr_t) p) % 32) != 0)
abort ();
p[0] = 5;
p[419 / sizeof (int)] = 6;
q = (int *) omp_alloc (768, omp_null_allocator);
if ((((uintptr_t) q) % 128) != 0)
abort ();
q[0] = 7;
q[767 / sizeof (int)] = 8;
if (omp_alloc (768, omp_null_allocator) != NULL)
abort ();
#ifdef __cplusplus
omp_free (p);
omp_free (q);
omp_free (NULL);
#else
omp_free (p, omp_null_allocator);
omp_free (q, omp_null_allocator);
omp_free (NULL, omp_null_allocator);
#endif
omp_free (NULL, omp_null_allocator);
omp_destroy_allocator (a2);
omp_destroy_allocator (a);
return 0;
}