@@ -38,19 +38,33 @@ 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) return ;
56+
57+ auto & map = context->memoryToSizeMap ;
58+
59+ if (map.find (devPtr) == map.end ()){
60+ return ; // the std::throw is throwing some errors during the program finalization
61+ }
62+
63+ context->statistics .deallocatedMemBytes += map.at (devPtr);
64+ map.erase (devPtr);
65+ auto & queue = context->queueBuffer .getDefaultQueue ();
66+ sycl::free (devPtr, queue.get_context ());
67+ queue.wait ();
5468}
5569
5670void ConcreteAPI::freeGlobMem (void * devPtr) {
0 commit comments