added operator tests. single value operators not working yet
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-25 13:01:58 +02:00
parent 929789b6ff
commit 6a1811ae6b
3 changed files with 83 additions and 28 deletions

View File

@ -21,7 +21,8 @@ function expr_to_postfix(expr::Expr)::PostfixType
postfix = PostfixType() postfix = PostfixType()
operator = get_operator(expr.args[1]) operator = get_operator(expr.args[1])
# push!(postfix, expr.args[2], expr.args[3], operator) # TODO: Add suppport for single value operators like "abs(x)"
for j in 2:length(expr.args) for j in 2:length(expr.args)
arg = expr.args[j] arg = expr.args[j]
if typeof(arg) === Expr if typeof(arg) === Expr
@ -80,13 +81,13 @@ function convert_var_to_int(var::Symbol)::Int
end end
function convert_to_ExpressionElement(element)::ExpressionElement function convert_to_ExpressionElement(element)::ExpressionElement
Value = reinterpret(Int64, element) value = reinterpret(Int64, element)
if element isa Float64 if element isa Float64
return ExpressionElement(FLOAT64, Value) return ExpressionElement(FLOAT64, value)
elseif element isa Int64 elseif element isa Int64
return ExpressionElement(INDEX, Value) return ExpressionElement(INDEX, value)
elseif element isa Operator elseif element isa Operator
return ExpressionElement(OPERATOR, Value) return ExpressionElement(OPERATOR, value)
else else
error("Element was of type '$(typeof(element))', which is not supported.") error("Element was of type '$(typeof(element))', which is not supported.")
end end

View File

