2727#include < csignal>
2828#include < cstdlib>
2929#include < iostream>
30+ #include < sstream>
3031#include < string>
3132#include < thread>
3233#include < vector>
3334
3435// Signal handler - handles signal without affecting execution
36+ namespace
37+ {
38+ int signal_received = 0 ;
3539void
3640signal_handler (int signum)
3741{
38- std::cout << " Attachment test process " << getpid () << " received signal " << signum << " \n " ;
42+ signal_received = signum;
3943}
44+ } // namespace
4045
4146/* Macro for checking GPU API return values */
4247#define HIP_ASSERT (call ) \
@@ -86,8 +91,13 @@ execute_kernels(const size_t tid, const size_t device_id)
8691 }
8792
8893 // Run kernels in a loop for a while
89- std::cout << " Starting kernel execution loop for thread " << tid << " on device " << device_id
90- << " ...\n " ;
94+ {
95+ // compose string first to avoid multithreaded handling of cout << operator
96+ std::stringstream msg;
97+ msg << " Starting kernel execution loop for thread " << tid << " on device " << device_id
98+ << " ...\n " ;
99+ std::cout << msg.str ();
100+ }
91101 const int num_iterations = 30 ;
92102
93103 for (int iter = 0 ; iter < num_iterations; ++iter)
@@ -143,8 +153,13 @@ execute_kernels(const size_t tid, const size_t device_id)
143153 std::this_thread::sleep_for (std::chrono::milliseconds (500 ));
144154 }
145155
146- std::cout << " Kernel execution loop completed for thread " << tid << " on device " << device_id
147- << " ...\n " ;
156+ {
157+ // compose string first to avoid multithreaded handling of cout << operator
158+ std::stringstream msg;
159+ msg << " Kernel execution loop completed for thread " << tid << " on device " << device_id
160+ << " ...\n " ;
161+ std::cout << msg.str ();
162+ }
148163
149164 HIP_ASSERT (hipStreamDestroy (stream));
150165 // Cleanup
@@ -191,7 +206,7 @@ main(int argc, char** argv)
191206 if (ndevices > device_count)
192207 {
193208 std::cout << " Using " << device_count << " HIP devices instead of the requested "
194- << ndevices << " \n " ;
209+ << ndevices << std::endl ;
195210 ndevices = device_count;
196211 }
197212
@@ -205,6 +220,11 @@ main(int argc, char** argv)
205220 for (auto & itr : _threads)
206221 itr.join ();
207222
223+ if (signal_received)
224+ {
225+ std::cout << " Attachment test process " << getpid () << " received signal "
226+ << signal_received << std::endl;
227+ }
208228 std::cout << " Attachment test app finished" << std::endl;
209229
210230 return 0 ;
0 commit comments