memory leak, multi-thread, af::matmul

41 views
Skip to first unread message

Johnny

unread,
Jan 13, 2025, 12:57:27 AMJan 13
to ArrayFire Users
Hi arrayfire

I found a situation of memory leak. Arrayfire version 3.9.0
When I run af::matmul in the thread, gpu memory is continuously increasing.
I tested it as a form of mainthread processing but memory leak didn't happened.

I'm using Visual Studio 2022, RTX 6000 Ada Generation.
Thank you

arrayfire matmul memory leak.png

#include <thread>

#include <arrayfire.h>

auto MatMul() -> void {
  float h_A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  float h_B[] = {1, 2, 3};

  const af::array A(3, 3, h_A);
  const af::array B(3, 1, h_B);

  af::array X1 = af::matmul(A, B);
}

int main() {
  af::info();

  for (auto i = 0; i < 5000; ++i) {
    auto thread1 = std::thread(MatMul);
    thread1.join();
    af::deviceGC();
  }

  return 0;
}

Francisco José Solís

unread,
Feb 8, 2025, 10:10:02 PMFeb 8
to ArrayFire Users
I could not find a way to reproduce the current inconvenience.
Currently, considering my RAM restrictions I was able to build and run the application by using a 1000 iterations loop.

If my investigation is correct, ArrayFire operates with a lazy evaluation model, meaning operations are queued rather than executed immediately.
In a multithreaded context as yours, without synchronization, memory may not be freed promptly because the computation might still be in progress when the thread terminates.
Would you kindly consider to use af::sync(); as the last line of your MatMul function? I could not give a firm answer, as I could not reproduce the inconvenience under my computer, even if compiling the same application with 1000 iterations.

Hello Johnny!
```
auto MatMul() -> void {
  float h_A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
  float h_B[] = {1, 2, 3};

  const af::array A(3, 3, h_A);
  const af::array B(3, 1, h_B);

  af::array X1 = af::matmul(A, B);
  af::sync();
}
```
Reply all
Reply to author
Forward
0 new messages