How to add Threads to ParallelAccelerator.jl in julia? - multithreading

I was reading the documentations.But I never fond "By which command I can add threads for ParallelAccelerator.jl?"
is that export JULIA_NUM_THREADS=4
or export OMP_NUM_THREADS=4?
And how can I get number of threads in the code? am I able to get number of threads by Threads.nthread() in the code?
Thank you.

Use JULIA_NUM_THREADS to set number of threads, note that this must be done before the julia process is started. You can read about environment variables in the manual.
Yes, the documentation for Base.Threads.nthreads:
help?> Base.Threads.nthreads
Threads.nthreads()
Get the number of threads available to the Julia process. This is the
inclusive upper bound on threadid().

Related

Julia: spawnat always runs on the same threads even if asked to run for multipleprocesses

I am trying to run the same simulation on different threads in Julia. My computer has 4 cores.
I checked that Threads.nthreads() =4, and added processes so that nprocs() = 4.
Now I am trying to run the same simulation:simulation() in parallel.
My code is
#spawnat 1 simulation()
#spawnat 2 simulation()
#spawnat 3 simulation()
#spawnat 4 simulation()
the simulation function displays as well on which threads it runs thanks to a println(Threads.threadid())
The problem is that all processes run on the same thread - thread number 1.
I thought that creating workers and using the #spawnat macro should be enough for the simulation to run on all threads.
Would you have any idea on what to do, and what I did wrong? Thanks in advance
I think you might be confusing multi-threading and multi-processing? From the docs:
help?> #spawnat
#spawnat p expr
Create a closure around an expression and run the closure asynchronously on process p
So your code is executed on one of theworker processes you created with addprocs. Whether or not the code that is being executed uses more than one thread though depends on whether the code itself is multithreaded, so e.g. is using the #threads macro.
The Julia manual discusses multi-threading here and multi-processing here.
EDIT to add: there's a discussion on the Julia Discourse here which includes an example of running multi-threaded worker processes - to do so you need to set an environment variable to ensure worker processes are started with multiple threads.

Two threads accessing the same file in scala

I have a shell script that copies files into a location and another one that picks these up for further processing. I want to use multithreading to pick up files parallelly in Scala using a threadpool.
However, if there are two threads and two files, both of them are picking up the same file. I have tried the program a lot of times, and it always ends up like this. I need the threads to pick up different files in parallel.
Can someone help me out? What approaches can I use? If you could point me in the right direction that would be enough.
I think you can use a parallel sequence to do the processing in parallel.
You don't have to handle this logic yourself. for ex. the code could be like this:
newFiles:Seq[String] = listCurrentFilesNames()
newFiles.par.foreach { fileName =>
processFile(fileName)
}
This code will be executed in parallel. and you could set the number of threads to a specific number as mentioned here: https://stackoverflow.com/a/37725987/2201566
You can also try using actors - for eg- for your reference - https://github.com/tsheppard01/akka-parallel-read-csv-file

How to get the number of threads in the current process on FreeBSD?

I believe no portable solution exists. On linux, we look at /proc//task/, on darwin we use task_threads(). What about FreeBSD?
Seems like kvm_getprocs is the answer.
melisgl is right -- but only if you are using the default thread implementation, where the process' threads are "kernel-visible". (There are other implementations, such as pth, where this may not be true.)
The kernel-visible threads of a process get be counted with kvm_getprocs() using KERN_PROC_PID|KERN_PROC_INC_THREAD (untested). But using the function may require root-privileges :(

Design problem: Running sequence numbr across multi-threaded processes

I have 3 multithreaded processes.
I want to implement a sequence number generator (every call to it shall return next number in sequence).
All the three processes or their threads can request generation of next sequence number.
I am looking for a very low latency solution.
Thanks in advance for ideas.
Sorry for missing this earlier. My platform is:-
- Linux platform
- C++
Since you don't provide much details just a general idea:
You write that there are 3 processes which run multiple threads... I assume they run on the same machine... another assumption: you are using some current version of Windows...
Implement shared memory (via MemoryMappedFile or its native counterpart) and use an atomic increment (InterlockedAdd64 or its managed counterpart Interlocked.Add) from each process to get the next number...
EDIT - after the addition of platform (Linux) by the OP:
You can use the same approach as described above with Linux too:
shared memory can be done via mmap API with the flag MAP_SHARED
atomic operations (like increment/decrement) can be done via libatomic see http://packages.debian.org/source/sid/libatomic-ops
Shared memory area stores the last number in the sequence. Every process, when it needs a new number in the sequence, calculates it and InterlockedCompareExchange()'s it with the old number. If the ICE() succeeds, it uses the number, if it doesn't, the routine is repeated until the ICE() succeeds.
if you are looking for one in JVM world:
class Sequencer {
private AtomicLong sequenceNumber = new AtomicLong(0);
public long next() { return sequenceNumber.getAndIncrement(); }
}

Jmeter Current Thread Number?

I am using a Thread Group with Number Of Threads = 5 with an HTTP request.
In the request I want to include a parameter with the value of the thread number, e.g.,
"pageno": ${threadno}
I want to get the thread number like ${threadno}.
How can I do that?
The thread number is available as:
${__threadNum}
See: functions reference
While the above-mentioned ${__threadNum} will work in many places within jMeter, you'll need to use something else where it is not allowed, e.g., script elements within Pre/Post-Processors.
This answer explains how to get thread number or count within such a script in jMeter.
To get the number of the current thread (out of 5 in your case) use ctx.getThreadNum() which will get the number of the thread.
To get the total number of threads being used by jMeter you can use ctx.getThreadGroup().getNumThreads() or ctx.getThreadGroup().getNumberOfThreads() for total active threads.
https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html#getThreadNum()
https://jmeter.apache.org/api/org/apache/jmeter/threads/AbstractThreadGroup.html
For those looking for the number of active threads in the entire test plan, not just a particular ThreadGroup.
${__groovy(org.apache.jmeter.threads.JMeterContextService.getNumberOfThreads())}
${__threadNum} does not work well alone.
You will need use ${__eval(${__threadNum})}.
Try to use this:
int threadNum=ctx.getThreadGroup().getNumThreads();
ctx is from JmeterContext

Resources