CommDist.c 24.2 KB
Newer Older
iker_martin's avatar
iker_martin committed
1
2
3
4
5
6
7
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <string.h>
#include "distribution_methods/block_distribution.h"
#include "CommDist.h"

8
void prepare_redistribution(int qty, int mal_type, int myId, int numP, int numO, int is_children_group, int is_intercomm, void **recv, struct Counts *s_counts, struct Counts *r_counts);
iker_martin's avatar
iker_martin committed
9
10
void check_requests(struct Counts s_counts, struct Counts r_counts, MPI_Request **requests, size_t *request_qty);

11
void sync_point2point(void *send, void *recv, int mal_type, int is_intercomm, int myId, struct Counts s_counts, struct Counts r_counts, MPI_Comm comm);
iker_martin's avatar
iker_martin committed
12

13
void async_point2point(void *send, void *recv, int mal_type, struct Counts s_counts, struct Counts r_counts, MPI_Comm comm, MPI_Request *requests);
iker_martin's avatar
iker_martin committed
14

15
void perform_manual_communication(void *send, void *recv, int mal_type, int myId, struct Counts s_counts, struct Counts r_counts);
iker_martin's avatar
iker_martin committed
16

17
18
19
void *ind_send, *ind_recv; //FIXME Borrar
void recalculate_counts(struct Counts *counts, int *array, void **recv, int mal_type);
int recalculate_elems(int *array, int ini, int fin);
iker_martin's avatar
iker_martin committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

/*
 * Reserva memoria para un vector de hasta "qty" elementos.
 * Los "qty" elementos se disitribuyen entre los "numP" procesos
 * que llaman a esta funcion.
 */
void malloc_comm_array(char **array, int qty, int myId, int numP) {
    struct Dist_data dist_data;

    get_block_dist(qty, myId, numP, &dist_data);
    if( (*array = calloc(dist_data.tamBl, sizeof(char))) == NULL) {
      printf("Memory Error (Malloc Arrays(%d))\n", dist_data.tamBl); 
      exit(1); 
    }

/*
        int i;
	for(i=0; i<dist_data.tamBl; i++) {
	  (*array)[i] = '!' + i + dist_data.ini;
	}
	
        printf("P%d Tam %d String: %s\n", myId, dist_data.tamBl, *array);
*/
}

//================================================================================
//================================================================================
//========================SYNCHRONOUS FUNCTIONS===================================
//================================================================================
//================================================================================

/*
 * Performs a communication to redistribute an array in a block distribution.
 * In the redistribution is differenciated parent group from the children and the values each group indicates can be
 * different.
 *
 * - send (IN):  Array with the data to send. This data can not be null for parents.
 * - recv (OUT): Array where data will be written. A NULL value is allowed if the process is not going to receive data.
 *               If the process receives data and is NULL, the behaviour is undefined.
 * - qty  (IN):  Sum of elements shared by all processes that will send data.
 * - myId (IN):  Rank of the MPI process in the local communicator. For the parents is not the rank obtained from "comm".
 * - numP (IN):  Size of the local group. If it is a children group, this parameter must correspond to using
 *               "MPI_Comm_size(comm)". For the parents is not always the size obtained from "comm".
 * - numO (IN):  Amount of processes in the remote group. For the parents is the target quantity of processes after the 
 *               resize, while for the children is the amount of parents.
 * - is_children_group (IN): Indicates wether this MPI rank is a children(TRUE) or a parent(FALSE).
 * - comm (IN):  Communicator to use to perform the redistribution.
 *
 * returns: An integer indicating if the operation has been completed(TRUE) or not(FALSE). //FIXME In this case is always true...
 */
