Blender V5.3
hiprt/queue.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#ifdef WITH_HIPRT
6
7# include "device/hiprt/queue.h"
8
9# include <hiprt/hiprt.h>
10
12# include "device/hip/kernel.h"
14
16
18
19HIPRTDeviceQueue::HIPRTDeviceQueue(HIPRTDevice *device)
20 : HIPDeviceQueue((HIPDevice *)device), hiprt_device_(device)
21{
22}
23
24int HIPRTDeviceQueue::num_concurrent_states(const size_t state_size) const
25{
26 /* Add global traversal stack of each path. */
27 return HIPDeviceQueue::num_concurrent_states(state_size + HIPRT_THREAD_STACK_SIZE * sizeof(int));
28}
29
30bool HIPRTDeviceQueue::enqueue(DeviceKernel kernel,
31 const int work_size,
32 const DeviceKernelArguments &args)
33{
34 if (hiprt_device_->have_error()) {
35 return false;
36 }
37
38 if (!device_kernel_has_intersection(kernel)) {
39 return HIPDeviceQueue::enqueue(kernel, work_size, args);
40 }
41
42 const HIPContextScope scope(hiprt_device_);
43 const HIPDeviceKernel &hip_kernel = hiprt_device_->kernels.get(kernel);
44
45 /* Compute kernel launch parameters. */
46 const int num_threads_per_block = HIPRT_THREAD_GROUP_SIZE;
47 const int num_blocks = divide_up(work_size, num_threads_per_block);
48
49 /* Allocate and grow stack buffer just in time, its size is only known here. */
50 const int num_stacks = num_blocks * num_threads_per_block;
51 hiprtGlobalStackBuffer &stack_buffer = hiprt_device_->global_stack_buffer;
52 if (!stack_buffer.stackData || stack_buffer.stackCount < num_stacks) {
53 const hiprtContext hiprt_context = hiprt_device_->get_hiprt_context();
54
55 if (stack_buffer.stackData) {
56 /* Wait for kernels still using the current buffer. */
57 if (!synchronize()) {
58 return false;
59 }
60 hiprtDestroyGlobalStackBuffer(hiprt_context, stack_buffer);
61 stack_buffer = {0};
62 }
63
64 hiprtGlobalStackBufferInput stack_buffer_input{hiprtStackTypeGlobal,
65 hiprtStackEntryTypeInteger,
67 uint32_t(num_stacks)};
68
69 hiprtError rt_result = hiprtCreateGlobalStackBuffer(
70 hiprt_context, stack_buffer_input, stack_buffer);
71
72 if (rt_result != hiprtSuccess) {
73 LOG_ERROR << "Failed to create hiprt Global Stack Buffer";
74 return false;
75 }
76 }
77
78 debug_enqueue_begin(kernel, work_size);
79
80 DeviceKernelArguments args_copy = args;
82 (void *)(&stack_buffer),
83 sizeof(hiprtGlobalStackBuffer));
84
85 int shared_mem_bytes = 0;
86
87 assert_success(hipModuleLaunchKernel(hip_kernel.function,
88 num_blocks,
89 1,
90 1,
91 num_threads_per_block,
92 1,
93 1,
94 shared_mem_bytes,
95 hip_stream_,
96 const_cast<void **>(args_copy.values),
97 nullptr),
98 "enqueue");
99
100 debug_enqueue_end();
101
102 return !(hiprt_device_->have_error());
103}
104
106
107#endif /* WITH_HIPRT */
#define CCL_NAMESPACE_END
#define HIPRT_THREAD_STACK_SIZE
#define HIPRT_THREAD_GROUP_SIZE
bool device_kernel_has_intersection(DeviceKernel kernel)
ccl_gpu_kernel_postfix const ccl_global int ccl_global float const int work_size
DeviceKernel
#define LOG_ERROR
Definition log.h:101
unsigned int uint32_t
Definition BKE_text.h:81
void * values[MAX_ARGS]
void add(const KernelFilmConvert *value)
ccl_device_inline size_t divide_up(const size_t x, const size_t y)
Definition types_base.h:54