kernelMap = new HashMap<>();
+ OpenCLKernel disposeKernel = mock(OpenCLKernel.class);
+ kernelMap.put(method.getName(), disposeKernel);
+ InvocationHandler sut = new OpenCLDevice.OpenCLInvocationHandler(program, kernelMap);
+ // test
+ sut.invoke(PROXY, method, ARGS);
+ sut.invoke(PROXY, method, ARGS);
+ }
+
+
+ static class ReservedInterfaceMethods {
+ public void put() {
+ }
+
+ ;
+
+ public void get() {
+ }
+
+ ;
+
+ public void begin() {
+ }
+
+ ;
+
+ public void end() {
+ }
+
+ ;
+
+ public void dispose() {
+ }
+
+ ;
+
+ public void getProfileInfo() {
+ }
+
+ ;
+ }
+}
diff --git a/src/test/java/com/aparapi/device/Utils.java b/src/test/java/com/aparapi/device/Utils.java
new file mode 100644
index 00000000..571c79fe
--- /dev/null
+++ b/src/test/java/com/aparapi/device/Utils.java
@@ -0,0 +1,45 @@
+/**
+ * Copyright (c) 2016 - 2018 Syncleus, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.aparapi.device;
+
+import com.aparapi.internal.opencl.OpenCLPlatform;
+
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class Utils {
+
+ static final int DEVICE_ID = 1;
+
+ static OpenCLDevice createDevice(OpenCLPlatform platform, Device.TYPE type) {
+ return new OpenCLDevice(platform, DEVICE_ID, type);
+ }
+
+ static OpenCLPlatform createPlatform(String name) {
+ OpenCLPlatform platform = mock(OpenCLPlatform.class);
+ when(platform.getName()).thenReturn(name);
+ return platform;
+ }
+
+ static Method methodByName(String name, Class> clazz) {
+ return Arrays.stream(clazz.getMethods())
+ .filter(m -> m.getName().equals(name))
+ .findFirst().orElseThrow(() -> new RuntimeException("method with name not found " + name));
+ }
+}
diff --git a/src/test/java/com/aparapi/internal/kernel/KernelDeviceProfileTest.java b/src/test/java/com/aparapi/internal/kernel/KernelDeviceProfileTest.java
new file mode 100644
index 00000000..2bfa61ce
--- /dev/null
+++ b/src/test/java/com/aparapi/internal/kernel/KernelDeviceProfileTest.java
@@ -0,0 +1,165 @@
+/**
+ * Copyright (c) 2016 - 2018 Syncleus, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.aparapi.internal.kernel;
+
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.util.Map;
+
+import static com.aparapi.internal.kernel.ProfilingEvent.*;
+import static com.aparapi.internal.kernel.Utils.createKernelDeviceProfile;
+import static junit.framework.TestCase.assertEquals;
+import static junit.framework.TestCase.assertTrue;
+
+public class KernelDeviceProfileTest {
+ private static final double MILLION = 1000 * 1000;
+
+ @Test
+ public void shouldReturnZeroForElapsedTimeForStageStart() {
+ KernelDeviceProfile sut = createKernelDeviceProfile();
+ assertEquals(0d, sut.getElapsedTimeCurrentThread(START.ordinal()));
+ }
+
+ @Test
+ public void shouldReturnNonZeroForElapsedTimeIfProfilingEventWasCalled() {
+ KernelDeviceProfile sut = createKernelDeviceProfile();
+ sut.onEvent(PREPARE_EXECUTE);
+ long[] currentTimes = getCurrentTimesArrayForCurrentThread(sut);
+ long prepareExecuteTime = currentTimes[PREPARE_EXECUTE.ordinal()];
+ assertTrue(prepareExecuteTime > 0);
+ assertEquals(prepareExecuteTime / MILLION, sut.getElapsedTimeCurrentThread(PREPARE_EXECUTE.ordinal()));
+ }
+
+ @Test
+ public void shouldReturnNaNForElapsedTimeIfProfilingEventWasNotCalled() {
+ KernelDeviceProfile sut = createKernelDeviceProfile();
+ assertEquals(Double.NaN, sut.getElapsedTimeCurrentThread(EXECUTED.ordinal()));
+ }
+
+ @Test
+ public void shouldReturnZeroElapsedTimeIfNothingWasProfiled() {
+ KernelDeviceProfile sut = createKernelDeviceProfile();
+ assertEquals(0d, sut.getCumulativeElapsedTimeAllCurrentThread());
+ }
+
+ @Test
+ public void shouldReturnZeroForProfiledEventsIfExecutedEventWasNotCalled() {
+ KernelDeviceProfile sut = createKernelDeviceProfile();
+ sut.onEvent(START);
+ sut.onEvent(ProfilingEvent.PREPARE_EXECUTE);
+ sut.onEvent(ProfilingEvent.CLASS_MODEL_BUILT);
+ assertEquals(0d, sut.getCumulativeElapsedTimeAllCurrentThread());
+ }
+
+ @Test
+ public void shouldReturnSumOfProfileTimesForProfiledEventsAfterExecutedEventIsProfiled() {
+ KernelDeviceProfile sut = createKernelDeviceProfile();
+ sut.onEvent(START);
+ sut.onEvent(ProfilingEvent.PREPARE_EXECUTE);
+ sut.onEvent(ProfilingEvent.EXECUTED);
+ long[] accumulatedTimes = getAccumulatedTimesArrayForCurrentThread(sut);
+ long prepareExecuteTime = accumulatedTimes[PREPARE_EXECUTE.ordinal()];
+ long executedTime = accumulatedTimes[EXECUTED.ordinal()];
+ assertTrue(prepareExecuteTime > 0);
+ assertTrue(executedTime > 0);
+ assertEquals((double) (prepareExecuteTime + executedTime), sut.getCumulativeElapsedTimeAllCurrentThread());
+ }
+
+ @Test
+ public void shouldReturnZeroForElapsedTimeLastThreadForStageStart() {
+ KernelDeviceProfile sut = createKernelDeviceProfile();
+ assertEquals(0d, sut.getElapsedTimeLastThread(START.ordinal()));
+ }
+
+ @Test
+ public void shouldReturnNanForElapsedTimeLastThreadIfNothingWasProfiled() {
+ KernelDeviceProfile sut = createKernelDeviceProfile();
+ assertEquals(Double.NaN, sut.getElapsedTimeLastThread(EXECUTED.ordinal()));
+ }
+
+ @Test
+ public void shouldReturnDifferenceBetweenElapsedTimesForElapsedTimeLastThreadIfTheyWereProfiled() {
+ KernelDeviceProfile sut = createKernelDeviceProfile();
+ sut.onEvent(ProfilingEvent.START);
+ sut.onEvent(ProfilingEvent.CLASS_MODEL_BUILT);
+ sut.onEvent(ProfilingEvent.INIT_JNI);
+ sut.onEvent(EXECUTED);
+ long[] currentTimes = getCurrentTimesArrayForCurrentThread(sut);
+ long jniExecuteTime = currentTimes[INIT_JNI.ordinal()];
+ long classModelBuiltTime = currentTimes[CLASS_MODEL_BUILT.ordinal()];
+ assertTrue(classModelBuiltTime > 0);
+ assertTrue(jniExecuteTime > 0);
+ assertEquals((jniExecuteTime - classModelBuiltTime) / MILLION, sut.getElapsedTimeLastThread(INIT_JNI.ordinal()));
+ }
+
+ @Test
+ public void shouldReturnDifferenceBetweenElapsedTimesForLastThreadByIndex() {
+ KernelDeviceProfile sut = createKernelDeviceProfile();
+ sut.onEvent(ProfilingEvent.START);
+ sut.onEvent(ProfilingEvent.CLASS_MODEL_BUILT);
+ sut.onEvent(ProfilingEvent.INIT_JNI);
+ sut.onEvent(EXECUTED);
+ long[] currentTimes = getCurrentTimesArrayForCurrentThread(sut);
+ long jniExecuteTime = currentTimes[INIT_JNI.ordinal()];
+ long classModelBuiltTime = currentTimes[CLASS_MODEL_BUILT.ordinal()];
+ assertTrue(classModelBuiltTime > 0);
+ assertTrue(jniExecuteTime > 0);
+// assertEquals((jniExecuteTime - classModelBuiltTime) / MILLION, sut.getElapsedTimeLastThread(INIT_JNI.ordinal(), EXECUTED.ordinal()));
+ assertTrue(sut.getElapsedTimeLastThread(INIT_JNI.ordinal(), EXECUTED.ordinal()) > 0);
+ }
+
+ private static long[] getCurrentTimesArrayForCurrentThread(KernelDeviceProfile kernelDeviceProfile) {
+ try {
+ Map allAccumulators = getAccumulatorMap(kernelDeviceProfile);
+ Object currentThreadAccumulator = allAccumulators.get(Thread.currentThread());
+ return (long[]) getFieldAndMakeItAccessible(currentThreadAccumulator.getClass(), "currentTimes")
+ .get(currentThreadAccumulator);
+ } catch (Exception e) {
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ private static long[] getAccumulatedTimesArrayForCurrentThread(KernelDeviceProfile kernelDeviceProfile) {
+ try {
+ Map allAccumulators = getAccumulatorMap(kernelDeviceProfile);
+ Object currentThreadAccumulator = allAccumulators.get(Thread.currentThread());
+ return (long[]) getFieldAndMakeItAccessible(currentThreadAccumulator.getClass(), "accumulatedTimes")
+ .get(currentThreadAccumulator);
+ } catch (Exception e) {
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ private static Map getAccumulatorMap(KernelDeviceProfile kernelDeviceProfile) {
+ try {
+ return (Map) getFieldAndMakeItAccessible(KernelDeviceProfile.class, "accs").get(kernelDeviceProfile);
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e.getMessage());
+ }
+ }
+
+ private static Field getFieldAndMakeItAccessible(Class> clazz, String fieldName) {
+ Field field = null;
+ try {
+ field = clazz.getDeclaredField(fieldName);
+ } catch (NoSuchFieldException e) {
+ throw new RuntimeException(e.getMessage());
+ }
+ field.setAccessible(true);
+ return field;
+ }
+}
diff --git a/src/test/java/com/aparapi/internal/kernel/KernelRunnerCheckPropertiesSetTest.java b/src/test/java/com/aparapi/internal/kernel/KernelRunnerCheckPropertiesSetTest.java
new file mode 100644
index 00000000..0f72dcf9
--- /dev/null
+++ b/src/test/java/com/aparapi/internal/kernel/KernelRunnerCheckPropertiesSetTest.java
@@ -0,0 +1,86 @@
+/**
+ * Copyright (c) 2016 - 2018 Syncleus, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.aparapi.internal.kernel;
+
+import com.aparapi.opencl.OpenCL;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.mockito.internal.util.collections.Sets;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.Collection;
+import java.util.HashSet;
+
+import static java.util.Arrays.asList;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(Parameterized.class)
+public class KernelRunnerCheckPropertiesSetTest {
+ private final String methodName;
+ private final String capability;
+
+ public KernelRunnerCheckPropertiesSetTest(String methodName, String capability) {
+ this.methodName = methodName;
+ this.capability = capability;
+ }
+
+ @Parameterized.Parameters
+ public static Collection