i am implementing Floyd–Warshall algorithm with c++ , c# and java. in each language i use sequential and parallel implementation after testing the result was :
(Elapsed time is only for main Function and Reading Files , Inti of Variable and ... are not measured.)
download sources here SourceCodes
c++
- IDE: Netbeans
- Compiler: MinGW-4.8.1
- sequential time : 9.333000
- Parallel time : 2.539000
- using
OpenMp
for Prallel
if
NumOfThreads
=1 then is Sequential else is Parallel
variables
#define n 1000 /* Then number of nodes */
double dist[n][n];
void floyd_warshall(int NumOfThreads) {
int i, j, k;
omp_set_num_threads(NumOfThreads);
for (k = 0; k < n; ++k)
#pragma omp parallel for private(i,j)
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
if ((dist[i][k] * dist[k][j] != 0) && (i != j))
if ((dist[i][k] + dist[k][j] < dist[i][j]) || (dist[i][j] == 0))
dist[i][j] = dist[i][k] + dist[k][j]; }
java
- IDE: Netbeans
- Compiler: Netbeans default
- sequential time : 11.632
- Parallel time : 3.089
- -Xms512m -Xmx1024m
- import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
java variables
public final int numNodes =1000;
public final double[][] costs= new double[numNodes][numNodes] ;
i did not put java code here because its working right ( i think )
c#
- IDE: Visual Studio 2012
- Compiler: Visual Studio 2012 default
- sequential time : 31.312
- Parallel time : 8.920
- using System.Threading.Tasks;
variable
const int n = 1000;
static double[,] dist = new double[n, n];
parallel code
static void floyd_warshall(ParallelOptions pOp)
{
int k;
for (k = 0; k < n; ++k)
Parallel.For<int>(0, n, pOp, () => 0, (i, loop, j) =>
{
for (j = 0; j < n; ++j)
if ((dist[i, k] * dist[k, j] != 0) && (i != j))
if ((dist[i, k] + dist[k, j] < dist[i, j]) || (dist[i, j] == 0))
dist[i, j] = dist[i, k] + dist[k, j];
return 0;
}, (j) => { });
single code
static void floyd_warshallSingle()
{
int i, j, k;
for (k = 0; k < n; ++k)
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
if ((dist[i,k] * dist[k,j] != 0) && (i != j))
if ((dist[i,k] + dist[k,j] < dist[i,j]) || (dist[i,j] == 0))
dist[i,j] = dist[i,k] + dist[k,j];
}
whats wrong with my c# implementation ?
all use the same file
now my question is why it takes more time to solve this algorithm with c# ?
java and c++ have nearly the same elapsed time but i think my implementation with c# is wrong because this difference between c# and c++ is strange !
please help me to improve my C# implementation or name some reasons Thank you !
edit 1
i changed my arrays to jagged array and the result changed to :
- sequential time : 19.22
- Parallel time : 4.903
still its a huge different between c# and c++ or java ! any idea why ?
new variables
const int n = 1000;
static double[][] dist = new double[n][];
new codes :
static void floyd_warshallSingle()
{
int i, j, k;
for (k = 0; k < n; ++k)
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
if ((dist[i][k] * dist[k][j] != 0) && (i != j))
if ((dist[i][k] + dist[k][j] < dist[i][j]) || (dist[i][j] == 0))
dist[i][j] = dist[i][k] + dist[k][j];
}
static void floyd_warshall(ParallelOptions pOp)
{
int k;
for (k = 0; k < n; ++k)
Parallel.For<int>(0, n, pOp, () => 0, (i, loop, j) =>
{
for (j = 0; j < n; ++j)
if ((dist[i][k] * dist[k][j] != 0) && (i != j))
if ((dist[i][ k] + dist[k][j] < dist[i][ j]) || (dist[i][j] == 0))
dist[i][ j] = dist[i][k] + dist[k][j];
return 0;
}, (j) => { });
}