R:如何检查有多少 cores/CPU 可用
R: how to check how many cores/CPU usage available
R 是单线程的。
- 使用R,如何检查Windows和Linux中有多少cores/threads是运行R? (或者有多少Rs运行)
- 使用R,如何查看Windows和Linux中运行R的每个内核的使用情况? (或者每个 R 使用的 CPU 的百分比)
例如,如果我有两个 R 打开了 运行 个项目。我希望有 2 个线程 运行 R,每个线程有 % CPU。然后我打开另一个R。如何使用第三个R来检查线程数(在这种情况下为2)和R使用的CPU百分比?
如果您打开多个 R windows,每个 window 将 运行 在不同的内核上运行,直到您拥有的最大内核数。这是在 windows 和 mac 计算机上自动实现的。如果想知道自己有多少核,可以运行:
library(parallel)
detectCores()
在 Linux 上,您可以向系统发送 ps 命令:它会为您提供平均 cpu 使用情况以及名为 rsession:
的程序的内存使用情况
splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
df <- do.call(rbind, lapply(splitted[-1],
function(x) data.frame(
cpu = as.numeric(x[2]),
mem = as.numeric(x[4]),
pid = as.numeric(x[5]),
cmd = paste(x[-c(1:5)], collapse = " "))))
df
# cpu mem pid cmd
#1 0.8 0.7 11001 /usr/lib/rstudio/bin/rsession
#2 0.0 0.2 12397 /usr/lib/rstudio/bin/rsession
#3 0.1 0.7 14960 /usr/lib/rstudio/bin/rsession
#4 0.4 0.2 26122 /usr/lib/rstudio-server/bin/rsession
#5 0.3 8.3 35782 /usr/lib/rstudio/bin/rsession
您可能可以改进它以获取父 ID 和瞬时 CPU 使用情况以及传递给 ps 或 top 的其他选项,并推断出每个会话使用的核心数。
在 Windows 你可以试试这个:
a <- system("wmic path Win32_PerfFormattedData_PerfProc_Process get Name,PercentProcessorTime", intern = TRUE)
df <- do.call(rbind, lapply(strsplit(a, " "), function(x) {x <- x[x != ""];data.frame(process = x[1], cpu = x[2])}))
df[grepl("Rgui|rstudio", df$process),]
# process cpu
# 105 Rgui 0
# 108 rstudio 0
对于那些想知道 cores/cpus and/or 节点数量的 workers/compute 节点 1) 在机器上可用或 2) 由 HPC 集群分配当前的 R 程序是 运行,试试这个(使用来自并行和 future 包的函数):
library(parallel) # for using parallel::mclapply() and checking #totalCores on compute nodes / workstation: detectCores()
library(future) # for checking #availble cores / workers on compute nodes / workstation: availableWorkers() / availableCores()
workers <- availableWorkers()
cat(sprintf("#workders/#availableCores/#totalCores: %d/%d/%d, workers:\n", length(workers), availableCores(), detectCores()))
print( workers )
对于那些感兴趣的人,我和一个朋友创建了一个 Github 存储库,其中包含从 R 控制台探测 computer/server 上正在进行的进程的功能。
这是link:https://github.com/mathosi/cluster_check
要回答这个问题你可以使用我制作的函数ps.to.df()
:
devtools::source_url("https://github.com/mathosi/cluster_check/blob/master/ps_to_df.R?raw=TRUE")
ps.to.df() #List All processes sorted by %CPU usage
ps.to.df(bylist.selection = "C rsession") #List All processes named 'rsession' sorted by %CPU usage
输出是一个 data.frame,因此您可以按照您希望在 R 中的方式对其子集进行排序,以查找您想看到的任何内容!
我还没有尝试所有可能的查询 ps.to.df()
支持,但我想它应该支持其他查询。
还有提高输出的灵活性和可读性的空间,也许还可以创建其他功能。任何有兴趣的人都可以加入并做出贡献。
Using R, how to check how many cores/threads are running R in Windows
and Linux? (Or how many Rs are running)
我在这里还没有读到的一个有效答案是简单地使用 ps R 包和函数 ps()
然后你可以子集table 由名称为 "rsession" 的进程返回:
ps::ps()[ps::ps()$name == "rsession",]
行数将为您提供 computer/server:
上现有的会话数
nrow(ps::ps()[ps::ps()$name == "rsession",])
我不完全确定 function ps_num_threads()
的作用,但检查结果是否有意义也可能很有趣:
ps::ps_num_threads(ps::ps_handle())
不幸的是,我没有在 ps R 包中找到任何关于 %CPU 用法的信息,但您可以尝试我引用的函数我的另一个答案,它应该在 Linux.
下工作
有一个更简单的方法,使用benchmarkme包。
library(benchmarkme)
get_cpu()$no_of_cores
R 是单线程的。
- 使用R,如何检查Windows和Linux中有多少cores/threads是运行R? (或者有多少Rs运行)
- 使用R,如何查看Windows和Linux中运行R的每个内核的使用情况? (或者每个 R 使用的 CPU 的百分比)
例如,如果我有两个 R 打开了 运行 个项目。我希望有 2 个线程 运行 R,每个线程有 % CPU。然后我打开另一个R。如何使用第三个R来检查线程数(在这种情况下为2)和R使用的CPU百分比?
如果您打开多个 R windows,每个 window 将 运行 在不同的内核上运行,直到您拥有的最大内核数。这是在 windows 和 mac 计算机上自动实现的。如果想知道自己有多少核,可以运行:
library(parallel)
detectCores()
在 Linux 上,您可以向系统发送 ps 命令:它会为您提供平均 cpu 使用情况以及名为 rsession:
的程序的内存使用情况splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
df <- do.call(rbind, lapply(splitted[-1],
function(x) data.frame(
cpu = as.numeric(x[2]),
mem = as.numeric(x[4]),
pid = as.numeric(x[5]),
cmd = paste(x[-c(1:5)], collapse = " "))))
df
# cpu mem pid cmd
#1 0.8 0.7 11001 /usr/lib/rstudio/bin/rsession
#2 0.0 0.2 12397 /usr/lib/rstudio/bin/rsession
#3 0.1 0.7 14960 /usr/lib/rstudio/bin/rsession
#4 0.4 0.2 26122 /usr/lib/rstudio-server/bin/rsession
#5 0.3 8.3 35782 /usr/lib/rstudio/bin/rsession
您可能可以改进它以获取父 ID 和瞬时 CPU 使用情况以及传递给 ps 或 top 的其他选项,并推断出每个会话使用的核心数。
在 Windows 你可以试试这个:
a <- system("wmic path Win32_PerfFormattedData_PerfProc_Process get Name,PercentProcessorTime", intern = TRUE)
df <- do.call(rbind, lapply(strsplit(a, " "), function(x) {x <- x[x != ""];data.frame(process = x[1], cpu = x[2])}))
df[grepl("Rgui|rstudio", df$process),]
# process cpu
# 105 Rgui 0
# 108 rstudio 0
对于那些想知道 cores/cpus and/or 节点数量的 workers/compute 节点 1) 在机器上可用或 2) 由 HPC 集群分配当前的 R 程序是 运行,试试这个(使用来自并行和 future 包的函数):
library(parallel) # for using parallel::mclapply() and checking #totalCores on compute nodes / workstation: detectCores()
library(future) # for checking #availble cores / workers on compute nodes / workstation: availableWorkers() / availableCores()
workers <- availableWorkers()
cat(sprintf("#workders/#availableCores/#totalCores: %d/%d/%d, workers:\n", length(workers), availableCores(), detectCores()))
print( workers )
对于那些感兴趣的人,我和一个朋友创建了一个 Github 存储库,其中包含从 R 控制台探测 computer/server 上正在进行的进程的功能。
这是link:https://github.com/mathosi/cluster_check
要回答这个问题你可以使用我制作的函数ps.to.df()
:
devtools::source_url("https://github.com/mathosi/cluster_check/blob/master/ps_to_df.R?raw=TRUE")
ps.to.df() #List All processes sorted by %CPU usage
ps.to.df(bylist.selection = "C rsession") #List All processes named 'rsession' sorted by %CPU usage
输出是一个 data.frame,因此您可以按照您希望在 R 中的方式对其子集进行排序,以查找您想看到的任何内容!
我还没有尝试所有可能的查询 ps.to.df()
支持,但我想它应该支持其他查询。
还有提高输出的灵活性和可读性的空间,也许还可以创建其他功能。任何有兴趣的人都可以加入并做出贡献。
Using R, how to check how many cores/threads are running R in Windows and Linux? (Or how many Rs are running)
我在这里还没有读到的一个有效答案是简单地使用 ps R 包和函数 ps()
然后你可以子集table 由名称为 "rsession" 的进程返回:
ps::ps()[ps::ps()$name == "rsession",]
行数将为您提供 computer/server:
上现有的会话数nrow(ps::ps()[ps::ps()$name == "rsession",])
我不完全确定 function ps_num_threads()
的作用,但检查结果是否有意义也可能很有趣:
ps::ps_num_threads(ps::ps_handle())
不幸的是,我没有在 ps R 包中找到任何关于 %CPU 用法的信息,但您可以尝试我引用的函数我的另一个答案,它应该在 Linux.
下工作有一个更简单的方法,使用benchmarkme包。
library(benchmarkme)
get_cpu()$no_of_cores