我在做一个连锁组词的题目时候尝试用并行计算代码加速,然后不知道为什么没有结果出来全是nothing,没有使用并行处理时候是能出来结果的。
题目是
连锁组词
在词典中查找连锁词。 两个单词交替取字母组成一个新词, 如shoe和cold交替取字母组成schooled, 称为连锁词(interlocked words)。
function indict(dict,word)
#二分法
a = firstindex(word)
b = lastindex(word)
while true
if dict[a] == word || dict[b] == word
return true
elseif (b-a)<= 1
return false
end
c = (a + b) ÷ 2
##println("n = ", n, " word = ", dict[a], ", ", dict[c], ", ", dict[b])
word <= dict[c] ? b = c : a = c
end
return false
end
# 做连接词
function makeinterlock(a,b)
n = length(a)
new = ""
if length(b) == n
#法1
for i in 1:n
new *= a[i]
new *= b[i]
end
end
new
end
# 并行处理
function findinterlockparal(dictsub, dictres)
@sync @distributed (vcat) for a in dictsub
for b in dictsub
newword = makeinterlock(a,b)
indict(dictres,newword) ? (a, b, newword) : []
end
end
end
# 按长度对词汇分类和拆分
function interlockparal(dict)
rank = map(length,dict)
maxlen = maximum(rank)
println("最大单词长度:", maxlen)
dicttable = map(i -> dict[rank .== i], 1:maxlen)
amountword = map(length, dicttable)
println("各长度单词个数:", amountword)
maylen = maxlen ÷ 2
# 只搜索2-5个字母组成的单词连锁
result = []
for wordlen in 2:5 # 2:maylen
println("==== 由 $(wordlen) 个字母组成的单词交织:")
dictsub = copy(dicttable[wordlen])
dictres = copy(dicttable[2*wordlen])
push!(result, findinterlockparal(dictsub, dictres))
end
return result
end
然后终端里运行这些代码跑的
mydict = readlines("words.txt")
using Distributed
addprocs(6)
@everywhere include("interlock.jl")
@time res = interlockparal(mydict)
words.txt来自于words数据