From 6a1811ae6b56f8001f1edc30d3e323b2ee2eea74 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 25 Aug 2024 13:01:58 +0200 Subject: [PATCH] added operator tests. single value operators not working yet --- package/src/ExpressionProcessing.jl | 11 ++-- package/src/Interpreter.jl | 8 +-- package/test/InterpreterTests.jl | 92 +++++++++++++++++++++++------ 3 files changed, 83 insertions(+), 28 deletions(-) diff --git a/package/src/ExpressionProcessing.jl b/package/src/ExpressionProcessing.jl index 85c6911..049db05 100644 --- a/package/src/ExpressionProcessing.jl +++ b/package/src/ExpressionProcessing.jl @@ -21,7 +21,8 @@ function expr_to_postfix(expr::Expr)::PostfixType postfix = PostfixType() 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) arg = expr.args[j] if typeof(arg) === Expr @@ -80,13 +81,13 @@ function convert_var_to_int(var::Symbol)::Int end function convert_to_ExpressionElement(element)::ExpressionElement - Value = reinterpret(Int64, element) + value = reinterpret(Int64, element) if element isa Float64 - return ExpressionElement(FLOAT64, Value) + return ExpressionElement(FLOAT64, value) elseif element isa Int64 - return ExpressionElement(INDEX, Value) + return ExpressionElement(INDEX, value) elseif element isa Operator - return ExpressionElement(OPERATOR, Value) + return ExpressionElement(OPERATOR, value) else error("Element was of type '$(typeof(element))', which is not supported.") end diff --git a/package/src/Interpreter.jl b/package/src/Interpreter.jl index 95600b3..7ba9301 100644 --- a/package/src/Interpreter.jl +++ b/package/src/Interpreter.jl @@ -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 # 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 for i in eachindex(expressions) @@ -33,8 +33,6 @@ function interpret(expressions::Vector{ExpressionProcessing.PostfixType}, variab end # TODO: Wait for all the kernels to finish to return the result - # return cudaResults - println(cudaResults) return cudaResults 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 operationStackTop = 0 # stores index of the last defined/valid value - # return - for setIndex in index:stride firstVariableIndex = ((setIndex - 1) * stepsize[3]) # Exclusive @@ -76,7 +72,7 @@ function interpret_expression(expressions::CuDeviceArray{ExpressionElement}, var operationStack[operationStackTop] = reinterpret(Float64, expressions[i].Value) elseif expressions[i].Type == OPERATOR # TODO Maybe put this in seperate function - type = expressions[i].Type + type = reinterpret(Operator, expressions[i].Value) if type == ADD operationStackTop -= 1 operationStack[operationStackTop] = operationStack[operationStackTop] + operationStack[operationStackTop + 1] diff --git a/package/test/InterpreterTests.jl b/package/test/InterpreterTests.jl index 968147c..bd3d474 100644 --- a/package/test/InterpreterTests.jl +++ b/package/test/InterpreterTests.jl @@ -19,12 +19,19 @@ parameters[1][1] = 5.0 parameters[2][1] = 5.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]) postfixExprs = Vector([postfixExpr]) push!(postfixExprs, expr_to_postfix(expressions[2])) - CUDA.@sync interpret(postfixExprs, variables, parameters) + # CUDA.@sync interpret(postfixExprs, variables, parameters) end @testset "Test conversion to matrix" begin @@ -33,31 +40,82 @@ end reference[2,1] = NaN64 reference[1,2] = 5.0 reference[2,2] = 0.0 - # fill!(reference, [[], [5.0, 0.0]]) # reference = Matrix([5.0, NaN], # [5.0, 0.0]) - result = Interpreter.convert_to_matrix(parameters, NaN64) + CUDA.@sync result = Interpreter.convert_to_matrix(parameters, NaN64) @test isequal(result, reference) end -@testset "Test Add Operator" begin - # One test with fixed values - # One test with variables - # One test with parameters +@testset "Test commutative interpretation" begin + var = Matrix{Float64}(undef, 2, 1) + param = Vector{Vector{Float64}}(undef, 1) + 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 -@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 variables - # One test with parameters + expr = :(5.0 - 3.0) + 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 -@testset "Test Abs Operator" begin - # One test with fixed value - # One test with variable - # One test with parameter + +@testset "Test single value operator interpretation" begin + var = Matrix{Float64}(undef, 1, 1) + 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 # 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 \ No newline at end of file