Browse Source

compiler for loop, comment

Alexis Juven 6 years ago
parent
commit
b971ff887f

+ 15 - 5
julia/src/Compiler/C/add_for_loop_declarations.jl

@@ -1,6 +1,9 @@
 
 
-
+"""
+    Returns the list of instruction that will be added before for loop of shape
+        "for for_index_var in set ..."
+"""
 function interval_evaluation_declarations(set :: StarpuExprInterval, for_index_var :: Symbol)
 
     const decl_pattern = @parse € :: Int64
@@ -12,6 +15,17 @@ function interval_evaluation_declarations(set :: StarpuExprInterval, for_index_v
     start_var = starpu_parse(Symbol(:start_, id))
     start_decl = replace_pattern(affect_pattern, start_var, set.start)
 
+    index_var = starpu_parse(for_index_var)
+    index_decl = replace_pattern(decl_pattern, index_var)
+
+    if isa(set.step, StarpuExprValue)
+
+        stop_var = starpu_parse(Symbol(:stop_, id))
+        stop_decl = replace_pattern(affect_pattern, stop_var, set.stop)
+
+        return StarpuExpr[start_decl, stop_decl, index_decl]
+    end
+
     step_var = starpu_parse(Symbol(:step_, id))
     step_decl = replace_pattern(affect_pattern, step_var, set.step)
 
@@ -21,15 +35,11 @@ function interval_evaluation_declarations(set :: StarpuExprInterval, for_index_v
     iter_var = starpu_parse(Symbol(:iter_, id))
     iter_decl = replace_pattern(decl_pattern, iter_var)
 
-    index_var = starpu_parse(for_index_var)
-    index_decl = replace_pattern(decl_pattern, index_var)
-
 
     return StarpuExpr[start_decl, step_decl, dim_decl, iter_decl, index_decl]
 end
 
 
-
 function add_for_loop_declarations(expr :: StarpuExpr)
 
     function func_to_apply(x :: StarpuExpr)

+ 0 - 3
julia/src/Compiler/C/substitute_args.jl

@@ -62,9 +62,6 @@ function substitute_args(expr :: StarpuExprFunction)
         new_affect = starpu_parse( :($ptr :: Ptr{$type_in_arg} = $func_interface($buffer_arg_name[$i])) )
         push!(function_start_affectations, new_affect)
 
-        #var_to_replace = starpu_parse(expr.args[i].name)
-        #replace_with = starpu_parse(ptr)
-        #new_body = substitute(new_body, var_to_replace, replace_with)
         new_body = substitute_argument_usage(new_body, i, buffer_arg_name, expr.args[i].name, ptr)
     end
 

+ 14 - 3
julia/src/Compiler/Expressions/for.jl

@@ -64,9 +64,20 @@ function print(io :: IO, x :: StarpuExprFor ; indent = 0)
     iter = "iter_" * id
 
     print_newline(io, indent, 2)
-    print(io, "for ($iter = 0, $(x.iter) = $start ; ")
-    print(io, "$iter < $dim ; ")
-    print(io, "$iter += 1, $(x.iter) += $step)")
+
+    if isa(x.set.step, StarpuExprValue)
+        print(io, "for ($(x.iter) = $start ; ")
+        comparison_op = (x.set.step.value >= 0) ? "<=" : ">="
+        print(io, "$(x.iter) $comparison_op $stop ; ")
+        print(io, "$(x.iter) += $(x.set.step.value))")
+
+    else
+        print(io, "for ($iter = 0, $(x.iter) = $start ; ")
+        print(io, "$iter < $dim ; ")
+        print(io, "$iter += 1, $(x.iter) += $step)")
+
+    end
+
     print_newline(io, indent)
     print(io, "{")
     print_newline(io, indent + starpu_indent_size)

+ 1 - 1
julia/src/Compiler/Expressions/interval.jl

@@ -28,7 +28,7 @@ function starpu_parse_interval(x :: Expr)
     steop = starpu_parse(x.args[2])
 
     if (length(x.args) == 2)
-        return StarpuExprInterval(start, (starpu_parse(Symbol(1))), steop)
+        return StarpuExprInterval(start, StarpuExprValue(1), steop)
     end
 
     stop = starpu_parse(x.args[3])

+ 9 - 2
julia/src/Compiler/Generate_files/c_files.jl

@@ -24,7 +24,9 @@ static inline long long jlstarpu_interval_size(long long start, long long step,
 "
 
 
-
+"""
+	Opens a new C source file, where generated CPU kernels will be written
+"""
 function starpu_new_cpu_kernel_file(file_name :: String)
 
     global generated_cpu_kernel_file_name = file_name
@@ -37,7 +39,12 @@ function starpu_new_cpu_kernel_file(file_name :: String)
 end
 
 
-
+"""
+	Executes the StarPU C compiler to the following function declaration.
+	If no call to starpu_new_cpu_kernel_file has been made before, it only
+	prints the reulting function. Otherwise, it writes into the source file
+	specified when starpu_new_cpu_kernel_file was called.
+"""
 macro cpu_kernel(x)
 
     starpu_expr = transform_to_cpu_kernel(starpu_parse(x))

+ 12 - 3
julia/src/Compiler/Generate_files/cuda_files.jl

@@ -41,7 +41,9 @@ __device__ static inline long long jlstarpu_interval_size__device(long long star
 
 "
 
-
+"""
+	Opens a new Cuda source file, where generated GPU kernels will be written
+"""
 function starpu_new_cuda_kernel_file(file_name :: String)
 
     global generated_cuda_kernel_file_name = file_name
@@ -54,7 +56,12 @@ function starpu_new_cuda_kernel_file(file_name :: String)
 end
 
 
-
+"""
+	Executes the StarPU Cuda compiler to the following function declaration.
+	If no call to starpu_new_cuda_kernel_file has been made before, it only
+	prints the reulting function. Otherwise, it writes into the source file
+	specified when starpu_new_cuda_kernel_file was called.
+"""
 macro cuda_kernel(x)
 
     prekernel, kernel = transform_to_cuda_kernel(starpu_parse(x))
@@ -81,7 +88,9 @@ end
 
 
 
-
+"""
+	Executes @cuda_kernel and @cpu_kernel
+"""
 macro cpu_cuda_kernel(x)
 
 	parsed = starpu_parse(x)

+ 17 - 2
julia/src/Compiler/Generate_files/so_files.jl

@@ -1,6 +1,11 @@
 
 
-
+"""
+	Compiles C source file opened by starpu_new_cpu_kernel_file
+    and filled by @cpu_kernel declarations.
+    Output file is a shared library which can be provided to starpu_init() in
+    order to find kernel.
+"""
 function compile_cpu_kernels(output_file :: String)
 
     starpu_cflags = readstring(`pkg-config --cflags starpu-1.3`)[1:end-1]
@@ -15,6 +20,12 @@ function compile_cpu_kernels(output_file :: String)
 end
 
 
+"""
+	Compiles Cuda source file opened by starpu_new_cuda_kernel_file
+    and filled by @cuda_kernel declarations.
+    Output file is a shared library which can be provided to starpu_init() in
+    order to find kernel.
+"""
 function compile_cuda_kernels(output_file :: String)
 
     starpu_cflags = readstring(`pkg-config --cflags starpu-1.3`)[1:end-1]
@@ -29,7 +40,11 @@ function compile_cuda_kernels(output_file :: String)
 end
 
 
-
+"""
+    Combines several shared library into a new one.
+    Can be used to have both CPU and Cuda kernels (from compile_cpu_kernels
+    compile_cuda_kernels) accessible from the same library.
+"""
 function combine_kernel_files(output_file :: String, input_files :: Vector{String})
 
     input_str = (*)(map((x -> x * " "), input_files)...)

+ 21 - 0
julia/src/Compiler/expression_manipulation.jl

@@ -1,4 +1,9 @@
 
+
+"""
+    Returns a new expression where every occurrence of expr_to_replace into expr
+    has been replaced by new_expr
+"""
 function substitute(expr :: StarpuExpr, expr_to_replace :: StarpuExpr, new_expr :: StarpuExpr)
 
     function func_to_apply(x :: StarpuExpr)
@@ -14,7 +19,13 @@ function substitute(expr :: StarpuExpr, expr_to_replace :: StarpuExpr, new_expr
 end
 
 
+"""
+    Returns an expression where "€" symbols  in expr were replaced
+    by the following expression list.
 
+    Ex : replace_pattern((@parse € = €), (@parse x), (@parse 1 + 1))
+            --> (StarpuExpr) "x = 1 + 1"
+"""
 function replace_pattern(expr :: StarpuExpr, replace_€ :: StarpuExpr...)
 
     replace_index = 0
@@ -57,6 +68,11 @@ end
 
 
 import Base.any
+
+"""
+    Returns true if one of the sub-expression x in expr
+    is such as cond(x) is true, otherwise, it returns false.
+"""
 function any(cond :: Function, expr :: StarpuExpr)
 
     err_to_catch = "Catch me, condition is true somewhere !"
@@ -86,6 +102,11 @@ end
 
 
 import Base.all
+
+"""
+    Returns true if every sub-expression x in expr
+    is such as cond(x) is true, otherwise, it returns false.
+"""
 function all(cond :: Function, expr :: StarpuExpr)
     return !any(!cond, expr)
 end

+ 7 - 2
julia/src/Compiler/parsing.jl

@@ -8,7 +8,9 @@
 
 starpu_parse_key_word_parsing_function = Dict{Symbol, Function}()
 
-
+"""
+    Translates x Expr into a new StarpuExpr object
+"""
 function starpu_parse(x :: Expr)
 
     if (x.head == :macrocall)
@@ -43,7 +45,10 @@ starpu_parse_key_word_parsing_function[:(=)] = starpu_parse_affect
 starpu_parse_key_word_parsing_function[:(.)] = starpu_parse_field
 
 
-
+"""
+    Executes the starpu_parse function on the following expression,
+    and returns the obtained StarpuExpr
+"""
 macro parse(x)
     y = Expr(:quote, x)
     :(starpu_parse($y))

+ 3 - 29
julia/src/Wrapper/Julia/starpu_data_handle.jl

@@ -15,23 +15,6 @@ function StarpuNewDataHandle(ptr :: StarpuDataHandlePointer, destr :: Function..
     return StarpuDestructible(ptr, destr...)
 end
 
-#=
-struct StarpuDataHandle
-
-    pointer :: StarpuDataHandlePointer
-    #destructors :: Vector{Function}
-
-    function StarpuDataHandle(ptr :: StarpuDataHandlePointer, destr = Function[])
-        return new(ptr)
-        #output = new(ptr)#, destr)
-        #push!(starpu_data_handle_list, output)
-        #return output
-    end
-
-end
-=#
-#starpu_data_handle_list = StarpuDataHandle[]
-
 
 
 function starpu_data_unregister_pointer(ptr :: StarpuDataHandlePointer)
@@ -134,18 +117,6 @@ end
 
 
 
-
-#function starpu_data_get_sub_data(root_data :: StarpuDataHandle, ids...)
-#
-#    nb_ids = lengths(ids)
-#    type_args = Tuple((Cint for i in (1 : nb_ids)))
-
-#    ccall((:starpu_data_get_sub_data, "libjlstarpu_c_wrapper"),
-#            Ptr{Void}, (Ptr{Void}, Cuint, type_args...),
-#            root_data, nb_ids, ids...
-#        )
-#end
-
 export starpu_data_get_sub_data
 
 function starpu_data_get_sub_data(root_data :: StarpuDataHandle, id)
@@ -190,6 +161,9 @@ export STARPU_MATRIX_FILTER_VERTICAL_BLOCK, STARPU_MATRIX_FILTER_BLOCK
 )
 
 export StarpuDataFilter
+"""
+    TODO : use real function pointers loaded from starpu shared library
+"""
 mutable struct StarpuDataFilter
 
     filter_func :: StarpuDataFilterFunc

+ 6 - 10
julia/src/Wrapper/Julia/starpu_define.jl

@@ -2,18 +2,21 @@
 
 
 
-STARPU_MAXIMPLEMENTATIONS = 1 # TODO : good value
-STARPU_NMAXBUFS = 8 # TODO : good value
+STARPU_MAXIMPLEMENTATIONS = 1 # TODO : These must be the same values as defined in C macros !
+STARPU_NMAXBUFS = 8 # TODO : find a way to make it automatically match
 
 
 STARPU_CPU = 1 << 1
 STARPU_CUDA = 1 << 3
 
-
 macro starpufunc(symbol)
     :($symbol, "libjlstarpu_c_wrapper")
 end
 
+"""
+    Used to call a StarPU function compiled inside "libjlstarpu_c_wrapper.so"
+    Works as ccall function
+"""
 macro starpucall(func, ret_type, arg_types, args...)
     return Expr(:call, :ccall, (func, "libjlstarpu_c_wrapper"), esc(ret_type), esc(arg_types), map(esc, args)...)
 end
@@ -44,10 +47,3 @@ function jlstarpu_set_to_zero(x :: T) :: Ptr{Void} where {T}
           Ref{T}(x), 0, sizeof(x)
         )
 end
-
-
-
-
-macro mutableview(t)
-    :(unsafe_wrap( Vector{eltype($t)}, Ptr{eltype($t)}(pointer_from_objref($t)), length($t)))
-end

+ 26 - 48
julia/src/Wrapper/Julia/starpu_destructible.jl

@@ -1,7 +1,10 @@
 
 
 
-
+"""
+    Object used to store a lost of function which must
+    be applied to and object
+"""
 mutable struct StarpuDestructible{T}
 
     object :: T
@@ -39,7 +42,9 @@ function starpu_enter_new_block()
     push!(starpu_block_list, LinkedList{StarpuDestructible}())
 end
 
-
+"""
+    Applies every stored destructores to the StarpuDestructible stored object
+"""
 function starpu_destruct!(x :: StarpuDestructible)
 
     for destr in x.destructors
@@ -62,7 +67,10 @@ function starpu_exit_block()
 
 end
 
-
+"""
+    Adds new destructors to the list of function. They will be executed before
+        already stored ones when calling starpu_destruct!
+"""
 function starpu_add_destructor!(x :: StarpuDestructible, destrs :: Function...)
 
     for d in destrs
@@ -72,7 +80,9 @@ function starpu_add_destructor!(x :: StarpuDestructible, destrs :: Function...)
     return nothing
 end
 
-
+"""
+    Removes detsructor without executing it
+"""
 function starpu_remove_destructor!(x :: StarpuDestructible, destr :: Function)
 
     @foreach_asc x.destructors lnk begin
@@ -86,6 +96,13 @@ function starpu_remove_destructor!(x :: StarpuDestructible, destr :: Function)
     return nothing
 end
 
+
+"""
+    Executes "destr" function. If it was one of the stored destructors, it
+    is removed.
+    This function can be used to allow user to execute a specific action manually
+        (ex : explicit call to starpu_data_unpartition() without unregistering)
+"""
 function starpu_execute_destructor!(x :: StarpuDestructible, destr :: Function)
 
     starpu_remove_destructor!(x, destr)
@@ -94,6 +111,11 @@ end
 
 
 export @starpu_block
+
+"""
+    Declares a block of code. Every declared StarpuDestructible in this code
+    will execute its destructors on its object, once the block is exited
+"""
 macro starpu_block(expr)
     quote
         starpu_enter_new_block()
@@ -101,47 +123,3 @@ macro starpu_block(expr)
         starpu_exit_block()
     end
 end
-
-
-
-if false
-
-@starpu_block let
-    println("Begining of block")
-    x = StarpuDestructible(1, println)
-    println("End of block")
-end
-
-
-
-@starpu_block let
-    println("Begining of block")
-    x = StarpuDestructible(2, (x -> @show x), println)
-    println("End of block")
-end
-
-
-@starpu_block let
-    println("Begining of block")
-    x = StarpuDestructible(3, (x -> @show x), println)
-    starpu_add_destructor!(x, (x -> @show x+1))
-    println("End of block")
-end
-
-@starpu_block let
-    println("Begining of block")
-    x = StarpuDestructible(4, (x -> @show x), println)
-    starpu_add_destructor!(x, (x -> @show x+1))
-    starpu_remove_destructor!(x, println)
-    println("End of block")
-end
-
-@starpu_block let
-    println("Begining of block")
-    x = StarpuDestructible(4, (x -> @show x), println)
-    starpu_add_destructor!(x, (x -> @show x+1))
-    starpu_execute_destructor!(x, println)
-    println("End of block")
-end
-
-end

+ 0 - 1
julia/src/Wrapper/Julia/starpu_include.jl

@@ -1,6 +1,5 @@
 
 __precompile__()
-
 module StarPU
 
 

+ 10 - 0
julia/src/Wrapper/Julia/starpu_init_shutdown.jl

@@ -1,5 +1,11 @@
 
 export starpu_init
+
+"""
+    Must be called before any other starpu function. Field extern_task_path is the
+    shared library path which will be used to find StarpuCodelet
+    cpu and gpu function names
+"""
 function starpu_init(; extern_task_path = "")
 
     if (!isempty(extern_task_path))
@@ -17,6 +23,10 @@ end
 
 
 export starpu_shutdown
+
+"""
+    Must be called at the end of the program
+"""
 function starpu_shutdown()
     starpu_exit_block()
     @starpucall starpu_shutdown Void ()

+ 4 - 1
julia/src/Wrapper/Julia/starpu_simple_functions.jl

@@ -1,5 +1,8 @@
 
-
+"""
+    Declares a Julia function wich is just calling the StarPU function
+    having the same name.
+"""
 macro starpu_noparam_function(func_name, ret_type)
 
     func = Symbol(func_name)

+ 6 - 20
julia/src/Wrapper/Julia/starpu_task.jl

@@ -10,26 +10,6 @@ mutable struct StarpuTask
 
     c_task :: Ptr{Void}
 
-    #=function StarpuTask()
-
-        output = new()
-        output.handles = StarpuDataHandle[]
-        output.handle_pointers = StarpuDataHandlePointer[]
-        output.synchronous = false
-        output.cl_arg = nothing
-
-
-        c_task = @starpucall starpu_task_create Ptr{Void} ()
-
-        if (c_task == C_NULL)
-            error("Couldn't create new task: starpu_task_create() returned NULL")
-    	end
-
-        output.c_task = c_task
-
-        return output
-    end=#
-
 
     """
         StarpuTask(; cl :: StarpuCodelet, handles :: Vector{StarpuDataHandle}, cl_arg :: Ref)
@@ -67,6 +47,9 @@ mutable struct StarpuTask
 end
 
 
+"""
+    Updates fields of the real C structures stored at "c_task" field
+"""
 function starpu_c_task_update(task :: StarpuTask)
 
     task_translator = StarpuTaskTranslator(task)
@@ -79,6 +62,9 @@ function starpu_c_task_update(task :: StarpuTask)
 end
 
 
+"""
+    Structure used to update fields of the real C task structure 
+"""
 mutable struct StarpuTaskTranslator
 
     cl :: Ptr{Void}

+ 25 - 1
julia/src/Wrapper/Julia/starpu_task_submit.jl

@@ -2,6 +2,11 @@
 
 
 export starpu_task_submit
+
+"""
+    Launches task execution, if "synchronous" task field is set to "false", call
+    returns immediately
+"""
 function starpu_task_submit(task :: StarpuTask)
 
     if (length(task.handles) != length(task.cl.modes))
@@ -11,11 +16,15 @@ function starpu_task_submit(task :: StarpuTask)
     starpu_c_task_update(task)
 
     @starpucall starpu_task_submit Cint (Ptr{Void},) task.c_task
-
 end
 
 
 export @starpu_async_cl
+
+"""
+    Creates and submits an asynchronous task running cl Codelet function.
+    Ex : @starpu_async_cl cl(handle1, handle2)
+"""
 macro starpu_async_cl(expr)
 
     if (!isa(expr, Expr) || expr.head != :call)
@@ -33,6 +42,9 @@ end
 
 
 export starpu_task_wait_for_all
+"""
+    Blocks until every submitted task has finished.
+"""
 function starpu_task_wait_for_all()
     @threadcall(@starpufunc(:starpu_task_wait_for_all),
                           Cint, ())
@@ -40,6 +52,18 @@ end
 
 
 export @starpu_sync_tasks
+
+"""
+    Blocks until every submitted task has finished.
+    Ex : @starpu_sync_tasks begin
+                [...]
+                starpu_task_submit(task)
+                [...]
+        end
+
+    TODO : Make the macro only wait for tasks declared inside the following expression.
+            (similar mechanism as @starpu_block)
+"""
 macro starpu_sync_tasks(expr)
     quote
         $(esc(expr))

+ 0 - 38
julia/src/Wrapper/Julia/starpu_worker.jl

@@ -1,38 +0,0 @@
-
-
-
-@enum(StarpuWorkerArchtype,
-
-	STARPU_CPU_WORKER,
-	STARPU_CUDA_WORKER,
-	STARPU_OPENCL_WORKER,
-	STARPU_MIC_WORKER,
-	STARPU_SCC_WORKER,
-	STARPU_MPI_MS_WORKER,
-	STARPU_ANY_WORKER
-)
-
-
-function starpu_worker_get_count_by_type(arch_type :: StarpuWorkerArchtype)
-    @starpucall(starpu_worker_get_count_by_type,
-            Cint, (StarpuWorkerArchtype,), arch_type
-        )
-end
-
-
-#= TODO : NOT C_NULL but stdout FILE *
-function starpu_worker_display_names(arch_type :: StarpuWorkerArchtype)
-	@starpucall(starpu_worker_display_names,
-            Void, (Ptr{Void}, StarpuWorkerArchtype),
-			C_NULL, arch_type
-        )
-end
-=#
-
-
-
-
-@starpu_noparam_function "starpu_worker_get_id" Cint
-
-@starpu_noparam_function "starpu_cpu_worker_get_count" Cuint
-@starpu_noparam_function "starpu_cuda_worker_get_count" Cuint

+ 9 - 2
julia/src/Wrapper/Julia/static_structures.jl

@@ -4,7 +4,12 @@
 const jlstarpu_allocated_structures = Vector{Ptr{Void}}([])
 
 
-
+"""
+    Copies x_c to a new allocated memory zone.
+    Returns the pointer toward the copied object. Every pointer
+    returned by this function will be freed after a call to
+    jlstarpu_free_allocated_structures
+"""
 function jlstarpu_allocate_and_store(x_c :: T) where {T}
 
     allocated_ptr = Ptr{T}(Libc.malloc(sizeof(T)))
@@ -20,7 +25,9 @@ function jlstarpu_allocate_and_store(x_c :: T) where {T}
 end
 
 
-
+"""
+    Frees every pointer allocated by jlstarpu_allocate_and_store
+"""
 function jlstarpu_free_allocated_structures()
     map(Libc.free, jlstarpu_allocated_structures)
     empty!(jlstarpu_allocated_structures)

+ 2 - 2
julia/tst/gpu_mult.cu

@@ -26,10 +26,10 @@ __global__ void gpuMultKernel
 	sum = 0.;
 
 	for (k = 0 ; k < nyA ; k++){
-		sum += subA[i + k * ldA] * subB[k + j * ldB];
+		sum += subA[i + k*ldA] * subB[k + j*ldB];
 	}
 
-	subC[i + j * ldC] = sum;
+	subC[i + j*ldC] = sum;
 
 }
 

+ 1 - 1
julia/tst/mult_def.jl

@@ -93,7 +93,7 @@ function median_time(nb_tests, xdim, zdim, ydim, nslicesx, nslicesy)
 
     sort!(exec_times)
 
-    return exec_times[div(nb_tests, 2)]
+    return exec_times[1 + div(nb_tests-1, 2)]
 end