unique_ptr > memAlloc( unique_ptr > memAlloc(
| 60 | // unique_ptr<cl::Buffer, function<void(cl::Buffer *)>> memAlloc( |
| 61 | // unique_ptr<int, function<void(int *)>> memAlloc( |
| 62 | std::unique_ptr<sycl::buffer<T>, std::function<void(sycl::buffer<T> *)>> |
| 63 | memAlloc(const size_t &elements) { |
| 64 | if (elements) { |
| 65 | dim4 dims(elements); |
| 66 | |
| 67 | // The alloc function returns a pointer to a buffer<std::byte> object. |
| 68 | // We need to reinterpret that object into buffer<T> while keeping the |
| 69 | // same pointer value for memory accounting purposes. We acheive this |
| 70 | // assigning the renterpreted buffer back into the original pointer. |
| 71 | // This would delete the buffer<std::byte> object and replace it with |
| 72 | // the buffer<T> object. We do the reverse in the memFree function |
| 73 | auto *ptr = static_cast<sycl::buffer<std::byte> *>( |
| 74 | memoryManager().alloc(false, 1, dims.get(), sizeof(T))); |
| 75 | sycl::buffer<T> *optr = static_cast<sycl::buffer<T> *>((void *)ptr); |
| 76 | size_t bytes = ptr->byte_size(); |
| 77 | |
| 78 | // TODO(umar): This could be a DANGEROUS function becasue we are calling |
| 79 | // delete on the reniterpreted buffer<T> instead of the orignal |
| 80 | // buffer<byte> object |
| 81 | *optr = ptr->template reinterpret<T>(sycl::range(bytes / sizeof(T))); |
| 82 | return unique_ptr<sycl::buffer<T>, function<void(sycl::buffer<T> *)>>( |
| 83 | optr, memFree<T>); |
| 84 | } else { |
| 85 | return unique_ptr<sycl::buffer<T>, function<void(sycl::buffer<T> *)>>( |
| 86 | nullptr, memFree<T>); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void *memAllocUser(const size_t &bytes) { |
| 91 | dim4 dims(bytes); |