作为一个ActivePivot解决方案,我如何重用其中的现有C++库?
我考虑了一个基于ActivePivot的CVA (CounterParty值调整)项目,我想重用我现有的C++代码,将我的附属逻辑应用于ActivePivot的doubles聚合数组。有没有特殊的后处理器来调用C++代码?
发布于 2012-09-26 02:05:35
Java后处理器是一个普通的ActivePivot类。它没有什么特别之处。因此您可以使用任何现有技术,因此可以在C++程序内部调用Java函数。
例如,这可以通过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();
}
}在性能方面,我在这里使用了大小为10000的2.000 double[],其影响约为100ms
https://stackoverflow.com/questions/12588480
复制相似问题