-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathTestCL.java
More file actions
105 lines (97 loc) · 2.55 KB
/
Copy pathTestCL.java
File metadata and controls
105 lines (97 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package javaforce.tests;
import java.util.*;
import javaforce.*;
import javaforce.cl.*;
import static javaforce.cl.CL.*;
/** Test OpenCL
*
* @author pquiring
*/
public class TestCL {
public static void main(String[] args) {
System.out.println("TestCL");
int SIZE;
int SIZE_SIZE;
Random rand = new Random();
Compute compute = null;
try {
compute = new Compute();
if (!compute.init(TYPE_GPU)) {
JFLog.log("Compute init failed");
System.exit(1);
}
System.out.println("Starting tests...");
} catch (Throwable t) {
JFLog.log(t);
}
//array_square
try {
SIZE = 16;
float[] a = new float[SIZE];
for(int i=0;i<SIZE;i++) {
a[i] = rand.nextFloat();
}
float[] b = new float[SIZE];
compute.array_square(a, b);
//confirm results
int correct = 0;
for(int i=0;i<SIZE;i++) {
float res = a[i] * a[i];
if (b[i] == res) {
correct++;
} else {
JFLog.log("error:b[]=" + b[i] + ",expected=" + res);
}
}
System.out.println("array_square:" + correct + " of " + SIZE + " are correct");
} catch (Throwable t) {
JFLog.log(t);
}
//matrix_mult
try {
boolean identity = true;
SIZE = 3; //3*3 = 9
SIZE_SIZE = SIZE * SIZE;
float[] a = new float[SIZE_SIZE];
float[] b = new float[SIZE_SIZE];
float[] c = new float[SIZE_SIZE];
int idx = 0;
for(int row=0;row<SIZE;row++) {
for(int col=0;col<SIZE;col++) {
a[idx] = rand.nextFloat();
if (identity) {
b[idx] = (row == col ? 1 : 0); //identity matrix
} else {
b[idx] = rand.nextFloat();
}
idx++;
}
}
compute.matrix_mult(SIZE, SIZE, SIZE, a, b, c);
//confirm results
int correct = 0;
for(int row=0;row<SIZE;row++) {
for(int col=0;col<SIZE;col++) {
int i = col * SIZE + row;
float res = 0;
for(int k=0;k<SIZE;k++) {
res += a[k * SIZE + row] * b[col * SIZE + k];
}
if (c[i] == res) {
correct++;
} else {
JFLog.log("error:c[] = " + c[i] + ":expected=" + res);
}
}
}
if (true) {
javaforce.Console.printArray(a);
javaforce.Console.printArray(b);
javaforce.Console.printArray(c);
}
System.out.println("matrix_mult:" + correct + " of " + SIZE_SIZE + " are correct");
} catch (Throwable t) {
JFLog.log(t);
}
}
}