Groups 69 of 99+ julia-users › Julian way of tracking a "global" module setting 10 posts by 4 authors Seth 9/13/15 Hi all, I'd like to track a setting throughout my module (that will cause the [transparent] dispatch of either single-threaded or parallel versions of many different functions). Is there a more Julian way of doing the following? This seems inelegant: _parallel = false # start off without parallelism - user calls parallelize() to set/unset. function parallelize(p::Bool=true) global _parallel = p end function foo(a::Int) # there will be many functions like this if _parallel _foo_parallel(a) else _foo_singlethread(a) end end andrew cooke 9/13/15 i don't know of a good way to do this. really, parameterised modules would be great. the simplest thing i can think of is if modules are first class and using etal can take an expression, but the following doesn't run: module A foo() = println("a") end module B foo() = println("b") end module AB m(x) = x ? A : B end using AB using m(true) foo() # wouldn't it be nice if this printed "a"? andrew - show quoted text - andrew cooke 9/13/15 the following works.. first, defining module AB: module A export foo foo() = println("a") end module B export foo foo() = println("b") end module AB if Main.USE_A using A else using B end export foo end that depends on Main.USE_A, where Main is the initial module when things start up, so then you can do: USE_A = true include("ab.jl") using AB foo() which prints "a". no idea if this is considered kosher... andrew - show quoted text - Seth 9/13/15 This looks interesting but I'd want to give the user the option of turning parallelization on and off at will. Not sure this will do it. - show quoted text - andrew cooke 9/13/15 ah, no it wouldn't, sorry. - show quoted text - andrew cooke 9/13/15 although it's not clear that's much better than if USE_A using A else using B end really... - show quoted text - Cedric St-Jean 9/13/15 I'm not sure that I entirely understand your problem but it wouldn't be particularly hard to write macros such that you could define @single_threaded function foo(a::Int) ... end @parallel function foo(a::Int) ... end The expansion of @single_threaded would change the function name to be `foo_singled_threaded`, then define `function foo(a::Int)` to do the dispatching to either functions. Another solution that relies on type dispatching: immutable Flag_Single_Threaded end immutable Flag_Parallel end _parallel = Flag_Single_Threaded() function parallelize(p::Bool=true) global _parallel = p ? Flag_Parallel() : Flag_Single_Threaded() end function foo(a::Int, _running_state::Flag_Single_Threaded=_parallel) println("Single") end function foo(a::Int, _running_state::Flag_Parallel=_parallel) println("Parallel") end foo(5) # outputs "Single" parallelize() foo(10) # outputs "Parallel" This is probably fast enough depending on what you're doing with it, but for what it's worth, by passing `_running_state` to functions called by foo you wouldn't incur any runtime penalty. Best, Cédric - show quoted text - Tom Breloff 9/13/15 Here's a pattern I like to use a lot, that should be what you're looking for. Wrap your value in a type, and have a global const of that type, with a getter and setter method (you can skip the getter/setter, but it makes it nicer). julia> type GlobalVarHolder mybool::Bool end julia> const GLOBALVAR = GlobalVarHolder(true) GlobalVarHolder(true) julia> gv() = GLOBALVAR.mybool gv (generic function with 1 method) julia> gv!(b::Bool) = (GLOBALVAR.mybool = b) gv! (generic function with 1 method) julia> f() = @show gv() f (generic function with 1 method) julia> f() gv() = true true julia> gv!(false) false julia> f() gv() = false false - show quoted text - Seth 9/13/15 That's interesting - but what advantage does it have over a single global variable? - show quoted text - Tom Breloff 9/13/15 Re: [julia-users] Re: Julian way of tracking a "global" module setting It lets you use a constant global (which is much more performant than a mutable global). The const has known type so the compiler can do a better job inferring types for other variables that interact with it. See: http://julia.readthedocs.org/en/latest/manual/performance-tips/#avoid-global-variables - show quoted text -