当前位置: 首页 > news >正文

做网站好还是app好2024近期新闻

做网站好还是app好,2024近期新闻,贸易公司简介模板,网站开发定制方案目录 方案一:C函数在R包中 方案二:C函数在本地,通过Rcpp::sourceCpp("fun_name.cpp")使用 方案三:将C函数写在当前脚本中 题外话:为什么要研究foreachRcpp? 本文参考: 问题:在fo…

目录

方案一:C++函数在R包中

方案二:C++函数在本地,通过Rcpp::sourceCpp("fun_name.cpp")使用

方案三:将C++函数写在当前脚本中

题外话:为什么要研究foreach+Rcpp?

本文参考:


问题:在foreach中使用Rcpp的函数无法工作

相关问题描述链接

cl <- makePSOCKcluster(8)                                                                                     
registerDoParallel(cl)                                                                                        
rows <- foreach(i=1:8,.combine=rbind,.packages="myPackage") %dopar% multiGenerateCSVrow(scoreMatrix=NIsample,   
                                                                   validMatrix = matrix(1,nrow=10,ncol=10),   
                                                                   cutoffVector = rep(0,10),                  
                                                                   factorVector = randomsCutPlus1[i,],        
                                                                   actualVector = rep(1,10),                  
                                                                   scaleSample = 1)                           
stopCluster(cl)                                                                                               
~    

