【问题标题】:Matplotlib and mpi4py: Tclerror: no display name and no $DISPLAYMatplotlib 和 mpi4py:Tclerror:没有显示名称,也没有 $DISPLAY
【发布时间】:2020-05-12 17:01:27
【问题描述】:

根据 ngoldbaum 的建议,我在此处发布了以前 Python 脚本的可运行示例。最后,我想让 root 执行一个绘图(来自调用 matplotlib 的包 scitools),但它遇到错误并且不产生任何输出。

import matplotlib
matplotlib.use('Agg')
import sys
import numpy as np
import yt
from scitools.all import *
from scitools.std import *
from matplotlib import rc
from mpi4py import MPI

yt.enable_parallelism()


comm = MPI.COMM_WORLD
rank = comm.Get_rank()
nprocs = comm.Get_size()

print "rank, nprocs", rank, nprocs

# 
out_tmax = []
out_zmax = []
n_el = np.zeros(nprocs, dtype=int)

xmin = 0.
xmax = 2.2
deltax = (xmax - xmin)/nprocs

xmin_loc = xmin + rank*deltax
xmax_loc = xmin_loc + deltax

n_el[rank] = 10*(1 + rank)
if rank == 0:
    out_tmax = np.linspace(xmin_loc,xmax_loc,n_el[rank])
    out_zmax = sin(out_tmax)
    print "ok", out_tmax, out_zmax

# Plot
if yt.is_root():
#if rank == 0:
    rc('font',**{'family':'serif','serif':['Times']})
## for Palatino and other serif fonts use:
#    rc('font',**{'family':'serif','serif':['Palatino']})
    rc('text', usetex=True)
    rc('legend',fontsize=12)
    rc('axes',labelsize=14, titlesize=18)
    rc('xtick', labelsize=14)
    rc('ytick', labelsize=14)
    plot(out_tmax, out_zmax, 'b', fontname='Times new Roman', xlabel=(r'Time (Myr)'), ylabel=(r'\mathrm{z_{HS}}'), hardcopy='fig1.png')

看起来根进程没有继承开始时所做的“use: Agg”选择(我没有得到 plot() 中指定的任何 *png 输出。我使用 s 脚本将其提交给 PBS 批处理系统:

#!/bin/bash
## Name of the job
#PBS -N test_mpi4py_matlib

#PBS -l select=1:ncpus=4:mpiprocs=4:mem=24gb
#PBS -l place=scatter

#PBS -e test_gatherv_print.err
#PBS -o test_gatherv_print.out
#PBS -l walltime=00:20:00
....
mpirun python2.7 test_gatherv_print.py

此脚本的前两行应防止 plot(基于 matplotlib)查找 DISPLAY 环境变量。

此脚本失败并出现以下错误:

  File "test_gatherv_print.py", line 49, in <module>
    plot(out_tmax, out_zmax, 'b', fontname='Times new Roman', xlabel=(r'Time (Myr)'), ylabel=(r'\mathrm{z_{H
S}}'), hardcopy='fig1.png')
  File "/home/van/software/python/scitools/lib/python2.7/site-packages/scitools/easyviz/common.py", line 313
9, in plot
    self.setp(**kwargs)
  File "/home/van/software/python/scitools/lib/python2.7/site-packages/scitools/easyviz/common.py", line 194
1, in setp
    self.hardcopy(kwargs['hardcopy'])
  File "/home/van/software/python/scitools/lib/python2.7/site-packages/scitools/easyviz/matplotlib_.py", lin
e 1065, in hardcopy
    self._replot()
  File "/home/van/software/python/scitools/lib/python2.7/site-packages/scitools/easyviz/matplotlib_.py", lin
e 944, in _replot
    self._g.figure(self.getp('curfig'))
  File "/opt/Python-2.7.9/lib/python2.7/site-packages/matplotlib/pyplot.py", line 533, in figure
    **kwargs)
  File "/opt/Python-2.7.9/lib/python2.7/site-packages/matplotlib/backend_bases.py", line 161, in new_figure_
