Skip to content

Commit 34a3a90

Browse files
committed
leader: write operation execution
1 parent 4ca81ed commit 34a3a90

1 file changed

Lines changed: 38 additions & 21 deletions

File tree

src/network/leader.c

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -302,29 +302,46 @@ void execute_write(struct leader_resources *l_r) {
302302
// 2 Get the block to write to (Warning to multiple parts allocation)
303303
// (Warning to size bigger than block)
304304
size_t to_write_address_v = d_w->address;
305-
ssize_t x = d_w->size;
306-
for (size_t i = part_s; i < c_a->number_parts; i++) {
305+
size_t x = d_w->size;
306+
for (size_t i = part_s; x > 0 && i < c_a->number_parts; i++) {
307307
// TODO handle size overflow
308-
while (x > 0) {
309-
struct block *b = c_a->parts[i];
310-
// compute size to write for this block
311-
ssize_t to_write_size = b->size - x;
312-
x -= to_write_size;
313-
if (to_write_size < 0) {
314-
debug("Fatal error in write operation, size to write negative", l_r->id);
315-
}
316-
// compute local address to write
317-
size_t local_address = b->virtual_address - to_write_address_v;
318-
to_write_address_v += local_address;
319-
320-
struct queue *q = queue_init();
321-
printf("Size to send for Write %ld\n\n", to_write_size);
322-
// 3 Send Write OP to each node (Warning to the local address of the node, not the virtual)
323-
struct message *m = generate_message(l_r->id, b->id, b->id, local_address, to_write_size, OP_WRITE);
324-
send_safe_message(m, q);
325-
debug_n(d_w->data, l_r->id, d_w->size);
326-
MPI_Send(d_w->data, to_write_size, MPI_BYTE, b->id, 4, MPI_COMM_WORLD);
308+
struct block *b = c_a->parts[i];
309+
// compute size to write for this block
310+
size_t to_write_size = 0;
311+
if (x <= b->size) {
312+
to_write_size = x;
313+
x = 0;
314+
} else {
315+
x -= b->size;
316+
to_write_size = b->size;
317+
}
318+
319+
// compute local address to write
320+
size_t local_address = 0;
321+
if (b->virtual_address == to_write_address_v) {
322+
local_address = 0;
323+
to_write_address_v += to_write_size;
324+
} else {
325+
local_address += to_write_address_v - b->virtual_address;
326+
to_write_address_v += to_write_size;
327327
}
328+
329+
// struct queue *q = queue_init();
330+
// printf("Size to send for Write %zu\n\n", to_write_size);
331+
// 3 Send Write OP to each node (Warning to the local address of the node, not the virtual)
332+
struct message *m = generate_message(l_r->id, b->id, b->id, local_address, to_write_size, OP_WRITE);
333+
debug("Send Write OP", l_r->id);
334+
MPI_Send(m, sizeof(struct message), MPI_BYTE, b->id, 3, MPI_COMM_WORLD);
335+
struct message m2;
336+
MPI_Status st;
337+
MPI_Recv(&m2, sizeof(struct message), MPI_BYTE, b->id, 3, MPI_COMM_WORLD, &st);
338+
debug("Send Data", l_r->id);
339+
// debug_n(d_w->data, l_r->id, d_w->size);
340+
MPI_Send(d_w->data, to_write_size, MPI_BYTE, b->id, 4, MPI_COMM_WORLD);
341+
}
342+
343+
if (x > 0) {
344+
debug("Write asked is overflowing the allocated block", l_r->id);
328345
}
329346

330347
// TODO Confirmation ?

0 commit comments

Comments
 (0)