How to obtain the number of cores (CPU or GPU) in Julia

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.

Number of CPU cores

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_THREADS
println("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)

Number of GPU cores

We can only obtain the number of GPU cores if a GPU exists. The code to obtain the GPU cores is:

using CUDA
if CUDA.functional()
num_gpu_cores = CUDA.device().attributes.MULTIPROCESSOR_COUNT * CUDA.device().attributes.MULTIPROCESSOR_CORE_COUNT
println("Number of GPU cores: ", num_gpu_cores)
else
println("No GPU found.")
end

Conclusion

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.

Copyright ©2024 Educative, Inc. All rights reserved