Some checks failed
CI / Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }} (x64, ubuntu-latest, 1.10) (push) Has been cancelled
CI / Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }} (x64, ubuntu-latest, 1.6) (push) Has been cancelled
CI / Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }} (x64, ubuntu-latest, pre) (push) Has been cancelled
110 lines
4.1 KiB
Julia
110 lines
4.1 KiB
Julia
module Interpreter
|
|
using CUDA
|
|
using ..ExpressionProcessing
|
|
|
|
export interpret
|
|
|
|
"Interprets the given expressions with the values provided.
|
|
# Arguments
|
|
- expressions::Vector{ExpressionProcessing.PostfixType} : The expressions to execute in postfix form
|
|
- variables::Matrix{Float64} : The variables to use. Each column is mapped to the variables x1..xn
|
|
- parameters::Vector{Vector{Float64}} : The parameters to use. Each Vector contains the values for the parameters p1..pn. The number of parameters can be different for every expression
|
|
"
|
|
function interpret(expressions::Vector{ExpressionProcessing.PostfixType}, variables::Matrix{Float64}, parameters::Vector{Vector{Float64}})
|
|
# TODO:
|
|
# create CUDA array for calculation results
|
|
variableRows = size(variables, 1)
|
|
cudaVars = CuArray(variables)
|
|
cudaParams = create_cuda_array(parameters, NaN64)
|
|
cudaExprs = create_cuda_array(expressions, ExpressionElement(EMPTY, 0))
|
|
cudaStepsize = CuArray([get_max_inner_length(expressions), get_max_inner_length(parameters)]) # put into seperate cuArray, as this is static and would be inefficient to send seperatly to every kernel
|
|
|
|
# Start kernel for each expression to ensure that no warp is working on different expressions
|
|
for i in eachindex(expressions)
|
|
kernel = @cuda launch=false interpret_expression(cudaExprs, cudaVars, cudaParams, cudaStepsize, i)
|
|
config = launch_configuration(kernel.fun)
|
|
threads = min(variableRows, config.threads)
|
|
blocks = cld(variableRows, threads)
|
|
|
|
kernel(cudaExprs, cudaVars, cudaParams, cudaStepsize, i; threads, blocks)
|
|
end
|
|
end
|
|
|
|
function interpret_expression(expressions::CuDeviceArray{ExpressionElement}, variables::CuDeviceArray{Float64}, parameters::CuDeviceArray{Float64}, stepsize::CuDeviceArray{Int}, exprIndex::Int)
|
|
firstExprIndex = (exprIndex - 1 * stepsize[1]) + 1 # Inclusive
|
|
lastExprIndex = firstExprIndex + stepsize[1] # Exclusive
|
|
firstParamIndex = (exprIndex - 1 * stepsize[2]) + 1 # Inclusive
|
|
# lastParamIndex = firstParamIndex + stepsize[2] # Exclusive (probably not needed)
|
|
|
|
for i in firstExprIndex:lastExprIndex
|
|
# TODO Implement interpreter
|
|
# - start at firstExprIndex and interpret until the first ExpressionElement is "Empty" or we reached lastExprIndex
|
|
end
|
|
|
|
return
|
|
end
|
|
|
|
|
|
"Retrieves the number of entries for the largest inner vector"
|
|
function get_max_inner_length(vec::Vector{Vector{T}})::Int where T
|
|
maxLength = 0
|
|
@inbounds for i in eachindex(vec)
|
|
if length(vec[i]) > maxLength
|
|
maxLength = length(vec[i])
|
|
end
|
|
end
|
|
|
|
return maxLength
|
|
end
|
|
|
|
"Returns a CuArray filed with the data provided. The inner vectors do not have to have the same length. All missing elements will be the value ```invalidElement```"
|
|
function create_cuda_array(data::Vector{Vector{T}}, invalidElement::T)::CuArray{T} where T
|
|
dataCols = get_max_inner_length(data)
|
|
dataRows = length(data)
|
|
dataMat = convert_to_matrix(data, invalidElement)
|
|
cudaArr = CuArray{T}(undef, dataCols, dataRows) # length(parameters) == number of expressions
|
|
copyto!(cudaArr, dataMat)
|
|
|
|
return cudaArr
|
|
end
|
|
|
|
"Converts a vector of vectors into a matrix. The inner vectors do not need to have the same length.
|
|
|
|
All entries that cannot be filled have ```invalidElement``` as their value
|
|
"
|
|
function convert_to_matrix(vec::Vector{Vector{T}}, invalidElement::T)::Matrix{T} where T
|
|
vecCols = get_max_inner_length(vec)
|
|
vecRows = length(vec)
|
|
vecMat = fill(invalidElement, vecCols, vecRows)
|
|
|
|
for i in eachindex(vec)
|
|
vecMat[:,i] = copyto!(vecMat[:,i], vec[i])
|
|
end
|
|
|
|
return vecMat
|
|
end
|
|
|
|
|
|
|
|
# @deprecate InterpretExplicit!(op::Operator, x, y) interpret_expression(expression, variables, parameters, exprIndex::Int)
|
|
# Kernel
|
|
function InterpretExplicit!(op::Operator, x, y)
|
|
index = (blockIdx().x - 1) * blockDim().x + threadIdx().x
|
|
stride = gridDim().x * blockDim().x
|
|
|
|
if op == ADD
|
|
# @cuprintln("Performing Addition") # Will only be displayed when the GPU is synchronized
|
|
for i = index:stride:length(y)
|
|
@inbounds y[i] += x[i]
|
|
end
|
|
return
|
|
elseif op == SUBTRACT
|
|
# @cuprintln("Performing Subtraction") # Will only be displayed when the GPU is synchronized
|
|
for i = index:stride:length(y)
|
|
@inbounds y[i] -= x[i]
|
|
end
|
|
return
|
|
end
|
|
end
|
|
|
|
end |