How to define a macro everywhere in Julia? - parallel-processing

I have a trivial macro
macro lit_str(s)
s
end
which I am using to generate regex patterns. (Context here). Normally I have this wrapped in a module called HelperFunctions (and export it using ```export #lit_str). I'm trying to use it in a function called via RemoteRef, but even if I do
#everywhere using HelperFunctions
I get an error like
exception on exception on 3: exception on 2: 4: ERROR: #lit_str not defined
in eval at C:\cygwin\home\vagrant\buildbot\slave\package_win8_1-x64\build\base\sysimg.jl:7
in anonymous at multi.jl:1305
in anonymous at multi.jl:855
in run_work_thunk at multi.jl:621
in anonymous at task.jl:855
ERROR: #lit_str not defined
in eval at C:\cygwin\home\vagrant\buildbot\slave\package_win8_1-x64\build\base\sysimg.jl:7
in anonymous at multi.jl:1305
in anonymous at multi.jl:855
in run_work_thunk at multi.jl:621
in anonymous at task.jl:855
ERROR: #lit_str not defined
in eval at C:\cygwin\home\vagrant\buildbot\slave\package_win8_1-x64\build\base\sysimg.jl:7
in anonymous at multi.jl:1305
in anonymous at multi.jl:855
in run_work_thunk at multi.jl:621
in anonymous at task.jl:855
Is there any way to export a macro so it can be used by processes spawned with remoteref?

This was a no-brainer, but I'll put the solution here in case anyone else has the same issue. I needed to do
addprocs(numprocs)
before
#everywhere using WhatNot
since I was doing this in Jupyter and not by calling julia -p 8 myfile.jl.

Related

panic: testing: Verbose called before Init

Trying to run
https://github.com/adonovan/gopl.io/blob/master/ch8/cake/cake_test.go
but got
panic: testing: Verbose called before Init
goroutine 1 [running]:
testing.Verbose(...)
/usr/lib/go-1.17/src/testing/testing.go:453
.../cake_test.init()
It says the error comes from cake_test.init(), yet the cake_test.go file doesn't contain init():
$ grep init cake_test.go | wc
0 0 0
What exactly is the problem?
This happens because the testing package itself has a init to process test flags and you are callind Verbose to create a global variable
https://cs.opensource.google/go/go/+/refs/tags/go1.18:src/testing/testing.go;l=548
To avoid this you can:
Use a ponter to function to capture testing.Verbose and use inside functions, after all initializations ( Verbose will be type func() bool) but I think this is complex
Add a helper that creates a new default on each test with a safe call to testing.Verbose
Create a constructor (instead a test helper) that fill with the most common options and you can set the value of Verbose on each test
Create a two constructors, a second one that receive the value of Verbose
Also, Consider create a method Debug with same signature of Println, to call fmt.Println if Verbose is true
If you have a constructor, you can create a second field: Logger, with is a ponter to function with same signature as Println and you initialize with fmt.Println and in the tests you can set as t.Log to have a better experience
Of course perhaps some suggestions are more advanced than others, feel free to play a little bit and choose the best ones

Symbol not found in Lauterbach cmm script

I have declared clk_proc on top of my script:
LOCAL &clk_proc
Later in my script I am using the variable
&clk_proc=v.value(clk_proc)
I am getting an error:
"Symbol not found in this context"
Any idea ?
you are getting error here because clk_proc is not defined only **&**clk_proc is defined as lauterbach will treat clk_proc as a new variable which is not defined any where so throwing error.
&clk_proc=v.value(clk_proc)
We need to use the "&" always while using the variable in lauterbach.
Try with below:
&clk_proc=v.value(**&**clk_proc)
this will work.

Parallel processing: #everywhere, distributions and types

I have recently started studying parallel processing in Julia and I am having an issue which I don't really understand how to fix.
After having executed Julia with julia -p 4 I want to load the Distributions module in all the processes and I would like to define a Type which depends on Distributions.
The following apparently works correctly when I include it:
#everywhere using Distributions
type TypeDistrib{T <: Float64}
d::Distributions.Normal{T}
end
If I write exactly the same code as a module, then it doesn't:
module test
#everywhere using Distributions
type TypeDistrib{T <: Float64}
d::Distributions.Normal{T}
end
export TypeDistrib
end
The above gives the following error message:
ERROR: LoadError: UndefVarError: Distributions not defined
in include_from_node1(::String) at ./loading.jl:488
in include_from_node1(::String) at /Applications/Julia-0.5.app/Contents/Resources/julia/lib/julia/sys.dylib:?
while loading /***/test.jl, in expression starting on line 4
Would you please clarify what I am not doing correctly here?
Note: I replaced the full path in the error message with *** since it was confusing.
#everywhere something evaluates something in the Main module across all processes. And in this case, not in test module, and therefore the error in the second case and not in the first.
Perhaps
#everywhere module test
using Distributions
type TypeDistrib{T <: Float64}
d::Distributions.Normal{T}
end
export TypeDistrib
end
does what is meant.

Can I make an alias for `set -l` in the fish shell?

I make liberal use of set -l/set --local in my fish scripts, so I considered making an alias for it. The name local seems apt. Unfortunately, the obvious approach doesn't work:
function local
set -l $argv
end
This fails because, of course, the variable this creates is local to the local function itself. Using alias doesn't work, either, since it's just a (perhaps legacy) shorthand for the above function.
Is there any way I can create a local variable in the calling scope without doing anything more complicated than local foo 'bar'? Obviously I could make something like eval (local foo 'bar') work, but that would sort of defeat the purpose. I'm open to any sort of trickery or hacks that would make this work, if any exist.
I'm afraid not, local variables are always local to the topmost scope, and there's no way to wrap set without creating a new scope. The best you can do is this:
function def --no-scope-shadowing
set $argv
end
This creates a variable in the calling function scope. This is subtly different than local: local is scoped to a block, while the above is scoped to the function. Still it may be sufficient for your use case.

Julia: Having a function f() containing the macro #printf, how can I access the output outside f()?

In the Julia NMF package a verbose option provides information on convergence using the #printf macro.
How can I access this output without rewriting the NMF package io?
To rephrase, having a function f() containing the macro #printf, how can I access the output outside f()?
This does seem like useful functionality to have: I would suggest that you file an issue with the package.
However, as a quick hack, something like the following should work:
oldout = STDOUT
(rd,wr) = redirect_stdout()
start_reading(rd)
# call your function here
flush_cstdio()
redirect_stdout(oldout)
close(wr)
s = readall(rd)
close(rd)
s

Resources