【问题标题】:How can I use multicore and GPU processing in my code?如何在我的代码中使用多核和 GPU 处理?
【发布时间】:2022-11-08 16:30:40
【问题描述】:

我正在使用 swmmr 包和 SCE-UA 包进行参数优化。 问题是,当我获得更多变量时,我的 PC 上的处理速度正在减慢。 通过谷歌搜索,我发现有多核或 GPU 处理。 但是,我不知道如何将它们应用到我的代码中

library(swmmr) #Load package
library(DEoptim)
library(openxlsx)
library(readxl)
library(xts)
library(package='hydromad')
setwd("C:/Users/BORAM/Desktop/R_example/optimization/samho") #Assign working directory
#getwd() #Check working directory
inp_file<-file.path("C:/Users/BORAM/Desktop/R_example/optimization/samho/samho2.inp") #Read initial input file

swmm_file<-run_swmm(inp_file)

obs<-read_xlsx("obs2.xlsx")

obs2<-xts(obs$V1,order.by=obs$...1)

#write.csv(as.data.frame(obs),"obs.csv")

inp<-read_inp(swmm_file$inp)

nse <- function(x){1-sum((x[,1]-x[,2])^2)/sum((x[,1]-mean(x[,1]))^2)}
obj_fun <- function(x, inp, obs2){
#  inp$subcatchments <- within(inp$subcatchments,{
#    Perc_Imperv<-x[1:107]
#    Width<-x[108:214]
#  })
  inp$subareas<-within(inp$subareas,{
    "N-Imperv"<-x[1]
    "N-Perv"<-x[2]
    "S-Imperv"<-x[3]
    "S-Perv"<-x[4]
    PctZero<-x[5]
  })
  inp$infiltration<-within(inp$infiltration,{
    MaxRate<-x[6]
    MinRate<-x[7]
    Decay<-x[8]
  })
  inp$conduits<-within(inp$conduits,{
    Roughness<-x[9]
  })
#  inp$infiltration<-within(inp$infiltration,{
#    DryTime<-x[224]
#  })
#  inp$subcatchments <- within(inp$subcatchments,{
#    Perc_Slope<-x[225]
#  }) 

    
  tmp_inp<-file.path("C:/Users/BORAM/Desktop/R_example/optimization/samho/inp/sh2.inp")
#  tmp_inp<-tempfile()
  write_inp(inp, tmp_inp)
  swmm_file<-suppressMessages(run_swmm(tmp_inp, stdout = NULL))
#  on.exit(file.remove(unlist(swmm_file)))
  sim<-read_out(swmm_file$out,
                iType = 2,
                object_name = "1036-7500",
                vIndex = 0)[["1036-7500"]]$flow_rate
  nse(merge(obs2,sim))*-1
}


set.seed(1234)

system.time({sce<-SCEoptim(
  obj_fun,
  par=c(#inp[["subcatchments"]][["Perc_Imperv"]], #Perc_Imperv
        #inp[["subcatchments"]][["Width"]], #Perc_Imperv
        0.03, #N-Imperv
        0.3, #N-Perv
        2.5, #S-Imperv
        5.0, #S-Perv
        10, #PctZero
        254, #Max_R
        25.4, #Min_R
        2, #Decay
        0.015),
  lower=c(#inp[["subcatchments"]][["Perc_Imperv"]]*0.5, #Perc_Imperv
          #inp[["subcatchments"]][["Width"]]*0.5, #Perc_Imperv
          0.011, #N-Imperv
          0.1, #N-Perv
          1.6, #S-Imperv
          3.8, #S-Perv
          10, #PctZero
          76, #Max_R
          2.5, #Min_R
          1, #Decay
          0.011 ),  #Manning
  upper=c(#inp[["subcatchments"]][["Perc_Imperv"]]*1.5,
          #inp[["subcatchments"]][["Width"]]*1.5,
          0.02, #N-Imperv
          0.35, #N-Perv
          3.8, #S-Imperv
          6.4, #S-Perv
          30, #PctZero
          254, #Max_R
          25.4, #Min_R
          4, #Decay
          0.02),  #Manning
#  lower = c(0.011,0.1,1.6,3.8,10,76,2.5,1,0.011),
#  upper = c(0.02,0.35,3.8,6.4,30,254,25.4,4,0.02),
  control = list(
    trace=1,
    ncomplex=20,
    maxit=3),
  inp=inp,
  obs=obs2
  )})

有什么帮助吗?

我使用了未来的包。但它不能正常工作。 所以,我想缩短这段代码的运行时间。

【问题讨论】:

    标签: r optimization gpu multicore


    【解决方案1】:

    能否提供一下这个过程的数据文件,比如inp文件,以便我们解决这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多