intel / llvm

Intel staging area for llvm.org contribution. Home for Intel LLVM-based projects.
Other
1.22k stars 731 forks source link

[SYCL] Enqueuing USM memory free #6350

Open AidanBeltonS opened 2 years ago

AidanBeltonS commented 2 years ago

In cases where a user wants to create an asynchronous function which creates some USM there is currently no good way to release the memory. It would be useful to have a method to enqueue the release of USM depending on an event. A possible solution could be something along the lines of a queue member function: sycl::event queue::free(void *ptr, const std::vector<event> &depEvents);.

Currently, a user could enqueue freeing USM with a host_task. However, I think this does not lineup with how people expect to use the host_task or sycl::free.

It could also argue malloc should also have an asychronous call. However this may be more complicated then enqueuing freeing memory. As a pointer would have to be returned to the user, prior to the actual pointer being known.

Options:

Example use case, for enqueued free.

sycl::event Foo(sycl::queue &queue, float *a) {

  // Create scratch usm
  float *scratch = int *ipiv32 =
      (float *)malloc_device(sizeof(float) * size, queue);

  // Do work on a and scratch
  sycl::event event = queue.submit([&](sycl::handler &cgh) {
    cgh.parallel_for(sycl::range<1>{size}, [=](sycl::id<1> index) {
      // Do work
    });
  });

  // Can free memory using `host_task`, to make asynchronous.
  // This should work but is quite verbose, and goes against expected language
  // syntax
  sycl::event final_event = queue.submit([&](sycl::handler &cgh) {
    cgh.depends_on(event);
    cgh.host_task([=](sycl::interop_handle ih) { sycl::free(scratch, queue); });
  });

  // Desired
  sycl::event final_event = queue.free(scratch, e);

  return final_event
}
AidanBeltonS commented 2 years ago

@jbrodman