Pergunta

I am trying to compute the pairwise distances between two struct objects. This distance is symmetric. I have about N = 8000, such objects in an array.

So I need to compute N * (N+1)/2 distances only. How can I parallelize this computation, since each computation is independent ?

Here my objects are stored in Array X and I want to store the distances in Array A which is of size N*(N+1)/2. BDHMM() is a function which returns the distance between two objects.

I have tried the following Matlab Code.

N = 8000;
load inithmm.mat
size = N*(N+1)/2;
A = zeros(size,1);

matlabpool open local 4
  parfor i = 1:N-1
    i
    T = [];
    for j = i:N
      if(j == i)
          temp = 0;
      else
          temp = BDHMM(X(i),X(j));
      end
      T = [T; temp];
    end

    beg = size - (N + 1 - i)*(N + 2 - i)/2 + 1;
    l = length(T);
    A(beg:beg+l-1, 1) = T;
  end
matlabpool close

I am getting the following error:

Error: The variable A in a parfor cannot be classified.

Please help.

Foi útil?

Solução

You cannot assassin to indexes you calculate withing the parfor, Matlab needs to know in advance what sections of the matrix will be assassin by witch iteration. this makes sense if you think about it.

this should solve it:

N = 800;
size = N*(N+1)/2;
A = cell(N,1);
matlabpool open local 4
  parfor i = 1:N-1
    i
    T = zeros(N-i+1,1);
    for j = i:N
      if(j == i)
          T(j-i+1) = 0;
      else
          T(j-i+1) = BDHMM(X(i),X(j));
      end
    end

    A{i, 1} = T;
  end

matlabpool close
B=vertcat(A{:})
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top