33#include "globals.h"
44#include "message.h"
55#include "debug.h"
6+ #include "utils.h"
67
78#include <mpi.h>
89
9- struct leader_resources * generate_leader_resources (size_t nb_nodes /*, struct node *n*/ ) {
10- struct leader_resources * l_r = malloc (sizeof (struct leader_resources ));
10+ size_t get_message_size () {
11+ return (sizeof (unsigned short ) * 3 + 2 * sizeof (size_t ) + sizeof (enum operation ));
12+ }
13+
14+ struct leader_resources * generate_leader_resources (size_t nb_nodes , size_t id ) {
15+ struct leader_resources * l_r = malloc (128 );
1116 l_r -> leader_blks = init_nodes_same_size (nb_nodes , 8 );
1217 l_r -> leader_command_queue = NULL ;
1318 l_r -> leader_reg = generate_allocs (4 );
14- // l_r->id = n-> id;
19+ l_r -> id = id ;
1520 return l_r ;
1621}
1722
18- int check_block_for_alloc (size_t * res_addr , struct block * b , size_t size , struct leader_resources * l_r ) {
23+ int check_block_for_alloc (size_t * res_addr , struct block * b , size_t size , struct leader_resources * l_r ) {
1924 if (0 == b -> free ) {
2025 if (size == b -> size ) {
2126 b -> free = 1 ;
2227 struct allocation * a = malloc (sizeof (struct allocation ));
2328 a -> number_parts = 1 ;
2429 a -> v_address_start = b -> virtual_address ;
25- a -> parts = malloc (a -> number_parts * sizeof (struct allocation * ));
30+ a -> parts = malloc (a -> number_parts * sizeof (struct allocation * ));
2631 a -> parts [0 ] = b ;
2732 add_allocation (l_r -> leader_reg , a );
2833 * res_addr = b -> virtual_address ;
2934 return 1 ;
30- }
31- else if (size < b -> size ) {
35+ } else if (size < b -> size ) {
3236 b -> free = 1 ;
3337 b = split_block_u (b , size );
3438 if (b ) {
35- struct allocation * a = malloc (sizeof (struct allocation ));
39+ struct allocation * a = malloc (32 + sizeof (struct allocation ));
3640 a -> number_parts = 1 ;
3741 a -> v_address_start = b -> virtual_address ;
38- a -> parts = malloc (a -> number_parts * sizeof (struct allocation * ));
42+ a -> parts = malloc (34 + ( a -> number_parts * sizeof (struct allocation * ) ));
3943 a -> parts [0 ] = b ;
4044 add_allocation (l_r -> leader_reg , a );
4145 * res_addr = b -> virtual_address ;
4246 return 1 ;
47+ } else {
48+ debug ("Fatal Error, split invalid or memory error" , 1 );
4349 }
44- }
45- else {
50+ } else {
4651
4752 }
4853
@@ -63,15 +68,42 @@ size_t alloc_memory(size_t size, struct leader_resources *l_r) {
6368 debug ("ERROR not malloc blockS !!!" , 0 ); //l_r->id);
6469 return 999 ;
6570 }
66- size_t address = 0 ;
71+ // Single Part
6772 for (size_t i = 0 ; i < blks -> nb_blocks ; i ++ ) {
6873 struct block * b = blks -> blks [i ];
69- if (!b ) {
70- debug ("ERROR not malloc block !!!" , 0 ); // l_r->id);
71- return 1999 ;
74+ while (b != NULL ) {
75+ if (0 == b -> free ) {
76+ if (size == b -> size ) {
77+ b -> free = 1 ;
78+ struct allocation * a = malloc (sizeof (struct allocation ));
79+ a -> number_parts = 1 ;
80+ a -> v_address_start = b -> virtual_address ;
81+ a -> parts = malloc (a -> number_parts * sizeof (struct allocation * ));
82+ a -> parts [0 ] = b ;
83+ add_allocation (l_r -> leader_reg , a );
84+ return b -> virtual_address ;
85+ } else if (size < b -> size ) {
86+ b -> free = 1 ;
87+ b = split_block_u (b , size );
88+ if (b ) {
89+ struct allocation * a = malloc (32 + sizeof (struct allocation ));
90+ a -> number_parts = 1 ;
91+ a -> v_address_start = b -> virtual_address ;
92+ a -> parts = malloc (34 + (a -> number_parts * sizeof (struct allocation * )));
93+ a -> parts [0 ] = b ;
94+ add_allocation (l_r -> leader_reg , a );
95+ return b -> virtual_address ;
96+ } else {
97+ debug ("Fatal Error, split invalid or memory error" , 1 );
98+ }
99+ }
100+ }
101+ b = b -> next ;
72102 }
73- if (1 == check_block_for_alloc (& address , b , size , l_r ))
74- return address ;
103+ }
104+ // Multi - Parts
105+ for (size_t i = 0 ; i < blks -> nb_blocks ; i ++ ) {
106+
75107 }
76108 return 999999999 ;
77109}
@@ -94,68 +126,69 @@ struct address_search *search_at_address(size_t address, struct leader_resources
94126}
95127
96128
97- void get_command (struct node * n , struct leader_resources * l_r , unsigned short user ) {
129+ void get_command (struct leader_resources * l_r , unsigned short user ) {
98130 // int MPI_Irecv(void *buf, int count, MPI_Datatype datatype, int source,
99131 // int tag, MPI_Comm comm, MPI_Request *request);
100- void * buff = malloc ( sizeof ( struct message )) ;
132+ struct message buff ;
101133 int count = sizeof (struct message );
102134 MPI_Request r ;
103135 MPI_Status st ;
104- MPI_Irecv (buff , count , MPI_BYTE , MPI_ANY_SOURCE , 0 , MPI_COMM_WORLD , & r );
136+ MPI_Irecv (& buff , count , MPI_BYTE , MPI_ANY_SOURCE , 0 , MPI_COMM_WORLD , & r );
105137
106138 if (0 == MPI_Wait (& r , & st )) {
107- struct message * m = buff ;
139+ struct message * m = & buff ;
108140 struct command_queue * n_command = generate_command_queue (m -> op , NULL );
109141 switch (m -> op ) {
110142 case OP_OK :
111- debug ("Leader recv OP OK from User" , n -> id );
143+ debug ("Leader recv OP OK from User" , l_r -> id );
112144 break ;
113145 case OP_WRITE :
114- debug ("Leader recv OP WRITE from User" , n -> id );
146+ debug ("Leader recv OP WRITE from User" , l_r -> id );
115147 n_command -> command = m -> op ;
116148 n_command -> data = NULL ;
117149 struct data_write * d_w = generate_data_write (m -> size , m -> size , NULL );
118150 void * wbuff = malloc (sizeof (char ) * (m -> size + 1 ));
119- debug ("Leader wait DATA from User for OP WRITE" , n -> id );
151+ debug ("Leader wait DATA from User for OP WRITE" , l_r -> id );
120152 MPI_Irecv (wbuff , m -> size * sizeof (char ), MPI_BYTE , user , 0 , MPI_COMM_WORLD , & r );
121153 if (0 == MPI_Wait (& r , & st )) {
122154 d_w -> data = wbuff ;
123155 n_command -> data = d_w ;
124156 }
125- debug ("Leader recv DATA from User for OP WRITE : " , n -> id );
126- debug_n (d_w -> data , n -> id , d_w -> size );
157+ debug ("Leader recv DATA from User for OP WRITE : " , l_r -> id );
158+ debug_n (d_w -> data , l_r -> id , d_w -> size );
127159 l_r -> leader_command_queue = push_command (l_r -> leader_command_queue , n_command );
128160 break ;
129161 case OP_READ :
130- debug ("Leader recv OP READ from User" , n -> id );
162+ debug ("Leader recv OP READ from User" , l_r -> id );
131163 n_command -> command = m -> op ;
132164 n_command -> data = NULL ;
133165 struct data_read * d_r = generate_data_read (m -> address , m -> size );
134166 n_command -> data = d_r ;
135167 l_r -> leader_command_queue = push_command (l_r -> leader_command_queue , n_command );
136168 break ;
137169 case OP_MALLOC :
138- debug ("Leader recv OP MALLOC from User" , n -> id );
170+ debug ("Leader recv OP MALLOC from User" , l_r -> id );
139171 n_command -> command = m -> op ;
140172 n_command -> data = NULL ;
141173 struct data_size * d_s = generate_data_size (m -> size );
142174 n_command -> data = d_s ;
143175 l_r -> leader_command_queue = push_command (l_r -> leader_command_queue , n_command );
144176 break ;
145177 default :
146- debug ("Leader recv ANY OP from User" , n -> id );
178+ debug ("Leader recv ANY OP from User" , l_r -> id );
147179 break ;
148180 }
149181 }
150- free (buff );
182+ // free(buff);
151183}
152184
153185void execute_malloc (struct node * n , struct leader_resources * l_r ) {
154186 struct data_size * d_s = peek_command (l_r -> leader_command_queue );
155187 if (!d_s ) {
156188 debug ("ERROR allocation data_size for OP MALLOC execution [LEADER]" , n -> id );
157189 }
158- size_t v_addr = alloc_memory (d_s -> size , l_r );
190+ size_t v_addr = alloc_memory (d_s -> size , l_r );
191+ size_t sss = d_s -> size ;
159192 if (v_addr == 999 || v_addr == 1999 ) {
160193 debug ("FATAL ERROR" , n -> id );
161194 if (v_addr == 1999 ) {
@@ -164,12 +197,15 @@ void execute_malloc(struct node *n, struct leader_resources *l_r) {
164197 return ;
165198 }
166199 debug ("Allocation at " , n -> id );
167- printf ("%zu\n" , v_addr );
168- struct message * m = generate_message (n -> id , DEF_NODE_USER , DEF_NODE_USER , v_addr , d_s -> size , OP_MALLOC );
169-
170- MPI_Request r ;
171- MPI_Isend ((void * ) m , sizeof (struct message ), MPI_BYTE , m -> id_t , 0 , MPI_COMM_WORLD , & r );
172- free (m );
200+ printf ("%zu of %zu bytes\n" , v_addr , sss );
201+ if (n -> id != l_r -> id ) {
202+ debug ("WTFFFFFFFFFFFFFFFFFFFf" , n -> id );
203+ } else {
204+ struct message * m = generate_message (l_r -> id , DEF_NODE_USER , DEF_NODE_USER , v_addr , sss , OP_MALLOC );
205+ MPI_Request r ;
206+ MPI_Isend ((void * ) m , sizeof (struct message ), MPI_BYTE , m -> id_t , 0 , MPI_COMM_WORLD , & r );
207+ free (m );
208+ }
173209}
174210
175211void execute_read (struct node * n , struct leader_resources * l_r ) {
@@ -231,9 +267,24 @@ void execute_command(struct node *n, struct leader_resources *l_r) {
231267
232268void leader_loop (struct node * n , unsigned short terminal_id , unsigned short nb_nodes ) {
233269 // Init leader resource
234- struct leader_resources * l_r = generate_leader_resources (nb_nodes /*, n*/ );
270+ printf ("NB NODES :%d," , nb_nodes );
271+
272+ printf ("NB NODES :%d," , nb_nodes );
273+ void * k = malloc (sizeof (struct message ));
274+ (void ) k ;
275+ debug ("START LEADER LOOP" , n -> id );
276+ struct leader_resources * l_r = generate_leader_resources (nb_nodes , n -> id );
235277
236- for (size_t i = 0 ; i < l_r -> leader_blks -> nb_blocks ; i ++ ) {
278+ debug ("PASS LEADER_RESOURCES" , n -> id );
279+ for (size_t o = 0 ; o < 1 ; o ++ ) {
280+ // void *g =generate_message(0, DEF_NODE_USER, DEF_NODE_USER, 0, 0, OP_MALLOC);
281+ // (void)g;
282+ void * jj = malloc (48 ); //
283+ // sizeof(struct message));
284+ (void ) jj ;
285+ }
286+
287+ for (size_t i = 0 ; i < l_r -> leader_blks -> nb_blocks ; i ++ ) {
237288 struct block * b = l_r -> leader_blks -> blks [i ];
238289 printf ("%u\n" , b -> id );
239290 }
@@ -245,18 +296,18 @@ void leader_loop(struct node *n, unsigned short terminal_id, unsigned short nb_n
245296 // MPI_Irecv();
246297 // MPI_Wait();
247298 // MPI_Test();
248-
249299 int MPI_Irecv(void *buf, int count, MPI_Datatype datatype, int source,
250300 int tag, MPI_Comm comm, MPI_Request *request);
251301 */
252302 int die = -1 ;
253303 while (1 ) {
304+ print_allocations_table (l_r );
254305 // Get command from user
255- get_command (n , l_r , terminal_id );
256- debug ("COMMANDS LISTEN DONE" , n -> id );
306+ get_command (l_r , terminal_id );
307+ // debug("COMMANDS LISTEN DONE", n->id);
257308 // Execute Commands
258309 execute_command (n , l_r );
259- debug ("COMMANDS EXEC DONE" , n -> id );
310+ // debug("COMMANDS EXEC DONE", n->id);
260311 // Break on death
261312 if (die == 1 )
262313 break ;
0 commit comments