Skip to content

Commit 8d7ea96

Browse files
authored
Merge pull request #6 from SidoShiro/graphe
Graphe into Dev
2 parents 6b3d40d + 8add417 commit 8d7ea96

6 files changed

Lines changed: 143 additions & 10 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ project(DistributedMalloc)
88
find_package(MPI REQUIRED)
99

1010
# Vars with files for compilation
11-
set(SRCS src/main/main.c src/utils/utils.c src/cli/cli.c src/network/message.c src/network/block.c src/network/node.c src/utils/command_queue.c)
11+
set(SRCS src/main/main.c src/utils/utils.c src/cli/cli.c src/network/message.c src/network/block.c src/network/node.c src/utils/command_queue.c src/graph/graph.c)
1212

1313
include_directories("include/")
1414
include_directories(SYSTEM ${MPI_INCLUDE_PATH})

include/graph.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef DISTRIBUTEDMALLOC_GRAPH_H
2+
#define DISTRIBUTEDMALLOC_GRAPH_H
3+
4+
#include <stdlib.h>
5+
6+
7+
struct adjacency_matrix {
8+
unsigned **matrix;
9+
unsigned dimension;
10+
};
11+
12+
struct adjacency_matrix* dot_reader(char* path);
13+
14+
void free_adjacency_matrix(struct adjacency_matrix* a);
15+
16+
#endif //DISTRIBUTEDMALLOC_GRAPH_H

include/utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define DISTRIBUTEDMALLOC_UTILS_H
33

44
#include <stdlib.h>
5-
5+
#include <stdio.h>
66
struct a {
77
int x;
88
int y;

simplegraph.dot

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
graph myGraph{
2-
a -- b;
3-
b -- c;
4-
c -- d;
5-
d -- e;
6-
e -- f;
7-
a -- d;
8-
b -- d;
9-
c -- f;
2+
1 -- 2;
3+
2 -- 3;
4+
3 -- 4;
5+
4 -- 5;
6+
5 -- 6;
7+
1 -- 4;
8+
2 -- 4;
9+
3 -- 6;
1010
}

src/graph/graph.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include "utils.h"
2+
#include "graph.h"
3+
4+
void get_values(char str[128], unsigned values[2]);
5+
struct adjacency_matrix* generate_adjacency_matrix(unsigned* links, unsigned max, unsigned nb_links);
6+
void print_adjacency_matrix(struct adjacency_matrix* a);
7+
8+
9+
struct adjacency_matrix* dot_reader(char* path) {
10+
11+
FILE *file = fopen(path, "r" );
12+
size_t nb_links_possible = 300;
13+
unsigned* links = calloc(nb_links_possible, sizeof(unsigned[2]));
14+
unsigned max = 0;
15+
unsigned nb_links = 0;
16+
17+
if (file != NULL) {
18+
19+
char line [ 128 ];
20+
unsigned i = 0;
21+
22+
void *trash = fgets(line, sizeof line, file);
23+
trash = trash; // boring flags
24+
25+
while (fgets(line, sizeof line, file) != NULL && line[0] != '}') {
26+
unsigned val[2] = {0, 0};
27+
get_values(line, val);
28+
29+
unsigned tmp_max = val[0] > val[1] ? val[0] : val[1];
30+
max = max > tmp_max ? max : tmp_max; // The idea is to capture the largest id, for adjacency dimensions
31+
32+
if (nb_links == nb_links_possible) {
33+
nb_links_possible *= 2;
34+
links = realloc(links, nb_links_possible * sizeof(unsigned[2]));
35+
}
36+
37+
links[i] = val[0];
38+
links[i + 1] = val[1];
39+
i += 2;
40+
nb_links += 1;
41+
}
42+
fclose ( file );
43+
}
44+
45+
struct adjacency_matrix *a = generate_adjacency_matrix(links, max + 1, nb_links);
46+
free(links);
47+
48+
print_adjacency_matrix(a);
49+
50+
return a;
51+
}
52+
53+
54+
void get_values(char str[128], unsigned values[2]) {
55+
unsigned i = 0;
56+
57+
// spaces
58+
while(str[i] == ' ')
59+
++i;
60+
// first val
61+
while(str[i] != ' ') {
62+
values[0] = values[0] * 10 + str[i] - 48;
63+
++i;
64+
}
65+
// sep
66+
i += 4;
67+
// second val
68+
69+
while(str[i] != ';') {
70+
values[1] = values[1] * 10 + (unsigned) str[i] - 48;
71+
++i;
72+
}
73+
}
74+
75+
struct adjacency_matrix* generate_adjacency_matrix(unsigned* links, unsigned dim, unsigned nb_links) { // unused nb_links
76+
77+
struct adjacency_matrix* a = malloc(sizeof(struct adjacency_matrix));
78+
a->matrix = calloc(sizeof(unsigned*), dim);
79+
a->dimension = dim;
80+
// +1 cause id0 is for user ?
81+
for (unsigned i = 0; i < dim; ++i) {
82+
a->matrix[i] = calloc(sizeof(unsigned*), dim);
83+
}
84+
85+
for (unsigned i = 0; i < nb_links; ++i) {
86+
a->matrix[links[2 * i]][links[2 * i + 1]] = 1;
87+
a->matrix[links[2 * i + 1]][links[2 * i]] = 1;
88+
89+
i += 1;
90+
}
91+
92+
93+
94+
return a;
95+
}
96+
97+
void free_adjacency_matrix(struct adjacency_matrix* a) {
98+
for (unsigned i = 0; i < a->dimension; ++i) {
99+
free(a->matrix[i]);
100+
}
101+
free(a->matrix);
102+
free(a);
103+
}
104+
105+
void print_adjacency_matrix(struct adjacency_matrix* a) {
106+
for (unsigned i = 0; i < a->dimension; ++i) {
107+
unsigned j = 0;
108+
for (; j < a->dimension - 1; ++j) {
109+
printf("%u ", a->matrix[i][j]);
110+
}
111+
printf("%u\n", a->matrix[i][j]);
112+
}
113+
}
114+

src/main/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "cli.h"
2+
#include "graph.h"
23
#include "utils.h"
34

45
#include <mpi.h>
@@ -10,6 +11,8 @@ int main(int argc, char **argv) {
1011
char version[MPI_MAX_LIBRARY_VERSION_STRING];
1112

1213
// Load Graph Here
14+
struct adjacency_matrix *a = dot_reader("../simplegraph.dot");
15+
free_adjacency_matrix(a);
1316

1417
// Generate Adjacency Matrix Here
1518
void *adj_mat_network = NULL;

0 commit comments

Comments
 (0)