JOGL v2.6.0-rc-20250706
JOGL, High-Performance Graphics Binding for Java™ (public API).
TestMatrix4f02MulNOUI.java
Go to the documentation of this file.
1/**
2 * Copyright 2014 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.opengl.test.junit.math;
30
31import org.junit.Assert;
32import org.junit.Test;
33import org.junit.FixMethodOrder;
34import org.junit.runners.MethodSorters;
35
36import com.jogamp.common.os.Platform;
37import com.jogamp.junit.util.JunitTracer;
38import com.jogamp.math.FloatUtil;
39import com.jogamp.math.Matrix4f;
40
41@FixMethodOrder(MethodSorters.NAME_ASCENDING)
42public class TestMatrix4f02MulNOUI extends JunitTracer {
43
44 final float[] m1_0 = new float[]{ 1, 3, 4, 0,
45 6, 7, 8, 5,
46 98, 7, 6, 9,
47 54, 3, 2, 5 };
48 final Matrix4f m1 = new Matrix4f(m1_0);
49 final Matrix4fb n1 = new Matrix4fb(m1_0);
50
51
52 final float[] m2_0 = new float[]{ 1, 6, 98, 54,
53 3, 7, 7, 3,
54 4, 8, 6, 2,
55 0, 5, 9, 5 };
56 final Matrix4f m2 = new Matrix4f(m2_0);
57 final Matrix4fb n2 = new Matrix4fb(m2_0);
58
59 final float[] m2xm1_0 =
60 new float[]{ 26, 59, 143, 71,
61 59, 174, 730, 386,
62 143, 730, 9770, 5370,
63 71, 386, 5370, 2954 };
64 final Matrix4f m2xm1 = new Matrix4f(m2xm1_0);
65 final Matrix4fb n2xn1 = new Matrix4fb(m2xm1_0);
66
67 final float[] m1xm2_0 =
68 new float[]{12557, 893, 748, 1182,
69 893, 116, 116, 113,
70 748, 116, 120, 104,
71 1182, 113, 104, 131 };
72 final Matrix4f m1xm2 = new Matrix4f(m1xm2_0);
73 final Matrix4fb n1xn2 = new Matrix4fb(m1xm2_0);
74
75 @Test
76 public void test01_mul(){
77 {
78 final float[] r_0 = new float[16];
79 FloatUtil.multMatrix(m1_0, 0, m2_0, 0, r_0, 0);
80 Assert.assertArrayEquals(m1xm2_0, r_0, 0f);
81
82 Assert.assertEquals(m1xm2, new Matrix4f(m1).mul(m2));
83 Assert.assertEquals(m1xm2, new Matrix4f().mul(m1, m2));
84
85 Assert.assertEquals(n1xn2, new Matrix4fb(n1).mul(n2));
86 Assert.assertEquals(n1xn2, new Matrix4fb().mul(n1, n2));
87 }
88 {
89 final float[] r_0 = new float[16];
90 FloatUtil.multMatrix(m2_0, 0, m1_0, 0, r_0, 0);
91 Assert.assertArrayEquals(m2xm1_0, r_0, 0f);
92
93 Assert.assertEquals(m2xm1, new Matrix4f(m2).mul(m1));
94 Assert.assertEquals(m2xm1, new Matrix4f().mul(m2, m1));
95
96 Assert.assertEquals(n2xn1, new Matrix4fb(n2).mul(n1));
97 Assert.assertEquals(n2xn1, new Matrix4fb().mul(n2, n1));
98 }
99 }
100
101 @Test
102 public void test05Perf01(){
103 final float[] res = new float[16];
104
105 final Matrix4f res_m = new Matrix4f();
106 final Matrix4fb res_n = new Matrix4fb();
107
108 final int warmups = 1000;
109 final int loops = 25*1000*1000;
110 long tI1 = 0;
111 long tI2 = 0;
112 long tI4a = 0;
113 long tI4b = 0;
114 long tI5a = 0;
115 long tI5b = 0;
116
117 // warm-up
118 for(int i=0; i<warmups; i++) {
119 FloatUtil.multMatrix(m1_0, 0, m2_0, 0, res, 0);
120 FloatUtil.multMatrix(m2_0, 0, m1_0, 0, res, 0);
121 }
122 long t_0 = Platform.currentTimeMillis();
123 for(int i=0; i<loops; i++) {
124 FloatUtil.multMatrix(m1_0, 0, m2_0, 0, res, 0);
125 FloatUtil.multMatrix(m2_0, 0, m1_0, 0, res, 0);
126 }
127 tI1 = Platform.currentTimeMillis() - t_0;
128
129 // warm-up
130 for(int i=0; i<warmups; i++) {
131 FloatUtil.multMatrix(m1_0, m2_0, res);
132 FloatUtil.multMatrix(m2_0, m1_0, res);
133 }
134 t_0 = Platform.currentTimeMillis();
135 for(int i=0; i<loops; i++) {
136 FloatUtil.multMatrix(m1_0, m2_0, res);
137 FloatUtil.multMatrix(m2_0, m1_0, res);
138 }
139 tI2 = Platform.currentTimeMillis() - t_0;
140
141 // avoid optimizing out unused computation results by simply adding up determinat
142 double dr = 1;
143
144 //
145 // Matrix4f
146 //
147
148 // warm-up
149 for(int i=0; i<warmups; i++) {
150 res_m.mul(m1, m2);
151 dr += res_m.determinant();
152 res_m.mul(m2, m1);
153 dr += res_m.determinant();
154 }
155 t_0 = Platform.currentTimeMillis();
156 for(int i=0; i<loops; i++) {
157 res_m.mul(m1, m2);
158 dr += res_m.determinant();
159 res_m.mul(m2, m1);
160 dr += res_m.determinant();
161 }
162 tI4a = Platform.currentTimeMillis() - t_0;
163
164 // warm-up
165 for(int i=0; i<warmups; i++) {
166 res_m.load(m1);
167 res_m.mul(m2);
168 dr += res_m.determinant();
169 res_m.load(m2);
170 res_m.mul(m1);
171 dr += res_m.determinant();
172 }
173 t_0 = Platform.currentTimeMillis();
174 for(int i=0; i<loops; i++) {
175 res_m.load(m1);
176 res_m.mul(m2);
177 dr += res_m.determinant();
178 res_m.load(m2);
179 res_m.mul(m1);
180 dr += res_m.determinant();
181 }
182 tI4b = Platform.currentTimeMillis() - t_0;
183
184 //
185 // Matrix4fb
186 //
187
188 // warm-up
189 for(int i=0; i<warmups; i++) {
190 res_n.mul(n1, n2);
191 dr += res_n.determinant();
192 res_n.mul(n2, n1);
193 dr += res_n.determinant();
194 }
195 t_0 = Platform.currentTimeMillis();
196 for(int i=0; i<loops; i++) {
197 res_n.mul(n1, n2);
198 dr += res_n.determinant();
199 res_n.mul(n2, n1);
200 dr += res_n.determinant();
201 }
202 tI5a = Platform.currentTimeMillis() - t_0;
203 Assert.assertTrue( dr > 0 );
204
205 // warm-up
206 for(int i=0; i<warmups; i++) {
207 res_n.load(n1);
208 res_n.mul(n2);
209 dr += res_n.determinant();
210 res_n.load(n2);
211 res_n.mul(n1);
212 dr += res_n.determinant();
213 }
214 t_0 = Platform.currentTimeMillis();
215 for(int i=0; i<loops; i++) {
216 res_n.load(n1);
217 res_n.mul(n2);
218 dr += res_n.determinant();
219 res_n.load(n2);
220 res_n.mul(n1);
221 dr += res_n.determinant();
222 }
223 tI5b = Platform.currentTimeMillis() - t_0;
224 Assert.assertTrue( dr > 0 );
225
226 System.err.printf("Checkmark %f%n", dr);
227 System.err.printf("Summary loops %6d: I1 %6d ms total, %f us/mul%n", loops, tI1, tI1*1e3/loops);
228 System.err.printf("Summary loops %6d: I2 %6d ms total, %f us/mul, I2 / I1 %f%%%n", loops, tI2, tI2*1e3/2.0/loops, (double)tI2/(double)tI1*100.0);
229 System.err.printf("Summary loops %6d: I4a %6d ms total, %f us/mul, I4a / I2 %f%%, I4a / I4b %f%%%n", loops, tI4a, tI4a*1e3/2.0/loops, (double)tI4a/(double)tI2*100.0, (double)tI4a/(double)tI4b*100.0);
230 System.err.printf("Summary loops %6d: I4b %6d ms total, %f us/mul, I4b / I2 %f%%, I4b / I4a %f%%%n", loops, tI4b, tI4b*1e3/2.0/loops, (double)tI4b/(double)tI2*100.0, (double)tI4b/(double)tI4a*100.0);
231 System.err.printf("Summary loops %6d: I5a %6d ms total, %f us/mul, I5a / I2 %f%%, I5a / I5b %f%%%n", loops, tI5a, tI5a*1e3/2.0/loops, (double)tI5a/(double)tI2*100.0, (double)tI5a/(double)tI5b*100.0);
232 System.err.printf("Summary loops %6d: I5b %6d ms total, %f us/mul, I5b / I2 %f%%, I5b / I5a %f%%%n", loops, tI5b, tI5b*1e3/2.0/loops, (double)tI5b/(double)tI2*100.0, (double)tI5b/(double)tI5a*100.0);
233 }
234
235 public static void main(final String args[]) {
236 org.junit.runner.JUnitCore.main(TestMatrix4f02MulNOUI.class.getName());
237 }
238}
Basic Float math utility functions.
Definition: FloatUtil.java:83
static void multMatrix(final float[] a, final int a_off, final float[] b, final int b_off, final float[] d, final int d_off)
Multiply matrix: [d] = [a] x [b].
Definition: FloatUtil.java:785
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
Definition: Matrix4f.java:89
final Matrix4f mul(final Matrix4f b)
Multiply matrix: [this] = [this] x [b].
Definition: Matrix4f.java:726
Matrix4f load(final Matrix4f src)
Load the values of the given matrix src to this matrix.
Definition: Matrix4f.java:186
float determinant()
Returns the determinant of this matrix.
Definition: Matrix4f.java:450
Basic 4x4 float matrix implementation using fields for intensive use-cases (host operations).
Definition: Matrix4fb.java:97
float determinant()
Returns the determinant of this matrix.
Definition: Matrix4fb.java:308
Matrix4fb load(final Matrix4fb src)
Load the values of the given matrix b to this matrix.
Definition: Matrix4fb.java:159
final Matrix4fb mul(final Matrix4fb b)
Multiply matrix: [this] = [this] x [b].
Definition: Matrix4fb.java:569