我在下面的示例代码中使用了foreach和doParallel。
<p:dataTable id="tbl"
value="#{measurementRecordingLazyDataActions.measurementRecordings}"
var="tempMeasurementRecording"
rowKey="#{tempMeasurementRecording.measurementRecordingID}"
widgetVar="#{tableConsts.widgetVar}"
tableStyleClass="#{tableConsts.tableStyleClass}"
rows="#{tableConsts.rows}"
rowsPerPageTemplate="#{tableConsts.rowsPerPageTemplate}"
resizableColumns="#{tableConsts.resizableColumns}"
scrollable="true" scrollHeight="470" lazy="true">
<f:facet name="{Exporters}">
<h:commandLink>
<i class="fa fa-file-excel-o" />
<p:dataExporter type="xls" target="tbl"
fileName="measurementrecording" />
</h:commandLink>
</f:facet>
<c:forEach
items="#{tempMeasurementRecording.doubleRecordingComponents}"
var="doubleRecordingComponent">
<p:column
headerText="#{doubleRecordingComponent.minMaxComponent.name}"
filterBy="#{doubleRecordingComponent.value}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{doubleRecordingComponent.value}"
title="#{doubleRecordingComponent.value}">
<f:converter converterId="decimalNumberConverter" />
</h:outputText>
</f:facet>
<f:facet name="input">
<h:inputText styleClass="#{tableConsts.errorClass}"
value="#{doubleRecordingComponent.value}"
style="width:100%"
label="#{doubleRecordingComponent.value}">
<f:converter converterId="decimalNumberConverter" />
</h:inputText>
</f:facet>
</p:cellEditor>
</p:column>
<p:column
headerText="#{doubleRecordingComponent.minMaxComponent.name}"
filterBy="#{doubleRecordingComponent.minMaxComponent.min}">
<p:cellEditor>
<f:facet name="output">
<h:outputText
value="#{doubleRecordingComponent.minMaxComponent.min}"
title="#{doubleRecordingComponent.minMaxComponent.min}">
<f:converter converterId="decimalNumberConverter" />
</h:outputText>
</f:facet>
<f:facet name="input">
<h:inputText styleClass="#{tableConsts.errorClass}"
value="#{doubleRecordingComponent.minMaxComponent.min}"
style="width:100%"
label="#{doubleRecordingComponent.minMaxComponent.min}">
<f:converter converterId="decimalNumberConverter" />
</h:inputText>
</f:facet>
</p:cellEditor>
</p:column>
</c:forEach>
</p:dataTable>
在clusterExport的envir参数中,当我使用 envir = .GlobalEnv 时,我收到错误
library(foreach)
library(doParallel)
clusters = makeCluster(4)
registerDoParallel(clusters)
fun1 <- function(param1, param2, param3)
{
param4 = param1+param2
param5 = param2+param3
param6 = param3+param1
print(ls(envir = environment()))
print(ls(envir = .GlobalEnv))
# clusterExport(cl = clusters, varlist = ls(), envir = environment())
clusterExport(cl = clusters, varlist = ls(), envir = .GlobalEnv)
mmm = foreach(i = 1:length(param1), .combine = rbind) %dopar% fun2(i, param4, param5, param6)
print(mmm)
}
fun2 <- function(i, param4, param5, param6)
{
j = param4[i] * param5[i] * param6[i]
}
param1 = 1:10
param2 = 2:11
param3 = 3:12
fun1(param1, param2, param3)
。
当我使用 envir = environment()时,错误是
**"Error in get(name, envir = envir) : object 'param4' not found"**
我的问题是如何将全局和功能环境中的所有对象导出到集群。
答案 0 :(得分:0)
当您使用某个功能时,无需手动导出所需的对象。但是,如果您希望fun2
中的fun1
访问foreach
,则必须将fun1
传递给fun1 <- function(param1, param2, param3, fun2)
{
param4 = param1+param2
param5 = param2+param3
param6 = param3+param1
mmm = foreach(i = 1:length(param1), .combine = rbind) %dopar% {
fun2(i, param4, param5, param6)
}
print(mmm)
}
fun1(param1, param2, param3, fun2)
。
所以,就这样做:
{{1}}