We want to know about the CPU cores in multi-process programs because it helps:
To allocate the resources.
To optimize the program with the help of parallel processing.
To check the hardware compatibility for software.
In this Answer, we will discuss how to obtain the number of cores in the CPU or GPU in Julia.
Note: If you want perform parallel programming in Julia, then here is the link.
There are several methods to obtain the number of CPU cores which are as below:
Sys.cpu_info()
Sys.CPU_THREADS
Base.Sys.cpu_summary
Let us discuss them one by one,
Sys.cpu_info()
Sys.cpu_info()
gives you the number of cores and also provides detailed information about each core.
CPU_Info=Sys.cpu_info()println(CPU_Info)
Sys.CPU_THREADS
This command will only give you the number of CPU cores in your system.
CPU_Cores = Sys.CPU_THREADSprintln("Number of CPU cores: ",CPU_Cores )
Base.Sys.cpu_summary()
This will retrieve the same result as Sys.cpu_info() .
cpu_summary=Base.Sys.cpu_summary()println(cpu_summary)
We can only obtain the number of GPU cores if a GPU exists. The code to obtain the GPU cores is:
using CUDAif CUDA.functional()num_gpu_cores = CUDA.device().attributes.MULTIPROCESSOR_COUNT * CUDA.device().attributes.MULTIPROCESSOR_CORE_COUNTprintln("Number of GPU cores: ", num_gpu_cores)elseprintln("No GPU found.")end
In this Answer, we learned how to retrieve the number of cores in the CPU and GPU. We discussed different methods to achieve this purpose.
Note: Here is the link if you want to learn how to measure the elapsed CPU time in Julia.