diff --git a/src/mp.c b/src/mp.c index 8a642f2a756faaaa1783b40a5446db9a79c05b66..0623060624d278da6298c20dad7ba6dde82fb742 100644 --- a/src/mp.c +++ b/src/mp.c @@ -1,4 +1,3 @@ -#include "mpi.h" #include <stdio.h> #include <stdbool.h> #include <stdlib.h> @@ -9,30 +8,30 @@ 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 = INT_MAX; int min_index = -1; for (int j = 0; j < N; j++) { - if (!pickedVertices[j] && graph[sourceVertex][j] <= minDistance) { - minDistance = graph[sourceVertex][j]; + if (!pickedVertices[j] && graph[sourceVertex*N+j] <= minDistance) { + minDistance = graph[sourceVertex*N+j]; min_index = j; } } return min_index; } -void print(long **graph){ +void print(long *graph){ printf("Matrix: \n"); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { - printf("%ld ", graph[i][j]); + printf("%ld ", graph[i*N+j]); } printf("\n"); } } -void dijkstra(long** graph, int sourceVertex) { +void dijkstra(long* graph, int sourceVertex) { // Distance from single source to all of the nodes bool pickedVertices[N]; @@ -51,43 +50,40 @@ void dijkstra(long** graph, int sourceVertex) { // 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[min_index*N+vertex]) && + (graph[sourceVertex*N+min_index] != INT_MAX) && + (graph[sourceVertex*N+min_index] + graph[min_index*N+vertex] < graph[sourceVertex*N+vertex])) { - graph[sourceVertex][vertex] = graph[sourceVertex][min_index] + graph[min_index][vertex]; + graph[sourceVertex*N+vertex] = graph[sourceVertex*N+min_index] + graph[min_index*N+vertex]; } } } return; } + int main(int argc, char *argv[]) { - + // Get matrix size from argument vector in , convert to int int thread_count = strtol(argv[1], NULL, 10); - N = strtol(argv[2], NULL, 10) + N = strtol(argv[2], NULL, 10); - long** graph; - graph = (long**) malloc(sizeof(long*) * N); - for (int i = 0; i < N; ++i) - { - graph[i] = (long*) malloc(sizeof(long) * N); - } + long* graph; + graph = (long*) malloc(sizeof(long) * N*N); srand(13517115); - // Fill the matrix with rand() function + // Fill the matrix with rand() function for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { - graph[i][j] = rand(); + graph[i*N+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])){ - graph[i][j] = INT_MAX; + if (!(i == j || graph[i*N+j])){ + graph[i*N+j] = INT_MAX; } } } @@ -95,80 +91,27 @@ int main(int argc, char *argv[]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (i == j){ - graph[i][j] = 0; + graph[i*N+j] = 0; } } } - // Calculate start time - clock_t tStart = clock(); - - #pragma omp parallel num_threads (thread_count) { - - } + printf("start\n"); - int numtasks, rank, dest, source, rc, count, tag=1; - double start_time, end_time, total_time; - - - int jobs = N/(numtasks-1); - long* dataRecv; - int destinationRank = 0; - count = 0; - if (!rank){ - dataRecv = (long*) malloc(sizeof(long) * N*jobs); - 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 < 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(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; - - if (rank == 0) { - printf("%f µs\n", total_time*100000); - // 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) + // Calculate start time + double start = omp_get_wtime(); + int idx; + #pragma omp parallel for num_threads (thread_count) + for (idx = 0; idx < N; ++idx) { - free(graph[i]); + dijkstra(graph, idx); } - free(graph); - MPI_Finalize(); + double end = omp_get_wtime(); + double total = end - start; + printf("%f µs\n", total*100000); + printf("end\n"); + free(graph); return 0; -} \ No newline at end of file +} diff --git a/src/omp_hello b/src/omp_hello deleted file mode 100755 index 760831ee5f7157e95db478d660fb7269cf89ce15..0000000000000000000000000000000000000000 Binary files a/src/omp_hello and /dev/null differ diff --git a/src/omp_hello.c b/src/omp_hello.c deleted file mode 100644 index c2f945451ecc05ffb4e1ae119f696879cd68fec5..0000000000000000000000000000000000000000 --- a/src/omp_hello.c +++ /dev/null @@ -1,21 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <omp.h> - -void Hello(void); /* Thread function */ - -int main(int argc, char *argv[]) { - int thread_count = strtol(argv[1], NULL, 10); - printf("hi1\n"); - #pragma omp parallel num_threads(thread_count) - Hello(); - - return 0; -} - -void Hello(void) { - int my_rank = omp_get_thread_num(); - int thread_count = omp_get_num_threads(); - - printf("Hello from thread %d of %d\n", my_rank, thread_count); -} \ No newline at end of file diff --git a/src/omp_hello.dSYM/Contents/Info.plist b/src/omp_hello.dSYM/Contents/Info.plist deleted file mode 100644 index ae70f05edd8e18497b76be74ca58a609067be423..0000000000000000000000000000000000000000 --- a/src/omp_hello.dSYM/Contents/Info.plist +++ /dev/null @@ -1,20 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> -<plist version="1.0"> - <dict> - <key>CFBundleDevelopmentRegion</key> - <string>English</string> - <key>CFBundleIdentifier</key> - <string>com.apple.xcode.dsym.omp_hello</string> - <key>CFBundleInfoDictionaryVersion</key> - <string>6.0</string> - <key>CFBundlePackageType</key> - <string>dSYM</string> - <key>CFBundleSignature</key> - <string>????</string> - <key>CFBundleShortVersionString</key> - <string>1.0</string> - <key>CFBundleVersion</key> - <string>1</string> - </dict> -</plist> diff --git a/src/omp_hello.dSYM/Contents/Resources/DWARF/omp_hello b/src/omp_hello.dSYM/Contents/Resources/DWARF/omp_hello deleted file mode 100644 index 2610f74a9ebd17b990b29944e12ff394c91893a1..0000000000000000000000000000000000000000 Binary files a/src/omp_hello.dSYM/Contents/Resources/DWARF/omp_hello and /dev/null differ