我知道这个问题之前已经回答过好几次了,但我检查了所有以前的答案来纠正我的情况,但它没有帮助。
我需要做的是并行化我的循环,以便并行处理每个城市(或内部循环)。但是在使用 parfor 时出现错误“无法对 parfor 中的变量 A 进行分类”。二维矩阵的大小固定为 n X n。我不知道看到问题。请帮帮我...
我提供的 c 实现是使用 mpi.h 完成的。使用 mpicc。我需要实现的是应该有 n 个进程,每个进程负责找到从本地城市到所有其他城市的最短路径。
每个案例都不同。就我而言:
my_first_city=2;
my_last_city=n;
parpool(n-1);
parfor (int_city=2:n,n-1)
% broadcast all -- create threads to handle each city
for local_city=my_first_city:n
for city2=2:n
A(local_city,city2)=min(A(local_city,city2),A(local_city,int_city)+A(int_city,city2));
end
end
end
这是我计算最短路径的函数:
function [ A,init ] = floydWarshall(input_matrix, n )
%% Floyd_Warshall algorithm is an analytical algorithm for finding shortest paths in weighted graph ,
% for example an adjacency matrix or a map graph.
% Floyd_Warshall algorithm compares all possible paths through a graph between each pair of vertices,
% The complexity of this algorithm is O(n^3) where n is the number of vertices, or nodes.
%% Floyd_Warshall
% inputs :
% n = number of vertices to initialize an adjacency matrix.
% input_matrix = the input matrix of initial weights or path costs. a nXn matrix
% outputs:
% A = the matrix after floydWarshall algo is applied and the matrix contains the shortest
% paths from each node to each other node
% init = The original matrix with all the costs from each node to each other node.
if(nargin<2)
n=size(input_matrix);
elseif (nargin<1)
n=size(input_matrix);
A=magic(n);
end
for i=1:n % marking the border rows and columns with cities
A(i,1)=i-1;
A(1,i)=i-1;
end
for i=1:n % making sure that the distance from a city i to city i is 0
A(i,i)=0;
end
for i=2:n
for j=2:n
A(i,j)=input_matrix(i,j); % input matrix, values
end
end
init=A; % temp variable to store the old matrix
for int_city=2:n
for city1=2:n
for city2=2:n
A(city1,city2)=min(A(city1,city2),A(city1,int_city)+A(int_city,city2));
end % floyd-warshall
end
end