manager
    return cls.new_figure_manager_given_figure(num, fig)
  File "/opt/Python-2.7.9/lib/python2.7/site-packages/matplotlib/backends/_backend_tk.py", line 1046, in new
_figure_manager_given_figure
    window = Tk.Tk(className="matplotlib")
  File "/opt/Python-2.7.9/lib/python2.7/lib-tk/Tkinter.py", line 1810, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
P000 yt : [ERROR    ] 2020-05-13 16:28:32,964 TclError: no display name and no $DISPLAY environment variable
P000 yt : [ERROR    ] 2020-05-13 16:28:32,965 Error occured on rank 0.

虽然我在上面的 matplotlib.use() 中指定了“Agg”输出。

【问题讨论】:

  • 你能做一个最小的可运行示例来演示这个问题吗?
  • 我希望我明白你所说的“最小可运行示例”是什么意思...脚本基本上读取一系列 FLASH 输出,然后我调用 yt(一个 Python 包数据分析包)来共享工作在 Np 个进程中(使用 .piter() 方法并行化该段),输出数组被发送回根,根依次将它们收集到 2 个 np 数组(tmax 和 zmax)中,最后我发出 plot(tmax, zmax,. ..) 如上图所示。这是你要问的吗?我理解正确吗?
  • 我在上面编辑了我的问题以包含完整的脚本。
  • 这似乎是 matplotlib 和 mpi4py 之间的交互,但除了这些之外,您还导入了大量模块,尤其是 yt。您还使用了只有您拥有的数据。
  • 一个最小的例子需要在一台不属于你的机器上运行。它还应该尽可能少做无关的工作。

标签: python matplotlib yt


【解决方案1】:

感谢您的建议。我已经尝试了您的所有建议:1)在本地目录和.config下都配置了matplotlibrc(后端:Agg); 2) 使用 matplotlib 命令而不是 scitools 中的“绘图”。关于后者,它是由 P. Langtangen 开发的实用程序包;特别是我在这里使用了 easyviz 子包(参见:https://hplgit.github.io/scitools/doc/api/html/easyviz.html)。但是,即使我不使用 scitools.easyviz 而是直接使用 matplotlib,我也无法从 MPI 生成的根进程中生成 .png 输出。我在最后替换了以下几行:

...
    plt.plot(out_tmax, out_zmax)
    plt.savefig('fig1.png')

现在我收到以下错误:

P000 yt : [ERROR    ] 2020-05-14 10:32:14,200 OSError: [Errno 2] No such file or directory: 'latex'
P000 yt : [ERROR    ] 2020-05-14 10:32:14,200 Error occured on rank 0.

请注意,当我以交互方式运行脚本时,它会正确生成 fig1.png 输出。 如您所见,问题现在已经转移:root 不读取某些环境变量。请注意,我将作业提交给 PBS 批处理系统。最后一个脚本是这样的:

#!/bin/tcsh
## Name of the job
#PBS -N test_mpi4py_matlib

## Number of nodes (in this case I require 8 nodes with 1 CPU each)
## The total number of nodes passed to mpirun will be nodes*ppn 
##PBS -l nodes=5:ppn=16
##PBS -l nodes=2:ppn=8
#PBS -l select=1:ncpus=4:mpiprocs=4:mem=24gb
#PBS -l place=scatter
#PBS -V 
# inherits all environment variables, including DISPLAY

#! Name of output files for std output and error;
#! if non specified defaults are <job-name>.o<job number> and <job-name>.e<job-number>
#
#PBS -e test_gatherv_print.err
#PBS -o test_gatherv_print.out
#PBS -l walltime=00:20:00
##PBS -q high


#comandi
#printenv LD_LIBRARY_PATH
printenv DISPLAY

cd /home/van/projects/jet_ism/turbulence/python

date
#echo 'Hello world'
/opt/openmpi_icc/bin/mpirun python2.7 test_gatherv_print.py

这里我使用 tcsh,但在我使用 ksh 之前。

【讨论】:

  • 计算节点上可能没有安装 LaTeX。
猜你喜欢
  • 2019-10-09
  • 2020-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-02
  • 2015-05-08
  • 2018-09-03
  • 2019-11-03
相关资源
最近更新 更多