卤化物:滤除矢量中的元素(卤化物:: Runtime :: Buffer) [英] Halide: Filter elements out of vector (Halide::Runtime::Buffer)

查看:107
本文介绍了卤化物:滤除矢量中的元素(卤化物:: Runtime :: Buffer)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Halide::Runtime::Buffer,并且希望删除符合条件的元素,理想情况下,该操作应就地进行并且可以在Halide::Generator中定义该函数.

I have a Halide::Runtime::Buffer and would like to remove elements that match a criteria, ideally such that the operation occurs in-place and that the function can be defined in a Halide::Generator.

我已经研究过使用归约法,但是在我看来,我无法输出不同长度的向量-我只能将某些元素设置为我选择的值.

I have looked into using reductions, but it seems to me that I cannot output a vector of a different length -- I can only set certain elements to a value of my choice.

到目前为止,我使用它的唯一方法是使用extern "C"调用并传递我要过滤的缓冲区,以及一个布尔型缓冲区(以整数表示的1和0).我将缓冲区读入另一个库(Armadillo)的向量中,进行了所需的过滤器,然后将过滤后的向量读回Halide.

So far, the only way I got it to work was by using a extern "C" call and passing the Buffer I wanted to filter, along with a boolean Buffer (1's and 0's as ints). I read the Buffers into vectors of another library (Armadillo), conducted my desired filter, then read the filtered vector back into Halide.

这看起来很混乱,而且,通过这段代码,我传递的是Halide::Buffer对象,而不是Halide::Runtime::Buffer对象,所以我不知道如何在Halide::Generator中实现它.

This seems quite messy and also, with this code, I'm passing a Halide::Buffer object, and not a Halide::Runtime::Buffer object, so I don't know how to implement this within a Halide::Generator.

所以我的问题是双重的:

So my question is twofold:

  1. 可以在纯卤化物中,最好是在原位进行这种过滤吗?
  2. 是否有在Generators中使用extern "C"函数的示例?
  1. Can this kind of filtering be achieved in pure Halide, preferably in-place?
  2. Is there an example of using extern "C" functions within Generators?

推荐答案

第一部分是有效的流压缩.尽管输出大小将需要固定或者是输入大小的函数(例如与输入相同的大小),但是可以在Halide中完成.一个人也可以得到作为输出产生的最大指数,以指示产生了多少结果.我在这里写了一些有关如何进行基于前缀和的流压缩的答案:

The first part is effectively stream compaction. It can be done in Halide, though the output size will either need to be fixed or a function of the input size (e.g. the same size as the input). One can get the max index produced as output as well to indicate how many results were produced. I wrote up a bit of an answer on how to do a prefix sum based stream compaction here: Halide: Reduction over a domain for the specific values . It is an open question how to do this most efficiently in parallel across a variety of targets and we hope to do some work on exploring that space soon.

这是否到位取决于能否将所有内容放入Func的单个更新定义系列中.例如.不能对传递到Halide过滤器的输入进行就地完成,因为归约总是分配一个缓冲区来处理.如果输入是在Generator内部生成的,则可能会这样做.

Whether this is in-place or not depends on whether one can put everything into a single series of update definitions for a Func. E.g. It cannot be done in-place on an input passed into a Halide filter because reductions always allocate a buffer to work on. It may be possible to do so if the input is produced inside the Generator.

回复:第二个问题,您使用的是define_extern吗?它不能很好地与Halide::Runtime::Buffer集成在一起,因为必须使用halide_buffer_t来实现外部功能,但是从Generator内进行访问相当简单.我们还没有关于此的教程,但是测试中有许多示例.例如.: https://github.com/halide/Halide/blob/master/test/generator/define_extern_opencl_generator.cpp#L19

Re: the second question, are you using define_extern? This is not super well integrated with Halide::Runtime::Buffer as the external function must be implemented with halide_buffer_t but it is fairly straight forward to access from within a Generator. We don't have a tutorial on this yet, but there are a number of examples in the tests. E.g.: https://github.com/halide/Halide/blob/master/test/generator/define_extern_opencl_generator.cpp#L19

和定义:

https://github.com/halide/Halide/blob/master/test/generator/define_extern_opencl_aottest.cpp#L119

(这些不需要是extern "C",因为我不久前实现了C ++名称改写.只需将name mangling参数设置为define_externNameMangling::CPlusPlus并从外部函数的声明中删除extern "C"即可.非常有用,因为它可以对外部函数进行一次链接时间类型检查,从而捕获中等程度的错误.)

(These do not need to be extern "C" as I implemented C++ name mangling a while back. Just set the name mangling parameter to define_extern to NameMangling::CPlusPlus and remove the extern "C" from the external function's declaration. This is very useful as it gets one link time type checking on the external function, which catches a moderately frequent class of errors.)

这篇关于卤化物:滤除矢量中的元素(卤化物:: Runtime :: Buffer)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