在Renderscript使用rsAtomicInc计算直方图只返回地址,而不是计数 [英] calculating histogram using rsAtomicInc in Renderscript only returns address instead of the count

查看:248
本文介绍了在Renderscript使用rsAtomicInc计算直方图只返回地址,而不是计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个直方图计算的程序。所以我创建了10个号码,将它视为一维图像阵列的随机排列。以下是我的renderscript和Java code。我想直方图ø被返回作为没有产生的每个可能的计数阵列但它只返回遇到号码的第一地址。任何帮助将是非常美联社preciated!

Java的code:
公共类Histcal延伸活动{

 私人INT []的形象;
私人诠释[]的亮度;
私人浮动[] N​​ETTIME;
私人诠释[]的区别;
私人RenderScript的mRS;
私人分配mInimage;
私人分配mOutlumhistogram;
私人分配mOutallocation2;
私人TextView的T1;
私人ScriptC_histo mScript;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_histcal);
    T1 =新的TextView(本);
    T1 =(的TextView)findViewById(R.id.textview1);
    图像= randomArray(10);    createScript();
}公共静态INT [] randomArray(INT N){
    INT [] = randomArray新的INT [n];
    随机randNumGenerator =新的随机();
    的for(int i = 0; I< N;我++){
        randomArray [I] = randNumGenerator.nextInt(5);
        日志(生成+ randomArray [I]);
    }
    返回randomArray;
}私有静态无效日志(字符串aMessage){
    的System.out.println(aMessage);
}私人无效createScript(){
     INT []亮度=新INT [6];
        INT []差值= INT新[6];
    日志(我在createscript);
     mRS评分= RenderScript.create(本);     mInimage = Allocation.createSized(MRS,Element.I32(MRS),image.length);
     mInimage.copyFrom(图片);
    mOutlumhistogram = Allocation.createSized(MRS,Element.I32(MRS),luminance.length);
   mScript =新ScriptC_histo(MRS,getResources(),R.raw.histo);    mScript.bind_gOutarray(mOutlumhistogram);
    mScript.set_gIn(mInimage);
    mScript.set_gOut(mOutlumhistogram);
    mScript.set_gScript(mScript);
    mScript.invoke_increment();
    mOutlumhistogram.copyTo(差);
        对(INT I = 0; I&小于10;我++){
            日志(生成柱状图计数:+差[I]);        }
}

}


在renderscript code:

 的#pragma版(1)RS的#pragma java_package_name(com.example.histcal)
的#pragma rs_fp_im precise
#包括rs_time.rsh
#包括rs_atomic.rsh
挥发性int32_t * gOutarray;
rs_allocation GIN;
rs_allocation痛风;
rs_script版gscript;  无效根(常量int32_t * V_IN,int32_t * V_OUT,常量无效* usrData,uint32_t的X,uint32_t的Y){
  int32_t LUM = * V_IN;
  rsDebug(present值:,* V_IN);
  挥发性int32_t *地址= gOutarray + LUM;
  // rsDebug(present gOutarray地址:* gOutarray);
  rsDebug(present地址:*地址);
 int32_t rsAtomicInc(挥发性int32_t *地址);
   } 空隙增量(){
 rsForEach(版gscript,杜松子酒,痛风,NULL);
  }


在logcat中显示它显示的直方图输出的第一次遇到每一个号码的第一个地址。我也不解我看到数组中的2个元素怎么能有相同的地址?

  I /的System.out(1527):生成:0
  I /的System.out(1527):生成:1
  I /的System.out(1527):生成:3
  I /的System.out(1527):生成:4
   I /的System.out(1527):生成:0
 I /的System.out(1527):生成:2
 I /的System.out(1527):生成:1
 I /的System.out(1527):生成:1
 I /的System.out(1527):生成:2
 I /的System.out(1527):生成:2
  I /的System.out(1527):我在createscript
  V /的renderScript(1527):rsContextCreate开发= 0x2a14ea80
 V / ScriptC(1527):创建资源=组织相容脚本
 D /秒表(1527):秒表BCC:RSCompilerDriver :: loadScriptCache时间(我们):2942
D /秒表(1527):秒表BCC:RSCompilerDriver ::建立时间(我们):3763
 D /的renderScript(1527):present值:0为0x0
  D /的renderScript(1527):present地址:706096640 0x2a162e00
 D /的renderScript(1527):present值:1为0x1
 D /的renderScript(1527):present地址:1074179188 0x4006ac74
 D /的renderScript(1527):present值:3 0x3中
D /的renderScript(1527):present地址:0为0x0
 D /的renderScript(1527):present值:4为0x4
