0

我想修改並行減少算法。我使用邏輯將給定的數組長度分成2的冪。例如,如果我放入數字26,我使16個元素的數組1,8個元素的下一個數組和2個元素的最後一個數組。儘管26本身並不是2的冪,但是通過這種方法,我從主陣列創建了更多數量的子陣列,其中每個陣列都可以進行並行減少。但爲了能夠在rendercript中做到這一點,我使用循環來重寫Renderscript內存分配,而它只有一個上下文,但它會拋出錯誤。你能幫助我嗎?把我指向我的盲點?以下是我的代碼!我可以將renderscript內存分配放入循環中以處理一系列可變大小的數組嗎?

private void createScript() { 
    log ("i'm in createscript"); 
    int pp=0; 
    int qq=1; 
    int ii=0; 
    **mRS = RenderScript.create(this);** 
    for (int gstride=0; gstride < address.length/2; gstride++){ 
     log("im in stride noof gstride:"+gstride); 
     // this is the address array location 
     int strtadd=address[ii]; 
     int sze = 0; 
     sze=address[qq]-address[pp]+1; 
     tempin =new int [sze]; 
     System.arraycopy(input, strtadd, tempin, 0, tempin.length); 
     strtadd=address[ii+2]; 
     log("Generated size of array: " + tempin.length); 
     //renderscript declarations 



     `enter code here`**mInAllocation=Allocation.createSized(mRS,Element.I32(mRS),tempin.length);                
     `enter code here`mOutAllocation=Allocation.createSized(mRS,Element.I32(mRS),address.length/2); ` 

     mInAllocation.copyFrom(tempin); 
     mScript = new ScriptC_reduce2(mRS, getResources(), R.raw.reduce2); 
     //int row_width = input.length; 
     mScript.bind_gInarray(mInAllocation); 
     mScript.set_gIn(mInAllocation); 
     mScript.set_gOut(mOutAllocation); 
     mScript.set_gScript(mScript); 


     //time measurement 

     long lStartTime = new Date().getTime(); 
    for (int stride = tempin.length/2; stride > 0; stride /= 2) { 
     mScript.set_stride(stride); 
     mScript.invoke_filter(); 
    }** 
    long lEndTime = new Date().getTime(); 
    long difference = lEndTime - lStartTime; 
    nettime[gstride]=difference; 
    pp=pp+1; 
    qq=qq+1; 

    mInAllocation.copyTo(tempin); 
    output[gstride] = tempin[0]; 

     } 
    int sum=0; 
    int sum2=0; 
    int i = 0;  . 
    while(i < output.length) { 
     sum += output[i]; 
     sum2 +=nettime[i]; 
     i++;   
    } 

    t1.setText(String.format("output:%s\n\nExecution time:%s", 
      +sum, +sum2 +"ms")); //input:%s\n\n ArrayToString(input), 
} 


I get the following error as copied from the error log: I don't think its out of memory ` `error. 
V/RenderScript(2890): rsContextCreate dev=0x2a14ea68 
V/ScriptC (2890): Create script for resource = reduce2 
D/StopWatch(2890): StopWatch bcc: RSCompilerDriver::loadScriptCache time (us): 1988 
D/StopWatch(2890): StopWatch bcc: RSCompilerDriver::build time (us): 2485 
D/AndroidRuntime(2890): Shutting down VM 
W/dalvikvm(2890): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 
E/AndroidRuntime(2890): FATAL EXCEPTION: main 
E/AndroidRuntime(2890): java.lang.RuntimeException: Unable to start activity  `enter code `enter code here`ComponentInfo{com.example.paralleladd2/com.example.paralleladd2.Paralleladd2}:java.lang .NullPointerException 
E/AndroidRuntime(2890): at `enter code here`android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
E/AndroidRuntime(2890): at `enter code here`android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
E/AndroidRuntime(2890): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
E/AndroidRuntime(2890): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
E/AndroidRuntime(2890): at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime(2890): at android.os.Looper.loop(Looper.java:137) 
E/AndroidRuntime(2890): at android.app.ActivityThread.main(ActivityThread.java:5041) 
E/AndroidRuntime(2890): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(2890): at java.lang.reflect.Method.invoke(Method.java:511) 
E/AndroidRuntime(2890): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
E/AndroidRuntime(2890): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
E/AndroidRuntime(2890): at dalvik.system.NativeStart.main(Native Method) 
E/AndroidRuntime(2890): Caused by: java.lang.NullPointerException 
E/AndroidRuntime(2890): at com.example.paralleladd2.Paralleladd2.createScript(Paralleladd2.java:157) 
E/AndroidRuntime(2890): at com.example.paralleladd2.Paralleladd2.onCreate(Paralleladd2.java:48) 
E/AndroidRuntime(2890): at android.app.Activity.performCreate(Activity.java:5104) 
E/AndroidRuntime(2890): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
E/AndroidRuntime(2890): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
+0

什麼是錯誤?內存不足 ? –

回答

0

在循環內創建分配應該沒問題。我認爲你的問題來自在循環內創建腳本。我會在分配創建循環之外創建一個reduce腳本,並在循環中重新使用它。

腳本創建非常耗費資源,可能無法運行系統資源。

+0

我做了「mScript = new ScriptC_reduce2(mRS,getResources(),R.raw.reduce2);」循環外,但它仍然拋出相同的錯誤。不知道爲什麼!我用logcat輸出更新了我的帖子。 – user3302611

+0

所以它就像2個循環:Loop1:每次創建一個新的輸入數組,Loop2:以並行約簡的方式進行操作。現在我已經在loop1之外創建了腳本,並且只在循環1內部輸入_set()並調用invoke filter();從Loop2內部進行並行減少。任何幫助,我可能會錯過將非常感激! – user3302611

+0

哪一行是源代碼中的157? –

相關問題