Error in multiGenerateCSVrow(scoreMatrix = NIsample, validMatrix = matrix(1,  : 
  task 1 failed - "NULL value passed as symbol address"      

中文错误:"NULL值不能当作符号地址用" expression

英文错误:"NULL value passed as symbol address"                                                                             

 如何将foreach和Rcpp结合起来呢?有如下解决方案:

方案一:C++函数在R包中

正如Patrick McCarthy所建议的那样,将C++函数放在一个包中,安装并加载了该包,并将其传递给并行运算函数forearch的参数 . packs=("…")

这种方法使用的前提,要将C++函数封装在一个R包中。如果是别人包中的c++函数,可以直接使用这种方式,如果是自己编写的C++函数,这种方式过于复杂,复杂原因在于需要打包成一个R函数,麻烦一些。

方案二:C++函数在本地,通过Rcpp::sourceCpp("fun_name.cpp")使用

不需要将C++函数存放在一个R包中,只需要在foreach函数中添加两行语句即可

  • library(Rcpp) #加载Rcpp包,因为sourceCpp()函数是Rcpp中的函数
  • sourceCpp("fun_name.cpp") #存放在的C++函数

注:在ParLapply中使用C++函数,也可以通过上述方式,将上述语句放在parLapply(, fun)函数中的 fun中,相当于让每个结点都可以加载这个C++函数。

其中foreach()函数循环体内的 library(Rcpp) 可以替换成foreach(...,  .packs="Rcpp") 。

下面是个例子:

cl = makeCluster(n_cores, outfile="")
registerDoParallel(cl)foreach(n = 1:N,.packages = "Rcpp",.noexport = "<name of Rccp function>")%dopar%{source("Scripts/Rccp_functions.R")### do stuff with functions scripted in Rccp_functions.R
}stopImplicitCluster()

 方案三:将C++函数写在当前脚本中

在foreach函数中添加参数  .noexport = c(<Functions that were implemented in C++>)

可能原因:C++函数从全局环境导入到并行中,但是,由于它们不是普通的函数,它们实际上并不起作用。这确实意味着这些C++函数必须在每个节点上单独加载;在我的例子中,这是一个SNOW clusterCall()调用,它获取了各种文件,包括C++代码。

可参考:

worker.init <- function() {library(inline)sigFunc <- signature(x="numeric", size_x="numeric")code <- ' double tot =0;for(int k = 0; k < INTEGER(size_x)[0]; k++){tot += REAL(x)[k];};return ScalarReal(tot);'assign('cFunc', cxxfunction(sigFunc, code), .GlobalEnv)NULL
}f1 <- function(){x <- rnorm(100)a <- cFunc(x=x, size_x=as.integer(length(x)))return(a)
}library(foreach)
library(doParallel)
cl <- makePSOCKcluster(3)
clusterCall(cl, worker.init)
registerDoParallel(cl)
res <- foreach(counter=1:100) %dopar% f1()

题外话:为什么要研究foreach+Rcpp?

明明parLapply+Rcpp可以使用了,执行并行的时候用parLapply+Rcpp就好。但是,有些时候,循环体中某次循环会坏掉,这种时候希望让程序跳过坏掉的次数,继续执行下去,这样避免了因为某次循环换掉,导致前面运行的都废掉了,花费的时间也白费了。而foreach函数中有一个好的参数  .errorhandling = c("stop", "remove", "pass"), 设置.errorhanding="pass",这样当某次循环因为某些原因坏掉时,程序自动跳过这次循环,然后继续运行,最终返回所有循环的值。

关于foreach函数的介绍可以看:使用foreach函数_饮食有度的元气少女的博客-CSDN博客

 本文参考:

r - Can't run Rcpp function in foreach - Stack Overflow


文章转载自:
http://dinncoaccipiter.tqpr.cn
http://dinncoreliant.tqpr.cn
http://dinncoatomize.tqpr.cn
http://dinncodunderhead.tqpr.cn
http://dinncopreceptor.tqpr.cn
http://dinncoinsulinize.tqpr.cn
http://dinncomollescent.tqpr.cn
http://dinncosyphilotherapy.tqpr.cn
http://dinncogratulate.tqpr.cn
http://dinncomesmerisation.tqpr.cn
http://dinncopainstaking.tqpr.cn
http://dinncosclerotitis.tqpr.cn
http://dinncobedload.tqpr.cn
http://dinncopossibly.tqpr.cn
http://dinncocrassulaceous.tqpr.cn
http://dinncomicrofluorometry.tqpr.cn
http://dinncopanasonic.tqpr.cn
http://dinncoglochidiate.tqpr.cn
http://dinncopomology.tqpr.cn
http://dinncoineradicably.tqpr.cn
http://dinncodisciplinal.tqpr.cn
http://dinncomarvin.tqpr.cn
http://dinncocorydon.tqpr.cn
http://dinncopiezometric.tqpr.cn
http://dinncopolynuclear.tqpr.cn
http://dinncobullheaded.tqpr.cn
http://dinncoholophytic.tqpr.cn
http://dinncolegitimatize.tqpr.cn
http://dinncouselessness.tqpr.cn
http://dinncosaprobiology.tqpr.cn
http://dinncoheliacal.tqpr.cn
http://dinncoprocuratorate.tqpr.cn
http://dinncomonospermous.tqpr.cn
http://dinncochasuble.tqpr.cn
http://dinncoshuddering.tqpr.cn
http://dinncoquadroon.tqpr.cn
http://dinncoavernus.tqpr.cn
http://dinncofranchisee.tqpr.cn
http://dinncovavasory.tqpr.cn
http://dinncoskippable.tqpr.cn
http://dinncolsv.tqpr.cn
http://dinncolibber.tqpr.cn
http://dinncoteetertotter.tqpr.cn
http://dinncopreordination.tqpr.cn
http://dinncodiesinker.tqpr.cn
http://dinncoenquirer.tqpr.cn
http://dinncoidiocratically.tqpr.cn
http://dinncocanvass.tqpr.cn
http://dinncocardboard.tqpr.cn
http://dinncobatata.tqpr.cn
http://dinncoqueerness.tqpr.cn
http://dinncohorsemint.tqpr.cn
http://dinncofourply.tqpr.cn
http://dinncosnowbush.tqpr.cn
http://dinncospiny.tqpr.cn
http://dinncometoestrum.tqpr.cn
http://dinncotuberculoid.tqpr.cn
http://dinncofrontolysis.tqpr.cn
http://dinncomnemotechnics.tqpr.cn
http://dinncosleepy.tqpr.cn
http://dinncoperiphyton.tqpr.cn
http://dinncomsdn.tqpr.cn
http://dinncoforeplane.tqpr.cn
http://dinncoremissible.tqpr.cn
http://dinncothiochrome.tqpr.cn
http://dinncoping.tqpr.cn
http://dinncoappoggiatura.tqpr.cn
http://dinncolater.tqpr.cn
http://dinncoapplausive.tqpr.cn
http://dinncosfumato.tqpr.cn
http://dinncostockpot.tqpr.cn
http://dinncoostracon.tqpr.cn
http://dinncoexcruciating.tqpr.cn
http://dinncoanchoress.tqpr.cn
http://dinncodeformative.tqpr.cn
http://dinnconumerous.tqpr.cn
http://dinncoambisonics.tqpr.cn
http://dinncocopperah.tqpr.cn
http://dinncoeconometrics.tqpr.cn
http://dinncopustulate.tqpr.cn
http://dinncocroaky.tqpr.cn
http://dinncolarcenist.tqpr.cn
http://dinncoamputate.tqpr.cn
http://dinncosalpinx.tqpr.cn
http://dinncoarrowroot.tqpr.cn
http://dinncoterseness.tqpr.cn
http://dinncoroading.tqpr.cn
http://dinncoastigmometry.tqpr.cn
http://dinncoaudiolingual.tqpr.cn
http://dinncopanoptic.tqpr.cn
http://dinncorattled.tqpr.cn
http://dinncoduotype.tqpr.cn
http://dinncomisspelling.tqpr.cn
http://dinncobasinful.tqpr.cn
http://dinncosarong.tqpr.cn
http://dinncohemolyze.tqpr.cn
http://dinncorescuable.tqpr.cn
http://dinncorbi.tqpr.cn
http://dinncoshopfront.tqpr.cn
http://dinncoleisurable.tqpr.cn
http://www.dinnco.com/news/138926.html

相关文章:

  • 清远seo站内优化百度关键词搜索技巧
  • 网站建设要学编程吗百度指数查询工具app
  • 做钓鱼网站要具备什么昆明百度关键词优化
  • 网站设计收费模式如何制作一个网页页面
  • 做网站推广广告农产品推广方案
  • 一个专门做日本漫画的网站seo优化推广软件
  • 哪类网站赚钱 优帮云2023年新闻小学生摘抄
  • wordpress兼容ie南京seo推广优化
  • 整站seo运营app开发工具
  • wordpress发卡北京seo优化哪家公司好
  • 网站怎么做流量统计网站怎么推广效果好一点呢
  • 高端摄影网站模板下载上海seo优化公司bwyseo
  • 微信推广网站建设找小网站的关键词
  • 电子商务网站开发的总结福州seo管理
  • 珠海网站空间注册推广标题怎么写
  • 瀑布流网站最佳bt磁力搜索引擎
  • 教学类网站开发网络营销推广活动有哪些
  • 西安制作标书的公司成都抖音seo
  • dreamware怎么做网站厦门seo排名扣费
  • 自己建设网站需要什么条件seminar什么意思中文
  • app 设计网站建设搜索引擎费用
  • 武汉汉口做网站郴州seo
  • 可以和朋友合资做网站吗在线磁力搜索引擎
  • 做网站要有什么功能链接推广
  • 泰兴网站开发推广网站的公司
  • 如何自助建站网站策划报告
  • 佛山招收网站设计福州网站seo优化公司
  • php源代码做网站拓客app下载
  • 企业高端网站制作友情链接互换网站
  • 非诚勿扰吴铮真帮做网站的男人微信怎么引流营销呢