Files
TheRock/rocm-systems/projects/rocprofiler-sdk/tests/bin/openmp/host/openmp-host.cpp
T
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

124 lines
4.0 KiB
C++

// MIT License
//
// Copyright (c) 2023-2026 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// Host-only OpenMP workload used to exercise the CPU-side OMPT callbacks that
// rocprofv3 --ompt-trace exposes (parallel regions, worksharing, explicit
// tasks, and synchronization). It deliberately contains *no* target-offload
// regions so that it can run on any machine with an OMPT-capable OpenMP
// runtime, regardless of whether a GPU/target-offload toolchain is present.
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <string>
#include <string_view>
#include <vector>
// Host-side worksharing + reduction. Drives:
// omp_parallel_begin / omp_parallel_end, omp_implicit_task, omp_work,
// omp_sync_region (implicit barrier) and omp_sync_region_wait.
static int64_t
parallel_reduction(const std::vector<int>& data)
{
int64_t total = 0;
#pragma omp parallel for reduction(+ : total)
for(std::size_t i = 0; i < data.size(); ++i)
{
total += data[i];
}
return total;
}
// Explicit tasking + taskwait. Drives:
// omp_task_create, omp_task_schedule and an omp_sync_region (taskwait).
static int64_t
task_sum(const std::vector<int>& data)
{
int64_t total = 0;
#pragma omp parallel
{
#pragma omp single
{
const std::size_t chunks = 4;
const std::size_t span = (data.size() + chunks - 1) / chunks;
for(std::size_t c = 0; c < chunks; ++c)
{
const std::size_t begin = c * span;
const std::size_t end = std::min(begin + span, data.size());
#pragma omp task firstprivate(begin, end) shared(total)
{
int64_t local = 0;
for(std::size_t i = begin; i < end; ++i)
local += data[i];
// critical section -> omp_mutex_acquire / omp_mutex_acquired / omp_mutex_released
#pragma omp critical
total += local;
}
}
#pragma omp taskwait
}
}
return total;
}
int
main(int argc, char** argv)
{
const char* exe_name = argv[0];
uint64_t nitr = 4;
for(int i = 1; i < argc; ++i)
{
auto _arg = std::string_view{argv[i]};
if(_arg == "?" || _arg == "-h" || _arg == "--help")
{
fprintf(stderr, "usage: %s [NUM_ITERATION (%lu)]\n", exe_name, nitr);
return 0;
}
}
if(argc > 1) nitr = std::stoul(argv[1]);
constexpr int N = 100000;
std::vector<int> data(N);
for(int i = 0; i < N; ++i)
data[i] = i + 1;
const int64_t expected = static_cast<int64_t>(N) * (N + 1) / 2;
int n_errors = 0;
for(uint64_t it = 0; it < nitr; ++it)
{
if(parallel_reduction(data) != expected) ++n_errors;
if(task_sum(data) != expected) ++n_errors;
}
if(n_errors == 0)
{
printf("Success\n");
return 0;
}
printf("Total %d failures\n", n_errors);
printf("Fail\n");
return 1;
}