You need to enable JavaScript to run this app. R并行计算mclapply报核心用户代码错误,提示意外符号endtime但代码无对应内容
优惠活动
大模型
产品
解决方案
定价
更多

R并行计算mclapply报核心用户代码错误,提示意外符号endtime但代码无对应内容

编辑:问题已解决,最终确认报错原因与parallel包无关。

各位StackOverflow社区开发者:
我遇到一个棘手的问题:编写了一个可被并行调度到8个核心运行的带type参数的函数,传入type="points"时函数运行完全正常,输出符合预期;但传入type="lines"时,虽然代码逻辑和points分支高度相似,脚本会持续运行近2天后崩溃,仅输出如下报错:

Warning message: 
In mclapply(cluster_times, the_nt_function, all_trips, ellipses,  :
  all scheduled cores encountered errors in user code
Error: unexpected symbol in:
"
endtime"
Execution halted

我已经核查过the_nt_function的全部代码,确认几个核心特征:

  • type="points"分支的代码可稳定运行,输出结果符合预期
  • type="lines"分支的代码在近2天的运行过程中无异常表现,直到上述报错出现才终止
  • 报错提及的意外符号endtime完全不在我编写的代码范围内,暂未定位问题来源

the_nt_function的运行逻辑为:接收多组输入,针对times_df中的每一行,对dat数据集生成采样子集;当type="points"时,调用sf包的st_intersection计算dat中与ell对象存在地理交集的点数量;当type="lines"时逻辑基本一致,仅将统计对象从点替换为线,统计与ell对象存在交集的线数量。

我目前的运行环境是高性能计算集群,仅申请使用8个核心;在本地电脑上用7个核心运行times_df中7条测试数据的子集时,脚本可正常输出结果,无崩溃现象。
我判断报错并非来自the_nt_function本身,已经检索了公开渠道的同类问题参考方案,目前正抱着尝试的心态测试对应方案,想请教大家,该类报错通常指向哪些问题?

我使用mclapply并行调用该函数的代码如下,参数传递顺序完全符合要求:

system.time(
    out <- mclapply(cluster_times, the_nt_function, all_trips, ellipses, TRUE, opts$type, mc.cores = cpus)
  )

the_nt_function的核心代码框架如下:

the_nt_function <- function(times_df, dat, sf_object, type) {

  times_df$N_t = as.numeric(NA)
  
  # 此处为执行数据预处理、过滤生成data.table对象dat_filt_spc的相关代码
    

    # 遍历times_df的每一行,完成更多数据过滤操作,生成data.table对象dat_filt_time
      
      if(type == "points"){
      # 使用起点坐标将过滤后的数据转换为sf对象
        suppressWarnings(
          dat_start_sf <- st_as_sf(
            as.data.frame(dat_filt_time),
            coords = c("startloclon", "startloclat"), 
            crs = 4326
          )
        )
      } else if(type == "lines") {
        suppressWarnings(
          dat_start_sf <- st_as_sf(
            as.data.frame(dat_filt_time),
            crs = 4326
          )
        )
      }
      
      # 计算起点与椭圆对象的空间交集
      suppressMessages(
        start_intersect <- st_intersection(dat_start_sf, ell)$tripid
      )
      
      # 使用终点坐标将过滤后的数据转换为sf对象,过滤出未被交集匹配到的行程
      if(type == "points"){
        suppressWarnings(
          dat_end_sf <- st_as_sf(
            as.data.frame(dat_filt_time[!tripid %in% start_intersect]),
            coords = c("endloclon", "endloclat"), 
            crs = 4326
          )
        )
        # 计算终点与椭圆对象的空间交集
        suppressMessages(
          end_intersect <- st_intersection(dat_end_sf, ell)$tripid
        )
        # 合并起点、终点的交集行程ID,统计数量后写入times_df
        trips_intersect <- unique(c(start_intersect, end_intersect))
      } else if(type == "lines"){
        trips_intersect <- start_intersect
      }

      times_df[i, "N_t"] <- length(trips_intersect)
    }
    return(times_df)
  }
}

内容的提问来源于stack exchange,提问作者Ben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.14 01:15:08