【问题标题】:Can one use RenderScript's rsForEach on a non-root kernel?可以在非根内核上使用 RenderScript 的 rsForEach 吗?
【发布时间】:2013-09-21 05:33:15
【问题描述】:

可以使用rsForEach 调用非根 RenderScript 内核吗? 有许多使用rsForEach 从可调用的 RenderScript 函数中调用根内核的示例:

这些将脚本本身绑定到 RenderScript 上下文中的变量,然后从 RenderScript 中调用根内核。例如,在Activity 类中:

...
mScript = new ScriptC_gradient(mRS);
// bind Allocations and mScript to variables in the RenderScript context:
mScript.set_gIn(mImageAllocation);
mScript.set_gOut(mGradientAllocation);
mScript.set_gScript(mScript);
// invoke gradient function:
mScript.invoke_gradient();
...

gradient.rs:

#pragma version(1)
#pragma rs java_package_name(com.example.android.rs.hellocompute)

rs_allocation gOut;
rs_allocation gIn;
rs_script gScript;

void gradient() {
  rsForEach(gScript, gIn, gOut);
}
void root(const uchar4 *v_in, uchar4 *v_out, ...

但是如果我有另一个内核gray 我可以在root 之后调用它,在gradient 内吗?

// I thought it would look like this:
void gradient() {
  rsForEach(gScript, gIn, gOut);
  rsForEach(gScript, gIn, gOut, NULL, NULL, gExportForEachIdx_gray);
}
// Or:
void gradient() {
  rsForEach(gScript, gIn, gOut);
  rsSetMainKernel(&gScript, "gray");
  rsForEach(gScript, gIn, gOut);
}

但是the documentation for rsForEach 似乎表明它不支持这样的东西。也许可以通过在rs_script_call_t 中设置一些内容来完成,但the doc 对这种类型相当简洁:(2013 年 9 月 20 日检索)

typedef struct rs_script_call rs_script_call_t**

向 rsForEach 调用提供额外信息的结构。主要用于限制对分配中单元格子集的调用。

这个问题主要是出于好奇——我希望首选方法是从 Java 中调用它们:

...
mScript = new ScriptC_gradient(mRS);
// bind Allocations and mScript to variables in the RenderScript context:
mScript.forEach_root(mImageAllocation, mGradientAllocation);
mScript.forEach_gray(mGradientAllocation, mGrayGradientAllocation);
...

这些似乎是同步的。如果定义rootgray如下:

void root(...) { rsDebug("root", 0,0);  }
void gray(...) { rsDebug("gray", 1,1);  }

然后调用forEach_root 然后调用forEach_gray 会导致“root, {0,0}”在开始记录“gray, {1,1}”之前被记录 NxM 次 - 我还没有找到可以保证的文档, 尽管。有人知道那是哪里吗?

【问题讨论】:

    标签: android renderscript


    【解决方案1】:

    不幸的是,我们目前没有办法使用脚本中的 rsForEach() 调用非根 RenderScript 内核。您必须直接从 Java 调用它。您还可以将第二个内核作为 root() 放入另一个脚本中,然后也绑定该 rs_script(例如,您可以拥有 gScriptGradient 和 gScriptGray 并从主脚本中的单个调用顺序执行它们)。

    我最初错过了关于并行内核之间同步的第二个问题。它们确实是有序的。尽管内核是异步启动的,但在第一个内核完成之前,第二个内核不会运行。

    【讨论】:

    • 感谢您如此清楚地回答这两个问题!这是文档中的任何地方,还是已知only to RenderScript engineers
    • developer.android.com/guide/topics/renderscript/compute.html 有关于此的信息(异步启动,但启动 copyFrom/To 之间的顺序行为)。这种行为与其他并行计算语言没有什么不同。
    • 据此:developer.android.com/guide/topics/renderscript/…,实际上可以为函数设置不同的名称。怎么会?
    • 您可以为内核使用不同的名称,但您不能在脚本中将 root() 与 rsForEach() 一起使用。这是因为 rsForEach() 存在于每个脚本的多个内核之前,我们没有一种干净的方法来公开更多的函数名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    • 2011-07-04
    • 2016-12-18
    • 1970-01-01
    • 2021-01-08
    • 1970-01-01
    相关资源
    最近更新 更多