I want to use all cores in this Julia code: results = DataFrame(threshold = Int[],…
- User
I want to use all cores in this Julia code: results = DataFrame(threshold = Int[], nr_of_bikes=Int[],sr_brak_na_dzien = Float64[], sr_przewoz_na_dzien = Float64[]) # Empty DataFrame to store results
for threshold in 4:1:25,nr_of_bikes in 5:5:50 # Loop from 15 to 50 with a step size of 5 @async begin number_of_bikes=[1,1,1,1,1]*nr_of_bikes wyniki = run_sims_cum(liczba_rowerow_na_stacji, brak_roweru, overrun, threshold, 3,
code200, 30, prawdopodobienstwo_stacji, popyt_stacji, czas_stacji, popyt_roweru) sr_przewoz_na_dzien=mean(wyniki[!,5]) sr_brak_na_dzien=mean(wyniki[!,4]) push!(results, (threshold = threshold, nr_of_bikes=nr_of_bikes,sr_brak_na_dzien = sr_brak_na_dzien, sr_przewoz_na_dzien = sr_przewoz_na_dzien)) # Append results to the DataFrame endend
and it does not use all my cores. Functions here do have if else conditions, especially run_sims_cum. How to use all my cores to do this calculation?
- Assistant
To use all available CPU cores in your Julia code, you can use the
Threads.@threadsmacro instead of@async. First, make sure you've set the number of threads to the number of available cores on your machine. You can do this by setting theJULIA_NUM_THREADSenvironment variable, or by setting the-toption when starting Julia. For example, if your machine has 8 cores, you can start Julia with 8 threads using:codejulia -t 8Then you can modify your code as follows:
julia - User
How to increase number of threads within the notebook?
- Assistant
To increase the number of threads within a Jupyter notebook, you need to set the
JULIA_NUM_THREADSenvironment variable before starting the Jupyter server. Here's how to do that:-
Close any currently running Jupyter notebooks or Jupyter server instances.
-
Open a terminal/command prompt.
-
Set the
JULIA_NUM_THREADSenvironment variable to the number of threads you want to use. The number of threads should typically be equal to the number of CPU cores available on your machine.On Linux and macOS, in the terminal, run:
shexport JULIA_NUM_THREADS=<number_of_threads>Replace
<number_of_threads>with the desired number of threads, for example:sh
-
- User
Windows11
- Assistant
On Windows 11, you can set the
JULIA_NUM_THREADSenvironment variable and start Jupyter Notebook or JupyterLab using the following steps:-
Close any currently running Jupyter notebooks or Jupyter server instances.
-
Press
Win+Sto open the search bar and typecmdto find the Command Prompt. Click on "Run as administrator" to open an elevated Command Prompt. -
Set the
JULIA_NUM_THREADSenvironment variable to the number of threads you want to use. The number of threads should typically be equal to the number of CPU cores available on your machine. In the elevated Command Prompt, run:codesetx JULIA_NUM_THREADS <number_of_threads>Replace
<number_of_threads>with the desired number of threads, for example:
-
- User
using DataFrames, Statistics
results = DataFrame(threshold=Int[], nr_of_bikes=Int[], sr_brak_na_dzien=Float64[], sr_przewoz_na_dzien=Float64[])
threads = Threads.nthreads() lock = ReentrantLock()
@show threads
Threads.@threads for threshold in 4:1:25, nr_of_bikes in 5:5:50 number_of_bikes = [1, 1, 1, 1, 1] * nr_of_bikes wyniki = run_sims_cum(liczba_rowerow_na_stacji, brak_roweru, overrun, threshold, 3, 200, 30, prawdopodobienstwo_stacji, popyt_stacji, czas_stacji, popyt_roweru)
codesr_przewoz_na_dzien = mean(wyniki[!, 5]) sr_brak_na_dzien = mean(wyniki[!, 4]) lock(f) = lock(f, lock) lock(() -> push!( results, (threshold=threshold, nr_of_bikes=nr_of_bikes, sr_brak_na_dzien=sr_brak_na_dzien, sr_przewoz_na_dzien=sr_przewoz_na_dzien) ))end threads = 6 LoadError: ArgumentError: nested outer loops are not currently supported by @threads in expression starting at In[1]:11
Stacktrace: [1] var"@threads"(source::LineNumberNode, module::Module, args::Vararg{Any}) @ Base.Threads .\threadingconstructs.jl:308
- Assistant
Apologies for the confusion earlier. You're right, nested loops are not supported by the
Threads.@threadsmacro. However, you can still parallelize your code efficiently using other methods in Julia. One option is to use thepmapfunction from theDistributedpackage, which allows you to parallelize higher order functions easily. Here's how you can rewrite your code usingpmap:First, you need to add the required workers using . I recommend adding one worker per CPU core: