added more unit tests. Conversion to postfix not working
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
		
			
				
	
				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
				
			
		
			
				
	
				CI / Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }} (x64, ubuntu-latest, 1.10) (push) Has been cancelled
				
			
		
		
	
	
				
					
				
			
		
			Some checks failed
		
		
	
	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
				
			CI / Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }} (x64, ubuntu-latest, 1.10) (push) Has been cancelled
				
			This commit is contained in:
		@ -21,8 +21,6 @@ function expr_to_postfix(expr::Expr)::PostfixType
 | 
			
		||||
	postfix = PostfixType()
 | 
			
		||||
	operator = get_operator(expr.args[1])
 | 
			
		||||
 | 
			
		||||
	# 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
 | 
			
		||||
@ -35,12 +33,17 @@ function expr_to_postfix(expr::Expr)::PostfixType
 | 
			
		||||
			exprElement = convert_to_ExpressionElement(convert(Float64, arg))
 | 
			
		||||
			push!(postfix, exprElement)
 | 
			
		||||
		end
 | 
			
		||||
 | 
			
		||||
		if length(postfix) >= 2
 | 
			
		||||
			exprElement = convert_to_ExpressionElement(operator)
 | 
			
		||||
			push!(postfix, exprElement)
 | 
			
		||||
		end
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	# For the case this expression has an operator that only takes in a single value like "abs(x)"
 | 
			
		||||
	if length(postfix) == 1
 | 
			
		||||
		push!(postfix, convert_to_ExpressionElement(operator))
 | 
			
		||||
	end
 | 
			
		||||
	return postfix
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -22,6 +22,12 @@ function interpret(expressions::Vector{ExpressionProcessing.PostfixType}, variab
 | 
			
		||||
	# each expression has nr. of variable sets (nr. of columns of the variables) results and there are n expressions
 | 
			
		||||
	cudaResults = CuArray{Float64}(undef, variableCols, length(expressions))
 | 
			
		||||
 | 
			
		||||
	# println("cudaVars")
 | 
			
		||||
	# println(cudaVars)
 | 
			
		||||
	# println("cudaParams")
 | 
			
		||||
	# println(cudaParams)
 | 
			
		||||
	# println("cudaExprs")
 | 
			
		||||
	# println(cudaExprs)
 | 
			
		||||
	# 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, cudaResults, cudaStepsize, i)
 | 
			
		||||
@ -32,7 +38,6 @@ function interpret(expressions::Vector{ExpressionProcessing.PostfixType}, variab
 | 
			
		||||
		kernel(cudaExprs, cudaVars, cudaParams, cudaResults, cudaStepsize, i; threads, blocks)
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
	# TODO: Wait for all the kernels to finish to return the result
 | 
			
		||||
	return cudaResults
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -51,9 +56,9 @@ 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
 | 
			
		||||
	
 | 
			
		||||
	for setIndex in index:stride
 | 
			
		||||
		firstVariableIndex = ((setIndex - 1) * stepsize[3]) # Exclusive
 | 
			
		||||
	
 | 
			
		||||
	for varSetIndex in index:stride
 | 
			
		||||
		firstVariableIndex = ((varSetIndex - 1) * stepsize[3]) # Exclusive
 | 
			
		||||
		
 | 
			
		||||
		for i in firstExprIndex:lastExprIndex
 | 
			
		||||
			if expressions[i].Type == EMPTY
 | 
			
		||||
				break
 | 
			
		||||
@ -64,14 +69,13 @@ function interpret_expression(expressions::CuDeviceArray{ExpressionElement}, var
 | 
			
		||||
				if val > 0
 | 
			
		||||
					operationStack[operationStackTop] = variables[firstVariableIndex + val]
 | 
			
		||||
				else
 | 
			
		||||
					val = abs(val)
 | 
			
		||||
					val = -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 = reinterpret(Operator, expressions[i].Value)
 | 
			
		||||
				if type == ADD
 | 
			
		||||
					operationStackTop -= 1
 | 
			
		||||
@ -103,8 +107,8 @@ function interpret_expression(expressions::CuDeviceArray{ExpressionElement}, var
 | 
			
		||||
			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
 | 
			
		||||
		# "+ varSetIndex" -> 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 + varSetIndex) # Inclusive
 | 
			
		||||
		results[resultIndex] = operationStack[operationStackTop]
 | 
			
		||||
	end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -21,7 +21,6 @@ parameters[2][2] = 0.0
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
@ -35,6 +34,8 @@ end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@testset "Test conversion to matrix" begin
 | 
			
		||||
	return
 | 
			
		||||
 | 
			
		||||
	reference = Matrix{Float64}(undef, 2, 2)
 | 
			
		||||
	reference[1,1] = 5.0
 | 
			
		||||
	reference[2,1] = NaN64
 | 
			
		||||
@ -48,9 +49,11 @@ end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@testset "Test commutative interpretation" begin
 | 
			
		||||
	return
 | 
			
		||||
 | 
			
		||||
	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
 | 
			
		||||
	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
 | 
			
		||||
@ -68,9 +71,11 @@ end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@testset "Test non commutative interpretation" begin
 | 
			
		||||
	return
 | 
			
		||||
 | 
			
		||||
	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
 | 
			
		||||
	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
 | 
			
		||||
@ -98,14 +103,15 @@ end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@testset "Test single value operator interpretation" begin
 | 
			
		||||
	return
 | 
			
		||||
 | 
			
		||||
	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
 | 
			
		||||
	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)
 | 
			
		||||
@ -117,5 +123,30 @@ end
 | 
			
		||||
	testHelper(expr, var, param, expectedResult)
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
# TODO: Add several tests fo the mathematical expressions
 | 
			
		||||
#       And some more complicated expressions, with some only having variables, some only having parameters and some having both
 | 
			
		||||
@testset "Test complex expressions" begin
 | 
			
		||||
	var = Matrix{Float64}(undef, 2, 2)
 | 
			
		||||
	param = Vector{Vector{Float64}}(undef, 2)
 | 
			
		||||
	
 | 
			
		||||
	# var set 1
 | 
			
		||||
	var[1,1] = 3.0
 | 
			
		||||
	var[2,1] = 5.0
 | 
			
		||||
	# var set 2
 | 
			
		||||
	var[1,2] = 3.0
 | 
			
		||||
	var[2,2] = -5.0
 | 
			
		||||
 | 
			
		||||
	param[1] = [3.0]
 | 
			
		||||
	param[2] = [5.0, 2.0]
 | 
			
		||||
 | 
			
		||||
	expr1 = :((x1 + 5) * p1 - 3 / abs(x2) + (2^4) - log(8))
 | 
			
		||||
	expr2 = :(1 + 5 * x1 - 10^2 + (p1 - p2) / 9 + exp(x2))
 | 
			
		||||
 | 
			
		||||
	postfix = Vector([expr_to_postfix(expr1), expr_to_postfix(expr2)])
 | 
			
		||||
	result = Interpreter.interpret(postfix, var, param)
 | 
			
		||||
 | 
			
		||||
	# var set 1
 | 
			
		||||
	@test isapprox(result[1,1], 37.32, atol=0.01) # expr1
 | 
			
		||||
	@test isequal(result[1,2], 64.74, atol=0.01) # expr2
 | 
			
		||||
	# var set 2
 | 
			
		||||
	@test isequal(result[2,1], 37.32, atol=0.01) # expr1
 | 
			
		||||
	@test isequal(result[2,2], -83.65, atol=0.01) # expr2
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user