Function depending on the global variable inside module

no I mean things like

@threads for i in 1:n .... or @spawn begin .... end

Yes, that is basically β€œlike OpenMP:” shared-memory threads, with decorator directives to make it easy. As opposed to distributed-memory programming like MPI.

Thanks. The second point above seems to be a design choice. Sometimes I feel that it is quite cumbersome to specify the argument type in a function if that type itself is a parametric one.

Consider again your above example. In solve(prob::Problem, alg::EulerMethod), no constraints are exerted on Problem. However, I usually have some constraints in mind during design. For example, I will not expect that the type parameter G to be a String (at least in my design). Then, shall I enforce this constraint in the signature of solve(prob::Problem, alg::EulerMethod) or simply throw an error during execution?

Thank you!
Yes, I was thinking it must be something related with shared memory stuff that can cause issue for global variable. For MPI, it seems using global variable is safe because each core are independent.

I think both options can be fine. Sometimes I prefer to throw an error on execution, because the error message can be more user friendly.

2 Likes

Thanks @lmiq .

Did you mean we check the type on the first line of the function body (e.g., using something like @assert) or just let it run and encounter finally an error spontaneously? :grinning_face_with_smiling_eyes:

Yes, one or other option. The use of an assertion allow you to throw a customized error message.

(personally I would like to have an option to throw an error without launching the complete stack trace, meaning, I would like to have something that behaves like this:

julia> function f(x)
         if typeof(x) != Int
           println("x must be an Int")
           return nothing
         end
         x
       end
f (generic function with 1 method)

julia> f(1.0)
x must be an Int

Instead of:

julia> function f(x)
         @assert typeof(x) == Int "x must be an Int"
         x
       end
f (generic function with 1 method)

julia> f(1.0)
ERROR: AssertionError: x must be an Int
Stacktrace:
 [1] f(x::Float64)
   @ Main ./REPL[6]:2
 [2] top-level scope
   @ REPL[7]:1

In this case the stack trace is not large, but in some cases it can be overwhelming for a user.

1 Like