@ -20,7 +20,7 @@ function interpret(expressions::Vector{ExpressionProcessing.PostfixType}, variab
cudaStepsize = CuArray([get_max_inner_length(expressions), get_max_inner_length(parameters), size(variables, 1)]) # max num of values per expression; max nam of parameters per expression; number of variables per expression cudaStepsize = CuArray([get_max_inner_length(expressions), get_max_inner_length(parameters), size(variables, 1)]) # max num of values per expression; max nam of parameters per expression; number of variables per expression
# each expression has nr. of variable sets (nr. of columns of the variables) results and there are n expressions # each expression has nr. of variable sets (nr. of columns of the variables) results and there are n expressions
cudaResults = CuArray{Float64}(undef, length(expressions), variableCols) cudaResults = CuArray{Float64}(undef, variableCols, length(expressions))
# Start kernel for each expression to ensure that no warp is working on different expressions # Start kernel for each expression to ensure that no warp is working on different expressions
for i in eachindex(expressions) for i in eachindex(expressions)
@ -33,8 +33,6 @@ function interpret(expressions::Vector{ExpressionProcessing.PostfixType}, variab
end end
# TODO: Wait for all the kernels to finish to return the result # TODO: Wait for all the kernels to finish to return the result
# return cudaResults
println(cudaResults)
return cudaResults return cudaResults
end end
@ -53,8 +51,6 @@ function interpret_expression(expressions::CuDeviceArray{ExpressionElement}, var
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
# return
for setIndex in index:stride for setIndex in index:stride
firstVariableIndex = ((setIndex - 1) * stepsize[3]) # Exclusive firstVariableIndex = ((setIndex - 1) * stepsize[3]) # Exclusive
@ -76,7 +72,7 @@ function interpret_expression(expressions::CuDeviceArray{ExpressionElement}, var
operationStack[operationStackTop] = reinterpret(Float64, expressions[i].Value) operationStack[operationStackTop] = reinterpret(Float64, expressions[i].Value)
elseif expressions[i].Type == OPERATOR elseif expressions[i].Type == OPERATOR
# TODO Maybe put this in seperate function # TODO Maybe put this in seperate function
type = expressions[i].Type type = reinterpret(Operator, expressions[i].Value)
if type == ADD if type == ADD
operationStackTop -= 1 operationStackTop -= 1
operationStack[operationStackTop] = operationStack[operationStackTop] + operationStack[operationStackTop + 1] operationStack[operationStackTop] = operationStack[operationStackTop] + operationStack[operationStackTop + 1]

View File

@ -19,12 +19,19 @@ parameters[1][1] = 5.0
parameters[2][1] = 5.0 parameters[2][1] = 5.0
parameters[2][2] = 0.0 parameters[2][2] = 0.0
@testset "Test interpretation" begin function testHelper(expression::Expr, variables::Matrix{Float64}, parameters::Vector{Vector{Float64}}, expectedResult::Float64)
postfix = Vector([expr_to_postfix(expression)])
println(postfix)
result = Interpreter.interpret(postfix, variables, parameters)
@test isequal(result[1,1], expectedResult)
end
@testset "Test TMP interpretation" begin
postfixExpr = expr_to_postfix(expressions[1]) postfixExpr = expr_to_postfix(expressions[1])
postfixExprs = Vector([postfixExpr]) postfixExprs = Vector([postfixExpr])
push!(postfixExprs, expr_to_postfix(expressions[2])) push!(postfixExprs, expr_to_postfix(expressions[2]))
CUDA.@sync interpret(postfixExprs, variables, parameters) # CUDA.@sync interpret(postfixExprs, variables, parameters)
end end
@testset "Test conversion to matrix" begin @testset "Test conversion to matrix" begin
@ -33,31 +40,82 @@ end
reference[2,1] = NaN64 reference[2,1] = NaN64
reference[1,2] = 5.0 reference[1,2] = 5.0
reference[2,2] = 0.0 reference[2,2] = 0.0
# fill!(reference, [[], [5.0, 0.0]])
# reference = Matrix([5.0, NaN], # reference = Matrix([5.0, NaN],
# [5.0, 0.0]) # [5.0, 0.0])
result = Interpreter.convert_to_matrix(parameters, NaN64) CUDA.@sync result = Interpreter.convert_to_matrix(parameters, NaN64)
@test isequal(result, reference) @test isequal(result, reference)
end end
@testset "Test Add Operator" begin @testset "Test commutative interpretation" begin
# One test with fixed values var = Matrix{Float64}(undef, 2, 1)
# One test with variables param = Vector{Vector{Float64}}(undef, 1)
# One test with parameters expectedResult = 8.0 # Not using eval because the variables are not stored in global scope
var[1,1] = 3.0
var[2,1] = 5.0
param[1] = [3.0, 5.0]
# test with fixed values
expr = :(3.0 + 5.0)
testHelper(expr, var, param, expectedResult)
# test with variables
expr = :(x1 + x2)
testHelper(expr, var, param, expectedResult)
# test with parameters
expr = :(p1 + p2)
testHelper(expr, var, param, expectedResult)
end end
@testset "Test Subtract Operator" begin
# One test with fixed values @testset "Test non commutative interpretation" begin
var = Matrix{Float64}(undef, 2, 1)
param = Vector{Vector{Float64}}(undef, 1)
expectedResult = -2.0 # Not using eval because the variables are not stored in global scope
var[1,1] = 3.0
var[2,1] = 5.0
param[1] = [3.0, 5.0]
# test with fixed values
expr = :(3.0 - 5.0)
testHelper(expr, var, param, expectedResult)
# One Test with fixed values but swapped # One Test with fixed values but swapped
# One test with variables expr = :(5.0 - 3.0)
# One test with parameters testHelper(expr, var, param, -expectedResult)
# test with variables
expr = :(x1 - x2)
testHelper(expr, var, param, expectedResult)
# test with variables but swapped
expr = :(x2 - x1)
testHelper(expr, var, param, -expectedResult)
# test with parameters
expr = :(p1 - p2)
testHelper(expr, var, param, expectedResult)
expr = :(p2 - p1)
testHelper(expr, var, param, -expectedResult)
end end
@testset "Test Abs Operator" begin
# One test with fixed value @testset "Test single value operator interpretation" begin
# One test with variable var = Matrix{Float64}(undef, 1, 1)
# One test with parameter param = Vector{Vector{Float64}}(undef, 1)
expectedResult = 3.0 # Not using eval because the variables are not stored in global scope
var[1,1] = -3.0
param[1] = [-3.0]
println("SINGLE VALUE")
# test with fixed value
expr = :(abs(-3.0))
testHelper(expr, var, param, expectedResult)
# test with variable
expr = :(abs(x1))
testHelper(expr, var, param, expectedResult)
# test with parameter
expr = :(abs(p1))
testHelper(expr, var, param, expectedResult)
end 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
# 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