You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2011/10/09 14:35:59 UTC

svn commit: r1180595 - /commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/algorithmic/forward/ForwardModeAlgorithmicDifferentiator.java

Author: luc
Date: Sun Oct  9 12:35:58 2011
New Revision: 1180595

URL: http://svn.apache.org/viewvc?rev=1180595&view=rev
Log:
Added a debug message displaying generated bytecode on VeryfyException.

This message is intended for debug purposes only and should be removed
once Nabla is complete.

Modified:
    commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/algorithmic/forward/ForwardModeAlgorithmicDifferentiator.java

Modified: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/algorithmic/forward/ForwardModeAlgorithmicDifferentiator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/algorithmic/forward/ForwardModeAlgorithmicDifferentiator.java?rev=1180595&r1=1180594&r2=1180595&view=diff
==============================================================================
--- commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/algorithmic/forward/ForwardModeAlgorithmicDifferentiator.java (original)
+++ commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/algorithmic/forward/ForwardModeAlgorithmicDifferentiator.java Sun Oct  9 12:35:58 2011
@@ -19,6 +19,7 @@ package org.apache.commons.nabla.algorit
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.PrintWriter;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.InvocationTargetException;
 import java.util.HashMap;
@@ -32,8 +33,10 @@ import org.apache.commons.nabla.core.Uni
 import org.apache.commons.nabla.core.UnivariateDifferentiable;
 import org.apache.commons.nabla.core.UnivariateDifferentiator;
 import org.objectweb.asm.ClassReader;
+import org.objectweb.asm.ClassVisitor;
 import org.objectweb.asm.ClassWriter;
 import org.objectweb.asm.tree.ClassNode;
+import org.objectweb.asm.util.TraceClassVisitor;
 
 /** Algorithmic differentiator class in forward mode based on bytecode analysis.
  * <p>This class is an implementation of the {@link UnivariateDifferentiator}
@@ -56,7 +59,9 @@ public class ForwardModeAlgorithmicDiffe
 
     /** UnivariateDifferentiable/UnivariateDerivative map. */
     private final HashMap<Class<? extends UnivariateDifferentiable>,
-    Class<? extends UnivariateDerivative>> map;
+                          Class<? extends UnivariateDerivative>> map;
+
+    private final HashMap<String, byte[]> byteCodeMap;
 
     /** Math implementation classes. */
     private final Set<String> mathClasses;
@@ -67,6 +72,7 @@ public class ForwardModeAlgorithmicDiffe
     public ForwardModeAlgorithmicDifferentiator() {
         map = new HashMap<Class<? extends UnivariateDifferentiable>,
         Class<? extends UnivariateDerivative>>();
+        byteCodeMap = new HashMap<String, byte[]>();
         mathClasses = new HashSet<String>();
         addMathImplementation(Math.class);
         addMathImplementation(StrictMath.class);
@@ -120,6 +126,16 @@ public class ForwardModeAlgorithmicDiffe
         } catch (InvocationTargetException ite) {
             throw new DifferentiationException("class {0} instantiation from an instance of class {1} failed ({2})",
                                                derivativeClass.getName(), d.getClass().getName(), ite.getMessage());
+        } catch (VerifyError ve) {
+
+            // TODO: the following statements are for debug purposes only
+            final ClassReader  reader  = new ClassReader(byteCodeMap.get(derivativeClass.getName()));
+            final ClassVisitor visitor = new TraceClassVisitor(new PrintWriter(System.err));
+            reader.accept(visitor, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
+
+            throw new DifferentiationException("class {0} code generated from an instance of class {1} is incorrect ({2})",
+                                               derivativeClass.getName(), d.getClass().getName(), ve.getMessage());
+
         }
 
     }
@@ -179,7 +195,13 @@ public class ForwardModeAlgorithmicDiffe
             // create the derivative class
             final ClassNode   derived = differentiator.getDerivedClass();
             final ClassWriter writer  = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
-            return new DerivativeLoader(differentiableClass).defineClass(derived, writer);
+            final String name = derived.name.replace('/', '.');
+            derived.accept(writer);
+            final byte[] bytecode = writer.toByteArray();
+            Class<? extends UnivariateDerivative> dClass =
+                    new DerivativeLoader(differentiableClass).defineClass(name, bytecode);
+            byteCodeMap.put(name, bytecode);
+            return dClass;
 
         } catch (IOException ioe) {
             throw new DifferentiationException("class {0} cannot be read ({1})",
@@ -198,16 +220,13 @@ public class ForwardModeAlgorithmicDiffe
         }
 
         /** Define a derivative class.
-         * @param classNode differentiated class
-         * @param writer class writer
+         * @param name name of the differentiated class
+         * @param bytecode bytecode of the differentiated class
          * @return a generated derivative class
          */
         @SuppressWarnings("unchecked")
         public Class<? extends UnivariateDerivative>
-        defineClass(final ClassNode classNode, final ClassWriter writer) {
-            final String name = classNode.name.replace('/', '.');
-            classNode.accept(writer);
-            final byte[] bytecode = writer.toByteArray();
+        defineClass(final String name, byte[] bytecode) {
             return (Class<? extends UnivariateDerivative>) defineClass(name, bytecode, 0, bytecode.length);
         }
     }