RenderScript控制并行化线程数量
2014-07-11 13:50
134 查看
第一种方法:
在这里,我们只是把10个数组的计算展开。但是在实际得项目中,有时需要人为设定线程数量。此时,rs_script_call就可以发挥作用了。
rs_script_call结构体定义如下:
第00111行的枚举类型如下:
但是啥意思,目前还不知道。
第00118和00119啥意思,我也还是不知道。
只知道x,y,z的意思。具体看例子:
JAVA代码:
RS代码:
输出结果:
第二种方法:
在forEach_kernel_name()中,最后一个参数,我们可以传入Script.LaunchOptions类来裁剪kernel。
在Java代码中:
在这里,我们只是把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。
int | getXEnd() Returns the current X end |
int | getXStart() Returns the current X start |
int | getYEnd() Returns the current Y end |
int | getYStart() Returns the current Y start |
int | getZEnd() Returns the current Z end |
int | getZStart() Returns the current Z start |
Script.LaunchOptions | setX(int xstartArg, int xendArg) Set the X range. |
Script.LaunchOptions | setY(int ystartArg, int yendArg) Set the Y range. |
Script.LaunchOptions | setZ(int zstartArg, int zendArg) Set the Z range. |
Script.LaunchOptions lo=new Script.LaunchOptions(); lo.setX(1, M-1); lo.setY(1, N-1); mScript.forEach_root(Input, lo);
相关文章推荐
- C# 执行固定个数任务自行控制进入线程池的线程数量,多任务同时但是并发数据限定
- C#中的多线程-线程同步基础 (控制线程数量)
- 转 C#多线程及控制线程数量,对for循环输出效率
- 转 C#多线程及控制线程数量,对for循环输出效率
- Java中怎么控制线程訪问资源的数量
- 线程如何控制并发数量? Semaphore 线程协同类,用来控制线程并发数量
- 线程数量控制
- C# 执行固定个数任务自行控制进入线程池的线程数量,多任务同时但是并发数据限定
- 并发工具类(三)控制并发线程的数量 Semphore
- 改进后的客户端服务器聊天功能,此版本比上版本多了加锁处理,多了客户端数量控制,多了线程join操作
- python自定义线程池控制线程数量
- Java并发库(十四):控制线程访问数量Semaphore
- 线程的数量控制
- C#中的多线程-线程同步基础 (控制线程数量)
- Java中怎么控制线程访问资源的数量
- 线程系列09,线程的等待、通知,以及手动控制线程数量
- C#控制CheckedListBox选中的项目数量
- Hadoop中控制Map的数量
- Frame动画,执行5秒后停止,线程控制