【问题标题】:Is there a way to def function with mpi4py?有没有办法用 mpi4py 定义函数?
【发布时间】:2023-03-21 10:05:01
【问题描述】:

有一些模块需要在被调用时才被调用以执行某些过程。

喜欢:-

import os
from mpi4py import MPI
x= ["10","02","03,"04"]
def manta(int):
      os.systems("time manta ripper -l "+x[int]"")
def rip(int):
      os.systems("time rip -b -mb "+x[int]+"")
def save(int):
      os.systems("time merger -h -i "+x[int]" -l -s ")
if rank < 4:
   return(rank)

对于每个 def 函数,它必须使用从 mpi4py 获取的排名来运行。

【问题讨论】:

  • 请发布可运行代码。此外,使用 Python 内置类型作为变量名是一个非常糟糕的主意。我猜你想根据'rank'的值执行不同的功能。如果是这样,那么使用与相关函数相等的值键入排名的字典就是要走的路

标签: python mpi4py


【解决方案1】:

修复代码并重命名参数(避免使用内置 Python 关键字作为名称):

import os

x = ["10", "02", "03, "04"]

def manta(index):
      os.systems("time manta ripper -l " + x[index])


def rip(index):
      os.systems("time rip -b -mb " + x[index])


def save(index):
      os.systems("time merger -h -i " + x[index] + " -l -s ")

这是一种顺序运行这 3 个函数的方法:

# only run if rank can be used as valid index (inside array x)
if rank < 4:
  manta(rank)
  rip(rank)
  save(rank)
else:
  print('Rank outside of range [0..3], given rank was: ' + str(rank))

不确定return 的用途:

if rank < 4:
   return(rank)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    相关资源
    最近更新 更多