【问题标题】:Can I use my C++ DLL within ActivePivot?我可以在 ActivePivot 中使用我的 C++ DLL 吗?
【发布时间】:2012-09-25 17:53:48
【问题描述】:

ActivePivot 是一个 Java 解决方案,我如何重用其中的现有 C++ 库?

我考虑一个基于 ActivePivot 的 CVA(CounterParty 值调整)项目,我想重用我现有的 C++ 代码,将我的 Collat​​eral 逻辑应用于 ActivePivot 的双精度聚合数组。是否有专门的后处理器来调用 C++ 代码?

【问题讨论】:

    标签: java dll activepivot


    【解决方案1】:

    ActivePivot 后处理器是一个普通的 Java 类。它没有什么特别之处。因此,您可以使用任何现有技术,在 Java 程序中调用 C++ DLL 函数。

    这可以通过 JNA 和 BridJ 来实现。我不考虑 JNI,因为在大多数情况下,您不需要使用这么低级的 API。

    例如,对于 BridJ: 给定一个 C++ 标头,如下所示:

    __declspec(dllexport) int multiply(double multiplier, int size, double* const vector);
    

    我做了以下课程:

    import org.bridj.BridJ;
    import org.bridj.Pointer;
    import org.bridj.ann.Library;
    import org.bridj.ann.Runtime;
    import org.bridj.cpp.CPPRuntime;
    
    // Generated with http://code.google.com/p/jnaerator/
    @Library(CPP_Collateral.JNA_LIBRARY_NAME)
    @Runtime(CPPRuntime.class)
    public class CPP_Collateral {
    public static final String JNA_LIBRARY_NAME = "dummy";
    
    static {
        // In eclipse, the DLL will be loaded from a resource folder
        // Else, one should add a program property: -Djava.library.path
        BridJ.addLibraryPath("src/main/resources/DLL");
    
        BridJ.register();
    }
    
    /**
     * My dummy.dll has one method:
     * int multiply(double multiplier, int size, double* const vector)
     */
    public static native int multiply(double multiplier, int size, Pointer<Double> vector);
    }
    

    而我的 IPostProcessor 很简单

    @Override
    protected Object doEvaluation(ILocation location, Object[] underlyingMeasures) throws QuartetException {
        double[] asArray = (double[]) underlyingMeasures[0];
    
        if (asArray == null) {
            return null;
        } else {
            // Size of the array
            int size = asArray.length;
    
            // Allocate a Pointer to provide the double[] to the C++ DLL
            Pointer<Double> pCount = allocateDoubles(size);
    
            pCount.setDoubles(asArray);
    
            CPP_Collateral.multiply(2D, size, pCount);
    
            // Read again: the double[] is copied back in the heap
            return pCount.getDoubles();
        }
    }
    

    在性能方面,我这里处理了 2.000 个大小为 10000 的 double[],影响约为 100ms

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      • 2013-07-30
      • 2020-10-11
      • 2018-07-13
      • 1970-01-01
      • 2012-07-09
      • 2012-07-02
      相关资源
      最近更新 更多