gpt4 book ai didi

c++ - VexCL : set values common to all vector elements

转载 作者:行者123 更新时间:2023-11-30 05:39:47 25 4
gpt4 key购买 nike

我使用光线追踪并使用 GPU 计算像素颜色。我之前使用的是 NVIDIA CUDA,现在想转到 VexCL。我正在尝试使用这样的代码:

struct Ray;
vex::Context ctx(...);

...

unsigned int frame_width, frame_height;
std::array<float, 4> camera_direction, camera_up;
float camera_fov;

...

// initialize values and store them in GPU memory too

...

vex::vector<Ray> rays(ctx, frame_width * frame_height);

和类似的东西

rays = some_expression_to_calculate_ray(vex::element_index(), frame_width,
camera_direction, camera_up, camera_fov);

所以我的问题是:我如何向 VexCL 解释某些值必须对所有 vector 元素都是通用的?

我正在尝试 VEX_CONSTANTvex::raw_pointer,但这不是我需要的。

最佳答案

如果更改 camera_direction 的类型和 camera_up来自 std::array<float,4>cl_float4 ,那么您就可以直接在表达式中使用它们:

#include <vexcl/vexcl.hpp>

int main() {
vex::Context ctx(vex::Filter::Env);

VEX_FUNCTION(float, dummy, (size_t, idx)(cl_float4, dir)(cl_float4, up)(float, fov),
// whatever
return idx + length(dir - up) + fov;
);

cl_float4 camera_dir = {1, 2, 3, 4}, camera_up = {1, 0, 0, 0};
float camera_fov = 42;

vex::vector<float> rays(ctx, 1024);

rays = dummy(vex::element_index(), camera_dir, camera_up, camera_fov);
}

(为简单起见,我已将 rays 更改为浮点 vector ,请参阅 linked question 了解如何在 VexCL 中使用结构。) camera_dir , camera_up , 和 camera_fov在主机端定义,并作为参数传递给内核。因此,不会制作不必要的拷贝。这是生成的 OpenCL 内核:

float dummy(ulong idx, float4 dir, float4 up, float fov) {
return idx + length(dir - up) + fov;
}
kernel void vexcl_vector_kernel(
ulong n,
global float * prm_1,
ulong prm_2,
float4 prm_3,
float4 prm_4,
float prm_5
)
{
for(ulong idx = get_global_id(0); idx < n; idx += get_global_size(0))
{
prm_1[idx] = dummy( (prm_2 + idx), prm_3, prm_4, prm_5 );
}
}

关于c++ - VexCL : set values common to all vector elements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32116622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com