收集Python MPI中的结构列表?

发布于 2025-02-10 19:10:56 字数 1796 浏览 1 评论 0原文

我目前正在尝试传达struct对象的列表,但我遇到了一个错误:

    Traceback (most recent call last):
    File "Primary_secondary_co-sim.py", line 509, in <module>
    sm_clear_all(params, curr_pri_timestep, soln)
    File "/home/gridsan/jvineet9/vvc_sims/code/sm_clear_all.py", line 
    162, in sm_clear_all
    node_num = soln_node['node_num']
    TypeError: list indices must be integers or slices, not str
    Primary job  terminated normally, but 1 process returned
    a non-zero exit code. Per user-direction, the job has been 
    aborted.
    ----------------------------------------------------------------- 

    mpirun detected that one or more processes exited with non-zero 
    status, thus causing
    the job to be terminated. The first process to do so was:

    Process name: [[56840,1],1]
    Exit code:    1

这是我的代码:

from mpi4py import MPI
import os

data = [4,5]
num_SMOs = 5
solns_list = []

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

name = MPI.Get_processor_name()
pid = os.getpid()

idx = range(num_SMOs)
myidx = idx[rank:len(idx):size] 

for node in myidx:

    soln_node = structtype()

    # Add some data fields to struct
    soln_node.node_num = node
    soln_node.XX = data

    solns_list.append(soln_node)

# All nodes send their solns to rank 0

solns_nodes_all = comm.gather(solns_list, root=0)

if rank == 0:

    for soln_node in solns_nodes_all: 

        node_num = soln_node.node_num
        data = soln_node.XX

这是struct python对象的定义:

class structtype:
    def __init__(self):
        pass

我在solns_nodes_all中的soln_node处得到错误:节点。我希望Solns_nodes_all成为构造的结构列表,但是看起来它已成为MPI收集步骤之后的列表吗?

I am currently trying to communicate lists of struct objects but I run into an error:

    Traceback (most recent call last):
    File "Primary_secondary_co-sim.py", line 509, in <module>
    sm_clear_all(params, curr_pri_timestep, soln)
    File "/home/gridsan/jvineet9/vvc_sims/code/sm_clear_all.py", line 
    162, in sm_clear_all
    node_num = soln_node['node_num']
    TypeError: list indices must be integers or slices, not str
    Primary job  terminated normally, but 1 process returned
    a non-zero exit code. Per user-direction, the job has been 
    aborted.
    ----------------------------------------------------------------- 

    mpirun detected that one or more processes exited with non-zero 
    status, thus causing
    the job to be terminated. The first process to do so was:

    Process name: [[56840,1],1]
    Exit code:    1

Here is my code:

from mpi4py import MPI
import os

data = [4,5]
num_SMOs = 5
solns_list = []

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

name = MPI.Get_processor_name()
pid = os.getpid()

idx = range(num_SMOs)
myidx = idx[rank:len(idx):size] 

for node in myidx:

    soln_node = structtype()

    # Add some data fields to struct
    soln_node.node_num = node
    soln_node.XX = data

    solns_list.append(soln_node)

# All nodes send their solns to rank 0

solns_nodes_all = comm.gather(solns_list, root=0)

if rank == 0:

    for soln_node in solns_nodes_all: 

        node_num = soln_node.node_num
        data = soln_node.XX

And here's definition for the struct python object:

class structtype:
    def __init__(self):
        pass

I get the error at the line "for soln_node in solns_nodes_all:" when I loop through all the nodes. I expected solns_nodes_all to be a list of structs as constructed but instead it looks like it's become a list of lists after the MPI gather step?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余罪 2025-02-17 19:10:58

mpi4py s 收集操作从节点获取单个值,并将这些返回值放入列表中。您返回列表,因此将它们放入列表的列表中。如果您打算返回多个值,但希望平面列表迭代,则可以

import itertools

for soln_node in itertools.chain.from_iterable(solns_nodes_all):
    node_num = soln_node.node_num
    data = soln_node.XX

通过打印SOLNS_NODES_ALL的值来将其链接在一起:您可能已经找到了这一点:

[
  [<__main__.structtype object at 0x7ff148535c70>, <__main__.structtype object at 0x7ff148535700>],
  [<__main__.structtype object at 0x7ff148535ee0>, <__main__.structtype object at 0x7ff148535dc0>],
  [<__main__.structtype object at 0x7ff148535f40>]
]

的一般模式收集是这样的:

Node 1 returns A
Node 2 returns B
Node 3 returns C
Gathered into [A, B, C]

因此,如果您说a = [struct(struct(),struct()],...然后收集的内容为[[struct(struct(),struct ()],...

mpi4pys gather operation fetches individual values from nodes, and puts those return values into a list. You return lists, so they are put into a list of lists. If you intended to return multiple values but want a flat list to iterate over, you can chain your iterables together:

import itertools

for soln_node in itertools.chain.from_iterable(solns_nodes_all):
    node_num = soln_node.node_num
    data = soln_node.XX

You could have found this out by printing the value of solns_nodes_all:

[
  [<__main__.structtype object at 0x7ff148535c70>, <__main__.structtype object at 0x7ff148535700>],
  [<__main__.structtype object at 0x7ff148535ee0>, <__main__.structtype object at 0x7ff148535dc0>],
  [<__main__.structtype object at 0x7ff148535f40>]
]

The general pattern of gather goes like this:

Node 1 returns A
Node 2 returns B
Node 3 returns C
Gathered into [A, B, C]

So if you say A=[struct(), struct()], ... then what is gathered is [[struct(), struct()], ...]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文