我对并行编程很感兴趣。我写了一个串行基数排序算法。现在我想将其转换为并行算法。为了将其转换为并行,我可以应用什么方法?当我尝试应用parfor
而不是 时for
,出现错误:“'C' 的有效索引在 PARFOR 循环中受到限制。” 如何克服?
这是我写的代码:
function array = radixSort(array)
maxx = max(array);
base = 1;
while maxx/base > 0
array = counting_sort(array,base);
base = base * 10;
end
function W = counting_sort(array,base)
X = zeros(1,11);
W = zeros(1,numel(array));
for j = 1:numel(array)
X(rem(floor(array(j)/base),10)+1) = X(rem(floor(array(j)/base),10)+1) + 1;
end
for i = 2:11
X(i) = X(i) + X(i-1);
end
for j = numel(array):-1:1
W(X(rem(floor(array(j)/base),10)+1)) = array(j);
X(rem(floor(array(j)/base),10)+1) = X(rem(floor(array(j)/base),10)+1) - 1;
end
end
end