2012-09-25 2 views
2

ActivePivot이 Java 솔루션인데 어떻게 기존 C++ 라이브러리를 다시 사용할 수 있습니까?ActivePivot 내에서 C++ DLL을 사용할 수 있습니까?

ActivePivot을 기반으로하는 CVA (CounterParty Value Adjustment) 프로젝트를 고려하고 기존 C++ 코드를 ActivePivot에서 복식 집계 배열에 적용하기 위해 Collateral 논리를 적용하고 싶습니다. C++ 코드를 호출하는 특별한 후 처리 프로세서가 있습니까?

답변

3

ActivePivot 후 처리기는 일반적인 Java 클래스입니다. 특별한 것은 없습니다. 따라서 기존 기술을 사용하여 Java 프로그램 내에서 C++ DLL 함수를 호출 할 수 있습니다.

예를 들어 JNA와 BridJ를 사용하면이를 달성 할 수 있습니다. 대부분의 경우와 같이 JNI를 고려하지 않으므로 이러한 저수준 API를 사용할 필요가 없습니다. BridJ와 예를 들어

: 나는 다음 클래스 만든

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

: 처럼 찾고는 C++ 헤더를 감안할 때

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 배의 []와 impa ct는 약 100ms입니다.