diff --git a/a b/a new file mode 100755 index 0000000000000000000000000000000000000000..3240b4b80ce5cb727ef1cd6d34c9bdbbaa8c7f64 Binary files /dev/null and b/a differ diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..aa967d0561cbd75ce32e612948d14b97910abb8e Binary files /dev/null and b/a.out differ diff --git a/dijkstra.c b/dijkstra.c deleted file mode 100644 index e5e12d466466df266490a0ce4258ac84d9ae485c..0000000000000000000000000000000000000000 --- a/dijkstra.c +++ /dev/null @@ -1,152 +0,0 @@ -#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(); -} diff --git a/mpi.c b/mpi.c index 861df396de8e4bb7483d12e10e0c3c19d61c6d0c..77d3ef7c818f5fd23143c726b0289e091a572fdb 100644 --- a/mpi.c +++ b/mpi.c @@ -4,10 +4,10 @@ #include <stdlib.h> #include <limits.h> -int N = 1000; +int N = 0; int getmin_index(long **graph, bool pickedVertices[N], int sourceVertex) { - int minDistance = CHAR_MAX; + int minDistance = INT_MAX; int min_index = -1; for (int j = 0; j < N; j++) { @@ -70,6 +70,7 @@ int main(int argc, char *argv[]) { { graph[i] = (long*) malloc(sizeof(long) * N); } + int numtasks, rank, dest, source, rc, count, tag=1; double start_time, end_time, total_time; @@ -77,10 +78,11 @@ int main(int argc, char *argv[]) { // 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; + graph[i][j] = rand(); } } + + // Assign with infinity for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (!(i == j || graph[i][j])){ @@ -96,6 +98,8 @@ int main(int argc, char *argv[]) { } } + // print(graph); + MPI_Status Stat; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD, &numtasks); @@ -109,36 +113,58 @@ int main(int argc, char *argv[]) { count = 0; if (!rank){ 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); printf("Received from process %d ", Stat.MPI_SOURCE); - for (int i = 0; i < N*jobs; ++i) - { - graph[Stat.MPI_SOURCE*jobs-jobs][i] = dataRecv[i]; + for (int i = 0; i < jobs; ++i) { + for (int j = 0; j < N; ++j) { + graph[Stat.MPI_SOURCE * jobs - jobs + i][j] = dataRecv[i * N + j]; + } } count++; } free(dataRecv); } else{ + long *dataSend = (long*) malloc(sizeof(long*) * N * jobs); + int count = 0; for (int i = rank*jobs-jobs; i < rank*jobs; ++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); } - 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); end_time = MPI_Wtime(); total_time = end_time - start_time; - MPI_Finalize(); + if (rank == 0) { 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) { free(graph[i]); } free(graph); + + MPI_Finalize(); + + return 0; } \ No newline at end of file diff --git a/output.txt b/output.txt new file mode 100644 index 0000000000000000000000000000000000000000..bfd51d0dc2224e4c4b2e0a7df8e2e13675afd6b4 --- /dev/null +++ b/output.txt @@ -0,0 +1,10 @@ +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 diff --git a/prog b/prog index bc7444f0404da1a31988e24d3001a7225c403486..1494f0a73d5c8f86f2e87a916f10a9177b1d4b6a 100755 Binary files a/prog and b/prog differ diff --git a/reset.c b/reset.c deleted file mode 100644 index 971b1368b0edeec8feaf7db0d37216308f4a7d82..0000000000000000000000000000000000000000 --- a/reset.c +++ /dev/null @@ -1,96 +0,0 @@ -#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 diff --git a/ser.c b/ser.c deleted file mode 100644 index 028dd9c05b2e3224793a62871fee52d2fc829283..0000000000000000000000000000000000000000 --- a/ser.c +++ /dev/null @@ -1,98 +0,0 @@ -#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 diff --git a/sr.c b/sr.c deleted file mode 100644 index d66d98a2f8ac2f3ef59d267146c7f6a81fc6f236..0000000000000000000000000000000000000000 --- a/sr.c +++ /dev/null @@ -1,64 +0,0 @@ - -// 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(); -} diff --git a/test/aingcupu/share b/test/aingcupu/share index 79186e81ea81362fdea1a5b5750c7733ca0c3580..b24918cf4b96b3f19a87941fbc07613c50848e4a 100644 --- a/test/aingcupu/share +++ b/test/aingcupu/share @@ -1,5 +1,5 @@ -scp asd 13517109@167.205.35.151:~ -scp asd 13517109@167.205.35.152:~ -scp asd 13517109@167.205.35.153:~ -scp asd 13517109@167.205.35.154:~ -scp asd 13517109@167.205.35.155:~ +scp asd 13517115@167.205.35.151:~ +scp asd 13517115@167.205.35.152:~ +scp asd 13517115@167.205.35.153:~ +scp asd 13517115@167.205.35.154:~ +scp asd 13517115@167.205.35.155:~ \ No newline at end of file