Skip to content

Commit

Permalink
Merge branch 'blender-v4.4-release'
Browse files Browse the repository at this point in the history
  • Loading branch information
brechtvl committed Feb 13, 2025
2 parents aaef10e + c87a269 commit b487bcd
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 110 deletions.
8 changes: 4 additions & 4 deletions intern/cycles/device/cuda/device_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,7 @@ void CUDADevice::free_device(void *device_pointer)
cuda_assert(cuMemFree((CUdeviceptr)device_pointer));
}

bool CUDADevice::alloc_host(void *&shared_pointer, const size_t size)
bool CUDADevice::shared_alloc(void *&shared_pointer, const size_t size)
{
CUDAContextScope scope(this);

Expand All @@ -546,14 +546,14 @@ bool CUDADevice::alloc_host(void *&shared_pointer, const size_t size)
return mem_alloc_result == CUDA_SUCCESS;
}

void CUDADevice::free_host(void *shared_pointer)
void CUDADevice::shared_free(void *shared_pointer)
{
CUDAContextScope scope(this);

cuMemFreeHost(shared_pointer);
}

void *CUDADevice::transform_host_to_device_pointer(const void *shared_pointer)
void *CUDADevice::shared_to_device_pointer(const void *shared_pointer)
{
CUDAContextScope scope(this);
void *device_pointer = nullptr;
Expand Down Expand Up @@ -646,7 +646,7 @@ void CUDADevice::mem_zero(device_memory &mem)
return;
}

if (!(mem.is_host_mapped(this) && mem.host_pointer == mem.shared_pointer)) {
if (!(mem.is_shared(this) && mem.host_pointer == mem.shared_pointer)) {
const CUDAContextScope scope(this);
cuda_assert(cuMemsetD8((CUdeviceptr)mem.device_pointer, 0, mem.memory_size()));
}
Expand Down
33 changes: 17 additions & 16 deletions intern/cycles/device/cuda/device_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,42 +59,43 @@ class CUDADevice : public GPUDevice {
bool force_ptx = false);

bool load_kernels(const uint kernel_features) override;

void reserve_local_memory(const uint kernel_features);

void get_device_memory_info(size_t &total, size_t &free) override;
bool alloc_device(void *&device_pointer, const size_t size) override;
void free_device(void *device_pointer) override;
bool alloc_host(void *&shared_pointer, const size_t size) override;
void free_host(void *shared_pointer) override;
void *transform_host_to_device_pointer(const void *shared_pointer) override;
void copy_host_to_device(void *device_pointer, void *host_pointer, const size_t size) override;

/* All memory types. */
void mem_alloc(device_memory &mem) override;

void mem_copy_to(device_memory &mem) override;

void mem_move_to_host(device_memory &mem) override;

void mem_copy_from(
device_memory &mem, const size_t y, size_t w, const size_t h, size_t elem) override;

void mem_zero(device_memory &mem) override;

void mem_free(device_memory &mem) override;

device_ptr mem_alloc_sub_ptr(device_memory &mem, const size_t offset, size_t /*size*/) override;

void const_copy_to(const char *name, void *host, const size_t size) override;

/* Global memory. */
void global_alloc(device_memory &mem);
void global_copy_to(device_memory &mem);
void global_free(device_memory &mem);

/* Texture memory. */
void tex_alloc(device_texture &mem);
void tex_copy_to(device_texture &mem);
void tex_free(device_texture &mem);

/* Device side memory. */
void get_device_memory_info(size_t &total, size_t &free) override;
bool alloc_device(void *&device_pointer, const size_t size) override;
void free_device(void *device_pointer) override;

/* Shared memory. */
bool shared_alloc(void *&shared_pointer, const size_t size) override;
void shared_free(void *shared_pointer) override;
void *shared_to_device_pointer(const void *shared_pointer) override;

/* Memory copy. */
void copy_host_to_device(void *device_pointer, void *host_pointer, const size_t size) override;
void const_copy_to(const char *name, void *host, const size_t size) override;

bool should_use_graphics_interop() override;

unique_ptr<DeviceQueue> gpu_queue_create() override;
Expand Down
32 changes: 21 additions & 11 deletions intern/cycles/device/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,16 @@ OSLGlobals *Device::get_cpu_osl_memory()
return nullptr;
}

