smedegaard / hip-rs

A rust wrapper for HIP
Apache License 2.0
1 stars 0 forks source link

MemoryPointer::new() not using default device #102

Open smedegaard opened 4 days ago

smedegaard commented 4 days ago

when creating a memory allocation with MemoryPointer::new() , the call to sys::hipMalloc should allocate on the default device. The observation is that even if hipGetDevice() returns 0, the memory was allocated on another device.

https://rocm.docs.amd.com/projects/HIP/en/latest/reference/hip_runtime_api/modules/memory_management.html#_CPPv49hipMallocPPv6size_t

https://rocm.docs.amd.com/projects/HIP/en/latest/reference/hip_runtime_api/modules/device_management.html#group___device_1ga7e0e2e8c5f78e3c7449764657c254e0a

smedegaard commented 4 days ago

This behavior comes from the HIP C++ lib. Tested with the following

#include <hip/hip_runtime.h>
#include <iostream>
#include <chrono>
#include <thread>

int main() {
   int device;
   hipError_t err = hipGetDevice(&device);
   if (err != hipSuccess) {
       std::cerr << "Failed to get device: " << hipGetErrorString(err) << std::endl;
       return 1;
   }
   std::cout << "Using device: " << device << std::endl;

   void* ptr = nullptr;
   size_t size = 1024 * 1024 * 1024; // 1GB

   err = hipMalloc(&ptr, size);
   if (err != hipSuccess) {
       std::cerr << "Failed to allocate memory: " << hipGetErrorString(err) << std::endl;
       return 1;
   }

   std::cout << "Allocated 1GB of device memory at address: " << ptr << std::endl;
   std::this_thread::sleep_for(std::chrono::seconds(5));

   err = hipFree(ptr);
   if (err != hipSuccess) {
       std::cerr << "Failed to free memory: " << hipGetErrorString(err) << std::endl;
       return 1;
   }

   std::cout << "Freed memory\n";
   return 0;
}