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
On Sunday, September 13, 2015 at 9:32:53 PM UTC-4, andrew cooke wrote:
>
>
> although it's not clear that's much better than
>
> if USE_A
> using A
> else
> using B
> end
>
> really...
>
> On Sunday, 13 September 2015 21:59:37 UTC-3, andrew cooke wrote:
>>
>>
>> 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
>>
>>
>>
>>
>> On Sunday, 13 September 2015 21:45:57 UTC-3, andrew cooke wrote:
>>>
>>>
>>> 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
>>>
>>>
>>> On Sunday, 13 September 2015 19:33:16 UTC-3, Seth wrote:
>>>>
>>>> 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
>>>>
>>>>