Skip to content
Snippets Groups Projects
Commit 4363943c authored by Ft-N's avatar Ft-N
Browse files

(almost) done

parent 05b6c578
Branches
No related merge requests found
#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
}
File deleted
#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
<?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>
File deleted
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