bug fix in 2DConv and other small changes to makefile
[IRC.git] / Robust / src / Benchmarks / Prefetch / 2DConv / dsm / Convolution.java
1 public class Convolution extends Thread {
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     atomic {
19       initKernel(kernel);
20       double tempinput[][] = img.inputImage;
21       double tempout[][] = img.outputImage;
22
23       double tinput1[] = tempinput[x0];
24       double tinput2[] = tempinput[x0+1];
25       double tinput3[] = tempinput[x0+2];
26       double tinput4[] = tempinput[x0+3];
27       double tinput0[] = tinput1;
28
29       int l=x0+4;
30       for(int i=x0;i<x1;i++,l++){
31         double tout[] = tempout[i];
32         tinput0 = tinput1; tinput1=tinput2; tinput2=tinput3; tinput3=tinput4; tinput4=tempinput[l];
33         for(int j=y0;j<y1;++j){
34           tout[j] = 0;
35           for(int b=0;b<kernelHeight;++b){
36             tout[j] = tout[j] + (tinput0[j+b] * kernel[0][b] + tinput1[j+b] * kernel[1][b] + tinput2[j+b]*kernel[2][b] +
37                 tinput3[j+b]*kernel[3][b] + tinput4[j+b]*kernel[4][b]);
38           }
39         }
40       }
41     }
42   }
43
44   public static void main(String[] args) {
45     int SIZE = 256;
46     int NUM_THREADS = 1;
47     if(args.length>0) {
48       NUM_THREADS = Integer.parseInt(args[0]);
49       if(args.length>1)
50         SIZE = Integer.parseInt(args[1]);
51     }
52
53     int[] mid = new int[8];
54     mid[0] = (128<<24)|(195<<16)|(175<<8)|84; //dw-10
55     mid[1] = (128<<24)|(195<<16)|(175<<8)|85; //dw-11
56     mid[2] = (128<<24)|(195<<16)|(175<<8)|86; //dw-12
57     mid[3] = (128<<24)|(195<<16)|(175<<8)|87; //dw-13
58     mid[4] = (128<<24)|(195<<16)|(175<<8)|88; //dw-14
59     mid[5] = (128<<24)|(195<<16)|(175<<8)|89; //dw-15
60     mid[6] = (128<<24)|(195<<16)|(175<<8)|90; //dw-16
61     mid[7] = (128<<24)|(195<<16)|(175<<8)|91; //dw-17
62
63     Image img;
64     Convolution[] conv;
65     Convolution tmp;
66
67     int kernelHeight = 5;
68     int kernelWidth = 5;
69     atomic {
70       img = global new Image(SIZE,SIZE,kernelHeight,kernelWidth);
71       img.setValues();
72       conv = global new Convolution[NUM_THREADS];
73       int increment=SIZE/NUM_THREADS;
74       int base = 0;
75       for(int i = 0; i<NUM_THREADS; i++) {
76         if((i+1)==NUM_THREADS)
77           conv[i] = global new Convolution(img, base, SIZE, 0, SIZE);
78         else 
79           conv[i] = global new Convolution(img, base, base+increment, 0, SIZE);
80         base+=increment;
81       }
82     }
83
84     /*
85     atomic{
86       System.printString("img.outputImage[10][20] = " +(int) img.outputImage[10][20] + "\n");
87       System.printString("img.outputImage[256][890] = " +(int) img.outputImage[256][890] + "\n");
88     }
89     */
90
91     for(int i = 0; i <NUM_THREADS; i++) {
92       atomic {
93         tmp = conv[i];
94       }
95       tmp.start(mid[i]);
96     }
97
98     for(int i = 0; i < NUM_THREADS; i++) {
99       atomic {
100         tmp = conv[i];
101       }
102       tmp.join();
103     }
104     System.printString("Done!");
105
106     /*
107     atomic{
108       System.printString("img.outputImage[10][20] = " +(int) img.outputImage[10][20] + "\n");
109       System.printString("img.outputImage[256][890] = " +(int) img.outputImage[256][890] + "\n");
110     }
111     */
112   }
113
114   //define 5X5 Gaussian kernel
115   public void initKernel(double[][] kernel) {
116     kernel[0][0] = 1/256.0;
117     kernel[0][1] = 4/256.0;
118     kernel[0][2] = 6/256.0;
119     kernel[0][3] = 4/256.0;
120     kernel[0][4] = 1/256.0;
121     kernel[1][0] = 4/256.0;
122     kernel[1][1] = 16/256.0;
123     kernel[1][2] = 24/256.0;
124     kernel[1][3] = 16/256.0;
125     kernel[1][4] = 4/256.0;
126     kernel[2][0] = 6/256.0;
127     kernel[2][1] = 24/256.0;
128     kernel[2][2] = 36/256.0;
129     kernel[2][3] = 24/256.0;
130     kernel[2][4] = 6/256.0;
131     kernel[3][0] = 4/256.0;
132     kernel[3][1] = 16/256.0;
133     kernel[3][2] = 24/256.0;
134     kernel[3][3] = 16/256.0;
135     kernel[3][4] = 4/256.0;
136     kernel[4][0] = 1/256.0;
137     kernel[4][1] = 4/256.0;
138     kernel[4][2] = 6/256.0;
139     kernel[4][3] = 4/256.0;
140     kernel[4][4] = 1/256.0;
141   }
142 }
143
144 public class Image {
145   int width, height;
146   int kernelWidth, kernelHeight;
147   double[][] inputImage;
148   double[][] outputImage;
149
150   public Image(int width, int height, int kernelWidth, int kernelHeight) {
151     this.width = width;
152     this.height = height;
153     this.kernelWidth = kernelWidth;
154     this.kernelHeight = kernelHeight;
155     inputImage = global new double[height+kernelHeight-1][width+kernelWidth-1];
156     outputImage = global new double[height][width];
157   }
158
159   /* Create a valid image */
160   public void setValues() {
161     for (int i = 0; i < (height+kernelHeight - 1); i++) {
162       double ainput[] = inputImage[i];
163       for(int j = 0; j < (width+kernelWidth - 1); j++) {
164         ainput[j] = 256-j;
165       }
166     }
167
168     for (int i = 0; i < height; i++){
169       double aout[] = outputImage[i];
170       for(int j = 0; j < width; j++) {
171         aout[j] = 0;
172       }
173     }
174   }
175 }