finished interpreter. still need to test it
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

This commit is contained in:
Daniel 2024-08-18 11:55:41 +02:00
parent 62d10845e9
commit 929789b6ff
2 changed files with 79 additions and 50 deletions

View File

@ -11,7 +11,7 @@ export interpret
- variables::Matrix{Float64} : The variables to use. Each column is mapped to the variables x1..xn - 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 - 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}}) function interpret(expressions::Vector{ExpressionProcessing.PostfixType}, variables::Matrix{Float64}, parameters::Vector{Vector{Float64}})::Matrix{Float64}
variableCols = size(variables, 2) # number of sets of variables to use for each expression variableCols = size(variables, 2) # number of sets of variables to use for each expression
cudaVars = CuArray(variables) cudaVars = CuArray(variables)
cudaParams = create_cuda_array(parameters, NaN64) # column corresponds to data for one expression cudaParams = create_cuda_array(parameters, NaN64) # column corresponds to data for one expression
@ -31,75 +31,87 @@ function interpret(expressions::Vector{ExpressionProcessing.PostfixType}, variab
kernel(cudaExprs, cudaVars, cudaParams, cudaResults, cudaStepsize, i; threads, blocks) kernel(cudaExprs, cudaVars, cudaParams, cudaResults, cudaStepsize, i; threads, blocks)
end end
# TODO: Wait for all the kernels to finish to return the result
# return cudaResults
println(cudaResults)
return cudaResults
end end
#TODO: Add @inbounds to all indexing after it is verified that all works https://cuda.juliagpu.org/stable/development/kernel/#Bounds-checking
const MAX_STACK_SIZE = 25 # The max number of values the expression can have. so Constant values, Variables and parameters const MAX_STACK_SIZE = 25 # The max number of values the expression can have. so Constant values, Variables and parameters
function interpret_expression(expressions::CuDeviceArray{ExpressionElement}, variables::CuDeviceArray{Float64}, parameters::CuDeviceArray{Float64}, results::CuDeviceArray{Float64}, stepsize::CuDeviceArray{Int}, exprIndex::Int) function interpret_expression(expressions::CuDeviceArray{ExpressionElement}, variables::CuDeviceArray{Float64}, parameters::CuDeviceArray{Float64}, results::CuDeviceArray{Float64}, stepsize::CuDeviceArray{Int}, exprIndex::Int)
index = (blockIdx().x - 1) * blockDim().x + threadIdx().x
stride = gridDim().x * blockDim().x
firstExprIndex = ((exprIndex - 1) * stepsize[1]) + 1 # Inclusive firstExprIndex = ((exprIndex - 1) * stepsize[1]) + 1 # Inclusive
lastExprIndex = firstExprIndex + stepsize[1] - 1 # Inclusive lastExprIndex = firstExprIndex + stepsize[1] - 1 # Inclusive
firstParamIndex = ((exprIndex - 1) * stepsize[2]) # Exclusive firstParamIndex = ((exprIndex - 1) * stepsize[2]) # Exclusive
# lastParamIndex = firstParamIndex + stepsize[2] - 1 # Inclusive (probably not needed) # lastParamIndex = firstParamIndex + stepsize[2] - 1 # Inclusive (probably not needed)
variableCols = length(variables) / stepsize[3] variableCols = length(variables) / stepsize[3]
firstVariableIndex = ((exprIndex - 1) * stepsize[3]) # Exclusive # TODO: This is obviously not right because each expression calculates the cudaResults for each variable set and therefore needs to incorporate the block index + stride. This is only done for testing
firstResultsIndex = ((exprIndex - 1) * variableCols) + 1 # Inclusive # TODO: Same as above. to get the index of the variable set and therefore the index in the results matrix, use the block index and stride
operationStack = MVector{MAX_STACK_SIZE, Float64}(undef) # Try to get this to function with variable size too, to allow better memory usage operationStack = MVector{MAX_STACK_SIZE, Float64}(undef) # Try to get this to function with variable size too, to allow better memory usage
operationStackTop = 0 # stores index of the last defined/valid value operationStackTop = 0 # stores index of the last defined/valid value
# TODO: Look into Index and stride for the case that one thread handles multiple "variable sets" # return
return
for i in firstExprIndex:lastExprIndex for setIndex in index:stride
if expressions[i].Type == EMPTY firstVariableIndex = ((setIndex - 1) * stepsize[3]) # Exclusive
break
elseif expressions[i].Type == INDEX for i in firstExprIndex:lastExprIndex
val = expressions[i].Value if expressions[i].Type == EMPTY
operationStackTop += 1 break
elseif expressions[i].Type == INDEX
val = expressions[i].Value
operationStackTop += 1
if val > 0 if val > 0
operationStack[operationStackTop] = variables[firstVariableIndex + val] operationStack[operationStackTop] = variables[firstVariableIndex + val]
else
val = abs(val)
operationStack[operationStackTop] = parameters[firstParamIndex + val]
end
elseif expressions[i].Type == FLOAT64
operationStackTop += 1
operationStack[operationStackTop] = reinterpret(Float64, expressions[i].Value)
elseif expressions[i].Type == OPERATOR
# TODO Maybe put this in seperate function
type = expressions[i].Type
if type == ADD
operationStackTop -= 1
operationStack[operationStackTop] = operationStack[operationStackTop] + operationStack[operationStackTop + 1]
elseif type == SUBTRACT
operationStackTop -= 1
operationStack[operationStackTop] = operationStack[operationStackTop] - operationStack[operationStackTop + 1]
elseif type == MULTIPLY
operationStackTop -= 1
operationStack[operationStackTop] = operationStack[operationStackTop] * operationStack[operationStackTop + 1]
elseif type == DIVIDE
operationStackTop -= 1
operationStack[operationStackTop] = operationStack[operationStackTop] / operationStack[operationStackTop + 1]
elseif type == POWER
operationStackTop -= 1
operationStack[operationStackTop] = operationStack[operationStackTop] ^ operationStack[operationStackTop + 1]
elseif type == ABS
operationStack[operationStackTop] = abs(operationStack[operationStackTop])
elseif type == LOG
operationStack[operationStackTop] = log(operationStack[operationStackTop])
elseif type == EXP
operationStack[operationStackTop] = exp(operationStack[operationStackTop])
elseif type == SQRT
operationStack[operationStackTop] = sqrt(operationStack[operationStackTop])
end
else else
val = abs(val) operationStack[operationStackTop] = NaN
operationStack[operationStackTop] = parameters[firstParamIndex + val] break
end end
elseif expressions[i].Type == FLOAT64
operationStackTop += 1
operationStack[operationStackTop] = reinterpret(Float64, expressions[i].Value)
elseif expressions[i].Type == OPERATOR
# TODO Maybe put this in seperate function
type = expressions[i].Type
if type == ADD
operationStackTop -= 1
operationStack[operationStackTop] = operationStack[operationStackTop] + operationStack[operationStackTop + 1]
elseif type == SUBTRACT
operationStackTop -= 1
operationStack[operationStackTop] = operationStack[operationStackTop] - operationStack[operationStackTop + 1]
elseif type == MULTIPLY
operationStackTop -= 1
operationStack[operationStackTop] = operationStack[operationStackTop] * operationStack[operationStackTop + 1]
elseif type == DIVIDE
operationStackTop -= 1
operationStack[operationStackTop] = operationStack[operationStackTop] / operationStack[operationStackTop + 1]
elseif type == POWER
operationStackTop -= 1
operationStack[operationStackTop] = operationStack[operationStackTop] ^ operationStack[operationStackTop + 1]
elseif type == ABS
operationStack[operationStackTop] = abs(operationStack[operationStackTop])
elseif type == LOG
operationStack[operationStackTop] = log(operationStack[operationStackTop])
elseif type == EXP
operationStack[operationStackTop] = exp(operationStack[operationStackTop])
elseif type == SQRT
operationStack[operationStackTop] = sqrt(operationStack[operationStackTop])
end
else
operationStack[operationStackTop] = NaN
break
end end
# "(exprIndex - 1) * variableCols" -> calculates the column in which to insert the result (expression = column)
# "+ setIndex" -> to get the row inside the column at which to insert the result of the variable set (variable set = row)
resultIndex = convert(Int, (exprIndex - 1) * variableCols + setIndex) # Inclusive
results[resultIndex] = operationStack[operationStackTop]
end end
# results[] = operationStack[operationStackTop]
return return
end end

View File

@ -41,6 +41,23 @@ end
@test isequal(result, reference) @test isequal(result, reference)
end end
@testset "Test Add Operator" begin
# One test with fixed values
# One test with variables
# One test with parameters
end
@testset "Test Subtract Operator" begin
# One test with fixed values
# One Test with fixed values but swapped
# One test with variables
# One test with parameters
end
@testset "Test Abs Operator" begin
# One test with fixed value
# One test with variable
# One test with parameter
end
# TODO: Add several tests fo the mathematical expressions # TODO: Add several tests fo the mathematical expressions
# One test for each operator. A second test if the operation order matters # One test for each operator. A second test if the operation order matters
# And some more complicated expressions, with some only having variables, some only having parameters and some having both # And some more complicated expressions, with some only having variables, some only having parameters and some having both