Skip to content
Snippets Groups Projects
Commit 97ad24cb authored by Edward Alexander Jaya's avatar Edward Alexander Jaya
Browse files

Not tested parallel program, but at least compileable

parent 2f3770d0
No related merge requests found
...@@ -4,113 +4,151 @@ ...@@ -4,113 +4,151 @@
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
int NUMBER_OF_VERTICES = 0; int N = 0;
long getVertexWithMinDistance(long dist[], bool pickedVertices[]) { long getVertexWithMinDistance(long dist[], bool pickedVertices[]) {
long minDistance = LONG_MAX; long minDistance = LONG_MAX;
int vertexWithMinDistance = -1; int vertexWithMinDistance = -1;
for (int vertex = 0; vertex < NUMBER_OF_VERTICES; vertex++) { for (int vertex = 0; vertex < N; vertex++) {
if (!pickedVertices[vertex] && dist[vertex] <= minDistance) { if (!pickedVertices[vertex] && dist[vertex] <= minDistance) {
minDistance = dist[vertex]; minDistance = dist[vertex];
vertexWithMinDistance = vertex; vertexWithMinDistance = vertex;
}
} }
} return vertexWithMinDistance;
return vertexWithMinDistance;
} }
void dijkstra(int graph[NUMBER_OF_VERTICES][NUMBER_OF_VERTICES], int sourceVertex) { long* dijkstra(int graph[N][N], int sourceVertex) {
// Distance from single source to all of the nodes // Distance from single source to all of the nodes
long dist[NUMBER_OF_VERTICES]; long *dist = (long*) malloc(sizeof(long) * N);
bool pickedVertices[NUMBER_OF_VERTICES]; bool pickedVertices[N];
for (int vertex = 0; vertex < NUMBER_OF_VERTICES; vertex++) { for (int vertex = 0; vertex < N; vertex++) {
if (vertex == sourceVertex) { if (vertex == sourceVertex) {
dist[vertex] = 0; dist[vertex] = 0;
} else { } else {
// Initialize all distance to be infinity. // Initialize all distance to be infinity.
dist[vertex] = LONG_MAX; dist[vertex] = LONG_MAX;
}
pickedVertices[vertex] = false;
} }
pickedVertices[vertex] = false;
}
for (int iteration = 0; iteration < NUMBER_OF_VERTICES - 1; iteration++) { for (int iteration = 0; iteration < N - 1; iteration++) {
// Get minimum distance // Get minimum distance
int vertexWithMinDistance = getVertexWithMinDistance(dist, pickedVertices); int vertexWithMinDistance = getVertexWithMinDistance(dist, pickedVertices);
// Mark the vertice as picked
pickedVertices[vertexWithMinDistance] = true;
// Update distance value
for (int vertex = 0; vertex < N; vertex++) {
if ((!pickedVertices[vertex]) &&
(graph[vertexWithMinDistance][vertex]) &&
(dist[vertexWithMinDistance] != LONG_MAX) &&
(dist[vertexWithMinDistance] + graph[vertexWithMinDistance][vertex] < dist[vertex])) {
// Change dist[]
dist[vertex] = dist[vertexWithMinDistance] + graph[vertexWithMinDistance][vertex];
}
}
}
// Mark the vertice as picked return dist;
pickedVertices[vertexWithMinDistance] = true;
// Update distance value // // Print solution
for (int vertex = 0; vertex < NUMBER_OF_VERTICES; vertex++) { // for (int vertex = 0; vertex < N; vertex++) {
if (!pickedVertices[vertex] && graph[vertexWithMinDistance][vertex] && dist[vertexWithMinDistance] != LONG_MAX && dist[vertexWithMinDistance] + graph[vertexWithMinDistance][vertex] < dist[vertex]) { // // printf("%d to %ld\n", vertex, dist[vertex]);
dist[vertex] = dist[vertexWithMinDistance] + graph[vertexWithMinDistance][vertex]; // printf("%d to %d\n", vertex, graph[sourceVertex][vertex]);
} // }
}
}
// Edit the real graph
for (int vertex = 0; vertex < NUMBER_OF_VERTICES; vertex++) {
graph[sourceVertex][vertex] = dist[vertex];
}
// Print solution
for (int vertex = 0; vertex < NUMBER_OF_VERTICES; vertex++) {
// printf("%d to %ld\n", vertex, dist[vertex]);
printf("%d to %d\n", vertex, graph[sourceVertex][vertex]);
}
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// Get matrix size from argument vector in , convert to int // Test function
NUMBER_OF_VERTICES = strtol(argv[1], NULL, 10);
// for (int i = 0; i < N; i++) {
// Initialize matrix // for (int j = 0; j < N; j++) {
int graph[NUMBER_OF_VERTICES][NUMBER_OF_VERTICES]; // scanf("%d", &graph[i][j]);
// }
// Seed with NIM: Edward Alexander Jaya // }
srand(13517115);
// Fill the matrix with rand() function // graph = { 0, 4, 0, 0, 0, 0, 0, 8, 0,
for (int i = 0; i < NUMBER_OF_VERTICES; i++) { // 4, 0, 8, 0, 0, 0, 0, 11, 0,
for (int j = 0; j < NUMBER_OF_VERTICES; j++) { // 0, 8, 0, 7, 0, 4, 0, 0, 2,
// Mod by 100 so the result won't be too big. // 0, 0, 7, 0, 9, 14, 0, 0, 0,
graph[i][j] = rand() % 100; // 0, 0, 0, 9, 0, 10, 0, 0, 0,
// 0, 0, 4, 14, 10, 0, 2, 0, 0,
// 0, 0, 0, 0, 0, 2, 0, 1, 6,
// 8, 11, 0, 0, 0, 0, 1, 0, 7,
// 0, 0, 2, 0, 0, 0, 6, 7, 0 };
// dijkstra(graph, 1);
// // Print function
// printf("Matrix: \n");
// for (int i = 0; i < N; i++) {
// for (int j = 0; j < N; j++) {
// printf("%d ", graph[i][j]);
// }
// printf("\n");
// }
// Get matrix size from argument vector in , convert to int
N = strtol(argv[1], NULL, 10);
// Initialize matrix
int graph[N][N];
// Seed with NIM: Edward Alexander Jaya
srand(13517115);
// Fill the matrix with rand() function
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
// Mod by 100 so the result won't be too big.
graph[i][j] = rand() % 100;
}
} }
}
MPI_Status Stat;
// Test function MPI_Init(&argc, &argv);
// rank is the id of processes, numtasks is the number of processes
// for (int i = 0; i < NUMBER_OF_VERTICES; i++) { int rank, numtasks;
// for (int j = 0; j < NUMBER_OF_VERTICES; j++) { MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
// scanf("%d", &graph[i][j]); MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// }
// } double start_time, end_time, total_time = 0.0;
// graph = { 0, 4, 0, 0, 0, 0, 0, 8, 0, int numOfTaskPerProcess = N / (numtasks - 1);
// 4, 0, 8, 0, 0, 0, 0, 11, 0, int destinationRank = 0;
// 0, 8, 0, 7, 0, 4, 0, 0, 2, int tag = 1;
// 0, 0, 7, 0, 9, 14, 0, 0, 0, int vertex = 0;
// 0, 0, 0, 9, 0, 10, 0, 0, 0,
// 0, 0, 4, 14, 10, 0, 2, 0, 0, // for each thread, synchronize before start
// 0, 0, 0, 0, 0, 2, 0, 1, 6, MPI_Barrier(MPI_COMM_WORLD);
// 8, 11, 0, 0, 0, 0, 1, 0, 7,
// 0, 0, 2, 0, 0, 0, 6, 7, 0 }; // Do not count the initial synchronization time
start_time = MPI_Wtime();
dijkstra(graph, 1);
if (rank == 0) {
// Print function long* dataRecv;
printf("Matrix: \n"); // Receive
for (int i = 0; i < NUMBER_OF_VERTICES; i++) { MPI_Recv(&dataRecv, N, MPI_LONG, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &Stat);
for (int j = 0; j < NUMBER_OF_VERTICES; j++) { printf("Received from process %d ", MPI_ANY_SOURCE);
printf("%d ", graph[i][j]); // To do: store in the array
free(dataRecv);
} else {
for (int vertex = (rank - 1) * numOfTaskPerProcess; vertex < (rank - 1) * numOfTaskPerProcess + numOfTaskPerProcess; vertex++) {
long* dataSend = dijkstra(graph, vertex);
MPI_Send(dataSend, N, MPI_LONG, destinationRank, tag, MPI_COMM_WORLD);
// Possible bug
free(dataSend);
}
} }
printf("\n");
}
// Send argument vector // Synchronize again and count the time
MPI_Init(&argc, &argv); MPI_Barrier(MPI_COMM_WORLD);
// dijkstra(graph, 0); end_time = MPI_Wtime();
MPI_Finalize(); total_time = end_time - start_time;
MPI_Finalize();
} }
No preview for this file type
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment