namespace internal

// structs

template <
    typename DT,
    int N,
    int M
    >
struct xf_2D_array

// unions

union double_cast_new

// classes

template <
    typename DT,
    bool StepFirst,
    int SampNum,
    bool WithAntithetic
    >
class BSPathGenerator

template <
    typename DT,
    int SampleNm,
    int ASSETS
    >
class CORRAND

template <
    typename DT,
    int PathNm,
    int ASSETS,
    int CFGNM
    >
class CORRAND_2

template <
    typename DT,
    typename RNG,
    int SampleNum,
    int ASSETS
    >
class CORRAND_2_Sequence <DT, RNG, SampleNum, ASSETS, false>

template <
    typename DT,
    typename RNG,
    int SampleNum,
    int ASSETS,
    bool Antithetic
    >
class CORRAND_2_Sequence

template <
    typename DT,
    int SampNum
    >
class CapFloorPathPricer

template <
    typename DT,
    typename RNG,
    bool WithAntithetic
    >
class GaussUniformSequence

template <
    typename DT,
    int SampNum,
    bool WithAntithetic
    >
class HestonPathGenerator <kDTPartialTruncation, DT, SampNum, WithAntithetic>

template <
    typename DT,
    int SampNum,
    bool WithAntithetic
    >
class HestonPathGenerator <kDTQuadraticExponentialMartingale, DT, SampNum, WithAntithetic>

template <
    typename DT,
    int SampNum,
    bool WithAntithetic
    >
class HestonPathGenerator <kDTQuadraticExponential, DT, SampNum, WithAntithetic>

template <
    typename DT,
    int SampNum,
    bool WithAntithetic
    >
class HestonPathGenerator <kDTReflection, DT, SampNum, WithAntithetic>

template <
    DiscreType discrT,
    typename DT,
    int SampNum,
    bool WithAntithetic
    >
class HestonPathGenerator

template <
    typename DT,
    int SampNum,
    bool WithAntithetic
    >
class HestonPathGenerator <kDTFullTruncation, DT, SampNum, WithAntithetic>

template <
    typename DT,
    int SampNum
    >
class HullWhitePathGen

template <
    typename DT,
    int SampNum,
    int ASSETS,
    DiscreType discrT
    >
class MultiAssetHestonPathGenerator

template <
    OptionStyle style,
    typename DT,
    int ASSETS,
    int SampleNum
    >
class MultiAssetPathPricer

template <
    typename DT,
    int ASSETS,
    int SampleNum
    >
class MultiAssetPathPricer <European, DT, ASSETS, SampleNum>

template <
    typename DT,
    bool StepFirst,
    int SampNum,
    bool WithAntithetic
    >
class PathPricer <American, DT, StepFirst, SampNum, WithAntithetic>

template <
    typename DT,
    bool StepFirst,
    int SampNum,
    bool WithAntithetic,
    int MaxSteps
    >
class PathPricer <LongstaffSchwartz, DT, StepFirst, SampNum, WithAntithetic, MaxSteps>

template <
    typename DT,
    bool StepFirst,
    int SampNum,
    bool WithAntithetic
    >
class PathPricer <Digital, DT, StepFirst, SampNum, WithAntithetic>

template <
    typename DT,
    bool StepFirst,
    int SampNum,
    bool WithAntithetic
    >
class PathPricer <BarrierBiased, DT, StepFirst, SampNum, WithAntithetic>

template <
    typename DT,
    bool StepFirst,
    int SampNum,
    bool WithAntithetic
    >
class PathPricer <Asian_GP, DT, StepFirst, SampNum, WithAntithetic>

template <
    typename DT,
    bool StepFirst,
    int SampNum
    >
class PathPricer <Cliquet, DT, StepFirst, SampNum, false>

template <
    typename DT,
    bool StepFirst,
    int SampNum,
    bool WithAntithetic
    >
class PathPricer <BarrierNoBiased, DT, StepFirst, SampNum, WithAntithetic>

template <
    typename DT,
    bool StepFirst,
    int SampNum,
    bool WithAntithetic
    >
class PathPricer <EuropeanBypass, DT, StepFirst, SampNum, WithAntithetic>

template <
    typename DT,
    bool StepFirst,
    int SampNum,
    bool WithAntithetic
    >
class PathPricer <Asian_AP, DT, StepFirst, SampNum, WithAntithetic>

template <
    typename DT,
    bool StepFirst,
    int SampNum,
    bool WithAntithetic
    >
class PathPricer <European, DT, StepFirst, SampNum, WithAntithetic>

template <
    OptionStyle style,
    typename DT,
    bool StepFirst,
    int SampNum,
    bool WithAntithetic,
    int MaxSteps = 1024
    >
class PathPricer

template <
    typename DT,
    bool StepFirst,
    int SampNum,
    bool WithAntithetic
    >
