Skip to content

Commit 4d0b5cc

Browse files
committed
leader: add execute_read
1 parent 295a54d commit 4d0b5cc

1 file changed

Lines changed: 35 additions & 17 deletions

File tree

src/network/leader.c

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <mpi.h>
1111
#include <stdint.h>
1212
#include <communication.h>
13+
#include <string.h>
1314

1415
size_t get_message_size() {
1516
return (sizeof(unsigned short) * 3 + 2 * sizeof(size_t) + sizeof(enum operation));
@@ -262,37 +263,54 @@ void execute_read(struct leader_resources *l_r) {
262263
return;
263264
}
264265

265-
// Readed bytes
266-
char *read_buff = NULL;
266+
// Buffer of all READS bytes
267+
char *read_buff = malloc(sizeof(char) * (d_r->size + 1));
267268

268269
// 2 Get the block to write to (Warning to multiple parts allocation)
269270
// (Warning to size bigger than block)
270271
size_t to_write_address_v = d_r->address;
271272
size_t nb_read = 0;
273+
size_t nb_read_size = 0;
274+
size_t x = d_r->size;
275+
size_t offset = 0;
272276
for (size_t i = part_s; i < c_a->number_parts; i++) {
273277
// TODO handle size overflow
274-
275-
276278
struct block *b = c_a->parts[i];
277-
// compute size to write for this block
278-
size_t to_read_size = d_r->size - b->size;
279-
d_r->size -= to_read_size;
279+
// compute size to read for this block
280+
size_t to_read_size = 0;
281+
if (x <= b->size) {
282+
to_read_size = x;
283+
x = 0;
284+
} else {
285+
x -= b->size;
286+
to_read_size = b->size;
287+
}
288+
280289
// compute local address to write
281-
size_t local_address = b->virtual_address - to_write_address_v;
282-
to_write_address_v += local_address;
290+
size_t local_address = 0;
291+
if (b->virtual_address == to_write_address_v) {
292+
local_address = 0;
293+
to_write_address_v += to_read_size;
294+
} else {
295+
local_address += to_write_address_v - b->virtual_address;
296+
to_write_address_v += to_read_size;
297+
}
283298

299+
nb_read_size += to_read_size;
284300
// 3 Send READ OP to each node (Warning to the local address of the node, not the virtual)
285301
struct message *m = generate_message(l_r->id, b->id, b->id, local_address, to_read_size, OP_READ);
286-
MPI_Send(m, sizeof(struct message), MPI_BYTE, b->id, 5, MPI_COMM_WORLD);
302+
debug("Send OP Read", l_r->id);
303+
MPI_Send(m, sizeof(struct message), MPI_BYTE, b->id, 3, MPI_COMM_WORLD);
304+
void *buff = malloc(sizeof(char) * (to_read_size + 1));
305+
MPI_Status st;
306+
MPI_Recv(buff, to_read_size, MPI_BYTE, b->id, 4, MPI_COMM_WORLD, &st);
307+
memcpy((void*)(read_buff + (offset * sizeof(char))), buff, to_read_size);
308+
offset += to_read_size;
287309
nb_read++;
310+
free(m);
288311
}
289-
290-
// 4 Receive bytes, append data from all nodes
291-
for (size_t i = 0; i < nb_read; i++) {
292-
// MPI_Irecv()
293-
}
294-
(void) read_buff;
295-
312+
debug("READ AND ASSEMBLED", l_r->id);
313+
debug_n(read_buff, l_r->id, d_r->size);
296314
}
297315

298316
void execute_write(struct leader_resources *l_r) {

0 commit comments

Comments
 (0)