| 1 |
|
|---|
| 2 | typedef $comm MPI_Comm;
|
|---|
| 3 |
|
|---|
| 4 | typedef enum {
|
|---|
| 5 | MPI_INT,
|
|---|
| 6 | MPI_FLOAT,
|
|---|
| 7 | MPI_DOUBLE,
|
|---|
| 8 | MPI_CHAR
|
|---|
| 9 | } MPI_Datatype;
|
|---|
| 10 |
|
|---|
| 11 | #define BCAST_TAG 999
|
|---|
| 12 |
|
|---|
| 13 | #define MPI_PROC_NULL -3
|
|---|
| 14 |
|
|---|
| 15 | typedef struct {
|
|---|
| 16 | int MPI_SOURCE;
|
|---|
| 17 | int MPI_TAG;
|
|---|
| 18 | int MPI_ERROR;
|
|---|
| 19 | int size;
|
|---|
| 20 | } MPI_Status;
|
|---|
| 21 |
|
|---|
| 22 | #define MPI_STATUS_IGNORE NULL
|
|---|
| 23 |
|
|---|
| 24 | #define MPI_STATUSES_IGNORE NULL
|
|---|
| 25 |
|
|---|
| 26 | int sizeofDatatype(MPI_Datatype datatype) {
|
|---|
| 27 | switch (datatype) {
|
|---|
| 28 | case MPI_INT:
|
|---|
| 29 | return sizeof(int);
|
|---|
| 30 | case MPI_FLOAT:
|
|---|
| 31 | return sizeof(float);
|
|---|
| 32 | case MPI_DOUBLE:
|
|---|
| 33 | return sizeof(double);
|
|---|
| 34 | case MPI_CHAR:
|
|---|
| 35 | return sizeof(char);
|
|---|
| 36 | default:
|
|---|
| 37 | $assert(0, "Unreachable");
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | int MPI_Init() {
|
|---|
| 42 | return 0;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | int MPI_Finalize() {
|
|---|
| 46 | return 0;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | int MPI_Comm_size(MPI_Comm comm, int *size) {
|
|---|
| 50 | *size = $comm_size(comm);
|
|---|
| 51 | return 0;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | int MPI_Comm_rank(MPI_Comm comm, int *rank) {
|
|---|
| 55 | *rank = $comm_place(comm);
|
|---|
| 56 | return 0;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | /* Sends a message. */
|
|---|
| 60 | int MPI_Send(void *buf, int count, MPI_Datatype datatype, int dest,
|
|---|
| 61 | int tag, MPI_Comm comm) {
|
|---|
| 62 | if (dest >= 0) {
|
|---|
| 63 | int size = count*sizeofDatatype(datatype);
|
|---|
| 64 | $message out =
|
|---|
| 65 | $message_pack(comm->place, dest, tag, buf, size);
|
|---|
| 66 |
|
|---|
| 67 | $comm_enqueue(comm, out);
|
|---|
| 68 | }
|
|---|
| 69 | return 0;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | /* Receives a message. */
|
|---|
| 73 | int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source,
|
|---|
| 74 | int tag, MPI_Comm comm, MPI_Status *status) {
|
|---|
| 75 | if (source >= 0) {
|
|---|
| 76 | $message in = $comm_dequeue(comm, source, tag);
|
|---|
| 77 | int size = count*sizeofDatatype(datatype);
|
|---|
| 78 |
|
|---|
| 79 | $message_unpack(in, buf, size);
|
|---|
| 80 | if (status != MPI_STATUS_IGNORE) {
|
|---|
| 81 | status->size = $message_size(in);
|
|---|
| 82 | status->MPI_SOURCE = $message_source(in);
|
|---|
| 83 | status->MPI_TAG = $message_tag(in);
|
|---|
| 84 | status->MPI_ERROR = 0;
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | return 0;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | int MPI_Get_count(MPI_Status *status, MPI_Datatype datatype, int *count) {
|
|---|
| 91 | *count = status->size/sizeofDatatype(datatype);
|
|---|
| 92 | return 0;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | int MPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 96 | int dest, int sendtag,
|
|---|
| 97 | void *recvbuf, int recvcount, MPI_Datatype recvtype,
|
|---|
| 98 | int source, int recvtag,
|
|---|
| 99 | MPI_Comm comm, MPI_Status *status) {
|
|---|
| 100 | MPI_Send(sendbuf, sendcount, sendtype, dest, sendtag, comm);
|
|---|
| 101 | MPI_Recv(recvbuf, recvcount, recvtype, source, recvtag, comm, status);
|
|---|
| 102 | return 0;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /* Broadcasts a message from root to everyone else.
|
|---|
| 106 | * Need to use a differnt comm.
|
|---|
| 107 | */
|
|---|
| 108 | int MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
|
|---|
| 109 | MPI_Comm comm) {
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | int place = $comm_place(comm);
|
|---|
| 113 |
|
|---|
| 114 | #ifdef DEBUG
|
|---|
| 115 | printf("MPI_Bcast: place=%d, count=%d, root=%d\n", place, count, root);
|
|---|
| 116 | #endif
|
|---|
| 117 | if (place == root) {
|
|---|
| 118 | int nprocs = $comm_size(comm);
|
|---|
| 119 |
|
|---|
| 120 | for (int i=0; i<nprocs; i++)
|
|---|
| 121 | if (i != root)
|
|---|
| 122 | MPI_Send(buf, count, datatype, i, BCAST_TAG, comm);
|
|---|
| 123 | } else {
|
|---|
| 124 | MPI_Recv(buf, count, datatype, root, BCAST_TAG, comm, MPI_STATUS_IGNORE);
|
|---|
| 125 | }
|
|---|
| 126 | return 0;
|
|---|
| 127 | }
|
|---|