RenderScript控制並行化線程數量

第一種方法:

這裏,我們只是把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.
在Java代碼中:

 Script.LaunchOptions lo=new Script.LaunchOptions();
  lo.setX(1, M-1);
  lo.setY(1, N-1);
 mScript.forEach_root(Input, lo);



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章