void *Device::host_alloc(const MemoryType /*type*/, const size_t size)
{
return util_aligned_malloc(size, MIN_ALIGNMENT_CPU_DATA_TYPES);
}

void Device::host_free(const MemoryType /*type*/, void *host_pointer, const size_t size)
{
util_aligned_free(host_pointer, size);
}

GPUDevice::~GPUDevice() noexcept(false) = default;

bool GPUDevice::load_texture_info()
Expand Down Expand Up @@ -572,7 +582,7 @@ void GPUDevice::move_textures_to_host(size_t size, const size_t headroom, const

/* Can only move textures allocated on this device (and not those from peer devices).
* And need to ignore memory that is already on the host. */
if (!mem.is_resident(this) || mem.is_host_mapped(this)) {
if (!mem.is_resident(this) || mem.is_shared(this)) {
continue;
}

Expand Down Expand Up @@ -677,14 +687,14 @@ GPUDevice::Mem *GPUDevice::generic_alloc(device_memory &mem, const size_t pitch_
}
else if (map_host_used + size < map_host_limit) {
/* Allocate host memory ourselves. */
mem_alloc_result = alloc_host(shared_pointer, size);
mem_alloc_result = shared_alloc(shared_pointer, size);

assert((mem_alloc_result && shared_pointer != nullptr) ||
(!mem_alloc_result && shared_pointer == nullptr));
}

if (mem_alloc_result) {
device_pointer = transform_host_to_device_pointer(shared_pointer);
device_pointer = shared_to_device_pointer(shared_pointer);
map_host_used += size;
status = " in host memory";
}
Expand Down Expand Up @@ -728,7 +738,7 @@ GPUDevice::Mem *GPUDevice::generic_alloc(device_memory &mem, const size_t pitch_
mem.host_pointer != shared_pointer)
{
memcpy(shared_pointer, mem.host_pointer, size);
util_aligned_free(mem.host_pointer, mem.memory_size());
host_free(mem.type, mem.host_pointer, mem.memory_size());
mem.host_pointer = shared_pointer;
}
mem.shared_pointer = shared_pointer;
Expand All @@ -752,7 +762,7 @@ void GPUDevice::generic_free(device_memory &mem)
DCHECK(device_mem_map.find(&mem) != device_mem_map.end());

/* For host mapped memory, reference counting is used to safely free it. */
if (mem.is_host_mapped(this)) {
if (mem.is_shared(this)) {
assert(mem.shared_counter > 0);
if (--mem.shared_counter == 0) {
if (mem.host_pointer == mem.shared_pointer) {
Expand All @@ -764,7 +774,7 @@ void GPUDevice::generic_free(device_memory &mem)
mem.host_pointer = mem.host_alloc(size);
memcpy(mem.host_pointer, mem.shared_pointer, size);
}
free_host(mem.shared_pointer);
shared_free(mem.shared_pointer);
mem.shared_pointer = nullptr;
}
map_host_used -= mem.device_size;
Expand All @@ -791,17 +801,17 @@ void GPUDevice::generic_copy_to(device_memory &mem)
/* If not host mapped, the current device only uses device memory allocated by backend
* device allocation regardless of mem.host_pointer and mem.shared_pointer, and should
* copy data from mem.host_pointer. */
if (!(mem.is_host_mapped(this) && mem.host_pointer == mem.shared_pointer)) {
if (!(mem.is_shared(this) && mem.host_pointer == mem.shared_pointer)) {
copy_host_to_device((void *)mem.device_pointer, mem.host_pointer, mem.memory_size());
}
}

bool GPUDevice::is_host_mapped(const void *shared_pointer,
const device_ptr device_pointer,
Device * /*sub_device*/)
bool GPUDevice::is_shared(const void *shared_pointer,
const device_ptr device_pointer,
Device * /*sub_device*/)
{
return (shared_pointer && device_pointer &&
(device_ptr)transform_host_to_device_pointer(shared_pointer) == device_pointer);
(device_ptr)shared_to_device_pointer(shared_pointer) == device_pointer);
}

/* DeviceInfo */
Expand Down
30 changes: 16 additions & 14 deletions intern/cycles/device/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ class Device {
return false;
}

virtual bool is_host_mapped(const void * /*shared_pointer*/,
const device_ptr /*device_pointer*/,
Device * /*sub_device*/)
virtual bool is_shared(const void * /*shared_pointer*/,
const device_ptr /*device_pointer*/,
Device * /*sub_device*/)
{
return false;
}
Expand Down Expand Up @@ -320,6 +320,9 @@ class Device {
friend class DeviceServer;
friend class device_memory;

virtual void *host_alloc(const MemoryType type, const size_t size);
virtual void host_free(const MemoryType type, void *host_pointer, const size_t size);

virtual void mem_alloc(device_memory &mem) = 0;
virtual void mem_copy_to(device_memory &mem) = 0;
virtual void mem_move_to_host(device_memory &mem) = 0;
Expand Down Expand Up @@ -398,22 +401,21 @@ class GPUDevice : public Device {
/* total - amount of device memory, free - amount of available device memory */
virtual void get_device_memory_info(size_t &total, size_t &free) = 0;

/* Device side memory. */
virtual bool alloc_device(void *&device_pointer, const size_t size) = 0;

virtual void free_device(void *device_pointer) = 0;

virtual bool alloc_host(void *&shared_pointer, const size_t size) = 0;

virtual void free_host(void *shared_pointer) = 0;

bool is_host_mapped(const void *shared_pointer,
const device_ptr device_pointer,
Device *sub_device) override;

/* Shared memory. */
virtual bool shared_alloc(void *&shared_pointer, const size_t size) = 0;
virtual void shared_free(void *shared_pointer) = 0;
bool is_shared(const void *shared_pointer,
const device_ptr device_pointer,
Device *sub_device) override;
/* This function should return device pointer corresponding to shared pointer, which
* is host buffer, allocated in `alloc_host`. */
virtual void *transform_host_to_device_pointer(const void *shared_pointer) = 0;
* is host buffer, allocated in `shared_alloc`. */
virtual void *shared_to_device_pointer(const void *shared_pointer) = 0;

/* Memory copy. */
virtual void copy_host_to_device(void *device_pointer,
void *host_pointer,
const size_t size) = 0;
Expand Down
8 changes: 4 additions & 4 deletions intern/cycles/device/hip/device_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ void HIPDevice::free_device(void *device_pointer)
hip_assert(hipFree((hipDeviceptr_t)device_pointer));
}

bool HIPDevice::alloc_host(void *&shared_pointer, const size_t size)
bool HIPDevice::shared_alloc(void *&shared_pointer, const size_t size)
{
HIPContextScope scope(this);

Expand All @@ -508,14 +508,14 @@ bool HIPDevice::alloc_host(void *&shared_pointer, const size_t size)
return mem_alloc_result == hipSuccess;
}

void HIPDevice::free_host(void *shared_pointer)
void HIPDevice::shared_free(void *shared_pointer)
{
HIPContextScope scope(this);

hipHostFree(shared_pointer);
}

void *HIPDevice::transform_host_to_device_pointer(const void *shared_pointer)
void *HIPDevice::shared_to_device_pointer(const void *shared_pointer)
{
HIPContextScope scope(this);
void *device_pointer = nullptr;
Expand Down Expand Up @@ -608,7 +608,7 @@ void HIPDevice::mem_zero(device_memory &mem)
return;
}

if (!(mem.is_host_mapped(this) && mem.host_pointer == mem.shared_pointer)) {
if (!(mem.is_shared(this) && mem.host_pointer == mem.shared_pointer)) {
const HIPContextScope scope(this);
hip_assert(hipMemsetD8((hipDeviceptr_t)mem.device_pointer, 0, mem.memory_size()));
}
Expand Down
32 changes: 17 additions & 15 deletions intern/cycles/device/hip/device_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,41 @@ class HIPDevice : public GPUDevice {
bool load_kernels(const uint kernel_features) override;
void reserve_local_memory(const uint kernel_features);

void get_device_memory_info(size_t &total, size_t &free) override;
bool alloc_device(void *&device_pointer, const size_t size) override;
void free_device(void *device_pointer) override;
bool alloc_host(void *&shared_pointer, const size_t size) override;
void free_host(void *shared_pointer) override;
void *transform_host_to_device_pointer(const void *shared_pointer) override;
void copy_host_to_device(void *device_pointer, void *host_pointer, const size_t size) override;

/* All memory types. */
void mem_alloc(device_memory &mem) override;

void mem_copy_to(device_memory &mem) override;

void mem_move_to_host(device_memory &mem) override;

void mem_copy_from(
device_memory &mem, const size_t y, size_t w, const size_t h, size_t elem) override;

void mem_zero(device_memory &mem) override;

void mem_free(device_memory &mem) override;

device_ptr mem_alloc_sub_ptr(device_memory &mem, const size_t offset, size_t /*size*/) override;

void const_copy_to(const char *name, void *host, const size_t size) override;

/* Global memory. */
void global_alloc(device_memory &mem);
void global_copy_to(device_memory &mem);
void global_free(device_memory &mem);

/* Texture memory. */
void tex_alloc(device_texture &mem);
void tex_copy_to(device_texture &mem);
void tex_free(device_texture &mem);

/* Device side memory. */
void get_device_memory_info(size_t &total, size_t &free) override;
bool alloc_device(void *&device_pointer, const size_t size) override;
void free_device(void *device_pointer) override;

/* Shared memory. */
bool shared_alloc(void *&shared_pointer, const size_t size) override;
void shared_free(void *shared_pointer) override;
void *shared_to_device_pointer(const void *shared_pointer) override;

/* Memory copy. */
void copy_host_to_device(void *device_pointer, void *host_pointer, const size_t size) override;
void const_copy_to(const char *name, void *host, const size_t size) override;

/* Graphics resources interoperability. */
bool should_use_graphics_interop() override;

Expand Down
8 changes: 4 additions & 4 deletions intern/cycles/device/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void *device_memory::host_alloc(const size_t size)
return nullptr;
}

void *ptr = util_aligned_malloc(size, MIN_ALIGNMENT_CPU_DATA_TYPES);
void *ptr = device->host_alloc(type, size);

if (ptr == nullptr) {
throw std::bad_alloc();
Expand All @@ -58,7 +58,7 @@ void device_memory::host_and_device_free()
{
if (host_pointer) {
if (host_pointer != shared_pointer) {
util_aligned_free(host_pointer, memory_size());
device->host_free(type, host_pointer, memory_size());
}
host_pointer = nullptr;
}
Expand Down Expand Up @@ -136,9 +136,9 @@ bool device_memory::is_resident(Device *sub_device) const
return device->is_resident(device_pointer, sub_device);
}

bool device_memory::is_host_mapped(Device *sub_device) const
bool device_memory::is_shared(Device *sub_device) const
{
return device->is_host_mapped(shared_pointer, device_pointer, sub_device);
return device->is_shared(shared_pointer, device_pointer, sub_device);
}

/* Device Sub `ptr`. */
Expand Down
2 changes: 1 addition & 1 deletion intern/cycles/device/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ class device_memory {
void restore_device();

bool is_resident(Device *sub_device) const;
bool is_host_mapped(Device *sub_device) const;
bool is_shared(Device *sub_device) const;

/* No copying and allowed.
*
Expand Down
2 changes: 1 addition & 1 deletion intern/cycles/device/metal/device_impl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@
if (mem.host_pointer && mem.host_pointer != mmem->hostPtr) {
memcpy(mmem->hostPtr, mem.host_pointer, size);

util_aligned_free(mem.host_pointer, mem.memory_size());
host_free(mem.type, mem.host_pointer, mem.memory_size());
mem.host_pointer = mmem->hostPtr;
}
mem.shared_pointer = mmem->hostPtr;
Expand Down
Loading

0 comments on commit b487bcd

Please sign in to comment.