class PathPricer <Asian_AS, DT, StepFirst, SampNum, WithAntithetic>

template <
    typename DT,
    typename RNG
    >
class RNGSequence

template <
    typename DT,
    typename RNG
    >
class RNGSequence_2

template <
    typename DT,
    typename RNG
    >
class RNGSequence_Heston_QuadraticExponential

template <
    typename DT,
    int LEN
    >
class TimeGrid

template <
    typename DT,
    int LEN2
    >
class TreeInstrument <DT, 1, LEN2>

template <
    typename DT,
    int LEN2
    >
class TreeInstrument <DT, 2, LEN2>

template <
    typename DT,
    int LEN2
    >
class TreeInstrument <DT, 0, LEN2>

template <
    typename DT,
    int IT,
    int LEN2
    >
class TreeInstrument

template <
    typename DT,
    int LEN2
    >
class TreeInstrument <DT, 3, LEN2>

// global variables

const ap_uint <63> initPara[128]

GenAtA

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int COEFNM,
    int SampNum,
    bool StepFirst
    >
void GenAtA (
    ap_uint <16> steps,
    DT underlying,
    DT strike,
    DT invStk,
    hls::stream <DT>& priceStrmIn,
    hls::stream <DT>& outStrm,
    hls::stream <DT> matrixOut [3 *(COEFNM-1)]
    )

generate the AtA from price data

MergeBuff

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int COEFNM
    >
void MergeBuff (
    ap_uint <27> steps,
    hls::stream <DT> matrixIn [3 *(COEFNM-1)],
    hls::stream <DT>& outStrm
    )

accumulate the AtA matrix data to a new AtA and output as stream

MonteCarloModel

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    typename RNG,
    typename PathGeneratorT,
    typename PathPricerT,
    typename RNGSeqT,
    int VariateNum,
    int SampNum,
    int COEFNM
    >
void MonteCarloModel (
    ap_uint <16> steps,
    DT underlying,
    DT strike,
    DT invStk,
    RNG rngInst [VariateNum],
    PathGeneratorT pathGenInst [1],
    PathPricerT pathPriInst [1],
    RNGSeqT rngSeqInst [1],
    hls::stream <DT>& priceStrm,
    hls::stream <DT>& matrixStrm
    )

The American engine Monte Carlo Model for calibration. Process 1024 paths (samples) per time step.

MultiMonteCarloModel

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    typename RNG,
    int UN,
    typename PathGeneratorT,
    typename PathPricerT,
    typename RNGSeqT,
    int VariateNum,
    int SampNum,
    int COEFNM
    >
void MultiMonteCarloModel (
    ap_uint <16> steps,
    DT underlying,
    DT strike,
    DT invStk,
    RNG rngInst [UN][VariateNum],
    PathGeneratorT pathGenInst [UN][1],
    PathPricerT pathPriInst [UN][1],
    RNGSeqT rngSeqInst [UN][1],
    hls::stream <DT> priceOutStrm [UN],
    hls::stream <DT> matrixStrm [UN]
    )

Monte Carlo with an unroll number UN,.

write_ddr_price

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int UN,
    int Size
    >
void write_ddr_price (
    int depth,
    int offset,
    hls::stream <DT> in_strm [UN],
    ap_uint <UN*Size>* Out
    )

write the priceMat data to DDR

write_ddr_matrix

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int Size
    >
void write_ddr_matrix (
    int depth,
    DT* Buffer,
    ap_uint <Size>* Out
    )

write matrix data to DDR,

MergeMatrixUN

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int UN
    >
void MergeMatrixUN (
    int depth,
    hls::stream <DT> matrixIn [UN],
    hls::stream <DT>& outStrm
    )

convert the UN streams from MultiMonteCarloModel to

MergeMatrixIter

#include "xf_fintech/early_exercise.hpp"
template <typename DT>
void MergeMatrixIter (
    int depth,
    hls::stream <DT>& matrixIn,
    DT* Buffer,
    int k
    )

save iterations of matdata to Buffer

MCProcess

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    typename RNG,
    int UN,
    typename PathGeneratorT,
    typename PathPricerT,
    typename RNGSeqT,
    int VariateNum,
    int SampNum,
    int COEFNM,
    int SZ
    >
void MCProcess (
    ap_uint <16> steps,
    DT underlying,
    DT strike,
    DT invStk,
    int mat_nm,
    RNG rngInst [UN][VariateNum],
    PathGeneratorT pathGenInst [UN][1],
    PathPricerT pathPriInst [UN][1],
    RNGSeqT rngSeqInst [UN][1],
    DT* Buffer,
    int k,
    int offset,
    ap_uint <UN*8*sizeof (DT)>* pOut
    )

Monte Carlo Process.

