-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSALG.c
More file actions
153 lines (128 loc) · 3.22 KB
/
SALG.c
File metadata and controls
153 lines (128 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
============================================================================
Name : SALG.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello MPI World in C
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "mpi.h"
typedef enum { false, true } bool;
int main(int argc, char* argv[]){
int my_rank; /* rank of process */
int p; /* number of processes */
// int source; /* rank of sender */
// int dest; /* rank of receiver */
// int tag=0; /* tag for messages */
// char message[100]; /* storage for message */
// MPI_Status status ; /* return status for receive */
/* start up MPI */
MPI_Init(&argc, &argv);
/* find out process rank */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* find out number of processes */
MPI_Comm_size(MPI_COMM_WORLD, &p);
// BROJ CVOROVA GRAFA
int n = atoi(argv[1]);
// GRANE GRAFA(VEZA IZMEDJU SVAKA SVA CVORA)
// 0 - VEZA SA SOBOM, SHRT_MAX - nepostojanje veze izmedju dva cvora
short* matricaGrafa = (short*) calloc(n*n,sizeof(short));;
if (my_rank == 0){
// UCITAVANJE MATRICE KOJA OPISUJE ULAZNI POVEZANI NEUSMEREN GRAF
FILE* file;
char* filename = argv[3];
file = fopen(filename,"rb");
fread(matricaGrafa,n*n*sizeof(short),1,file);
/*
for(int i=0; i<n; i++){
printf("\n");
for(int j=0; j<n; j++){
printf("%d \t", matricaGrafa[i*n+j]);
}
}
printf("\n"); fflush(stdout);
printf("\nUZORAK (6x6 + KRAJEVI): \n");
for(int j=0; j<6; j++){
for(int i=0; i<6; i++){
printf("%d \t", matricaGrafa[j*n+i]);
}
printf("| %d \t", matricaGrafa[j*n+(n-1)]);
printf("\n");
}
printf("--------------------------------------------------------\n");
for(int i=0; i<6; i++){
printf("%d \t", matricaGrafa[(n-1)*n+i]);
}
printf("| %d \t", matricaGrafa[(n-1)*n+(n-1)]);
printf("\n\n");
*/
fclose(file);
// PRONALAZENJE MIN STABLA
short d[n];
bool Vt[n];
int root = atoi(argv[2]);
/* SEKVENCIJALAN ALGORITAM ZA 1 PROCES */
// RAD SA VREDNOSTIMA GRANA
for(int v=0; v<n; v++){
Vt[v] = false;
d[v] = matricaGrafa[root*n+v];
}
Vt[root] = true;
d[root] = 0;
double start;
double finish;
if(my_rank == 0){
start = MPI_Wtime();
}
for(int i=1; i<n; i++){
int minU = 0;
short minUD = SHRT_MAX;
for(int j=0; j<n; j++){
if(Vt[j] != true && d[j] < minUD){
minU = j;
minUD = d[j];
}
}
minUD = SHRT_MAX;
Vt[minU] = true;
for(int v=0; v<n; v++){
if(Vt[v] != true && d[v] > matricaGrafa[minU*n+v]){
d[v] = matricaGrafa[minU*n+v];
}
}
}
finish = MPI_Wtime();
double elapsed = finish - start;
printf("RANK %d | %f sec\n",my_rank,elapsed);
printf("\n"); fflush(stdout);
/*
char c; int i = 0;
printf("\n");
for (c = 'A'; c <= 'Z'; ++c){
if(i == n)
break;
printf("%c\t", c); i++;
}
printf("\n");
int sum = 0;
for(int y=0; y<n; y++){
sum += d[y];
printf("%d\t", d[y]);
}
printf("= %d\n",sum);
printf("\n");
int sum = 0;
for(int y=0; y<n; y++){
sum += d[y];
} */
}
free(matricaGrafa);
/* shut down MPI */
MPI_Finalize();
return 0;
}