From 4afc15a737b578717a9e2344f0233e6a61ab6220 Mon Sep 17 00:00:00 2001 From: Daniel Date: Sun, 26 Jan 2025 10:16:23 +0100 Subject: [PATCH] small cleanup --- package/src/ExpressionProcessing.jl | 50 ----------------------------- package/src/Interpreter.jl | 26 ++------------- package/src/Transpiler.jl | 8 +++++ 3 files changed, 10 insertions(+), 74 deletions(-) diff --git a/package/src/ExpressionProcessing.jl b/package/src/ExpressionProcessing.jl index 2a635c0..bbed13f 100644 --- a/package/src/ExpressionProcessing.jl +++ b/package/src/ExpressionProcessing.jl @@ -133,54 +133,4 @@ function is_binary_operator(operator::Operator)::Bool end end -# -# Everything below is currently not needed. Left here for potential future use -# - -const SymbolTable32 = Dict{Tuple{Expr, Symbol},Float32} - -"Replaces all the variables and parameters of the given expression with their corresponding Value stored in the symtable -# Arguments - - `symtable::SymbolTable32`: Contains the values of all variables for each expression - - `originalExpr::Expr`: Contains a deep copy of the original expression. It is used to link the expression and variables to their according Value stored in the symtable -" -function replace_variables!(ex::Expr, symtable::SymbolTable32, originalExpr::Expr) - for i in 1:length(ex.args) - arg = ex.args[i] - if typeof(arg) === Expr - replace_variables!(arg, symtable, originalExpr) - elseif haskey(symtable, (originalExpr,arg)) # We found a variable/parameter and can replace it with the actual Value - ex.args[i] = symtable[(originalExpr,arg)] - end - end -end - -# TODO: Completly rewrite this function because I misunderstood it. Not every column is linked to an expression. therefore all other functions need to be reworked as well. Probably can't replace the variables in julia anymore, look into this. (see ExpressionExecutorCuda.jl for more info) -# Before rewriting, proceed with just creating a postfix notation and sending the variable matrix as well as the parameter "matrix" to the GPU to perform first calculations -function construct_symtable(expressions::Vector{Expr}, mat::Matrix{Float32}, params::Vector{Vector{Float32}})::SymbolTable32 - symtable = SymbolTable32() - - for i in eachindex(expressions) - expr = expressions[i] - values = mat[i,:] - parameters = params[i] - - fill_symtable!(expr, symtable, values, "x") - fill_symtable!(expr, symtable, parameters, "p") - end - - return symtable -end - -function fill_symtable!(expr::Expr, symtable::SymbolTable32, values::Vector{Float32}, symbolPrefix::String) - varIndex = 1 - for j in eachindex(values) - val = values[j] - sym = Symbol(symbolPrefix, varIndex) - - symtable[expr,sym] = val - varIndex += 1 - end -end - end \ No newline at end of file diff --git a/package/src/Interpreter.jl b/package/src/Interpreter.jl index 4ffeea7..bbf58e9 100644 --- a/package/src/Interpreter.jl +++ b/package/src/Interpreter.jl @@ -12,7 +12,7 @@ export interpret - parameters::Vector{Vector{Float32}} : 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{Float32}, parameters::Vector{Vector{Float32}})::Matrix{Float32} - variableCols = size(variables, 2) # number of sets of variables to use for each expression + variableCols = size(variables, 2) # number of variable sets to use for each expression cudaVars = CuArray(variables) cudaParams = create_cuda_array(parameters, NaN32) # column corresponds to data for one expression cudaExprs = create_cuda_array(expressions, ExpressionElement(EMPTY, 0)) # column corresponds to data for one expression @@ -36,7 +36,7 @@ function interpret(expressions::Vector{ExpressionProcessing.PostfixType}, variab 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 depth of the stack to store the values and intermediate results function interpret_expression(expressions::CuDeviceArray{ExpressionElement}, variables::CuDeviceArray{Float32}, parameters::CuDeviceArray{Float32}, results::CuDeviceArray{Float32}, stepsize::CuDeviceArray{Int}, exprIndex::Int) index = (blockIdx().x - 1) * blockDim().x + threadIdx().x # ctaid.x * ntid.x + tid.x stride = gridDim().x * blockDim().x # nctaid.x * ntid.x @@ -148,26 +148,4 @@ function convert_to_matrix(vec::Vector{Vector{T}}, invalidElement::T)::Matrix{T} return vecMat end - - -# 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 \ No newline at end of file diff --git a/package/src/Transpiler.jl b/package/src/Transpiler.jl index 508a36f..81aa049 100644 --- a/package/src/Transpiler.jl +++ b/package/src/Transpiler.jl @@ -25,6 +25,14 @@ using ..ExpressionProcessing const Operand = Union{Float32, String} # Operand is either fixed value or register +function evaluate(expression::ExpressionProcessing.PostfixType, variables::Matrix{Float32}, parameters::Vector{Vector{Float32}}) + # TODO: think of how to do this. Probably get all expressions. Transpile them in parallel and then execute the generatd code. + cudaVars = CuArray(variables) + + #kernel = transpile(expression, ) + # execute kernel. +end + # To increase performance, it would probably be best for all helper functions to return their IO Buffer and not a string # seekstart(buf1); write(buf2, buf1) function transpile(expression::ExpressionProcessing.PostfixType, varSetSize::Integer, paramSetSize::Integer)::String