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

Fix bug

parent f4338702
Branches
No related merge requests found
a 0 → 100755
File added
a.out 0 → 100755
File added
#include "mpi.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <limits.h>
int N = 10;
long getVertexWithMinDistance(long dist[], bool pickedVertices[]) {
long minDistance = LONG_MAX;
int vertexWithMinDistance = -1;
for (int vertex = 0; vertex < N; vertex++) {
if (!pickedVertices[vertex] && dist[vertex] <= minDistance) {
minDistance = dist[vertex];
vertexWithMinDistance = vertex;
}
}
return vertexWithMinDistance;
}
long* dijkstra(int graph[N][N], int sourceVertex) {
// Distance from single source to all of the nodes
long *dist = (long*) malloc(sizeof(long) * N);
bool pickedVertices[N];
for (int vertex = 0; vertex < N; vertex++) {
if (vertex == sourceVertex) {
dist[vertex] = 0;
} else {
// Initialize all distance to be infinity.
dist[vertex] = LONG_MAX;
}
pickedVertices[vertex] = false;
}
for (int iteration = 0; iteration < N - 1; iteration++) {
// Get minimum distance
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];
}
}
}
return dist;
// // Print solution
// for (int vertex = 0; vertex < N; vertex++) {
// // printf("%d to %ld\n", vertex, dist[vertex]);
// printf("%d to %d\n", vertex, graph[sourceVertex][vertex]);
// }
}
int main(int argc, char *argv[]) {
// Test function
// for (int i = 0; i < N; i++) {
// for (int j = 0; j < N; j++) {
// scanf("%d", &graph[i][j]);
// }
// }
// graph = { 0, 4, 0, 0, 0, 0, 0, 8, 0,
// 4, 0, 8, 0, 0, 0, 0, 11, 0,
// 0, 8, 0, 7, 0, 4, 0, 0, 2,
// 0, 0, 7, 0, 9, 14, 0, 0, 0,
// 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;
MPI_Init(&argc, &argv);
// rank is the id of processes, numtasks is the number of processes
int rank, numtasks;
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
double start_time, end_time, total_time = 0.0;
int numOfTaskPerProcess = N / (numtasks - 1);
int destinationRank = 0;
int tag = 1;
int vertex = 0;
// for each thread, synchronize before start
MPI_Barrier(MPI_COMM_WORLD);
// Do not count the initial synchronization time
start_time = MPI_Wtime();
if (rank == 0) {
long* dataRecv;
// Receive
MPI_Recv(&dataRecv, N, MPI_LONG, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &Stat);
printf("Received from process %d ", MPI_ANY_SOURCE);
// 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, 0, tag, MPI_COMM_WORLD);
}
}
// Synchronize again and count the time
MPI_Barrier(MPI_COMM_WORLD);
end_time = MPI_Wtime();
total_time = end_time - start_time;
MPI_Finalize();
}
...@@ -4,10 +4,10 @@ ...@@ -4,10 +4,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
int N = 1000; int N = 0;
int getmin_index(long **graph, bool pickedVertices[N], int sourceVertex) { int getmin_index(long **graph, bool pickedVertices[N], int sourceVertex) {
int minDistance = CHAR_MAX; int minDistance = INT_MAX;
int min_index = -1; int min_index = -1;
for (int j = 0; j < N; j++) { for (int j = 0; j < N; j++) {
...@@ -70,6 +70,7 @@ int main(int argc, char *argv[]) { ...@@ -70,6 +70,7 @@ int main(int argc, char *argv[]) {
{ {
graph[i] = (long*) malloc(sizeof(long) * N); graph[i] = (long*) malloc(sizeof(long) * N);
} }
int numtasks, rank, dest, source, rc, count, tag=1; int numtasks, rank, dest, source, rc, count, tag=1;
double start_time, end_time, total_time; double start_time, end_time, total_time;
...@@ -77,10 +78,11 @@ int main(int argc, char *argv[]) { ...@@ -77,10 +78,11 @@ int main(int argc, char *argv[]) {
// Fill the matrix with rand() function // Fill the matrix with rand() function
for (int i = 0; i < N; i++) { for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) { for (int j = 0; j < N; j++) {
// Mod by 100 so the result won't be too big. graph[i][j] = rand();
graph[i][j] = rand() % 100;
} }
} }
// Assign with infinity
for (int i = 0; i < N; i++) { for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) { for (int j = 0; j < N; j++) {
if (!(i == j || graph[i][j])){ if (!(i == j || graph[i][j])){
...@@ -96,6 +98,8 @@ int main(int argc, char *argv[]) { ...@@ -96,6 +98,8 @@ int main(int argc, char *argv[]) {
} }
} }
// print(graph);
MPI_Status Stat; MPI_Status Stat;
MPI_Init(&argc,&argv); MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks); MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
...@@ -109,36 +113,58 @@ int main(int argc, char *argv[]) { ...@@ -109,36 +113,58 @@ int main(int argc, char *argv[]) {
count = 0; count = 0;
if (!rank){ if (!rank){
dataRecv = (long*) malloc(sizeof(long) * N*jobs); dataRecv = (long*) malloc(sizeof(long) * N*jobs);
while(count<numtasks-1){ while ( count < numtasks-1 ){
MPI_Recv(dataRecv, N*jobs, MPI_LONG, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &Stat); MPI_Recv(dataRecv, N*jobs, MPI_LONG, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &Stat);
printf("Received from process %d ", Stat.MPI_SOURCE); printf("Received from process %d ", Stat.MPI_SOURCE);
for (int i = 0; i < N*jobs; ++i) for (int i = 0; i < jobs; ++i) {
{ for (int j = 0; j < N; ++j) {
graph[Stat.MPI_SOURCE*jobs-jobs][i] = dataRecv[i]; graph[Stat.MPI_SOURCE * jobs - jobs + i][j] = dataRecv[i * N + j];
}
} }
count++; count++;
} }
free(dataRecv); free(dataRecv);
} }
else{ else{
long *dataSend = (long*) malloc(sizeof(long*) * N * jobs);
int count = 0;
for (int i = rank*jobs-jobs; i < rank*jobs; ++i) for (int i = rank*jobs-jobs; i < rank*jobs; ++i)
{ {
dijkstra(graph, i); dijkstra(graph, i);
for (int j = 0; j < N; j++) {
dataSend[count * N + j] = graph[i][j];
}
count++;
// printf("Print job %d from rank %d\n", i, rank); // printf("Print job %d from rank %d\n", i, rank);
} }
MPI_Send(graph[rank*jobs-jobs], N*jobs, MPI_LONG, destinationRank, tag, MPI_COMM_WORLD); MPI_Send(dataSend, N*jobs, MPI_LONG, destinationRank, tag, MPI_COMM_WORLD);
free(dataSend);
} }
MPI_Barrier(MPI_COMM_WORLD); MPI_Barrier(MPI_COMM_WORLD);
end_time = MPI_Wtime(); end_time = MPI_Wtime();
total_time = end_time - start_time; total_time = end_time - start_time;
MPI_Finalize();
if (rank == 0) { if (rank == 0) {
printf("%f\n", total_time); printf("%f\n", total_time);
// Write to file
FILE *f = fopen("output.txt", "w");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
fprintf(f, "%ld ", graph[i][j]);
}
fprintf(f, "\n");
}
fclose(f);
} }
for (int i = 0; i < N; ++i) for (int i = 0; i < N; ++i)
{ {
free(graph[i]); free(graph[i]);
} }
free(graph); free(graph);
MPI_Finalize();
return 0;
} }
\ No newline at end of file
0 490424743 74565607 288096063 35073978 450365432 83869292 560553895 389130630 373297152
204263410 0 278829017 416166025 148143896 531037102 288132702 576185956 593394040 453968822
417319369 741907867 0 213530456 361199855 776491309 9303685 886679772 314565023 699423029
757254738 1092124427 638775151 0 792328716 1207620170 591784729 1129171046 664781583 1130551890
56119514 455350765 130685121 344215577 0 415291454 139988806 525479917 445250144 338223174
127671809 443930510 202237416 415767872 87521598 0 211541101 549356759 516802439 425744772
1403832888 1199569478 1478398495 1615735503 1347713374 1413543162 0 1642756475 772226634 1569358817
819484865 1269497844 894050472 971260270 854558843 1004784094 533832147 0 1208615495 927715814
631606254 427342844 706171861 843508869 575486740 958379946 715475546 870529841 0 881311666
204740089 520998790 279305696 492836152 164589878 77068280 288609381 187256743 593870719 0
No preview for this file type
#include "mpi.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <limits.h>
int N = 10;
int getVertexWithMinDistance(int dist[], bool pickedVertices[]) {
int minDistance = INT_MAX;
int vertexWithMinDistance = -1;
for (int vertex = 0; vertex < N; vertex++) {
if (!pickedVertices[vertex] && dist[vertex] <= minDistance) {
minDistance = dist[vertex];
vertexWithMinDistance = vertex;
}
}
return vertexWithMinDistance;
}
void dijkstra(int graph[N][N], int sourceVertex) {
// Distance from single source to all of the nodes
bool pickedVertices[N];
for (int vertex = 0; vertex < N; vertex++) {
pickedVertices[vertex] = false;
}
for (int i = 0; i < N - 1; i++) {
// Get minimum distance
int vertexWithMinDistance = getVertexWithMinDistance(graph[i], 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]) &&
(graph[i][vertexWithMinDistance] + graph[vertexWithMinDistance][vertex] < graph[i][vertex])) {
graph[i][vertex] = graph[i][vertexWithMinDistance] + graph[vertexWithMinDistance][vertex];
}
}
}
return;
}
int main(int argc, char *argv[]) {
int graph[N][N];
int rank, numtasks;
double start_time, end_time, total_time;
MPI_Status Stat;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
start_time = MPI_Wtime();
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;
}
}
// 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");
}
MPI_Barrier(MPI_COMM_WORLD);
end_time = MPI_Wtime();
total_time = end_time - start_time;
MPI_Finalize();
if (rank == 0) {
MPI_Recv(&dataRecv, N, MPI_INT, MPI_ANY_SOURCE, tag, MPI_COMM_WORLD, &Stat);
printf("%f\n", total_time);
}
// long *data = dijkstra(graph, 0);
// for (int i = 0; i < N; ++i)
// {
// printf("%ld\n", data[i]);
// }
// free(data);
}
\ No newline at end of file
#include "mpi.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <limits.h>
int N = 100;
int getmin_index(short graph[N][N], bool pickedVertices[N], int sourceVertex) {
int minDistance = CHAR_MAX;
int min_index = -1;
for (int j = 0; j < N; j++) {
if (!pickedVertices[j] && graph[sourceVertex][j] <= minDistance) {
minDistance = graph[sourceVertex][j];
min_index = j;
}
}
return min_index;
}
void print(short graph[N][N]){
printf("Matrix: \n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", graph[i][j]);
}
printf("\n");
}
}
void dijkstra(short graph[N][N], int sourceVertex) {
// Distance from single source to all of the nodes
bool pickedVertices[N];
for (int vertex = 0; vertex < N; vertex++) {
pickedVertices[vertex] = false;
}
for (int i = 0; i < N - 1; i++) {
// Get minimum distance
int min_index = getmin_index(graph, pickedVertices, sourceVertex);
// Mark the vertice as picked
pickedVertices[min_index] = true;
// Update distance value
for (int vertex = 0; vertex < N; vertex++) {
if ((!pickedVertices[vertex]) &&
(graph[min_index][vertex]) &&
(graph[sourceVertex][min_index] != INT_MAX) &&
(graph[sourceVertex][min_index] + graph[min_index][vertex] < graph[sourceVertex][vertex])) {
graph[sourceVertex][vertex] = graph[sourceVertex][min_index] + graph[min_index][vertex];
}
}
}
return;
}
int main(int argc, char *argv[]) {
// Get matrix size from argument vector in , convert to int
N = strtol(argv[1], NULL, 10);
short graph[N][N];
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;
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (!(i == j || graph[i][j])){
graph[i][j] = CHAR_MAX;
}
}
}
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j){
graph[i][j] = 0;
}
}
}
for (int i = 0; i < N; ++i)
{
dijkstra(graph, i);
printf("%d\n", i);
}
print(graph);
}
\ No newline at end of file
// Copyright www.computing.llnl.gov
#include "mpi.h"
#include <stdio.h>
void clean(int arr[10][10]){
for (int i = 0; i < 10; ++i)
{
for (int j = 0; j < 10; ++j)
{
arr[i][j] = 99;
}
}
}
int main(int argc, char *argv[]) {
int numtasks, rank, dest, source, rc, count, tag=1;
int inmsg[10][10], outmsg[10][10];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
outmsg[i][j] = 10*i+j;
}
}
MPI_Status Stat;
MPI_Init(&argc,&argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
if (rank == 0) {
dest = 1;
source = 1;
rc = MPI_Send(&outmsg[5], 50, MPI_INT, dest, tag, MPI_COMM_WORLD);
rc = MPI_Recv(&inmsg[5], 50, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat);
for (int i = 5; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
printf("%d\n", inmsg[i][j]);
}
}
}
else if (rank == 1) {
dest = 0;
source = 0;
rc = MPI_Recv(&inmsg[5], 50, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat);
printf("%s\n", "pass");
for (int i = 5; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
printf("%d\n", inmsg[i][j]);
}
}
clean(inmsg);
rc = MPI_Send(&inmsg[5], 50, MPI_INT, dest, tag, MPI_COMM_WORLD);
}
rc = MPI_Get_count(&Stat, MPI_INT, &count);
printf("Task %d: Received %d char(s) from task %d with tag %d \n",
rank, count, Stat.MPI_SOURCE, Stat.MPI_TAG);
MPI_Finalize();
}
scp asd 13517109@167.205.35.151:~ scp asd 13517115@167.205.35.151:~
scp asd 13517109@167.205.35.152:~ scp asd 13517115@167.205.35.152:~
scp asd 13517109@167.205.35.153:~ scp asd 13517115@167.205.35.153:~
scp asd 13517109@167.205.35.154:~ scp asd 13517115@167.205.35.154:~
scp asd 13517109@167.205.35.155:~ scp asd 13517115@167.205.35.155:~
\ No newline at end of file
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