66#include " utils/env.h"
77#include < iostream>
88#include < iomanip>
9+ #include < mutex>
910#include < sstream>
1011#include < string>
12+ #include < thread>
1113#include " hip/hip_runtime.h"
1214
1315#ifdef PROFILING_ENABLED
1921
2022using namespace device ;
2123
24+ namespace {
25+ #ifdef DEVICE_CONTEXT_GLOBAL
26+ int currentDeviceId = 0 ;
27+ #else
28+ thread_local int currentDeviceId = 0 ;
29+ #endif
30+ }
31+
2232ConcreteAPI::ConcreteAPI () {
2333 hipInit (0 );
2434 CHECK_ERR;
2535 status[StatusID::DriverApiInitialized] = true ;
2636}
2737
2838void ConcreteAPI::setDevice (int deviceId) {
39+
2940 currentDeviceId = deviceId;
30- hipSetDevice (currentDeviceId);
41+
42+ hipSetDevice (deviceId);
3143 CHECK_ERR;
3244
3345 // Note: the following sets the initial HIP context
3446 hipFree (nullptr );
3547 CHECK_ERR;
3648
37- hipDeviceProp_t properties{};
38- hipGetDeviceProperties (&properties, currentDeviceId);
39- CHECK_ERR;
40-
41- // NOTE: hipDeviceGetAttribute internally calls hipGetDeviceProperties; hence it doesn't make sense to use it here
42-
43- if constexpr (HIP_VERSION >= 60200000 ) {
44- // cf. https://rocm.docs.amd.com/en/docs-6.2.0/about/release-notes.html
45- // (before 6.2.0, the flag hipDeviceAttributePageableMemoryAccessUsesHostPageTables had effectively the same effect)
46- // (cf. https://github.com/ROCm/clr/commit/7d5b4a8f7a7d34f008d65277f8aae4c98a6da375#diff-596cd550f7fdef76b39f1b7b179b20128313dd9cc9ec662b2eae562efa2b7f33L405 )
47- usmDefault = properties.integrated != 0 ;
48- }
49- else {
50- usmDefault = properties.directManagedMemAccessFromHost != 0 && properties.pageableMemoryAccessUsesHostPageTables != 0 ;
51- }
52-
53- hipDeviceGetStreamPriorityRange (&priorityMin, &priorityMax);
54- CHECK_ERR;
55-
5649 status[StatusID::DeviceSelected] = true ;
5750}
5851
@@ -68,7 +61,25 @@ void ConcreteAPI::initialize() {
6861 if (!status[StatusID::InterfaceInitialized]) {
6962 status[StatusID::InterfaceInitialized] = true ;
7063 hipStreamCreateWithFlags (&defaultStream, hipStreamNonBlocking); CHECK_ERR;
71- hipEventCreate (&defaultStreamEvent); CHECK_ERR;
64+
65+ hipDeviceProp_t properties{};
66+ hipGetDeviceProperties (&properties, getDeviceId ());
67+ CHECK_ERR;
68+
69+ // NOTE: hipDeviceGetAttribute internally calls hipGetDeviceProperties; hence it doesn't make sense to use it here
70+
71+ if constexpr (HIP_VERSION >= 60200000 ) {
72+ // cf. https://rocm.docs.amd.com/en/docs-6.2.0/about/release-notes.html
73+ // (before 6.2.0, the flag hipDeviceAttributePageableMemoryAccessUsesHostPageTables had effectively the same effect)
74+ // (cf. https://github.com/ROCm/clr/commit/7d5b4a8f7a7d34f008d65277f8aae4c98a6da375#diff-596cd550f7fdef76b39f1b7b179b20128313dd9cc9ec662b2eae562efa2b7f33L405 )
75+ usmDefault = properties.integrated != 0 ;
76+ }
77+ else {
78+ usmDefault = properties.directManagedMemAccessFromHost != 0 && properties.pageableMemoryAccessUsesHostPageTables != 0 ;
79+ }
80+
81+ hipDeviceGetStreamPriorityRange (&priorityMin, &priorityMax);
82+ CHECK_ERR;
7283 }
7384 else {
7485 logWarning () << " Device Interface has already been initialized" ;
@@ -78,9 +89,8 @@ void ConcreteAPI::initialize() {
7889void ConcreteAPI::finalize () {
7990 if (status[StatusID::InterfaceInitialized]) {
8091 hipStreamDestroy (defaultStream); CHECK_ERR;
81- hipEventDestroy (defaultStreamEvent); CHECK_ERR;
8292 if (!genericStreams.empty ()) {
83- printer. printInfo () << " DEVICE::WARNING:" << genericStreams.size ()
93+ logInfo () << " DEVICE::WARNING:" << genericStreams.size ()
8494 << " device generic stream(s) were not deleted." ;
8595 for (auto stream : genericStreams) {
8696 hipStreamDestroy (stream); CHECK_ERR;
@@ -189,6 +199,6 @@ void ConcreteAPI::popLastProfilingMark() {
189199}
190200
191201void ConcreteAPI::setupPrinting (int rank) {
192- printer. setRank (rank);
202+
193203}
194204
0 commit comments