Files
gcc-reflection/libgomp/testsuite/libgomp.c++/target-std__map-concurrent.C
Thomas Schwinge e9e76f7607 Fix up 'libgomp.c++/target-std__[...]-concurrent-usm.C' dynamic memory allocation
OpenMP/USM implies memory accessible from host as well as device, but doesn't
imply that allocation vs. deallocation may be done in the opposite context.
For most of the test cases, (by construction) we're not allocating memory
during device execution, so have nothing to clean up.  (..., but still document
these semantics.)  But for a few, we have to clean up:
'libgomp.c++/target-std__map-concurrent-usm.C',
'libgomp.c++/target-std__multimap-concurrent-usm.C',
'libgomp.c++/target-std__multiset-concurrent-usm.C',
'libgomp.c++/target-std__set-concurrent-usm.C'.

For 'libgomp.c++/target-std__multimap-concurrent-usm.C' (only), this issue
already got addressed in commit 90f2ab4b6e
"libgomp.c++/target-std__multimap-concurrent.C: Fix USM memory freeing".
However, instead of invoking the 'clear' function (which doesn't generally
guarantee to release dynamically allocated memory; for example, see PR123582
"C++ unordered associative container: dynamic memory management"), we properly
restore the respective object into pristine state.

	libgomp/
	* testsuite/libgomp.c++/target-std__array-concurrent-usm.C:
	'#define OMP_USM'.
	* testsuite/libgomp.c++/target-std__forward_list-concurrent-usm.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__list-concurrent-usm.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__span-concurrent-usm.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__map-concurrent-usm.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__multimap-concurrent-usm.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__multiset-concurrent-usm.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__set-concurrent-usm.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__valarray-concurrent-usm.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__vector-concurrent-usm.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__bitset-concurrent-usm.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__deque-concurrent-usm.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__array-concurrent.C: Comment.
	* testsuite/libgomp.c++/target-std__bitset-concurrent.C: Likewise.
	* testsuite/libgomp.c++/target-std__deque-concurrent.C: Likewise.
	* testsuite/libgomp.c++/target-std__forward_list-concurrent.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__list-concurrent.C: Likewise.
	* testsuite/libgomp.c++/target-std__span-concurrent.C: Likewise.
	* testsuite/libgomp.c++/target-std__valarray-concurrent.C:
	Likewise.
	* testsuite/libgomp.c++/target-std__vector-concurrent.C: Likewise.
	* testsuite/libgomp.c++/target-std__map-concurrent.C [OMP_USM]:
	Fix up dynamic memory allocation.
	* testsuite/libgomp.c++/target-std__multimap-concurrent.C
	[OMP_USM]: Likewise.
	* testsuite/libgomp.c++/target-std__multiset-concurrent.C
	[OMP_USM]: Likewise.
	* testsuite/libgomp.c++/target-std__set-concurrent.C [OMP_USM]:
	Likewise.
2026-01-14 16:00:56 +01:00

79 lines
1.7 KiB
C

// { dg-do run }
// { dg-additional-options -DMEM_SHARED { target offload_device_shared_as } }
#include <stdlib.h>
#include <time.h>
#include <set>
#include <map>
#define N 3000
void init (int data[], bool unique)
{
std::set<int> _set;
for (int i = 0; i < N; ++i)
{
// Avoid duplicates in data array if unique is true.
do
data[i] = rand ();
while (unique && _set.find (data[i]) != _set.end ());
_set.insert (data[i]);
}
}
bool validate (long long sum, int keys[], int data[])
{
long long total = 0;
for (int i = 0; i < N; ++i)
total += (long long) keys[i] * data[i];
return sum == total;
}
int main (void)
{
int keys[N], data[N];
std::map<int,int> _map;
srand (time (NULL));
init (keys, true);
init (data, false);
#ifndef MEM_SHARED
#pragma omp target enter data map (to: keys[ :N], data[ :N]) map (alloc: _map)
#endif
#pragma omp target
{
#ifndef MEM_SHARED
new (&_map) std::map<int,int> ();
#endif
for (int i = 0; i < N; ++i)
_map[keys[i]] = data[i];
}
long long sum = 0;
#pragma omp target teams distribute parallel for reduction (+:sum)
for (int i = 0; i < N; ++i)
sum += (long long) keys[i] * _map[keys[i]];
#ifdef OMP_USM
#pragma omp target
/* Restore the object into pristine state. In particular, deallocate
any memory allocated during device execution, which otherwise, back
on the host, we'd SIGSEGV on, when attempting to deallocate during
destruction of the object. */
__typeof__ (_map){}.swap (_map);
#endif
#ifndef MEM_SHARED
#pragma omp target
_map.~map ();
#endif
#ifndef MEM_SHARED
#pragma omp target exit data map (release: _map)
#endif
bool ok = validate (sum, keys, data);
return ok ? 0 : 1;
}