使用python并行化MatLab函数时,用射线进行酸洗酸洗时出错

问题描述

我想使用python和ray并行化MatLab函数

在此示例中:

  1. 我使用MathWorks将非常简单的matlab函数包装为Python应用程序(该函数只是返回输入整数的结果-参见下文)。这里的说明:https://uk.mathworks.com/help/compiler_sdk/gs/create-a-python-application-with-matlab-code.html

  2. 然后在python中调用应用程序(函数)(请参见下文)。使用ray包调用函数可以并行化调用

  3. 调用函数四次,使用一些整数:0、1、2和3作为输入。结果应为列表:[0、1、2、3]

  4. 调用失败,并显示错误:ray / cloudpickle / cloudpickle_fast.py,TypeError:'str'对象不可调用。无论matlab函数做什么以及我输入的任何(有效)matlab代码,该错误均保持不变。

  5. 相同的函数(test_function_for_python_2)在简单的for循环中效果很好,并返回[0,1,2,3],因此问题很可能不在将MatLab函数封装为python的MathWorks中。 / p>

总而言之,ray需要腌制matlab / python库(该函数),而腌制遇到了我无法解决的问题。

我们将不胜感激。

此示例中的操作系统是MacOSx Catalina,python版本是3.7.9,ray python模块版本是1.0.0。 mwpython版本也是3.7.9,并且必须使用mwpython(由MatLab SDK运行时安装)来运行python脚本,而不是python或python3。 MatLab和MatLab SDK运行时是R2020b版本。

Matlab函数

function result = test_function_for_python_2(an_integer_input)
    result = an_integer_input;
end

Python使上面的matlab函数并行化

import TestTwo # a class with a simple function returning the input integer
import matlab
import ray # for parallelisation

# Initialise  ===========
ray.shutdown() # shutdown any open cpu pool 
ray.init() # create a pool of parallel threads 
# initialise the matlab app containing the code of a simple test function (returns the same integer that is input)
my_test_function = TestTwo.initialize() 

print(type(my_test_function))
print(type(my_test_function.test_function_for_python_2))

@ray.remote
def function_two(an_integer):
    '''Here a single integer is input and returned. 
    The function responsible for it has been written in Matlab and wrapped as a python library in Mathworks.'''
    result = my_test_function.test_function_for_python_2(an_integer,1) # nargout=1)
    return result

# input to the function
some_integers = [0,1,2,3]

# run function and collect results with ray for parallelisation
ray_objects = [function_two.remote(an_integer=i) for i in some_integers] 
future_results = ray.get(ray_objects)

print('Results: {}'.format(future_results))

# Terminate ===== 
my_test_function.terminate() # close the matlab app containing the code for the test function
ray.shutdown() # shutdown any open cpu pool 

来自mwpython控制台的错误跟踪

% /Applications/MATLAB/MATLAB_Runtime/v99/bin/mwpython ~/Documents/TestTwo/for_redistribution_files_only/run_test_2_matlab_wrapped_in_python_parallel.py

2020-11-06 21:09:00,142 INFO services.py:1166 -- View the Ray dashboard at http://127.0.0.1:8265
/Applications/MATLAB/MATLAB_Runtime/v99/bin/maci64/mwpython3.7.app/Contents/MacOS/mwpython3.7: can't find '__main__' module in ''

<class 'matlab_pysdk.runtime.deployablepackage.DeployablePackage'>
<class 'matlab_pysdk.runtime.deployablefunc.DeployableFunc'>

Traceback (most recent call last):
  File "/Users/me/Documents/TestTwo/for_redistribution_files_only/run_test_2_matlab_wrapped_in_python_parallel.py",line 32,in <module>
    ray_objects = [function_two.remote(an_integer=i) for i in some_integers] 

  File "/Users/me/Documents/TestTwo/for_redistribution_files_only/run_test_2_matlab_wrapped_in_python_parallel.py",in <listcomp>
    ray_objects = [function_two.remote(an_integer=i) for i in some_integers] 

  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ray/remote_function.py",line 99,in _remote_proxy
    return self._remote(args=args,kwargs=kwargs)

  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ray/remote_function.py",line 207,in _remote
    self._pickled_function = pickle.dumps(self._function)

  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ray/cloudpickle/cloudpickle_fast.py",line 70,in dumps
    cp.dump(obj)

  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ray/cloudpickle/cloudpickle_fast.py",line 656,in dump
    return Pickler.dump(self,obj)

TypeError: 'str' object is not callable

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)