99
1010#include < iostream>
1111#include < string>
12+ #include < thread>
1213
1314using namespace device ;
1415
@@ -42,24 +43,21 @@ void ConcreteAPI::initDevices() {
4243 return compare (c1->queueBuffer .getDefaultQueue ().get_device (), c2->queueBuffer .getDefaultQueue ().get_device ());
4344 });
4445
45- this ->setDevice (this -> currentDeviceId );
46+ this ->setDevice (0 );
4647 this ->deviceInitialized = true ;
4748}
4849
4950void ConcreteAPI::setDevice (int id) {
50-
5151 if (id < 0 || id >= this ->getNumDevices ()) {
5252 throw std::out_of_range{" Device index out of range" };
5353 }
5454
55- this ->currentDeviceId = id;
56- auto *next = this ->availableDevices [id];
57- this ->currentStatistics = &next->statistics ;
58- this ->currentQueueBuffer = &next->queueBuffer ;
59- this ->currentDefaultQueue = &this ->currentQueueBuffer ->getDefaultQueue ();
60- this ->currentMemoryToSizeMap = &next->memoryToSizeMap ;
55+ if (deviceMap.empty ()) {
56+ // only print the first time
57+ printer.printInfo () << " Switched to device: " << this ->getDeviceName (id) << " by index " << id;
58+ }
6159
62- printer. printInfo () << " Switched to device: " << this -> getDeviceName (id) << " by index " << id;
60+ deviceMap[ std::this_thread::get_id ()] = id;
6361}
6462
6563void ConcreteAPI::initialize () {}
@@ -77,10 +75,7 @@ void ConcreteAPI::finalize() {
7775
7876 this ->graphs .clear ();
7977
80- this ->currentStatistics = nullptr ;
81- this ->currentQueueBuffer = nullptr ;
82- this ->currentDefaultQueue = nullptr ;
83- this ->currentMemoryToSizeMap = nullptr ;
78+ this ->deviceMap .clear ();
8479
8580 this ->m_isFinalized = true ;
8681 this ->deviceInitialized = false ;
@@ -92,15 +87,19 @@ int ConcreteAPI::getDeviceId() {
9287 if (!deviceInitialized) {
9388 logError () << " Device has not been selected. Please, select device before requesting device Id" ;
9489 }
95- return currentDeviceId;
90+ const auto myId = std::this_thread::get_id ()
91+ if (deviceMap.find (myId) == deviceMap.end ()) {
92+ logError () << " Thread device context not initialized. Error." ;
93+ }
94+ return deviceMap[myId];
9695}
9796
9897unsigned int ConcreteAPI::getGlobMemAlignment () {
99- auto device = this ->currentDefaultQueue ->get_device ();
98+ auto device = this ->currentQueueBuffer ()-> getDefaultQueue () ->get_device ();
10099 return 128 ; // ToDo: find attribute; not: device.get_info<info::device::mem_base_addr_align>();
101100}
102101
103- void ConcreteAPI::syncDevice () { this ->currentQueueBuffer ->syncAllQueuesWithHost (); }
102+ void ConcreteAPI::syncDevice () { this ->currentQueueBuffer () ->syncAllQueuesWithHost (); }
104103
105104std::string ConcreteAPI::getDeviceInfoAsText (int id) {
106105 if (id < 0 || id >= this ->getNumDevices ())
@@ -109,7 +108,7 @@ std::string ConcreteAPI::getDeviceInfoAsText(int id) {
109108 auto device = this ->availableDevices [id]->queueBuffer .getDefaultQueue ().get_device ();
110109 return this ->getDeviceInfoAsTextInternal (device);
111110}
112- std::string ConcreteAPI::getCurrentDeviceInfoAsText () { return this ->getDeviceInfoAsText (this -> currentDeviceId ); }
111+ std::string ConcreteAPI::getCurrentDeviceInfoAsText () { return this ->getDeviceInfoAsText (getDeviceId () ); }
113112
114113std::string ConcreteAPI::getDeviceInfoAsTextInternal (sycl::device& dev) {
115114 std::ostringstream info{};
@@ -126,7 +125,7 @@ std::string ConcreteAPI::getDeviceInfoAsTextInternal(sycl::device& dev) {
126125
127126bool ConcreteAPI::isUnifiedMemoryDefault () {
128127 // suboptimal (i.e. we'd need to query if USM needs to be migrated or not), but there's probably nothing better for now
129- auto device = this ->availableDevices [this -> currentDeviceId ]->queueBuffer .getDefaultQueue ().get_device ();
128+ auto device = this ->availableDevices [getDeviceId () ]->queueBuffer .getDefaultQueue ().get_device ();
130129 return device.has (sycl::aspect::usm_system_allocations);
131130}
132131
0 commit comments