您的位置:首页 > 其它

RenderScript控制并行化线程数量

2014-07-11 13:50 134 查看
第一种方法:

这里,我们只是把10个数组的计算展开。但是在实际得项目中,有时需要人为设定线程数量。此时,rs_script_call就可以发挥作用了。

rs_script_call结构体定义如下:

00110 typedef struct rs_script_call {
00111     enum rs_for_each_strategy strategy;
00112     uint32_t xStart;
00113     uint32_t xEnd;
00114     uint32_t yStart;
00115     uint32_t yEnd;
00116     uint32_t zStart;
00117     uint32_t zEnd;
00118     uint32_t arrayStart;
00119     uint32_t arrayEnd;
00120 } rs_script_call_t;
00121


第00111行的枚举类型如下:

00096 enum rs_for_each_strategy {
00097     RS_FOR_EACH_STRATEGY_SERIAL = 0,
00098     RS_FOR_EACH_STRATEGY_DONT_CARE = 1,
00099     RS_FOR_EACH_STRATEGY_DST_LINEAR = 2,
00100     RS_FOR_EACH_STRATEGY_TILE_SMALL= 3,
00101     RS_FOR_EACH_STRATEGY_TILE_MEDIUM = 4,
00102     RS_FOR_EACH_STRATEGY_TILE_LARGE = 5
00103 };


但是啥意思,目前还不知道。

第00118和00119啥意思,我也还是不知道。

只知道x,y,z的意思。具体看例子:

JAVA代码:

private void CreateScript(){
int[] s;
int[] s2,s3;
s=new int[10];
s2=new int[10];
s3=new int[10];
for(int i=0;i<10;i++){
s[i]=5;
}
mRS=RenderScript.create(this);
In1=Allocation.createSized(mRS, Element.I32(mRS), 10);
In2=Allocation.createSized(mRS, Element.I32(mRS), 10);
Out=Allocation.createSized(mRS, Element.I32(mRS), 10);
Out2=Allocation.createSized(mRS, Element.I32(mRS), 10);
In1.copyFrom(s);

for(int i=0;i<10;i++){
s[i]=10;
}
In2.copyFrom(s);

mScript=new ScriptC_test(mRS, getResources(), R.raw.test);

mScript.set_input2(In2);
mScript.set_output2(Out2);
mScript.set_gScript(mScript);

//mScript.forEach_root(In1,Out);
mScript.invoke_run_test(In1, Out);
Out.copyTo(s2);;
Out2.copyTo(s3);
for(int i=0;i<10;i++){
Log.v("lucas","i="+i+"  "+s2[i]+"   "+s3[i]);

}

}


RS代码:

#pragma rs java_package_name(com.example.rc_test)
#pragma version(1)
#include "rs_allocation.rsh"

rs_allocation input2;
rs_allocation output2;
rs_script gScript;

int __attribute__((kernel)) root(int v_out,uint32_t x){
if(x<5)
rsSetElementAt_int(output2, 5, x);
else
rsSetElementAt_int(output2, 0, x);
return *(int*)rsGetElementAt(input2,x);

}

void run_test(rs_allocation in_alloc,rs_allocation out_alloc )
{
struct rs_script_call restrict_for;

restrict_for.strategy = RS_FOR_EACH_STRATEGY_DONT_CARE;
restrict_for.xStart = 0;
restrict_for.xEnd = 6;

restrict_for.yStart = 0;
restrict_for.yEnd = 0;
restrict_for.zStart = 0;
restrict_for.zEnd = 0;
restrict_for.arrayStart = 0;
restrict_for.arrayEnd = 0;

rsForEach(gScript,  in_alloc, out_alloc,NULL, 0, &restrict_for);
}


输出结果:

V/lucas   ( 3111): i=0  10   5
V/lucas   ( 3111): i=1  10   5
V/lucas   ( 3111): i=2  10   5
V/lucas   ( 3111): i=3  10   5
V/lucas   ( 3111): i=4  10   5
V/lucas   ( 3111): i=5  10   0
V/lucas   ( 3111): i=6  1624773960   23
V/lucas   ( 3111): i=7  1624773978   1607968316
V/lucas   ( 3111): i=8  1623464176   0
V/lucas   ( 3111): i=9  0   1624328128


第二种方法:

在forEach_kernel_name()中,最后一个参数,我们可以传入Script.LaunchOptions类来裁剪kernel。

intgetXEnd()
Returns the current X end
intgetXStart()
Returns the current X start
intgetYEnd()
Returns the current Y end
intgetYStart()
Returns the current Y start
intgetZEnd()
Returns the current Z end
intgetZStart()
Returns the current Z start
Script.LaunchOptionssetX(int
xstartArg, int xendArg)
Set the X range.
Script.LaunchOptionssetY(int
ystartArg, int yendArg)
Set the Y range.
Script.LaunchOptionssetZ(int
zstartArg, int zendArg)
Set the Z range.
在Java代码中:

Script.LaunchOptions lo=new Script.LaunchOptions();
lo.setX(1, M-1);
lo.setY(1, N-1);
mScript.forEach_root(Input, lo);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: