|
| 1 | +/*Partitions MPI Unit Test |
| 2 | + * |
| 3 | + * Shows the behavior of the communication when a partitioned recv / send corridor is declared. |
| 4 | + * Parrived loops trying to find a recieved partition before any are sent and fails. |
| 5 | + * Parrived should timeout. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <stdio.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include "mpi.h" |
| 11 | +#include "assert.h" |
| 12 | +#include "time.h" |
| 13 | + |
| 14 | +#define PARTITIONS 8 |
| 15 | +#define COUNT 5 |
| 16 | +#define TIMEOUT 10000 |
| 17 | + |
| 18 | +int main (int argc, char *argv[]) |
| 19 | +{ |
| 20 | +//Buffer message |
| 21 | +double message [PARTITIONS * COUNT]; |
| 22 | + |
| 23 | +//MPI variables declarations |
| 24 | +int src = 0, dest = 1, tag = 100, flag = 0, flag2 = 0; |
| 25 | +int myrank, provided, timer, trigger, i, j; |
| 26 | +MPI_Count partitions = PARTITIONS; |
| 27 | +MPI_Request request; |
| 28 | + |
| 29 | +//Initializing threaded MPI |
| 30 | +MPI_Init_thread(&argc, &argv, MPI_THREAD_SERIALIZED, &provided); |
| 31 | +if (provided < MPI_THREAD_SERIALIZED) |
| 32 | + MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE); |
| 33 | +MPI_Comm_rank(MPI_COMM_WORLD, &myrank); |
| 34 | + |
| 35 | +if (myrank == 0) |
| 36 | +{ |
| 37 | + MPI_Psend_init(message, partitions, COUNT, MPI_DOUBLE, dest, tag, MPI_COMM_WORLD, MPI_INFO_NULL, &request); |
| 38 | + |
| 39 | + //This Barrier prevents task 0 to initialize a send operation before task 0 loops through Parrived |
| 40 | + |
| 41 | + MPI_Barrier(MPI_COMM_WORLD); |
| 42 | + |
| 43 | + MPI_Start(&request); |
| 44 | + |
| 45 | + //Iterating through each buffer partition, filling them and marking them ready to send |
| 46 | + for (i = 0; i < partitions; i++) |
| 47 | + { |
| 48 | + for (j = (i*COUNT); j < ((i+1)*COUNT); j++) |
| 49 | + { |
| 50 | + message[j] = j+1; |
| 51 | + } |
| 52 | + MPI_Pready(i, request); |
| 53 | + } |
| 54 | + |
| 55 | + //Test for overall send operation completion |
| 56 | + while (!flag) |
| 57 | + { |
| 58 | + MPI_Test(&request, &flag, MPI_STATUS_IGNORE); |
| 59 | + } |
| 60 | + MPI_Request_free(&request); |
| 61 | +} |
| 62 | +else if (myrank == 1) |
| 63 | +{ |
| 64 | + MPI_Precv_init(message, partitions, COUNT, MPI_DOUBLE, src, tag, MPI_COMM_WORLD, MPI_INFO_NULL, &request); |
| 65 | + MPI_Start(&request); |
| 66 | + |
| 67 | + //sets a timer in milliseconds equal to the time passed since beginning of operation |
| 68 | + timer = clock() * 1000 / CLOCKS_PER_SEC; |
| 69 | + //creates a trigger by adding a timeout time in millisecond to the current time passed |
| 70 | + trigger = timer + TIMEOUT; |
| 71 | + |
| 72 | + //Iterates through the partitions and check indefinetly to see if they have arrived |
| 73 | + for (i = 0; i < partitions; i++) |
| 74 | + { |
| 75 | + MPI_Parrived(request, i, &flag2); |
| 76 | + if (!flag2) { |
| 77 | + i--; |
| 78 | + } |
| 79 | + else { |
| 80 | + //Test the buffer to check that the message was recieved correctly |
| 81 | + for (j = (i * COUNT); j < ((i+1) * COUNT); j++) |
| 82 | + { |
| 83 | + assert(message[j] == (j + 1)); |
| 84 | + } |
| 85 | + } |
| 86 | + //set timer equal to the current time elapsed |
| 87 | + timer = clock() * 1000 / CLOCKS_PER_SEC; |
| 88 | + //Abort MPI if Parrived loops more than time time allowed |
| 89 | + if (timer > trigger){ |
| 90 | + printf("Parrived Timeout, No Partitions recieved in %d millisecond", TIMEOUT); |
| 91 | + MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + //This barrier allows task 0 to proceed |
| 96 | + MPI_Barrier(MPI_COMM_WORLD); |
| 97 | + |
| 98 | + //Test for overall recieve operation completion |
| 99 | + while (!flag) |
| 100 | + { |
| 101 | + MPI_Test(&request, &flag, MPI_STATUS_IGNORE); |
| 102 | + } |
| 103 | + |
| 104 | + printf("Test Passed Succesfully\n"); |
| 105 | + MPI_Request_free(&request); |
| 106 | +} |
| 107 | +MPI_Finalize(); |
| 108 | +return 0; |
| 109 | +} |
| 110 | + |
0 commit comments