D /的renderScript(1527):present地址:0为0x0
  D /的renderScript(1527):present值:0为0x0
 D /的renderScript(1527):present地址:706096640 0x2a162e00
  D /的renderScript(1527):present值:2 0X2
  D /的renderScript(1527):present地址:0为0x0
 D /的renderScript(1527):present值:1为0x1
 D /的renderScript(1527):present地址:1074179188 0x4006ac74
  D /的renderScript(1527):present值:1为0x1
 D /的renderScript(1527):present地址:1074179188 0x4006ac74
 D /的renderScript(1527):present值:2 0X2
  D /的renderScript(1527):present地址:0为0x0
 D /的renderScript(1527):present值:2 0X2
 D /的renderScript(1527):present地址:0为0x0
 I /的System.out(1527):产生直方图计数:706096640
I /的System.out(1527):产生直方图计数:1074179188
I /的System.out(1527):产生直方图计数:0
I /的System.out(1527):产生直方图计数:0

我/的System.out(1527):产生直方图计数:0
    I /的System.out(1527):产生直方图计数:-1879038464

  D / AndroidRuntime(1527):关闭VM
W / dalvikvm(1527):主题ID = 1:螺纹未捕获的异常退出(组= 0x40a71930)
E / AndroidRuntime(1527):致命异常:主要


解决方案

-1-
我测试了19 API和Nexus 5将code和我看到的区别[]打印正确地往外冒。我的Java code是一样的你,以下是脚本code有一些细微的差别。

 的#pragma版(1)
RS的#pragma java_package_name(com.clearvision.androidapps.clearview)
的#pragma rs_fp_full挥发性int32_t * gOutarray;
rs_allocation GIN;
rs_allocation痛风;
rs_script版gscript;  无效根(常量int32_t * V_IN,int32_t * V_OUT,常量无效* usrData,uint32_t的X,uint32_t的Y){
      int32_t LUM = * V_IN;
      rsDebug(present值:,* V_IN);
      挥发性int32_t *地址= gOutarray + LUM;
      // rsDebug(present gOutarray地址:* gOutarray);
      rsDebug(present地址:*地址);
      rsAtomicInc(地址);
  } 空隙增量(){
        rsForEach(版gscript,杜松子酒,痛风);
}

-2-
我是previously不正确。低于code是罚款。

 挥发性int32_t *地址= gOutarray + LUM;

-3-
你可能logcat中显示,因为下面的错误

 的for(int i = 0;我小于10;我++){
        日志(生成柱状图计数:+差[I]);
    }

差阵列与6 INT元件限定,而你正在循环直到10。这是一个问题。

您可以重新检查?

I am trying to write a program for histogram calculation. So i have created a random array of 10 numbers treating it as the image array in 1D. The following is my renderscript and Java code. I want the histogram o be returned as an array with counts of each possible no generated but it only returns the first address of that number encountered. Any help would be much appreciated!

Java code: public class Histcal extends Activity {

private int[] image;   
private int[] luminance;
private float[] nettime;
private int[] difference;
private RenderScript mRS;
private Allocation mInimage;
private Allocation mOutlumhistogram;  
private Allocation mOutallocation2;
private TextView t1;
private ScriptC_histo mScript;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_histcal);
    t1= new TextView(this);
    t1= (TextView)findViewById(R.id.textview1);
    image=randomArray(10) ;

    createScript();
}

public static int[] randomArray(int n) {
    int[] randomArray = new int[n];
    Random randNumGenerator = new Random();
    for (int i = 0; i < n; i++) {
        randomArray[i] = randNumGenerator.nextInt(5);
        log("Generated : " + randomArray[i]);
    }
    return randomArray;


}

private static void log(String aMessage){
    System.out.println(aMessage);
}

private void createScript() {
     int[] luminance = new int[6];
        int[] difference=new int[6];
    log ("i'm in createscript");
     mRS = RenderScript.create(this);

     mInimage = Allocation.createSized(mRS, Element.I32(mRS), image.length);
     mInimage.copyFrom(image);
    mOutlumhistogram = Allocation.createSized(mRS, Element.I32(mRS),luminance.length);
   mScript = new ScriptC_histo(mRS, getResources(), R.raw.histo);

    mScript.bind_gOutarray(mOutlumhistogram);
    mScript.set_gIn(mInimage);
    mScript.set_gOut(mOutlumhistogram);
    mScript.set_gScript(mScript);
    mScript.invoke_increment();
    mOutlumhistogram.copyTo(difference);
        for(int i=0;i<10;i++){
            log("generated histogram counts:"+difference[i]);

        }
}

}


the renderscript code:

#pragma version(1)

#pragma rs java_package_name(com.example.histcal)
#pragma rs_fp_imprecise
#include "rs_time.rsh"
#include "rs_atomic.rsh"