70
int sync_communication(void *send, void **recv, int qty, int mal_type, int dependency, int myId, int numP, int numO, int is_children_group, int red_method, MPI_Comm comm) {
iker_martin's avatar
iker_martin committed
71
72
73
    int is_intercomm, aux_comm_used = 0;
    struct Counts s_counts, r_counts;
    struct Dist_data dist_data;
74
    MPI_Datatype datatype;
iker_martin's avatar
iker_martin committed
75
76
77
78
    MPI_Comm aux_comm = MPI_COMM_NULL;

    /* PREPARE COMMUNICATION */
    MPI_Comm_test_inter(comm, &is_intercomm);
79
80
    prepare_redistribution(qty, mal_type, myId, numP, numO, is_children_group, is_intercomm, recv, &s_counts, &r_counts);
/*
iker_martin's avatar
iker_martin committed
81
82
83
84
        if(is_intercomm) {
          MPI_Intercomm_merge(comm, is_children_group, &aux_comm);
	  aux_comm_used = 1;
	}
85
86
87
88
89
90
91
92
93
94
95
*/
    if(mal_type == MAL_INT) {
      datatype = MPI_INT;
    } else if(mal_type == MAL_DOUBLE) {
      datatype = MPI_DOUBLE;
    } else if(mal_type == MAL_CHAR) {
      datatype = MPI_CHAR;
    } else {
      printf("Malleability -- Redistribution type not recognised\n");
      MPI_Abort(MPI_COMM_WORLD, -1);
    }
iker_martin's avatar
iker_martin committed
96

97
98
99
    if(dependency == 1+MAL_DATA_DEPENDENT) {
      if(is_children_group) {
        recalculate_counts(&r_counts, (int *) ind_recv, recv, mal_type);
100
101
//get_block_dist(qty, myId, numP, &dist_data);
//print_counts(dist_data, r_counts.counts, r_counts.displs, numO, 0, "Children C ");
102
103
      } else {
        recalculate_counts(&s_counts, (int *) ind_send, recv, mal_type);
104
105
//get_block_dist(qty, myId, numP, &dist_data);
//print_counts(dist_data, s_counts.counts, s_counts.displs, numO, 0, "Parents ");
106
107
	if(!is_intercomm) {
          recalculate_counts(&r_counts, (int *) ind_recv, recv, mal_type);
iker_martin's avatar
iker_martin committed
108
	}
109
110
      }
    }
iker_martin's avatar
iker_martin committed
111

112
113
    /* PERFORM COMMUNICATION */
    switch(red_method) {
iker_martin's avatar
iker_martin committed
114
      case MALL_RED_POINT:
115
        sync_point2point(send, *recv, mal_type, is_intercomm, myId, s_counts, r_counts, comm);
iker_martin's avatar
iker_martin committed
116
117
118
	break;
      case MALL_RED_BASELINE:
      default:
119
        MPI_Alltoallv(send, s_counts.counts, s_counts.displs, datatype, *recv, r_counts.counts, r_counts.displs, datatype, comm);
iker_martin's avatar
iker_martin committed
120
121
122
123
124
	break;
    }

    if(aux_comm_used) {
      MPI_Comm_free(&aux_comm);
125
126
127
128
129
130
131
132
133
134
135
136
137
    }

    if(dependency == 1+MAL_DATA_INDEPENDENT) {
      if(is_children_group) {
        ind_recv = *recv;
      } else {
	ind_send = send;
	if(!is_intercomm) {
	  ind_recv = *recv;
	}
      }
    }

iker_martin's avatar
iker_martin committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    freeCounts(&s_counts);
    freeCounts(&r_counts);
    return 1; //FIXME In this case is always true...
}

/*
 * Performs a series of blocking point2point communications to redistribute an array in a block distribution. 
 * It should be called after calculating how data should be redistributed.
 *
 * - send (IN):  Array with the data to send. This value can not be NULL for parents.
 * - recv (OUT): Array where data will be written. A NULL value is allowed if the process is not going to 
 *               receive data. If the process receives data and is NULL, the behaviour is undefined.
 * - is_intercomm (IN): Indicates wether the communicator is an intercommunicator (TRUE) or an
 *               intracommunicator (FALSE).
 * - myId (IN):  Rank of the MPI process in the local communicator. For the parents is not the rank obtained from "comm".
 * - s_counts (IN): Struct which describes how many elements will send this process to each children and 
 *               the displacements.
 * - r_counts (IN): Structure which describes how many elements will receive this process from each parent 
 *               and the displacements.
 * - comm (IN):  Communicator to use to perform the redistribution.
 *
 */
