@@ -38,19 +38,34 @@ void* ConcreteAPI::allocPinnedMem(size_t size, bool compress, Destination hint)
3838void ConcreteAPI::freeMem (void * devPtr) {
3939 // NOTE: Freeing nullptr results in segfault in oneAPI. It is an opposite behaviour
4040 // contrast to C++/CUDA/HIP
41- if (devPtr != nullptr && this ->currentMemoryToSizeMap != nullptr ) {
42- if (this ->currentMemoryToSizeMap ().find (devPtr) == this ->currentMemoryToSizeMap ().end ()) {
43- throw std::invalid_argument (
44- this ->getDeviceInfoAsText (getDeviceId ())
45- .append (" an attempt to delete memory that has not been allocated. Is this "
46- " a pointer to this device or was this a double free?" ));
47- }
48-
49- this ->currentStatistics ().deallocatedMemBytes += this ->currentMemoryToSizeMap ().at (devPtr);
50- this ->currentMemoryToSizeMap ().erase (devPtr);
51- free (devPtr, this ->currentDefaultQueue ().get_context ());
52- waitCheck (currentDefaultQueue ());
41+ if (devPtr == nullptr ) {
42+ return ;
5343 }
44+
45+ if (!this ->deviceInitialized ){
46+ return ;
47+ }
48+
49+ if (this ->availableDevices .empty ()){
50+ return ;
51+ }
52+
53+ // Use the first device context to free memory
54+ DeviceContext* context = this ->availableDevices [0 ];
55+ if (!context)
56+ return ;
57+
58+ auto & map = context->memoryToSizeMap ;
59+
60+ if (map.find (devPtr) == map.end ()){
61+ return ; // the std::throw is throwing some errors during the program finalization
62+ }
63+
64+ context->statistics .deallocatedMemBytes += map.at (devPtr);
65+ map.erase (devPtr);
66+ auto & queue = context->queueBuffer .getDefaultQueue ();
67+ sycl::free (devPtr, queue.get_context ());
68+ queue.wait ();
5469}
5570
5671void ConcreteAPI::freeGlobMem (void * devPtr) {
0 commit comments