volatile int32_t *gOutarray;
rs_allocation gIn;
rs_allocation gOut;
rs_script gScript; 

  void root(const int32_t *v_in, int32_t *v_out, const void *usrData, uint32_t x,  uint32_t y) {
  int32_t lum=*v_in;
  rsDebug("present value:", *v_in);
  volatile int32_t* addr=gOutarray+lum;
  //rsDebug("present gOutarray address:", *gOutarray);
  rsDebug("present address:", *addr);
 int32_t rsAtomicInc(volatile int32_t * addr);  
   }  

 void increment() {
 rsForEach(gScript, gIn, gOut, NULL); 
  }      


the logcat display where it shows the histogram output is the first address of the first time it encounters each number. Also I'm puzzled seeing how can 2 elements in an array have the same address?

  I/System.out( 1527): Generated : 0
  I/System.out( 1527): Generated : 1
  I/System.out( 1527): Generated : 3
  I/System.out( 1527): Generated : 4
   I/System.out( 1527): Generated : 0
 I/System.out( 1527): Generated : 2
 I/System.out( 1527): Generated : 1
 I/System.out( 1527): Generated : 1
 I/System.out( 1527): Generated : 2
 I/System.out( 1527): Generated : 2
  I/System.out( 1527): i'm in createscript
  V/RenderScript( 1527): rsContextCreate dev=0x2a14ea80
 V/ScriptC ( 1527): Create script for resource = histo
 D/StopWatch( 1527): StopWatch bcc: RSCompilerDriver::loadScriptCache time (us): 2942 
D/StopWatch( 1527): StopWatch bcc: RSCompilerDriver::build time (us): 3763 
 D/RenderScript( 1527): present value: 0  0x0
  D/RenderScript( 1527): present address: 706096640  0x2a162e00
 D/RenderScript( 1527): present value: 1  0x1
 D/RenderScript( 1527): present address: 1074179188  0x4006ac74
 D/RenderScript( 1527): present value: 3  0x3
D/RenderScript( 1527): present address: 0  0x0
 D/RenderScript( 1527): present value: 4  0x4
D/RenderScript( 1527): present address: 0  0x0
  D/RenderScript( 1527): present value: 0  0x0
 D/RenderScript( 1527): present address: 706096640  0x2a162e00
  D/RenderScript( 1527): present value: 2  0x2
  D/RenderScript( 1527): present address: 0  0x0
 D/RenderScript( 1527): present value: 1  0x1
 D/RenderScript( 1527): present address: 1074179188  0x4006ac74
  D/RenderScript( 1527): present value: 1  0x1
 D/RenderScript( 1527): present address: 1074179188  0x4006ac74
 D/RenderScript( 1527): present value: 2  0x2
  D/RenderScript( 1527): present address: 0  0x0
 D/RenderScript( 1527): present value: 2  0x2
 D/RenderScript( 1527): present address: 0  0x0
 I/System.out( 1527): generated histogram counts:706096640
I/System.out( 1527): generated histogram counts:1074179188
I/System.out( 1527): generated histogram counts:0
I/System.out( 1527): generated histogram counts:0

I/System.out( 1527): generated histogram counts:0 I/System.out( 1527): generated histogram counts:-1879038464

D/AndroidRuntime( 1527): Shutting down VM
W/dalvikvm( 1527): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
E/AndroidRuntime( 1527): FATAL EXCEPTION: main

解决方案

-1- I tested your code on API 19 and Nexus 5 and I see that difference[] prints are coming out correctly. My Java code is the same as yours, below is the script code with some minor differences.

#pragma version(1)
#pragma rs java_package_name(com.clearvision.androidapps.clearview)
#pragma rs_fp_full

volatile int32_t *gOutarray;
rs_allocation gIn;
rs_allocation gOut;
rs_script gScript; 

  void root(const int32_t *v_in, int32_t *v_out, const void *usrData, uint32_t x,  uint32_t y) {
      int32_t lum=*v_in;
      rsDebug("present value:", *v_in);
      volatile int32_t* addr=gOutarray + lum;
      //rsDebug("present gOutarray address:", *gOutarray);
      rsDebug("present address:", *addr);
      rsAtomicInc(addr);  
  }  

 void increment() {
        rsForEach(gScript, gIn, gOut); 
} 

-2- I was previously incorrect. Below code is fine.

volatile int32_t* addr=gOutarray + lum;

-3- Your logcat probably shows error because of the following

    for(int i=0;i<10;i++){
        log("generated histogram counts:"+difference[i]);
    }

difference array is defined with 6 int elements, while you are looping till 10. This is a problem.

Can you recheck?

这篇关于在Renderscript使用rsAtomicInc计算直方图只返回地址,而不是计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