160
void sync_point2point(void *send, void *recv, int mal_type, int is_intercomm, int myId, struct Counts s_counts, struct Counts r_counts, MPI_Comm comm) {
iker_martin's avatar
iker_martin committed
161
    int i, j, init, end, total_sends;
162
    size_t datasize, offset;
iker_martin's avatar
iker_martin committed
163
    MPI_Request *sends;
164
165
166
167
    MPI_Datatype datatype;

    if(mal_type == MAL_INT) {
      datatype = MPI_INT;
168
      datasize = sizeof(int);
169
170
    } else if(mal_type == MAL_DOUBLE) {
      datatype = MPI_DOUBLE;
171
      datasize = sizeof(double);
172
173
    } else if(mal_type == MAL_CHAR) {
      datatype = MPI_CHAR;
174
      datasize = sizeof(char);
175
176
177
178
    } else {
      printf("Malleability -- Redistribution type not recognised\n");
      MPI_Abort(MPI_COMM_WORLD, -1);
    }
iker_martin's avatar
iker_martin committed
179
180
181
182

    init = s_counts.idI;
    end = s_counts.idE;
    if(!is_intercomm && (s_counts.idI == myId || s_counts.idE == myId + 1)) {
183
      perform_manual_communication(send, recv, mal_type, myId, s_counts, r_counts);
iker_martin's avatar
iker_martin committed
184
185
186
187
188
189
190
191
192
193
194
195

      if(s_counts.idI == myId) init = s_counts.idI+1;
      else end = s_counts.idE-1;
    }

    total_sends = end - init;
    j = 0;
    if(total_sends > 0) {
      sends = (MPI_Request *) malloc(total_sends * sizeof(MPI_Request));
    }
    for(i=init; i<end; i++) {
      sends[j] = MPI_REQUEST_NULL;
196
197
      offset = s_counts.displs[i] * datasize;
      MPI_Isend(send+offset, s_counts.counts[i], datatype, i, 99, comm, &(sends[j]));
iker_martin's avatar
iker_martin committed
198
199
200
201
202
203
204
205
206
207
208
      j++;
    }

    init = r_counts.idI;
    end = r_counts.idE;
    if(!is_intercomm) {
      if(r_counts.idI == myId) init = r_counts.idI+1;
      else if(r_counts.idE == myId + 1) end = r_counts.idE-1;
    }

    for(i=init; i<end; i++) {
209
210
      offset = r_counts.displs[i] * datasize;
      MPI_Recv(recv+offset, r_counts.counts[i], datatype, i, 99, comm, MPI_STATUS_IGNORE);
iker_martin's avatar
iker_martin committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
    }

    if(total_sends > 0) {
      MPI_Waitall(total_sends, sends, MPI_STATUSES_IGNORE);
    }
}

//================================================================================
//================================================================================
//========================ASYNCHRONOUS FUNCTIONS==================================
//================================================================================
//================================================================================

/*
 * //TODO Añadir estrategia IBARRIER
 * Performs a communication to redistribute an array in a block distribution with non-blocking MPI functions.
 * In the redistribution is differenciated parent group from the children and the values each group indicates can be
 * different.
 *
 * - send (IN):  Array with the data to send. This data can not be null for parents.
 * - recv (OUT): Array where data will be written. A NULL value is allowed if the process is not going to receive data.
 *               If the process receives data and is NULL, the behaviour is undefined.
 * - qty  (IN):  Sum of elements shared by all processes that will send data.
 * - myId (IN):  Rank of the MPI process in the local communicator. For the parents is not the rank obtained from "comm".
 * - numP (IN):  Size of the local group. If it is a children group, this parameter must correspond to using
 *               "MPI_Comm_size(comm)". For the parents is not always the size obtained from "comm".
 * - numO (IN):  Amount of processes in the remote group. For the parents is the target quantity of processes after the 
 *               resize, while for the children is the amount of parents.
 * - is_children_group (IN): Indicates wether this MPI rank is a children(TRUE) or a parent(FALSE).
 * - comm (IN):  Communicator to use to perform the redistribution.
 * - requests (OUT): Pointer to array of requests to be used to determine if the communication has ended. If the pointer 
 *               is null or not enough space has been reserved the pointer is allocated/reallocated.
 * - request_qty (OUT): Quantity of requests to be used. If a process sends and receives data, this value will be 
 *               modified to the expected value.
 *
 * returns: An integer indicating if the operation has been completed(TRUE) or not(FALSE). //FIXME In this case is always false...
 */
