1//===-- Test for parallel GPU malloc interface ----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "test/IntegrationTest/test.h"
10
11#include "src/__support/GPU/utils.h"
12#include "src/stdlib/free.h"
13#include "src/stdlib/malloc.h"
14
15using namespace LIBC_NAMESPACE;
16
17static inline void use(uint8_t *ptr, uint32_t size) {
18 EXPECT_NE(ptr, nullptr);
19 for (int i = 0; i < size; ++i)
20 ptr[i] = uint8_t(i + gpu::get_thread_id());
21
22 // Try to detect if some other thread manages to clobber our memory.
23 for (int i = 0; i < size; ++i)
24 EXPECT_EQ(ptr[i], uint8_t(i + gpu::get_thread_id()));
25}
26
27TEST_MAIN(int, char **, char **) {
28 void *ptrs[256];
29 for (int i = 0; i < 256; ++i)
30 ptrs[i] = malloc(gpu::get_lane_id() % 2 ? 16 : 32);
31
32 for (int i = 0; i < 256; ++i)
33 use(reinterpret_cast<uint8_t *>(ptrs[i]), gpu::get_lane_id() % 2 ? 16 : 32);
34
35 for (int i = 0; i < 256; ++i)
36 free(ptrs[i]);
37 return 0;
38}
39

source code of libc/test/integration/src/stdlib/gpu/malloc_stress.cpp