MCIteration

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    typename RNG,
    int UN,
    typename PathGeneratorT,
    typename PathPricerT,
    typename RNGSeqT,
    int VariateNum,
    int SampNum,
    int COEFNM,
    int ITER,
    int SZ
    >
void MCIteration (
    ap_uint <16> steps,
    DT underlying,
    DT strike,
    DT invStk,
    int mat_nm,
    int iter,
    RNG rngInst [UN][VariateNum],
    PathGeneratorT pathGenInst [UN][1],
    PathPricerT pathPriInst [UN][1],
    RNGSeqT rngSeqInst [UN][1],
    ap_uint <UN*8*sizeof (DT)>* pOut,
    ap_uint <8*sizeof (DT)>* mOut,
    hls::stream <int>& phase_end
    )

Run multiple times of Monte Carlo Process,.

GenAty

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int COEFNM,
    int MAXPATH
    >
void GenAty (
    hls::stream <DT>& pStrm,
    ap_uint <16> paths,
    DT dF,
    DT y [MAXPATH],
    DT pBuff [MAXPATH],
    DT coef [COEFNM],
    DT outAty [COEFNM],
    bool optionType,
    DT strike,
    DT invStk
    )

Read samples for external memory.

SVDDec

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int COEFNM
    >
void SVDDec (
    ap_uint <16> steps,
    hls::stream <DT>& inStrm,
    hls::stream <DT>& Ustrm,
    hls::stream <DT>& Vstrm,
    hls::stream <DT>& Sstrm
    )

SVD process.

MultiSVD

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int COEFNM,
    int UN
    >
void MultiSVD (
    ap_uint <16> steps,
    hls::stream <DT> inStrm [UN],
    hls::stream <DT> Ustrm [UN],
    hls::stream <DT> Vstrm [UN],
    hls::stream <DT> Sstrm [UN]
    )

execute SVD in parallel

readin_ddr

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int UN,
    int samplesNm
    >
void readin_ddr (
    const int loopNm,
    const int steps,
    ap_uint <8*sizeof (DT)*UN>* in_data,
    hls::stream <ap_uint <8*sizeof (DT)*UN>>& outStrm
    )

read in the price mat data from DDR to stream

read_AtA

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int UN,
    int ORDER
    >
void read_AtA (
    int steps,
    ap_uint <8*sizeof (DT)>* in_data,
    hls::stream <DT>& dout_strm
    )

read in matrix B data from DDR

SplitStrm

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int COEFNM,
    int UN
    >
void SplitStrm (
    ap_uint <16> steps,
    hls::stream <DT>& inStrm,
    hls::stream <DT> outStrm [UN]
    )

split the matdata by UN_STEP, prepare the data for svd

MergeStrm

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int COEFNM,
    int UN
    >
void MergeStrm (
    ap_uint <16> steps,
    hls::stream <DT> mUstrm [UN],
    hls::stream <DT> mVstrm [UN],
    hls::stream <DT> mSstrm [UN],
    hls::stream <DT>& Ustrm,
    hls::stream <DT>& Vstrm,
    hls::stream <DT>& Sstrm
    )

convert svd result from UN(UN_STEP) streams to 1 stream

CalCoef

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int COEFNM,
    int SamplesNm,
    int UN
    >
void CalCoef (
    ap_uint <16> steps,
    ap_uint <16> paths,
    bool optionType,
    DT dF,
    DT strike,
    DT invStk,
    hls::stream <DT>& Ustrm,
    hls::stream <DT>& Vstrm,
    hls::stream <DT>& Sstrm,
    hls::stream <DT> pStrm [UN],
    hls::stream <DT> coefStrm [COEFNM]
    )

calculate the coefficients and output as streams

write_ddr

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int UN,
    int Size
    >
void write_ddr (
    int depth,
    hls::stream <DT> in_strm [UN],
    ap_uint <UN*Size>* Out
    )

write the coeff data to DDR, the data width is COEFNM*double

read_coef

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT,
    int UN,
    int Size,
    int COEFNM,
    bool SF,
    int SN,
    int MaxSteps,
    bool Antithetic
    >
void read_coef (
    hls::stream <int>& phase_start,
    int depth,
    ap_uint <COEFNM*Size>* In,
    PathPricer <LongstaffSchwartz, DT, SF, SN, Antithetic, MaxSteps> pathPriInst [UN][1]
    )

Read the coefficients from DDR,.

MCAmericanEngineCalibrateCalc

#include "xf_fintech/early_exercise.hpp"
template <
    typename DT = double,
    int UN = 2,
    int UN_STEP = 2
    >
void MCAmericanEngineCalibrateCalc (
    hls::stream <int>& phase_start,
    hls::stream <int>& phase_end,
    DT timeLength,
    DT riskFreeRate,
    DT strike,
    bool optionType,
    ap_uint <8*sizeof (DT)*UN>* priceIn,
    ap_uint <8*sizeof (DT)>* matIn,
    ap_uint <8*sizeof (DT)*4>* coefOut,
    unsigned int calibSamples = 4096,
    unsigned int timeSteps = 100
    )