248
int async_communication(void *send, void **recv, int qty, int mal_type, int dependency, int myId, int numP, int numO, int is_children_group, int red_method, int red_strategies, MPI_Comm comm, MPI_Request **requests, size_t *request_qty) {
iker_martin's avatar
iker_martin committed
249
250
    int is_intercomm, aux_comm_used = 0;
    struct Counts s_counts, r_counts;
251
    MPI_Datatype datatype;
iker_martin's avatar
iker_martin committed
252
253
254
255
    MPI_Comm aux_comm = MPI_COMM_NULL;

    /* PREPARE COMMUNICATION */
    MPI_Comm_test_inter(comm, &is_intercomm);
256
    prepare_redistribution(qty, mal_type, myId, numP, numO, is_children_group, is_intercomm, recv, &s_counts, &r_counts);
iker_martin's avatar
iker_martin committed
257
258
    check_requests(s_counts, r_counts, requests, request_qty);

259
260
261
262
263
264
265
266
267
268
269
    if(mal_type == MAL_INT) {
      datatype = MPI_INT;
    } else if(mal_type == MAL_DOUBLE) {
      datatype = MPI_DOUBLE;
    } else if(mal_type == MAL_CHAR) {
      datatype = MPI_CHAR;
    } else {
      printf("Malleability -- Redistribution type not recognised\n");
      MPI_Abort(MPI_COMM_WORLD, -1);
    }

270
271
272
273
274
275
276
277
278
279
280
    if(dependency == 1+MAL_DATA_DEPENDENT) {
      if(is_children_group) {
        recalculate_counts(&r_counts, (int *) ind_recv, recv, mal_type);
      } else {
        recalculate_counts(&s_counts, (int *) ind_send, recv, mal_type);
	if(!is_intercomm) {
          recalculate_counts(&r_counts, (int *) ind_recv, recv, mal_type);
	}
      }
    }

iker_martin's avatar
iker_martin committed
281
282
283
    /* PERFORM COMMUNICATION */
    switch(red_method) {
      case MALL_RED_POINT:
284
        async_point2point(send, *recv, mal_type, s_counts, r_counts, comm, *requests);
iker_martin's avatar
iker_martin committed
285
286
287
	break;
      case MALL_RED_BASELINE:
      default:
288
        MPI_Ialltoallv(send, s_counts.counts, s_counts.displs, datatype, *recv, r_counts.counts, r_counts.displs, datatype, comm, &((*requests)[0]));
iker_martin's avatar
iker_martin committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
	break;
    }

    /* POST REQUESTS CHECKS */
    if(is_children_group) {
      MPI_Waitall(*request_qty, *requests, MPI_STATUSES_IGNORE);
    }

    if(malleability_red_contains_strat(red_strategies, MALL_RED_IBARRIER, NULL)) { //FIXME Strategy not fully implemented
      MPI_Ibarrier(comm, &((*requests)[*request_qty-1]) ); //FIXME Not easy to read...
      if(is_children_group) {
        MPI_Wait(&((*requests)[*request_qty-1]), MPI_STATUSES_IGNORE); //FIXME Not easy to read...
      }
    }

    if(aux_comm_used) {
      MPI_Comm_free(&aux_comm);
    } 
307
308
309
310
311
312
313
314
315
316
317
318

    if(dependency == 1+MAL_DATA_INDEPENDENT) {
      if(is_children_group) {
        ind_recv = *recv;
      } else {
	ind_send = send;
	if(!is_intercomm) {
	  ind_recv = *recv;
	}
      }
    }

iker_martin's avatar
iker_martin committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
    freeCounts(&s_counts);
    freeCounts(&r_counts);
    return 0; //FIXME In this case is always false...
}

