-1

I am interested in parallel programming. I wrote a serial radix sort algorithm. Now I want to convert it to a parallel algorithm. What methodology can I apply to it in order to convert it to parallel? When I tried to apply parfor instead of for, I got the error: "valid indices for 'C' are restricted in PARFOR loops." How can this be overcome?

Here is the code I wrote:

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
Schorsch
  • 7,761
  • 6
  • 39
  • 65
MUMBUÇOĞLU
  • 251
  • 1
  • 8
  • 24

1 Answers1

1

When using parfor you should read about the classification of variables. For a sliced variable C the iteration i may only access C(i) (simplified). In your case you have dependencies between the iterations of your loop, one reading the data from the previous. This makes a parfor impossible.

As far as I understood you code, it is not the right choice to parallelize it using the parallel computing toolbox. Assuming you get your variable indices fixed, you will probably end up with code beeing slower. This is typically when having simple computations, then the overhead of the parfor wastes more time than you can potentially save.

To improve the performance of your code, instead take a look at vectorization or suitable built-in functions.

Instead of

for i = 2:11
    X(i) = X(i) + X(i-1);
end

Use:

X=cumsum(X);
Community
  • 1
  • 1
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • thanks for your reply.Also cumsum function is great.You said that it is not the right choice to parallelize it using the parallel computing toolbox,What is your suggestion to parallelize it.Do I need to create the algorithm from scratch?.I think There must be a way to overcome by using any method. – MUMBUÇOĞLU Dec 14 '15 at 22:40
  • To get fast running code, you can either optimize your m code or chose to implement it in c and make it available via mex. – Daniel Dec 14 '15 at 23:38