我正在使用 Go 开发 TCP 服务器。现在我想通知所有与客户端交谈的 goroutine 放弃他们的连接,转储他们所拥有的并停止。
关闭频道是一种通知所有人的方式。
问题是:这是惯用的 Go 语言吗?如果我错了;那么我应该怎么做(通知所有的 goroutines——比如 .NET 中的 ManualResetEvent)?
注意:我是 Go 新手,刚刚学习并开始使用 TCP Server,因为我之前用 C# 编写过。
在 MATLAB 中,我有一个变量proba,parfor loop如下所示:
parfor f = 1:N
proba = (1/M)*ones(1, M);
% rest of the code
end
pi_proba = proba;
Run Code Online (Sandbox Code Playgroud)
MATLAB 说:“临时变量 'proba' 在 PARFOR 循环之后使用,但它的值是不确定的”
我不明白如何纠正这个错误。我需要使用并行循环,我需要proba在循环之后。这该怎么做?
能够在 Windows 上多线程会很棒,但也许这个问题比我想象的要难.. :(
里面survey:::svyby.default有一个块,lapply或者mclapply取决于multicore=TRUE你的操作系统。lapply无论如何,Windows 用户都会被迫进入循环,我想知道是否有任何方法可以mclapply代替……加快计算速度。
我不太了解并行处理的内部结构,但我做了一些实验,看看是否有任何 Windows 可接受的替代方案可行。首先我尝试覆盖 mclapply
mclapply <-
function( X , FUN , ... ){
clusterApply(
x = X ,
fun = FUN ,
cl = makeCluster( detectCores() ) , ... )
}
Run Code Online (Sandbox Code Playgroud)
接下来我用来fixInNamespace( svyby.default , "survey" )删除该行
if (multicore) parallel:::closeAll()
但这只是让我达到了
> svyby(~api99, ~stype, dclus1, svymean , multicore=TRUE )
Error in checkForRemoteErrors(val) :
3 nodes produced errors; first error: object 'svymean' …Run Code Online (Sandbox Code Playgroud) 每个内核的最佳进程数是多少?假设你有一台有 2 个 CPU 和 4 个内核的机器,可以为你提供最佳性能的进程数是多少?
谢谢你的帮助。
我已经用 C++ 编码多年,过去我使用过线程,但我现在才开始学习多线程编程及其实际工作原理。
到目前为止,我在理解这些概念方面做得还不错,但是有一件事让我感到困惑。
我在网上找不到任何足以让我理解的内容。
我用 C++ 编写代码,但我确信这个问题适用于许多不同的编程语言。
在同一个变量上应用 firstprivate 和 lastprivate 是否正确?
例如:
void main (){
int a= 100, i;
#pragma omp for firstprivate(a) lastprivate(a)
for(i = 0; i <9; i++){
bla bla bla;
}
printf("a= %d",a);
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我无法开始parRF工作,即使其他事情如parApply工作正常。
我已经尝试makeCluster过以及类似makePSOCKcluster的一些变体。
它不断返回错误 task 1 failed - could not find function getDoParWorkers
cores_2_use <- detectCores() - 2
cl <- makeCluster(cores_2_use, useXDR = F)
clusterSetRNGStream(cl, 9956)
registerDoParallel(cl, cores_2_use)
rf_train <- train(y=y, x=x,
method='parRF', tuneGrid = data.frame(mtry = ncol(x)), na.action = na.omit,
trControl=trainControl(method='oob',number=10, allowParallel = TRUE)
)
Error in { : task 1 failed - "could not find function "getDoParWorkers""
Run Code Online (Sandbox Code Playgroud) 我有一个大小为 n_slice x 2048 x 3 的 numpy 坐标数组,其中 n_slice 数以万计。我想分别对每个 2048 x 3 切片应用以下操作
import numpy as np
from scipy.spatial.distance import pdist
# load coor from a binary xyz file, dcd format
n_slice, n_coor, _ = coor.shape
r = np.arange(n_coor)
dist = np.zeros([n_slice, n_coor, n_coor])
# this loop is what I want to parallelize, each slice is completely independent
for i in xrange(n_slice):
dist[i, r[:, None] < r] = pdist(coor[i])
Run Code Online (Sandbox Code Playgroud)
我尝试通过制作coor一个来使用 Dask dask.array,
import dask.array as …Run Code Online (Sandbox Code Playgroud) 假设我想以并行方式应用myfunction到myDataFrame. 假设这otherDataFrame是一个包含两列的数据框:COLUNM1_odf并且COLUMN2_odf出于某些原因在myfunction. 所以我想用这样的方式编写代码parApply:
clus <- makeCluster(4)
clusterExport(clus, list("myfunction","%>%"))
myfunction <- function(fst, snd) {
#otherFunction and aGlobalDataFrame are defined in the global env
otherFunction(aGlobalDataFrame)
# some code to create otherDataFrame **INTERNALLY** to this function
otherDataFrame %>% filter(COLUMN1_odf==fst & COLUMN2_odf==snd)
return(otherDataFrame)
}
do.call(bind_rows,parApply(clus,myDataFrame,1,function(r) { myfunction(r[1],r[2]) }
Run Code Online (Sandbox Code Playgroud)
这里的问题是 R 无法识别COLUMN1_odf,COLUMN2_odf即使我将它们插入clusterExport. 我怎么解决这个问题?有没有办法“导出”所有snow需要的对象,以便不枚举它们中的每一个?
编辑1:我添加了一个注释(在上面的代码中),以指定的otherDataFrame被interally创建myfunction。
编辑 2:为了概括,我添加了一些伪代码myfunction:它现在使用全局数据帧(aGlobalDataFrame …
我一直在互联网上搜索,试图了解并行处理。
他们似乎都假设我有某种循环函数在运行,例如在 N 个核心之间划分并随后组合的数据集的每第 N 行,并且我指向许多并行化apply()函数。
(警告,下面丑陋的代码)
我的情况是我已经在表格上
tempJob <- myFunction(filepath, string.arg1, string.arg2)
Run Code Online (Sandbox Code Playgroud)
其中路径是文件位置,字符串参数是对数据进行排序的各种方式。
我目前的工作流程只是积累了很多
tempjob1 <- myFunction(args)
tempjob2 <- myFunction(other args)
...
tempjobN <- myFunction(some other args here)
# Make a list of all temporary outputs in the global environment
temp.list <- lapply(ls(pattern = "temp"), get)
# Stack them all
df <- rbindlist(temp.list)
# Remove all variables from workspace matching "temp"
rm(list=ls(pattern="temp"))
Run Code Online (Sandbox Code Playgroud)
这些作业是完全独立的,原则上可以在 8 个独立的 R 实例中运行(尽管我猜这会很麻烦)。我如何将前 8 个作业分成 8 个内核,每当一个内核完成其工作并将处理过的数据集返回到全局环境时,它就会简单地执行下一个作业。