print(paste0("The number of cores on my computer is: ",
parallel::detectCores()))[1] "The number of cores on my computer is: 16"
Derek Sollberger
June 22, 2025
For my machine learning class (with AI topics), I should survey the students to get a sense of how powerful their computers are. In particular, I want to ask them
For my own skills, I want to provide the code for these tasks in both R and Python.
In R, we can use the detectCores() function from the parallel package for this task.
[1] "The number of cores on my computer is: 16"
Aside, upon looking up this function, I came across this neat blog post that advises developers to avoid using detectCores() (i.e. don’t use all of the users’ CPUs).
I have been moving to teaching in PyTorch, so I am familiar with torch packages.
At the time of this writing, my CUDA version was higher than what the R torch package was compatibile with.
# Check if CUDA is available
cuda_available = torch.cuda.is_available()
# Print the result
print(paste("CUDA available:", cuda_available))
# If CUDA is available, you can also print the number of GPUs
if (cuda_available) {
print(paste("Number of GPUs available:", torch.cuda.device_count()))
print(paste("GPU Name:", torch.cuda.get_device_name(0))) # Assuming at least one GPU
} else {
print("CUDA is not available on this system.")
}We can use the cpu_count() function from the multiprocessing library.
R version 4.5.0 (2025-04-11 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)
Matrix products: default
LAPACK version 3.12.1
locale:
[1] LC_COLLATE=English_United States.utf8
[2] LC_CTYPE=English_United States.utf8
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.utf8
time zone: America/New_York
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] digest_0.6.37 fastmap_1.2.0 xfun_0.52 Matrix_1.7-3
[5] lattice_0.22-6 reticulate_1.42.0 knitr_1.50 parallel_4.5.0
[9] htmltools_0.5.8.1 png_0.1-8 rmarkdown_2.29 cli_3.6.5
[13] grid_4.5.0 withr_3.0.2 compiler_4.5.0 rprojroot_2.0.4
[17] here_1.0.1 rstudioapi_0.17.1 tools_4.5.0 evaluate_1.0.3
[21] Rcpp_1.0.14 yaml_2.3.10 rlang_1.1.6 jsonlite_2.0.0
[25] htmlwidgets_1.6.4