|
| 1 | +// Copyright 2024 Alfredo A. Correa |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// https://www.boost.org/LICENSE_1_0.txt |
| 4 | + |
| 5 | +#define BOOST_TEST_MODULE "C++ Unit Tests for Multi thrust::fill with async (stream) execution policy" |
| 6 | + |
| 7 | +#include <boost/multi/array.hpp> |
| 8 | +#include <boost/multi/adaptors/thrust.hpp> |
| 9 | + |
| 10 | +#include <thrust/fill.h> |
| 11 | +#include <thrust/system/cuda/execution_policy.h> // for thrust::cuda::par_nosync |
| 12 | + |
| 13 | +#include <boost/core/lightweight_test.hpp> |
| 14 | + |
| 15 | +namespace multi = boost::multi; |
| 16 | + |
| 17 | +auto main() -> int { // NOLINT(bugprone-exception-escape) |
| 18 | + cudaStream_t stream; // NOLINT(cppcoreguidelines-init-variables) |
| 19 | + BOOST_TEST( cudaStreamCreate(&stream) == cudaSuccess ); |
| 20 | + |
| 21 | + multi::thrust::cuda::array<double, 2> darr({1024, 1024}); |
| 22 | + |
| 23 | + // `par_nosync` is the asynchronous execution policy: the algorithm is |
| 24 | + // enqueued on `stream` and the call returns without synchronizing. |
| 25 | + thrust::fill( |
| 26 | + thrust::cuda::par_nosync.on(stream), |
| 27 | + darr.elements().begin(), darr.elements().end(), |
| 28 | + 42.0 |
| 29 | + ); |
| 30 | + |
| 31 | + // nothing is guaranteed complete until the stream is drained |
| 32 | + BOOST_TEST( cudaStreamSynchronize(stream) == cudaSuccess ); |
| 33 | + |
| 34 | + multi::array<double, 2> host = darr; // copy back to verify |
| 35 | + BOOST_TEST( host[0][0] == 42.0 ); |
| 36 | + BOOST_TEST( host[1023][1023] == 42.0 ); |
| 37 | + |
| 38 | + BOOST_TEST( cudaStreamDestroy(stream) == cudaSuccess ); |
| 39 | + |
| 40 | + return boost::report_errors(); |
| 41 | +} |
0 commit comments