American Option Pricing Engine using Monte Carlo Method. Calibrate kernel: this kernel reads the sample price data from external memory and use them to calculate the coefficient.

Parameters:

DT supported data type including double and float data type, which decides the precision of result, default double-precision data type.
UN number of Monte Carlo Module in parallel (in path dimension), which affects the latency and resources utilization, default 2. [this unroll number should be equal to UN in MCAmericanEnginePresample]
UN_STEP number of Monte Carlo Module in parallel (in time steps dimension), which affects the latency and resources utilization, default 2. [this Unroll is completely resource bounded, unrelated to other params]
phase_start phase start
phase_end phase end
timeLength the time length of contract from start to end.
riskFreeRate risk-free interest rate.
strike the strike price also known as exericse price, which is settled in the contract.
optionType option type. 1: call option, 0: put option.
priceIn the price data, read in from DDR or HBM
matIn the matrix data, read in from DDR or HBM
coefOut the coef data that calculated by this kernel, the data can be stored to DDR or HBM
calibSamples sample numbers that used in calibration, default 4096.
timeSteps the number of discrete steps from 0 to T, T is the expiry time, default 100.

linearInterpolation

#include "xf_fintech/linear_interpolation.hpp"
template <typename DT>
DT linearInterpolation (
    DT x,
    int len,
    DT* arrX,
    DT* arrY
    )

linearInterpolation 1D linear interpolation

Parameters:

DT data type supported include float and double.
x interpolation coordinate x
len array of length
arrX array of coordinate x
arrY array of coordinate y

Returns:

return interpolation coordinate y

linearInterpolation2D

#include "xf_fintech/linear_interpolation.hpp"
template <typename DT>
DT linearInterpolation2D (
    DT x,
    DT y,
    int xLen,
    int yLen,
    DT* arrX,
    DT* arrY,
    DT* arrZ
    )

linearInterpolation 2D linear interpolation

Parameters:

DT data type supported include float and double.
x interpolation coordinate x
y interpolation coordinate y
xLen array of coordinate x of length
yLen array of coordinate y of length
arrX array of coordinate x
arrY array of coordinate y
arrZ array of coordinate z

Returns:

return interpolation coordinate z

pentadiag_step

#include "xf_fintech/pentadiag_cr.hpp"
template <
    typename T,
    unsigned int P_SIZE
    >
void pentadiag_step (
    T a [P_SIZE],
    T b [P_SIZE],
    T c [P_SIZE],
    T d [P_SIZE],
    T e [P_SIZE],
    T r [P_SIZE],
    T a_out [P_SIZE],
    T b_out [P_SIZE],
    T c_out [P_SIZE],
    T d_out [P_SIZE],
    T e_out [P_SIZE],
    T r_out [P_SIZE],
    int k
    )

Executes one step of odd-even elimination. For each row it calculates new diagonal element and right hand side element based on the paper.

Structure of input matrix:

\[\begin{split}\begin{vmatrix} a & d & e & 0 & 0\\ b & c & d & e & 0\\ a & b & c & d & e\\ 0 & a & b & c & d\\ 0 & 0 & a & b & c \end{vmatrix}\end{split}\]

Parameters:

T data type used in whole function (double by default)
P_SIZE Size of the operating matrix
c
  • Main diagonal
b
  • First lower
a
  • Second lower
d
  • First upper
e
  • Second upper
r
  • Right hand side vector of length n
k
  • Number of current calculating step. It is used to calculate indexes of diagonals
c_out
  • Main diagonal output
b_out
  • First lower output
a_out
  • Second lower output
d_out
  • First upper output
e_out
  • Second upper output
r_out
  • Right hand side vector of length n

trsv_step

#include "xf_fintech/trsv.hpp"
template <
    class T,
    unsigned int N,
    unsigned int NCU
    >
void trsv_step (
    T inlow [N],
    T indiag [N],
    T inup [N],
    T inrhs [N],
    T outlow [N],
    T outdiag [N],
    T outup [N],
    T outrhs [N]
    )

Executes one step of odd-even elimination.

For each row it calculates new diagonal element and right hand side element.

.

Please note the algorithm is very sensitive to zeros in main diagonal.

Any zeros in main diagonal will lead to division by zero and algorithm fail.

Parameters:

T data type used in whole function (double by default)
N Size of the operating matrix
NCU Number of compute units working in parallel
inlow Input vector of lower diagonal
indiag Input vector of main diagonal
inup Input vector of upper diagonal
inrhs Input vector of Right hand side
outlow Output vector of lower diagonal
outdiag Output vector of main diagonal
outup Output vector of upper diagonal
outrhs Output vector of Right hand side