/*
 * Performs a series of non-blocking point2point communications to redistribute an array in a block distribution. 
 * It should be called after calculating how data should be redistributed.
 *
 * - send (IN):  Array with the data to send. This value can not be NULL for parents.
 * - recv (OUT): Array where data will be written. A NULL value is allowed if the process is not going to 
 *               receive data. If the process receives data and is NULL, the behaviour is undefined.
 * - s_counts (IN): Struct which describes how many elements will send this process to each children and 
 *               the displacements.
 * - r_counts (IN): Structure which describes how many elements will receive this process from each parent 
 *               and the displacements.
 * - comm (IN):  Communicator to use to perform the redistribution.
 * - requests (OUT): Pointer to array of requests to be used to determine if the communication has ended.
 *
 */
339
void async_point2point(void *send, void *recv, int mal_type, struct Counts s_counts, struct Counts r_counts, MPI_Comm comm, MPI_Request *requests) {
iker_martin's avatar
iker_martin committed
340
    int i, j = 0;
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
    size_t datasize, offset;
    MPI_Datatype datatype;

    if(mal_type == MAL_INT) {
      datatype = MPI_INT;
      datasize = sizeof(int);
    } else if(mal_type == MAL_DOUBLE) {
      datatype = MPI_DOUBLE;
      datasize = sizeof(double);
    } else if(mal_type == MAL_CHAR) {
      datatype = MPI_CHAR;
      datasize = sizeof(char);
    } else {
      printf("Malleability -- Redistribution type not recognised\n");
      MPI_Abort(MPI_COMM_WORLD, -1);
    }
iker_martin's avatar
iker_martin committed
357
358

    for(i=s_counts.idI; i<s_counts.idE; i++) {
359
360
      offset = s_counts.displs[i] * datasize;
      MPI_Isend(send+offset, s_counts.counts[i], datatype, i, 99, comm, &(requests[j]));
iker_martin's avatar
iker_martin committed
361
362
363
364
      j++;
    }

    for(i=r_counts.idI; i<r_counts.idE; i++) {
iker_martin's avatar
iker_martin committed
365
      offset = r_counts.displs[i] * datasize;
366
      MPI_Irecv(recv+offset, r_counts.counts[i], datatype, i, 99, comm, &(requests[j]));
iker_martin's avatar
iker_martin committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
      j++;
    }
}

/*
 * ========================================================================================
 * ========================================================================================
 * ================================DISTRIBUTION FUNCTIONS==================================
 * ========================================================================================
 * ========================================================================================
*/

/*
 * Performs a communication to redistribute an array in a block distribution. For each process calculates
 * how many elements sends/receives to other processes for the new group.
 *
 * - qty  (IN):  Sum of elements shared by all processes that will send data.
 * - myId (IN):  Rank of the MPI process in the local communicator. For the parents is not the rank obtained from "comm".
 * - numP (IN):  Size of the local group. If it is a children group, this parameter must correspond to using
 *               "MPI_Comm_size(comm)". For the parents is not always the size obtained from "comm".
 * - numO (IN):  Amount of processes in the remote group. For the parents is the target quantity of processes after the 
 *               resize, while for the children is the amount of parents.
 * - is_children_group (IN): Indicates wether this MPI rank is a children(TRUE) or a parent(FALSE).
 * - is_intercomm (IN): Indicates wether the used communicator is a intercomunicator(TRUE) or intracommunicator(FALSE).
 * - recv (OUT): Array where data will be written. A NULL value is allowed if the process is not going to receive data.
 *               process receives data and is NULL, the behaviour is undefined.
 * - s_counts (OUT): Struct where is indicated how many elements sends this process to processes in the new group.
 * - r_counts (OUT): Struct where is indicated how many elements receives this process from other processes in the previous group.
 *
 */
