]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fiber/examples/hip/multiple_streams.cpp
2 // Copyright Oliver Kowalke 2017.
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
14 #include <hip/hip_runtime.h>
16 #include <boost/assert.hpp>
17 #include <boost/bind.hpp>
18 #include <boost/intrusive_ptr.hpp>
20 #include <boost/fiber/all.hpp>
21 #include <boost/fiber/hip/waitfor.hpp>
24 void vector_add(hipLaunchParm lp
, int * a
, int * b
, int * c
, int size
) {
25 int idx
= threadIdx
.x
+ blockIdx
.x
* blockDim
.x
;
27 c
[idx
] = a
[idx
] + b
[idx
];
34 boost::fibers::fiber
f1( [&done
]{
35 std::cout
<< "f1: entered" << std::endl
;
37 hipStream_t stream0
, stream1
;
38 hipStreamCreate( & stream0
);
39 hipStreamCreate( & stream1
);
40 int size
= 1024 * 1024;
41 int full_size
= 20 * size
;
42 int * host_a
, * host_b
, * host_c
;
43 hipHostMalloc( & host_a
, full_size
* sizeof( int), hipHostMallocDefault
);
44 hipHostMalloc( & host_b
, full_size
* sizeof( int), hipHostMallocDefault
);
45 hipHostMalloc( & host_c
, full_size
* sizeof( int), hipHostMallocDefault
);
46 int * dev_a0
, * dev_b0
, * dev_c0
;
47 int * dev_a1
, * dev_b1
, * dev_c1
;
48 hipMalloc( & dev_a0
, size
* sizeof( int) );
49 hipMalloc( & dev_b0
, size
* sizeof( int) );
50 hipMalloc( & dev_c0
, size
* sizeof( int) );
51 hipMalloc( & dev_a1
, size
* sizeof( int) );
52 hipMalloc( & dev_b1
, size
* sizeof( int) );
53 hipMalloc( & dev_c1
, size
* sizeof( int) );
54 std::minstd_rand generator
;
55 std::uniform_int_distribution
<> distribution(1, 6);
56 for ( int i
= 0; i
< full_size
; ++i
) {
57 host_a
[i
] = distribution( generator
);
58 host_b
[i
] = distribution( generator
);
60 for ( int i
= 0; i
< full_size
; i
+= 2 * size
) {
61 hipMemcpyAsync( dev_a0
, host_a
+ i
, size
* sizeof( int), hipMemcpyHostToDevice
, stream0
);
62 hipMemcpyAsync( dev_a1
, host_a
+ i
+ size
, size
* sizeof( int), hipMemcpyHostToDevice
, stream1
);
63 hipMemcpyAsync( dev_b0
, host_b
+ i
, size
* sizeof( int), hipMemcpyHostToDevice
, stream0
);
64 hipMemcpyAsync( dev_b1
, host_b
+ i
+ size
, size
* sizeof( int), hipMemcpyHostToDevice
, stream1
);
65 hipLaunchKernel( vector_add
, dim3(size
/ 256), dim3(256), 0, stream0
, dev_a0
, dev_b0
, dev_c0
, size
);
66 hipLaunchKernel( vector_add
, dim3(size
/ 256), dim3(256), 0, stream1
, dev_a1
, dev_b1
, dev_c1
, size
);
67 hipMemcpyAsync( host_c
+ i
, dev_c0
, size
* sizeof( int), hipMemcpyDeviceToHost
, stream0
);
68 hipMemcpyAsync( host_c
+ i
+ size
, dev_c1
, size
* sizeof( int), hipMemcpyDeviceToHost
, stream1
);
70 auto results
= boost::fibers::hip::waitfor_all( stream0
, stream1
);
71 for ( auto & result
: results
) {
72 BOOST_ASSERT( stream0
== std::get
< 0 >( result
) || stream1
== std::get
< 0 >( result
) );
73 BOOST_ASSERT( hipSuccess
== std::get
< 1 >( result
) );
75 std::cout
<< "f1: GPU computation finished" << std::endl
;
85 hipStreamDestroy( stream0
);
86 hipStreamDestroy( stream1
);
88 } catch ( std::exception
const& ex
) {
89 std::cerr
<< "exception: " << ex
.what() << std::endl
;
91 std::cout
<< "f1: leaving" << std::endl
;
93 boost::fibers::fiber
f2([&done
]{
94 std::cout
<< "f2: entered" << std::endl
;
96 std::cout
<< "f2: sleeping" << std::endl
;
97 boost::this_fiber::sleep_for( std::chrono::milliseconds( 1 ) );
99 std::cout
<< "f2: leaving" << std::endl
;
103 std::cout
<< "done." << std::endl
;
105 } catch ( std::exception
const& e
) {
106 std::cerr
<< "exception: " << e
.what() << std::endl
;
108 std::cerr
<< "unhandled exception" << std::endl
;