brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.9 KiB · 4c540a8 Raw
67 lines · cpp
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-exception6//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 uint32_t entropy() {18  return (static_cast<uint32_t>(gpu::processor_clock()) ^19          (gpu::get_thread_id_x() * 0x632be59b) ^20          (gpu::get_block_id_x() * 0x85157af5)) *21         0x9e3779bb;22}23 24static inline uint32_t xorshift32(uint32_t &state) {25  state ^= state << 13;26  state ^= state >> 17;27  state ^= state << 5;28  return state * 0x9e3779bb;29}30 31static inline void use(uint8_t *ptr, uint32_t size) {32  EXPECT_NE(ptr, nullptr);33  for (int i = 0; i < size; ++i)34    ptr[i] = uint8_t(i + gpu::get_thread_id());35 36  // Try to detect if some other thread manages to clobber our memory.37  for (int i = 0; i < size; ++i)38    EXPECT_EQ(ptr[i], uint8_t(i + gpu::get_thread_id()));39}40 41TEST_MAIN(int, char **, char **) {42  void *ptrs[256];43  for (int i = 0; i < 256; ++i)44    ptrs[i] = malloc(gpu::get_lane_id() % 2 ? 16 : 32);45 46  for (int i = 0; i < 256; ++i)47    use(reinterpret_cast<uint8_t *>(ptrs[i]), gpu::get_lane_id() % 2 ? 16 : 32);48 49  for (int i = 0; i < 256; ++i)50    free(ptrs[i]);51 52  uint32_t state = entropy();53  for (int i = 0; i < 1024; ++i) {54    if (xorshift32(state) % 2) {55      uint64_t size = xorshift32(state) % 256 + 16;56      uint64_t *ptr = reinterpret_cast<uint64_t *>(malloc(size));57      *ptr = gpu::get_thread_id();58 59      EXPECT_EQ(*ptr, gpu::get_thread_id());60      ASSERT_TRUE(ptr);61      ASSERT_TRUE(__builtin_is_aligned(ptr, 16));62      free(ptr);63    }64  }65  return 0;66}67