Using matplotlib on non-0 MPI rank causes QXcbConnection: Could not connect to display

2024/10/7 20:32:47

I have written a program that uses mpi4py to do some job (making an array) in the node of rank 0 in the following code. Then it makes another array in the node of rank 1. Then I plot both the arrays. The array in node 0 is broad casted to node 1. However the code gives a bizarre error. I used the following command:

mpiexec -n 2 -f mfile python mpi_test_4.py

The program goes as:

from mpi4py import MPI
import matplotlib.pyplot as plt
import numpy as npcomm = MPI.COMM_WORLD
rank = comm.rankx = np.linspace(-2*np.pi,2*np.pi,100)
if (rank == 0 ):y1 = np.sin(x)
else:y1 = None
y1 = comm.bcast(y1,root=0)
if ( rank == 1 ):y2 = np.cos(x)fig = plt.figure()ax = fig.gca()ax.plot(x,y1)  ax.plot(x,y2)plt.savefig('test.png')print(rank)

Error message is:

0
QXcbConnection: Could not connect to display ===================================================================================
=   BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
=   PID 6804 RUNNING AT 192.168.1.106
=   EXIT CODE: 134
=   CLEANING UP REMAINING PROCESSES
=   YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
===================================================================================
[proxy:0:0@alankar-Aspire-E5-571] HYD_pmcd_pmip_control_cmd_cb (/home/alankar/mpich3/mpich-3.2/src/pm/hydra/pm/pmiserv/pmip_cb.c:885): assert (!closed) failed
[proxy:0:0@alankar-Aspire-E5-571] HYDT_dmxu_poll_wait_for_event (/home/alankar/mpich3/mpich-3.2/src/pm/hydra/tools/demux/demux_poll.c:76): callback returned error status
[proxy:0:0@alankar-Aspire-E5-571] main (/home/alankar/mpich3/mpich-3.2/src/pm/hydra/pm/pmiserv/pmip.c:206): demux engine error waiting for event
[mpiexec@alankar-Aspire-E5-571] HYDT_bscu_wait_for_completion (/home/alankar/mpich3/mpich-3.2/src/pm/hydra/tools/bootstrap/utils/bscu_wait.c:76): one of the processes terminated badly; aborting
[mpiexec@alankar-Aspire-E5-571] HYDT_bsci_wait_for_completion (/home/alankar/mpich3/mpich-3.2/src/pm/hydra/tools/bootstrap/src/bsci_wait.c:23): launcher returned error waiting for completion
[mpiexec@alankar-Aspire-E5-571] HYD_pmci_wait_for_completion (/home/alankar/mpich3/mpich-3.2/src/pm/hydra/pm/pmiserv/pmiserv_pmci.c:218): launcher returned error waiting for completion
[mpiexec@alankar-Aspire-E5-571] main (/home/alankar/mpich3/mpich-3.2/src/pm/hydra/ui/mpich/mpiexec.c:344): process manager error waiting for completion

However when I plot on node 0 which is the node I am using to issue the command: mpiexec -n 2 -f mfile python mpi_test_4.py the code works. For example this:

from mpi4py import MPI
import matplotlib.pyplot as plt
import numpy as npcomm = MPI.COMM_WORLD
rank = comm.rankx = np.linspace(-2*np.pi,2*np.pi,100)
if (rank == 1 ):y1 = np.sin(x)
else:y1 = None
y1 = comm.bcast(y1,root=1)
if ( rank == 0 ):y2 = np.cos(x)fig = plt.figure()ax = fig.gca()ax.plot(x,y1)  ax.plot(x,y2)plt.savefig('test.png')print(rank)
Answer

This error is by no means bizarre. "Could not connect to display" means that something fails to do some graphics stuff. Given that MPI processes can run on separate compute nodes, you have no guarantee that any rank can do just that. According to this answer, you should be able to force matplotlib into using a different backend:

import matplotlib
matplotlib.use('Agg')
https://en.xdnf.cn/q/118777.html

Related Q&A

ioerror errno 13 permission denied: C:\\pagefile.sys

Below is my code, what I am trying to achieve is walking through the OS generating a MD5 hash of every file the code is functional, however, I receive the error in the title "ioerror errno 13 perm…

How can PyUSB be understood? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Resize image in python without using resize() - nearest neighbor

For an assignment I want to resize a .jpg image with a python code, but without using the pil.image.resize() function or another similar function. I want to write the code myself but I cant figure out …

Concatenate two dataframes based on no of rows

I have two dataframes:a b c d e f 2 4 6 6 7 1 4 7 9 9 5 87 9 65 8 2Now I want to create a new dataframe like this:a b c d e f 2 4 6 6 7 1 4 7 9 9 5 8 That is, I only want the rows of the …

Having Problems with AzureChatOpenAI()

people. Im trying to use the AzureChatOpenAI(), but even if I put the right parameters, it doesnt work. Here it is: from langchain_core.messages import HumanMessage from langchain_openai import AzureCh…

Finding cycles in a dictionary

I have a dictionary which has values as:m = {1: 2, 7: 3, 2: 1, 4: 4, 5: 3, 6: 9}The required output should be cyclic values like 1:2 -> 2:1 = cycle, which both are present in dictionary. 4:4 is also…

Create a dataframe from HTML table in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

Unable to load a Django model from a separate directory in a database script

I am having difficulty writing a python script that takes a directory of .txt files and loads them into my database that is utilized in a Django project. Based on requirements the python script needs …

Leetcode problem 14. Longest Common Prefix (Python)

I tried to solve the problem (you can read description here: https://leetcode.com/problems/longest-common-prefix/) And the following is code I came up with. It gives prefix value of the first string in…

Python BS: Fetching rows with and without color attribute

I have some html that looks like this (this represents rows of data in a table, i.e the data between tr and /tr is one row in a table)<tr bgcolor="#f4f4f4"> <td height="25"…