有没有办法指定QuTiP的parallel_映射应该迭代哪个参数?

2024-09-26 18:05:57 发布

您现在位置:Python中文网/ 问答频道 /正文

QuTiP的函数^{}提供了并行计算给定函数的多个参数值的可能性。所有examples都显示了第一个位置参数变化的情况,如下所示:

def testFunc1(a, b):
    return a, b

from qutip import parallel_map
parallel_map(testFunc1, (1, 2, 3), task_args=(4,))

这将返回[(1, 4), (2, 4), (3, 4)]。现在我想知道是否也可以为a设置一个固定值,为b设置一个元组。根据documentationtask_args也可以是字典,所以我试了一下

parallel_map(testFunc1, (1, 2, 3), task_args={'a': 4})
parallel_map(testFunc1, (1, 2, 3), task_args={'a': 4, 'b': (1, 2, 3)})

但这会导致TypeError: can only concatenate tuple (not "dict") to tuple
当我尝试

parallel_map(testFunc1, b=(1, 2, 3), task_args={'a': 4})

我得到TypeError: parallel_map() missing 1 required positional argument: 'values'

有人知道如何将parallel_map用于第n个位置参数(而不为每个n编写函数包装器)吗


Tags: 函数maptask参数paralleldef情况args
2条回答

Q : "how to use parallel_map for the n-th positional argument (w/o writing a function wrapper for each n)?"

避免在不存在问题的地方产生问题,并将n-ed函数调用签名中第def-th个外部填充的iterable直接放入parallel_map()-expected(如文档所示)中,并与迭代处理兼容tuple

#          (           )              parallel_map() expected TUPLE
#your Fun( (  a    vv )         -)        your FED-IN ITERABLE
testFunc1( ( 'a', 'b1' ), 'of-no-interest' )  > (('a', 'b1'), 'of-no-interest')
testFunc1( ( 'a', 'b2' ), 'of-no-interest' )  > (('a', 'b2'), 'of-no-interest')
testFunc1( ( 'a', 'b3' ), 'of-no-interest' )  > (('a', 'b2'), 'of-no-interest')

"Do you mean something like parallel_map(testFunc1, [(4, 1), (4, 2), (4, 3)], task_args=('of-no-interest',))? Here b always has the value 'of-no-interest'. – A. P.2 hours ago"

不,
该示例是一条通向卸载a和任何及所有n-th用户端代码输入s)的清晰路径,如上所述

def testFun2( a, b ):
    return [ item for item in tuple( a ) ], b

指路,打电话给:

testFun2( ( 'a', 'b', "c", None, 42, -3.14159, "The-N-th-ITERABLE" ),
          'not-important-one-(of-no-interest-HERE-in-solving-N-th-iterable-for-parallel_star()-calls)...'
           )

交付>

(['a', 'b', 'c', None, 42, -3.14159, "The-N-th-ITERABLE"], 'not-important-one-(of-no-interest-HERE-in-solving-N-th-iterable-for-parallel_star()-calls)...')

完全满足a)您对任何第N个表的自由手的愿望,不仅是第一个位置表,还有b)这正是^{}的调用签名所期望的,并且有文件记录在案:

parallel_map( testFun2,                                        # TASK         Callable
              ( <_USER-SIDE_GENERATOR_FEED-IN_TUPLEsOfPARs_> ),# TASK VALUE(s)Array / List
              any_other_wished2have_call-signature_parameters, # TASK_ARGS    Dict
              ...,                                             # TASK_KWARGS  Dict
              ...                                              # call  KWARGS Dict
              )

查看source code of ^{}可以揭示为什么它只适用于函数的第一个参数:

async_res = [pool.apply_async(task, (value,) + task_args, task_kwargs, _update_progress_bar)
             for value in values]

在这一行中创建了并行进程。函数task获取一个表示所有位置参数的元组,该元组由values的1个元素和所有其他task_args元素创建。由于values的元素位于组合元组((value,) + task_args)的第一个位置,因此它始终是函数的第一个参数,在其并行实例之间变化。如果字典用于task_args,则TypeError: can only concatenate tuple (not "dict") to tuple的原因是+-运算符只重载了(tuple, tuple),而不是重载了(tuple, dict)

因此,最终无法构建包装器。每个特定的n都有一个,或者是一个通用的,比如:

def wrapper(varyingArgument, moreArgs):
    n = moreArgs[0]
    orderedArgs = moreArgs[1:n] + (varyingArgument,) + moreArgs[n:]
    return testFunc1(*orderedArgs)

打电话给

parallel_map(wrapper, (1,2,3), task_args=((2, 4),))

返回

[(4, 1), (4, 2), (4, 3)]

相关问题 更多 >

    热门问题