How to engage all the cores of a CPU?

My cpu has 16 cores, but htops shows I’m using only one. Plz lemme know how can I put all of them to use.

Other than some linear algebra methods, you don’t get parallelism by default. Check these out for how to use threading in Julia:

https://docs.julialang.org/en/v1/manual/multi-threading/

https://docs.julialang.org/en/v1/base/multi-threading/

1 Like

What type of operations are you performing? Depending on the workload, you might want to try the recently-released OhMyThreads.jl, which is intended to provide a simple multithreading interface while avoiding some common performance/correctness pitfalls.

3 Likes

Thank you. I thought getting all the cores to use is called multiprocessing. I didn’t know it was actually multi-threading.

Thank you.I didn’t know it was actually multi-threading. I thought putting all the cores to use is called multiprocessing.

Multithreading and multiprocessing are two forms of computational parallelism that could be used to occupy your cores.

Multithreading involves mutiple parallel execution units called “threads” which share a common memory space and resources within a single process. In Julia, this is mainly handled through the Base.Threads module though packages with alternatives exist.

Multiprocessing involves parallel execution units called “processes” which each have their own memory space and resources. In Julia, this mainly handled through the Distributed standard library package though other packages with alternatives exist.

Multithreading can be more efficient than the multiprocessing model by taking advantage of shared resources. However, multiprocessing can be simpler due to a clear separation of resources. That said these distinctions are not so simple since there are ways to separate resources between threads and share resources between processes.

Julia uses a multithreading model based on virtual threads called Tasks. These virtual threads can be dynamically scheduled on actual threads to achieve parallelism. The potential advantage of virtual threads is that they can be created inexpensively compard to real threads.

To achieve parallelism to occupy all of your cores, you the programmer must ideally find opportunities when computational tasks can run independently of each other. In Julia, a basic parallel form looks like the following.

Threads.@threads for i in 1:16
    independent_task(i)
end

In practice, purely independent tasks are rare and some coordination mechanisms are required.

To simplify parallelism, some packages provide parallel versions of common tasks such as summation. For example, Folds.sum provides mechanisms to parallelize summation using either threads or processes.

4 Likes