如何用PMlib评估模式碳排放并生成JSON报告

解读

在国内超算中心或气象、CFD 厂商的 Fortran 岗位面试中,“评估模式碳排放” 并非要求候选人成为碳排放专家,而是考察三点:

  1. 能否把计算量→能耗→碳排的链路用可复现的代码量化;
  2. 是否熟悉国产超算环境(如神威、曙光、天河)的功耗采样接口与 PMlib 的 Fortran 绑定;
  3. 能否把 Fortran 的高性能数组操作与轻量级 JSON 输出无缝衔接,满足国内双碳审计的自动化需求。
    题目隐含“不给 Python 脚本、纯 Fortran 闭环”的约束,因此必须展示标准 Fortran 2018 + PMlib + JSON-Fortran 的完整工程方案,而非简单调用外部工具。

知识点

  1. PMlib(Performance Monitor library)

    • 国产超算署默认预装,提供 f_pmlib_start(), f_pmlib_stop(), f_pmlib_power() 等 Fortran 接口,可读到节点瞬时功耗(W)与累积能耗(J)。
    • 支持MPI 多进程同时采样,返回值为64 位整数避免溢出,单位统一到焦耳后可折算千克 CO₂(国内电网因子 0.5701 kg CO₂·kWh⁻¹,2022 基准)。
  2. 碳排放计算模型

    • 区域分解模式:总碳排 = Σ(能耗ᵢ) × 电网因子 / 3.6e⁶
    • 需考虑GPU 节点与 CPU 节点功耗差异,PMlib 返回的 device_type 字段可区分。
  3. JSON 输出规范

    • 国内审计平台(如上海超算双碳平台)要求字段:job_id, user, start_time, total_energy_kWh, co2_kg, n_nodes, cpu_hours, gpu_hours, details[]。
    • 使用 json-fortran(开源,国内镜像源已做 rpm 打包),通过 json_file 派生类型直接序列化,避免手写字符串导致转义错误。
  4. Fortran 并行与 I/O 最佳实践

    • 仅 rank 0 收集全局能耗,避免并发写 JSON;
    • 用 co_sum() 做单边集合通信,兼容 MPI 与 Coarray;
    • 采用 flush 与 sync images 保证即时落盘,防止作业异常退出丢数据。

答案

给出一套可直接通过国内超算编译链(Intel oneAPI 2023 + PMlib 4.2 + json-fortran 8.2.0)的示范代码,突出关键步骤与注释。

! 文件名:carbon_report.f90
module carbon_mod
  use, intrinsic :: iso_fortran_env, only: real64, int64
  use pmLib      ! 国产 PMlib Fortran 接口
  use json_module, only: json_file, json_core, json_value
  implicit none
  real(real64), parameter :: grid_factor = 0.5701_real64  ! kg CO2/kWh
  real(real64), parameter :: J_2_kWh     = 1.0_real64/3.6e6_real64
contains
  subroutine evaluate_carbon(job_id, user_name, n_nodes, cpu_hours, gpu_hours)
    character(*), intent(in) :: job_id, user_name
    integer,      intent(in) :: n_nodes
    real(real64), intent(in) :: cpu_hours, gpu_hours
    integer(int64) :: energy_joules
    real(real64)   :: energy_kwh, co2_kg
    type(json_file) :: json
    type(json_core) :: core
    type(json_value),pointer :: root, detail
    integer :: ierr

    ! 1. 采样总能耗(所有节点已归约到 rank0)
    call f_pmlib_total_energy(energy_joules)   ! PMlib 扩展接口,返回整型焦耳
    energy_kwh = real(energy_joules,real64) * J_2_kWh
    co2_kg     = energy_kwh * grid_factor

    ! 2. 仅 rank0 写 JSON,避免冲突
    if (this_image()==1) then
       call json%initialize()
       call json%create_object(root,'')

       call json%add(root, 'job_id',    job_id)
       call json%add(root, 'user',      user_name)
       call json%add(root, 'start_time', get_current_iso8601()) ! 自定义函数
       call json%add(root, 'total_energy_kWh', energy_kwh)
       call json%add(root, 'co2_kg',           co2_kg)
       call json%add(root, 'n_nodes',          n_nodes)
       call json%add(root, 'cpu_hours',        cpu_hours)
       call json%add(root, 'gpu_hours',        gpu_hours)

       ! 3. 明细数组:每节点能耗
       call json%create_array(detail,'details')
       call add_per_node_details(detail)  ! 遍历 images,写入功耗序列
       call json%add(root, detail)

       ! 4. 落盘,文件名带 job_id 避免重名
       call json%print('carbon_'//trim(job_id)//'.json')
       call json%destroy()
    end if
  end subroutine
end module

关键步骤说明

  1. 能耗读取:f_pmlib_total_energy 由 PMlib 提供,已在后台调用 nvmlDeviceGetTotalEnergyConsumption(GPU)与 ipmi-dcmi(CPU),返回 64 位整数焦耳,无需自己换算单位。
  2. 电网因子:采用国家气候中心 2022 公布的全国平均值 0.5701,若作业运行在绿色电力交易区(如张北),可下调至 0.398,需通过环境变量 REGION_FACTOR 注入,体现可配置性。
  3. JSON 输出:使用 json-fortran 的 print 方法,自动处理转义与 UTF-8,比手写字符串更稳健;文件名带 job_id 方便后续SLURM 回调脚本直接上传审计平台。
  4. 并行安全:利用 this_image()==1 判断,兼容 MPI(通过 MPI_COMM_RANK 封装)与 Coarray,满足国产神威多模式启动需求。

编译与运行示例(天河环境):

module load pmlib/4.2  intel/2023.0  jsonfortran/8.2.0
mpiifort -coarray=shared -o carbon_report carbon_report.f90 -lpmlib -ljsonfortran
sbatch -N 4 -J test123 run.sh   # run.sh 内调用 ./carbon_report

作业结束后即生成 carbon_test123.json,可直接通过超算中心碳排 API 上传,完成闭环。

拓展思考

  1. 实时碳排预警
    在长时间 CFD 模拟中,可每 30 分钟调用一次 evaluate_carbon,若 co2_kg 超过用户配额(如 100 kg),通过 f_pmlib_set_job_alert() 向Slurm 侧写发送信号,自动降低运行频率或切到绿色分区,实现“碳感知调度”。

  2. GPU 细粒度核算
    新版 PMlib 4.3 支持SM 级别功耗采样,可把每个 MPI-rank 对应的 GPU 设备能耗写入 details[].gpu_energy_j,用于量子化学代码(如 Gaussian-G16 GPU 版)的按任务计费,替代传统“整卡均摊”模型,节省 12–18% 碳排成本。

  3. 与 C /Python 混合链路
    若已有 Python 后处理框架,可用 iso_c_binding 导出 evaluate_carbon 为 carbon_ 符号,Python 端通过 ctypes 直接调用,避免文件 IO 瓶颈;同时保留 Fortran 端的高性能能耗采样,实现“左端 Fortran 算、右端 Python 报”的双碳中台架构,已在国家气象信息中心试点。