new 2D convolution benchmark
[IRC.git] / Robust / src / Benchmarks / Prefetch / 2DConv / javasingle / Convolution.java
1 public class Convolution {
2   Image img;
3   int x0,x1,y0,y1;
4
5   public Convolution(Image img, int x0, int x1, int y0, int y1) {
6     this.img = img;
7     this.x0 = x0;
8     this.x1 = x1;
9     this.y0 = y0;
10     this.y1 = y1;
11   }
12
13   public void run() {
14     int kernelHeight = 5;
15     int kernelWidth = 5;
16     double[][] kernel = new double[kernelHeight][kernelWidth];
17
18     initKernel(kernel);
19     double tempinput[][] = img.inputImage;
20     double tempout[][] = img.outputImage;
21
22     double tinput0[] = tempinput[x0];
23     double tinput1[] = tempinput[x0+1];
24     double tinput2[] = tempinput[x0+2];
25     double tinput3[] = tempinput[x0+3];
26     double tinput4[] = tempinput[x0+4];
27
28     for(int i=x0;i<x1;++i){
29       double tout[] = tempout[x0];
30       for(int j=y0;j<y1;++j){
31         tout[y0] = 0;
32         for(int b=0;b<kernelHeight;++b){
33           tout[y0] = tout[y0] + (tinput0[j+b] * kernel[0][b] + tinput1[j+b] * kernel[1][b] + tinput2[j+b]*kernel[2][b] +
34               tinput3[j+b]*kernel[3][b] + tinput4[j+b]*kernel[4][b]);
35         }
36       }
37       if(i != 4095) {
38         tinput0 = tinput1; tinput1=tinput2; tinput2=tinput3; tinput3=tinput4; tinput4=tempinput[i+5];
39       }
40     }
41   }
42
43   public static void main(String[] args) {
44     int SIZE = 256;
45     int NUM_THREADS = 1;
46     if(args.length>0) {
47       NUM_THREADS = Integer.parseInt(args[0]);
48       if(args.length>1)
49         SIZE = Integer.parseInt(args[1]);
50     }
51
52     Image img;
53     Convolution[] conv;
54     Convolution tmp;
55
56     int kernelHeight = 5;
57     int kernelWidth = 5;
58     img = new Image(SIZE,SIZE,kernelHeight,kernelWidth);
59     img.setValues();
60     conv = new Convolution[NUM_THREADS];
61     int increment=SIZE/NUM_THREADS;
62     int base = 0;
63     for(int i = 0; i<NUM_THREADS; i++) {
64       if((i+1)==NUM_THREADS)
65         conv[i] = new Convolution(img, base, SIZE, 0, SIZE);
66       else 
67         conv[i] = new Convolution(img, base, base+increment, 0, SIZE);
68       base+=increment;
69     }
70
71     for(int i = 0; i <NUM_THREADS; i++) {
72       tmp = conv[i];
73       tmp.run();
74     }
75
76     System.printString("Done!");
77   }
78
79   //define 5X5 Gaussian kernel
80   public void initKernel(double[][] kernel) {
81     kernel[0][0] = 1/256.0;
82     kernel[0][1] = 4/256.0;
83     kernel[0][2] = 6/256.0;
84     kernel[0][3] = 4/256.0;
85     kernel[0][4] = 1/256.0;
86     kernel[1][0] = 4/256.0;
87     kernel[1][1] = 16/256.0;
88     kernel[1][2] = 24/256.0;
89     kernel[1][3] = 16/256.0;
90     kernel[1][4] = 4/256.0;
91     kernel[2][0] = 6/256.0;
92     kernel[2][1] = 24/256.0;
93     kernel[2][2] = 36/256.0;
94     kernel[2][3] = 24/256.0;
95     kernel[2][4] = 6/256.0;
96     kernel[3][0] = 4/256.0;
97     kernel[3][1] = 16/256.0;
98     kernel[3][2] = 24/256.0;
99     kernel[3][3] = 16/256.0;
100     kernel[3][4] = 4/256.0;
101     kernel[4][0] = 1/256.0;
102     kernel[4][1] = 4/256.0;
103     kernel[4][2] = 6/256.0;
104     kernel[4][3] = 4/256.0;
105     kernel[4][4] = 1/256.0;
106   }
107 }
108
109 public class Image {
110   int width, height;
111   int kernelWidth, kernelHeight;
112   double[][] inputImage;
113   double[][] outputImage;
114
115   public Image(int width, int height, int kernelWidth, int kernelHeight) {
116     this.width = width;
117     this.height = height;
118     this.kernelWidth = kernelWidth;
119     this.kernelHeight = kernelHeight;
120     inputImage = new double[height+kernelHeight-1][width+kernelWidth-1];
121     outputImage = new double[height][width];
122   }
123
124   /* Create a valid image */
125   public void setValues() {
126     for (int i = 0; i < (height+kernelHeight - 1); i++) {
127       double ainput[] = inputImage[i];
128       for(int j = 0; j < (width+kernelWidth - 1); j++) {
129         ainput[j] = 256-j;
130       }
131     }
132
133     for (int i = 0; i < height; i++){
134       double aout[] = outputImage[i];
135       for(int j = 0; j < width; j++) {
136         aout[j] = 0;
137       }
138     }
139   }
140 }