397
void prepare_redistribution(int qty, int mal_type, int myId, int numP, int numO, int is_children_group, int is_intercomm, void **recv, struct Counts *s_counts, struct Counts *r_counts) {
iker_martin's avatar
iker_martin committed
398
399
  int array_size = numO;
  int offset_ids = 0;
400
  size_t datasize;
iker_martin's avatar
iker_martin committed
401
402
  struct Dist_data dist_data;

403
404
405
406
407
408
409
410
411
412
413
  if(mal_type == MAL_INT) {
    datasize = sizeof(int);
  } else if(mal_type == MAL_DOUBLE) {
    datasize = sizeof(double);
  } else if(mal_type == MAL_CHAR) {
    datasize = sizeof(char);
  } else {
    printf("Malleability -- Redistribution type not recognised\n");
    MPI_Abort(MPI_COMM_WORLD, -1);
  }

iker_martin's avatar
iker_martin committed
414
  if(is_intercomm) {
415
    //offset_ids = numP; //FIXME Modify only if active?
iker_martin's avatar
iker_martin committed
416
417
418
419
420
421
422
423
424
425
426
427
  } else {
    array_size = numP > numO ? numP : numO;
  }
  mallocCounts(s_counts, array_size+offset_ids);
  mallocCounts(r_counts, array_size+offset_ids);

  if(is_children_group) {
    offset_ids = 0;
    prepare_comm_alltoall(myId, numP, numO, qty, offset_ids, r_counts);
    
    // Obtener distribución para este hijo
    get_block_dist(qty, myId, numP, &dist_data);
428
    *recv = malloc(dist_data.tamBl * datasize);
iker_martin's avatar
iker_martin committed
429
//get_block_dist(qty, myId, numP, &dist_data);
430
//print_counts(dist_data, r_counts->counts, r_counts->displs, numO+offset_ids, 1, "Children C ");
iker_martin's avatar
iker_martin committed
431
432
433
434
435
436
437
438
  } else {
//get_block_dist(qty, myId, numP, &dist_data);

    prepare_comm_alltoall(myId, numP, numO, qty, offset_ids, s_counts);
    if(!is_intercomm && myId < numO) {
        prepare_comm_alltoall(myId, numO, numP, qty, offset_ids, r_counts);
        // Obtener distribución para este hijo y reservar vector de recibo
        get_block_dist(qty, myId, numO, &dist_data);
439
440
        *recv = malloc(dist_data.tamBl * datasize);
//print_counts(dist_data, r_counts->counts, r_counts->displs, array_size, 1, "Children P ");
iker_martin's avatar
iker_martin committed
441
    }
442
//print_counts(dist_data, s_counts->counts, s_counts->displs, numO+offset_ids, 1, "Parents ");
iker_martin's avatar
iker_martin committed
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  }
}

/*
 * Ensures that the array of request of a process has an amount of elements equal to the amount of communication
 * functions the process will perform. In case the array is not initialized or does not have enough space it is
 * allocated/reallocated to the minimum amount of space needed.
 *
 * - s_counts (IN): Struct where is indicated how many elements sends this process to processes in the new group.
 * - r_counts (IN): Struct where is indicated how many elements receives this process from other processes in the previous group.
 * - requests (OUT): Pointer to array of requests to be used to determine if the communication has ended. If the pointer 
 *               is null or not enough space has been reserved the pointer is allocated/reallocated.
 * - request_qty (OUT): Quantity of requests to be used. If the value is smaller than the amount of communication
 *               functions to perform, it is modified to the minimum value.
 */
void check_requests(struct Counts s_counts, struct Counts r_counts, MPI_Request **requests, size_t *request_qty) {
  size_t i, sum;
  MPI_Request *aux;

  sum = (size_t) s_counts.idE - s_counts.idI;
  sum += (size_t) r_counts.idE - r_counts.idI;

  if (*requests != NULL && sum <= *request_qty) return; // Expected amount of requests

  // FIXME Si es la estrategia Ibarrier como se tiene en cuenta en el total??
  if (*requests == NULL) {
    *requests = (MPI_Request *) malloc(sum * sizeof(MPI_Request));
  } else { // Array exists, but is too small
    aux = (MPI_Request *) realloc(*requests, sum * sizeof(MPI_Request));
    *requests = aux;
  }

  if (*requests == NULL) {
    fprintf(stderr, "Fatal error - It was not possible to allocate/reallocate memory for the MPI_Requests before the redistribution\n");
    MPI_Abort(MPI_COMM_WORLD, 1);
  }

  for(i=0; i < sum; i++) {
    (*requests)[i] = MPI_REQUEST_NULL;
  }
  *request_qty = sum;
}


