Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 50 additions & 14 deletions src/include/migraphx/simple_par_for.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved.
* Copyright (c) 2015-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
Expand All @@ -27,6 +27,8 @@
#include <thread>
#include <cmath>
#include <algorithm>
#include <exception>
#include <mutex>
Comment on lines +30 to +31
#include <vector>
#include <cassert>

Expand Down Expand Up @@ -62,15 +64,37 @@ auto thread_invoke(std::size_t i, std::size_t, F f) -> decltype(f(i))
f(i);
}

// Runs f over [start, last), capturing the first exception thrown
template <class F>
void simple_par_for_impl(std::size_t n, std::size_t threadsize, F f)
void thread_invoke_range(std::size_t start,
std::size_t last,
std::size_t tid,
F f,
std::exception_ptr& eptr,
std::mutex& eptr_mutex)
{
if(threadsize <= 1)
try
{
for(std::size_t i = 0; i < n; i++)
thread_invoke(i, 0, f);
for(std::size_t i = start; i < last; i++)
{
thread_invoke(i, tid, f);
}
}
else
catch(...)
{
std::lock_guard<std::mutex> lock(eptr_mutex);
if(eptr == nullptr)
eptr = std::current_exception();
}
}

// Runs f over [0, n) on the given number of threads. An exception thrown on a
// worker thread is captured and returned after all threads are joined.
template <class F>
std::exception_ptr simple_par_for_threads(std::size_t n, std::size_t threadsize, F f)
{
std::exception_ptr eptr;
std::mutex eptr_mutex;
{
std::vector<joinable_thread> threads(threadsize);
// Using const here causes gcc 5 to ICE
Expand All @@ -81,21 +105,33 @@ void simple_par_for_impl(std::size_t n, std::size_t threadsize, F f)

std::size_t work = 0;
std::size_t tid = 0;
std::generate(threads.begin(), threads.end(), [=, &work, &tid] {
auto result = joinable_thread([=] {
std::size_t start = work;
std::size_t last = std::min(n, work + grainsize);
for(std::size_t i = start; i < last; i++)
{
thread_invoke(i, tid, f);
}
std::generate(threads.begin(), threads.end(), [=, &work, &tid, &eptr, &eptr_mutex] {
auto result = joinable_thread([=, &eptr, &eptr_mutex] {
thread_invoke_range(work, std::min(n, work + grainsize), tid, f, eptr, eptr_mutex);
});
work += grainsize;
++tid;
return result;
});
assert(work >= n);
}
return eptr;
}

template <class F>
void simple_par_for_impl(std::size_t n, std::size_t threadsize, F f)
{
if(threadsize <= 1)
{
for(std::size_t i = 0; i < n; i++)
thread_invoke(i, 0, f);
}
else
{
auto eptr = simple_par_for_threads(n, threadsize, f);
if(eptr)
std::rethrow_exception(eptr);
}
}

template <class F>
Expand Down
63 changes: 63 additions & 0 deletions test/simple_par_for.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-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.
*/
#include <migraphx/par_for.hpp>
#include <migraphx/errors.hpp>
#include <algorithm>
#include <atomic>
#include <vector>

#include <test.hpp>

TEST_CASE(par_for_runs_all)
{
std::vector<int> data(64, 0);
migraphx::par_for(data.size(), 1, [&](std::size_t i) { data[i] = 1; });
EXPECT(std::all_of(data.begin(), data.end(), [](int x) { return x == 1; }));
}

TEST_CASE(par_for_exception_propagates)
{
EXPECT(test::throws<migraphx::exception>(
[] {
migraphx::par_for(64, 1, [](std::size_t i) {
if(i % 2 == 0)
MIGRAPHX_THROW("par_for_error");
});
Comment on lines +43 to +46
},
"par_for_error"));
}

TEST_CASE(par_for_exception_propagates_serial)
{
EXPECT(test::throws<migraphx::exception>(
[] {
migraphx::par_for(4, 100, [](std::size_t i) {
if(i == 2)
MIGRAPHX_THROW("par_for_error");
});
},
"par_for_error"));
}

int main(int argc, const char* argv[]) { test::run(argc, argv); }
Loading