|
10 | 10 | #include <mpi.h> |
11 | 11 | #include <stdint.h> |
12 | 12 | #include <communication.h> |
| 13 | +#include <string.h> |
13 | 14 |
|
14 | 15 | size_t get_message_size() { |
15 | 16 | return (sizeof(unsigned short) * 3 + 2 * sizeof(size_t) + sizeof(enum operation)); |
@@ -262,37 +263,54 @@ void execute_read(struct leader_resources *l_r) { |
262 | 263 | return; |
263 | 264 | } |
264 | 265 |
|
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)); |
267 | 268 |
|
268 | 269 | // 2 Get the block to write to (Warning to multiple parts allocation) |
269 | 270 | // (Warning to size bigger than block) |
270 | 271 | size_t to_write_address_v = d_r->address; |
271 | 272 | size_t nb_read = 0; |
| 273 | + size_t nb_read_size = 0; |
| 274 | + size_t x = d_r->size; |
| 275 | + size_t offset = 0; |
272 | 276 | for (size_t i = part_s; i < c_a->number_parts; i++) { |
273 | 277 | // TODO handle size overflow |
274 | | - |
275 | | - |
276 | 278 | 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 | + |
280 | 289 | // 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 | + } |
283 | 298 |
|
| 299 | + nb_read_size += to_read_size; |
284 | 300 | // 3 Send READ OP to each node (Warning to the local address of the node, not the virtual) |
285 | 301 | 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; |
287 | 309 | nb_read++; |
| 310 | + free(m); |
288 | 311 | } |
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); |
296 | 314 | } |
297 | 315 |
|
298 | 316 | void execute_write(struct leader_resources *l_r) { |
|
0 commit comments