我是python的新手,请原谅,如果我不完全以python的方式编写它的话 但是我想做一个慢速算法来并行计算,而python似乎有一个简单的解决方案。但是...我撞到了墙上。
我正在尝试对优化的修改后的ackermann函数进行线程计算。没有线程,它在一分钟内可能会产生1000个结果,并且我希望可以并行计算,因为我可以从任何输入开始。
这是我的代码示例(删除了决定从何处开始的大多数头)。我也将其硬编码为仅在单个线程上,因为当我遇到分段错误时,我希望看到它通过4-5结果标记。
resource.setrlimit(resource.RLIMIT_STACK, (resource.RLIM_INFINITY, resource.RLIM_INFINITY))
sys.setrecursionlimit(999999)
# max_threads = os.cpu_count()
max_threads = 1
print('Available CPU cores: ' + str(max_threads))
def calc(r0, r1, r7, memory):
key = str(r0) + '.' + str(r1)
if key in memory:
return memory[key]
if r0 == 0:
memory[key] = (r1 + 1) % 32768
return memory[key]
if r1 == 0:
memory[key] = calc(r0 - 1, r7, r7, memory)
return memory[key]
memory[key] = calc(r0 - 1, calc(r0, r1 - 1, r7, memory), r7, memory)
return memory[key]
def batch_calc():
start = 0
end = 1000
for reg in range(start, end):
print('Trying with register: ' + str(reg))
memory = dict()
result = calc(4, 1, reg, memory)
if result == 6:
print('Found it! Register: ' + str(reg))
break
threads = []
for i in range(max_threads):
thread = threading.Thread(target=batch_calc)
thread.start()
threads.append(thread)
我得到的结果是:
Available CPU cores: 1
Trying with register: 0
Trying with register: 1
Trying with register: 2
Trying with register: 3
Trying with register: 4
Segmentation fault (core dumped)
转储为:
systemd-coredump[5385]: Process 5382 (python) of user 1000 dumped core.
Stack trace of thread 5383:
#0 0x00007f93f9ce0e78 n/a (libpython3.7m.so.1.0)
#1 0x00007f93f9ce4f81 _PyArg_ParseTupleAndKeywords_SizeT (libpython3.7m.so.1.0)
#2 0x00007f93f9ce58a9 n/a (libpython3.7m.so.1.0)
#3 0x00007f93f9caeeb8 _PyObject_FastCallKeywords (libpython3.7m.so.1.0)
#4 0x00007f93f9cf7c5a _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#5 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#6 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#7 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#8 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#9 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#10 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#11 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#12 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#13 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#14 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#15 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#16 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#17 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#18 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#19 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#20 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#21 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#22 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#23 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#24 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#25 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#26 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#27 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#28 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#29 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#30 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#31 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#32 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#33 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#34 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#35 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#36 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#37 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#38 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#39 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#40 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#41 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#42 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#43 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#44 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#45 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#46 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#47 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#48 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#49 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#50 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#51 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#52 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#53 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#54 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#55 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#56 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#57 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#58 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#59 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#60 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#61 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#62 0x00007f93f9cf33cd _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#63 0x00007f93f9c817db _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
Stack trace of thread 5382:
#0 0x00007f93fa27a436 do_futex_wait.constprop.1 (libpthread.so.0)
#1 0x00007f93fa27a538 __new_sem_wait_slow.constprop.0 (libpthread.so.0)
#2 0x00007f93f9c2b529 PyThread_acquire_lock_timed (libpython3.7m.so.1.0)
#3 0x00007f93f9ce4922 n/a (libpython3.7m.so.1.0)
#4 0x00007f93f9c820a4 _PyMethodDef_RawFastCallKeywords (libpython3.7m.so.1.0)
#5 0x00007f93f9caec6f _PyMethodDescr_FastCallKeywords (libpython3.7m.so.1.0)
#6 0x00007f93f9cf7aee _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#7 0x00007f93f9c3a069 _PyEval_EvalCodeWithName (libpython3.7m.so.1.0)
#8 0x00007f93f9c81982 _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#9 0x00007f93f9cf3225 _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#10 0x00007f93f9c3a069 _PyEval_EvalCodeWithName (libpython3.7m.so.1.0)
#11 0x00007f93f9c81982 _PyFunction_FastCallKeywords (libpython3.7m.so.1.0)
#12 0x00007f93f9cf3225 _PyEval_EvalFrameDefault (libpython3.7m.so.1.0)
#13 0x00007f93f9c3b08b _PyFunction_FastCallDict (libpython3.7m.so.1.0)
#14 0x00007f93f9cbd1df _PyObject_CallMethodId (libpython3.7m.so.1.0)
#15 0x00007f93f9d4bd21 n/a (libpython3.7m.so.1.0)
#16 0x00007f93f9d6c32f Py_FinalizeEx (libpython3.7m.so.1.0)
#17 0x00007f93f9d6e6eb n/a (libpython3.7m.so.1.0)
#18 0x00007f93f9d6f420 _Py_UnixMain (libpython3.7m.so.1.0)
#19 0x00007f93fa0ca223 __libc_start_main (libc.so.6)
#20 0x000055f451c3877a _start (python3.7)
systemd[1]: Started Process Core Dump (PID 5384/UID 0).
kernel: python[5383]: segfault at 7f93f76c5ff8 ip 00007f93f9ce0e78 sp 00007f93f76c6000 error 6 in libpython3.7m.so.1.0[7f93f9b32000+2e4000]