Files
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

74 lines
2.5 KiB
C++

//===-- lib/openmp/omp_util.cpp ----------------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Implementation of PointerDeviceMap -- a thread-safe pointer-to-device-ID
// map used by the OpenMP allocator runtime to track allocation origins.
//
//===----------------------------------------------------------------------===//
#include "flang/Runtime/OpenMP/omp_util.h"
#include "flang-rt/runtime/lock.h"
#include "flang-rt/runtime/terminator.h"
#include <cstdlib>
#include <cstring>
namespace Fortran::runtime::omp {
static constexpr std::size_t initialCapacity{256};
static Lock pointerDeviceMapLock;
/// Double the capacity of the entries array (or set it to initialCapacity if
/// empty). Crashes on allocation failure. Must be called under the lock.
void PointerDeviceMap::grow() {
std::size_t newCapacity = capacity_ ? capacity_ * 2 : initialCapacity;
Entry *newEntries =
static_cast<Entry *>(std::realloc(entries_, newCapacity * sizeof(Entry)));
if (!newEntries) {
Terminator{__FILE__, __LINE__}.Crash(
"PointerDeviceMap: realloc failed (capacity %zu)", newCapacity);
}
entries_ = newEntries;
capacity_ = newCapacity;
}
/// Record that \p pointer was allocated on \p device. Thread-safe.
void PointerDeviceMap::insert(void *pointer, int device) {
CriticalSection guard(pointerDeviceMapLock);
if (count_ == capacity_) {
grow();
}
entries_[count_++] = {pointer, device};
}
/// Remove \p pointer from the map and return its device ID, or -1 if not
/// found. Uses swap-with-last for O(1) removal. Thread-safe.
int PointerDeviceMap::removeAndGet(void *pointer) {
CriticalSection guard(pointerDeviceMapLock);
for (std::size_t i = 0; i < count_; ++i) {
if (entries_[i].pointer == pointer) {
int device = entries_[i].device;
// Swap with last entry and shrink.
entries_[i] = entries_[--count_];
return device;
}
}
return -1;
}
/// Print all (pointer, device) entries to stderr. Thread-safe.
/// Can be used for debugging purposes.
void PointerDeviceMap::dump() const {
CriticalSection guard(pointerDeviceMapLock);
for (std::size_t i = 0; i < count_; ++i) {
std::fprintf(stderr, "%p -> %d\n", entries_[i].pointer, entries_[i].device);
}
}
} // namespace Fortran::runtime::omp