/*
 * Special case to perform a manual copy of data when a process has to send data to itself. Only used
 * when the MPI communication is not able to hand this situation. An example is when using point to point
 * communications and the process has to perform a Send and Recv to itself
 * - send (IN):  Array with the data to send. This value can not be NULL.
 * - recv (OUT): Array where data will be written. This value can not be NULL.
 * - myId (IN):  Rank of the MPI process in the local communicator. For the parents is not the rank obtained from "comm".
 * - s_counts (IN): Struct where is indicated how many elements sends this process to processes in the new group.
 * - r_counts (IN): Struct where is indicated how many elements receives this process from other processes in the previous group.
 */
497
void perform_manual_communication(void *send, void *recv, int mal_type, int myId, struct Counts s_counts, struct Counts r_counts) {
iker_martin's avatar
iker_martin committed
498
  int i;
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
  if(mal_type == MAL_INT) {
    int *new_recv, *new_send;
    new_recv = (int *) recv;
    new_send = (int *) send;
    for(i=0; i<s_counts.counts[myId];i++) {
      new_recv[i+r_counts.displs[myId]] = new_send[i+s_counts.displs[myId]];
    }
  } else if(mal_type == MAL_DOUBLE) {
    double *new_recv, *new_send;
    new_recv = (double *) recv;
    new_send = (double *) send;
    for(i=0; i<s_counts.counts[myId];i++) {
      new_recv[i+r_counts.displs[myId]] = new_send[i+s_counts.displs[myId]];
    }
  } else if(mal_type == MAL_CHAR) {
    char *new_recv, *new_send;
    new_recv = (char *) recv;
    new_send = (char *) send;
    for(i=0; i<s_counts.counts[myId];i++) {
      new_recv[i+r_counts.displs[myId]] = new_send[i+s_counts.displs[myId]];
    }
  } else {
    printf("Malleability -- Redistribution type not recognised\n");
    MPI_Abort(MPI_COMM_WORLD, -1);
iker_martin's avatar
iker_martin committed
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
  }
}

/*
 * Función para obtener si entre las estrategias elegidas, se utiliza
 * la estrategia pasada como segundo argumento.
 *
 * Devuelve en "result" 1(Verdadero) si utiliza la estrategia, 0(Falso) en caso
 * contrario.
 */
int malleability_red_contains_strat(int comm_strategies, int strategy, int *result) {
  int value = comm_strategies % strategy ? 0 : 1;
  if(result != NULL) *result = value;
  return value;
}
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576


void recalculate_counts(struct Counts *counts, int *array, void **recv, int mal_type) {
  int i, ini, fin;
  ini = 0;
  fin = counts->counts[counts->idI];
  counts->counts[counts->idI] = recalculate_elems(array, ini, fin);
  for(i=counts->idI+1; i<counts->idE; i++) {
    fin = counts->displs[i] + counts->counts[i];
    ini = counts->displs[i];
    counts->counts[i] = recalculate_elems(array, ini, fin);
    counts->displs[i] = counts->displs[i-1] + counts->counts[i-1];
  }

  if(*recv != NULL) {
    int datasize, qty;
    if(mal_type == MAL_INT) {
      datasize = sizeof(int);
    } else if(mal_type == MAL_DOUBLE) {
      datasize = sizeof(double);
    } else if(mal_type == MAL_CHAR) {
      datasize = sizeof(char);
    } else {
      printf("Malleability -- Redistribution type not recognised\n");
      MPI_Abort(MPI_COMM_WORLD, -1);
    }
    qty = counts->counts[counts->idE-1] + counts->displs[counts->idE-1];
    free(*recv);
    *recv = malloc(qty * datasize);
  }
}

int recalculate_elems(int *array, int ini, int fin) {
  int i, sol = 0;
  for(i=ini; i<fin; i++) {
    sol += array[i];
  }
  return sol;
}