IntelLabs / ParallelAccelerator.jl

The ParallelAccelerator package, part of the High Performance Scripting project at Intel Labs
BSD 2-Clause "Simplified" License
294 stars 32 forks source link

Support for function overloading #89

Open sarkar1 opened 8 years ago

sarkar1 commented 8 years ago

ParallelAccelerator fails to optimize the following function.

using ParallelAccelerator
function test(x::Int, k = nothing)
    if k == nothing
        println("1 Parameter")
    else
        println("2 Parameters")
    end
end
test(4)
test(4,1) 
DrTodd13 commented 8 years ago

Default arguments can certainly be viewed as a form of function overloading. I just tried a test with true function overloading (different arg types but no default args) and that didn't work either so indeed we have an issue with function overloading. Supporting default arguments goes one level beyond supporting function overloading though. Moreover, in this particular example, we don't support println or "nothing" either. I suggest you just rewrite the code so you don't try to accelerate something with default arguments or with function overloading.

Technical stuff below for those interested. I did a preliminary exploration of support some form of default argument support using the following example.

using ParallelAccelerator                                                                              
using CompilerTools                                                                                    

ParallelAccelerator.set_debug_level(3)                                                                 
CompilerTools.OptFramework.set_debug_level(3)                                                          

@acc function test(x::Int, k = 0)                                                                      
    if k == 0                                                                                          
        return 0                                                                                       
    else                                                                                               
        return 1                                                                                       
    end                                                                                                
end                                                                                                    
test(4)                                 

I can detect the default arg "k=0" and can try to construct the internal wrapper function with the same default arg. The args are created as symbols and then put into the dynamically generated wrapper function as follows: wrapper_ast = :(function $new_fname($(new_call_sig_args...))...

If I put "=0" in the new_call_sig_args for the second argument then it looks right in the print out of the wrapper but internally it isn't correct since it is treating "var=0" as a single symbol rather than treating var=0 as separate parseable items. Maybe we can make this work with simple default arguments like Numeric constants but in general we'd have to have the ability to go from Expr back to text and for anything very complicated this doesn't exist.

sarkar1 commented 8 years ago

Thank you @DrTodd13. For now I have bypassed this issue as you suggested.