| 1 | #ifndef __MPI_CVL__
|
|---|
| 2 | #define __MPI_CVL__
|
|---|
| 3 |
|
|---|
| 4 | #include<mpi.h>
|
|---|
| 5 | #include<civlc.cvh>
|
|---|
| 6 | #include<comm.cvh>
|
|---|
| 7 | #include<concurrency.cvh>
|
|---|
| 8 | #include<string.h>
|
|---|
| 9 |
|
|---|
| 10 | //TODO make a Datatype struct, which has a field "int size;" Define one of these objects for MPI_INT, MPI_DOUBLE, etc.
|
|---|
| 11 | //TODO Then provide methods like MPI provides for creating new ones.
|
|---|
| 12 | //TODO then support MPI_Type_contig(datatype, int n).
|
|---|
| 13 |
|
|---|
| 14 | #define BCAST_TAG 999
|
|---|
| 15 | #define REDUCE_TAG 998
|
|---|
| 16 |
|
|---|
| 17 | /* Completed definition for mpi-common.h */
|
|---|
| 18 | struct MPI_Status{
|
|---|
| 19 | int MPI_SOURCE;
|
|---|
| 20 | int MPI_TAG;
|
|---|
| 21 | int MPI_ERROR;
|
|---|
| 22 | int size;
|
|---|
| 23 | };
|
|---|
| 24 |
|
|---|
| 25 | /* Definition of CIVL-MPI */
|
|---|
| 26 | typedef enum {
|
|---|
| 27 | __UNINIT,
|
|---|
| 28 | __INIT,
|
|---|
| 29 | __FINALIZED
|
|---|
| 30 | }__MPI_Sys_status__;
|
|---|
| 31 |
|
|---|
| 32 | struct MPI_Request{
|
|---|
| 33 | int id;
|
|---|
| 34 | };
|
|---|
| 35 |
|
|---|
| 36 | /* Definition of CMPI_Gcomm and MPI_Comm */
|
|---|
| 37 | typedef struct CMPI_Gcomm {
|
|---|
| 38 | $gcomm p2p; // point-to-point communication
|
|---|
| 39 | $gcomm col; // collective communication
|
|---|
| 40 | $gbarrier gbarrier;
|
|---|
| 41 | } CMPI_Gcomm;
|
|---|
| 42 |
|
|---|
| 43 | struct MPI_Comm {
|
|---|
| 44 | $comm p2p; // point-to-point communication
|
|---|
| 45 | $comm col; // collective communication
|
|---|
| 46 | $barrier barrier;
|
|---|
| 47 | __MPI_Sys_status__ status;
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | /********************************** State **************************************/
|
|---|
| 51 |
|
|---|
| 52 | /* The number of times the MPI_Wtime function has been called */
|
|---|
| 53 | int CMPI_time_count = 0;
|
|---|
| 54 |
|
|---|
| 55 | /****************************** Helper Functions **********************************/
|
|---|
| 56 | int sizeofDatatype(MPI_Datatype datatype) {
|
|---|
| 57 | switch (datatype) {
|
|---|
| 58 | case MPI_INT:
|
|---|
| 59 | return sizeof(int);
|
|---|
| 60 | case MPI_FLOAT:
|
|---|
| 61 | return sizeof(float);
|
|---|
| 62 | case MPI_DOUBLE:
|
|---|
| 63 | return sizeof(double);
|
|---|
| 64 | case MPI_CHAR:
|
|---|
| 65 | return sizeof(char);
|
|---|
| 66 | default:
|
|---|
| 67 | $assert(0, "Unreachable");
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | /************************** MPI LIB Implementations *******************************/
|
|---|
| 72 |
|
|---|
| 73 | $abstract double CMPI_time(int count);
|
|---|
| 74 |
|
|---|
| 75 | double MPI_Wtime() {
|
|---|
| 76 | double result = CMPI_time(CMPI_time_count);
|
|---|
| 77 |
|
|---|
| 78 | CMPI_time_count++;
|
|---|
| 79 | return result;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | CMPI_Gcomm CMPI_Gcomm_create($scope scope, int size) {
|
|---|
| 83 | CMPI_Gcomm result;
|
|---|
| 84 |
|
|---|
| 85 | result.p2p = $gcomm_create(scope, size);
|
|---|
| 86 | result.col = $gcomm_create(scope, size);
|
|---|
| 87 | result.gbarrier = $gbarrier_create(scope, size);
|
|---|
| 88 | return result;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | void CMPI_Gcomm_destroy(CMPI_Gcomm gc) {
|
|---|
| 92 | $gcomm_destroy(gc.p2p);
|
|---|
| 93 | $gcomm_destroy(gc.col);
|
|---|
| 94 | $gbarrier_destroy(gc.gbarrier);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | MPI_Comm CMPI_Comm_create($scope scope, CMPI_Gcomm gc, int rank) {
|
|---|
| 98 | MPI_Comm result;
|
|---|
| 99 |
|
|---|
| 100 | result.p2p = $comm_create(scope, gc.p2p, rank);
|
|---|
| 101 | result.col = $comm_create(scope, gc.col, rank);
|
|---|
| 102 | result.barrier = $barrier_create(scope, gc.gbarrier, rank);
|
|---|
| 103 | result.status = __UNINIT;
|
|---|
| 104 | return result;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | void CMPI_Comm_destroy(MPI_Comm comm) {
|
|---|
| 108 | $comm_destroy(comm.p2p);
|
|---|
| 109 | $comm_destroy(comm.col);
|
|---|
| 110 | $barrier_destroy(comm.barrier);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | int __MPI_Init(MPI_Comm *comm) {
|
|---|
| 114 | comm->status = __INIT;
|
|---|
| 115 | return 0;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | int __MPI_Finalize(MPI_Comm *comm) {
|
|---|
| 119 | comm->status = __FINALIZED;
|
|---|
| 120 | return 0;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | int MPI_Comm_size(MPI_Comm comm, int *size) {
|
|---|
| 124 | $assert(comm.status == __INIT, "MPI_Comm_size() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| 125 | *size = $comm_size(comm.p2p);
|
|---|
| 126 | return 0;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | int MPI_Comm_rank(MPI_Comm comm, int *rank) {
|
|---|
| 130 | $assert(comm.status == __INIT, "MPI_Comm_rank() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| 131 | *rank = $comm_place(comm.p2p);
|
|---|
| 132 | return 0;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 | int CMPI_Send(void *buf, int count, MPI_Datatype datatype, int dest,
|
|---|
| 137 | int tag, $comm comm) {
|
|---|
| 138 | if (dest >= 0) {
|
|---|
| 139 | int size = count*sizeofDatatype(datatype);
|
|---|
| 140 | int place = $comm_place(comm);
|
|---|
| 141 | $message out = $message_pack(place, dest, tag, buf, size);
|
|---|
| 142 | $comm_enqueue(comm, out);
|
|---|
| 143 | }
|
|---|
| 144 | return 0;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | int MPI_Send(void *buf, int count, MPI_Datatype datatype, int dest,
|
|---|
| 148 | int tag, MPI_Comm comm) {
|
|---|
| 149 | $assert(comm.status == __INIT, "MPI_Send() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| 150 | return CMPI_Send(buf, count, datatype, dest, tag, comm.p2p);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 | int CMPI_Recv(void *buf, int count, MPI_Datatype datatype, int source,
|
|---|
| 155 | int tag, $comm comm, MPI_Status *status) {
|
|---|
| 156 | if (source >= 0 || source == MPI_ANY_SOURCE) {
|
|---|
| 157 | $message in = $comm_dequeue(comm, source, tag);
|
|---|
| 158 | int size = count*sizeofDatatype(datatype);
|
|---|
| 159 |
|
|---|
| 160 | $message_unpack(in, buf, size);
|
|---|
| 161 | if (status != MPI_STATUS_IGNORE) {
|
|---|
| 162 | status->size = $message_size(in);
|
|---|
| 163 | status->MPI_SOURCE = $message_source(in);
|
|---|
| 164 | status->MPI_TAG = $message_tag(in);
|
|---|
| 165 | status->MPI_ERROR = 0;
|
|---|
| 166 | }
|
|---|
| 167 | }
|
|---|
| 168 | return 0;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source,
|
|---|
| 172 | int tag, MPI_Comm comm, MPI_Status *status) {
|
|---|
| 173 | $assert(comm.status == __INIT,
|
|---|
| 174 | "MPI_Recv() cannot be invoked without "
|
|---|
| 175 | "MPI_Init() being called before.\n");
|
|---|
| 176 | return CMPI_Recv(buf, count, datatype, source, tag, comm.p2p, status);
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | int MPI_Get_count(MPI_Status *status, MPI_Datatype datatype, int *count) {
|
|---|
| 180 | *count = status->size/sizeofDatatype(datatype);
|
|---|
| 181 | return 0;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | int MPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype,
|
|---|
| 185 | int dest, int sendtag,
|
|---|
| 186 | void *recvbuf, int recvcount, MPI_Datatype recvtype,
|
|---|
| 187 | int source, int recvtag,
|
|---|
| 188 | MPI_Comm comm, MPI_Status *status) {
|
|---|
| 189 | $assert(comm.status == __INIT,
|
|---|
| 190 | "MPI_Sendrecv() cannot be invoked "
|
|---|
| 191 | "without MPI_Init() being called before.\n");
|
|---|
| 192 | // not correct for checking potential deadlock...rewrite:
|
|---|
| 193 | MPI_Send(sendbuf, sendcount, sendtype, dest, sendtag, comm);
|
|---|
| 194 | MPI_Recv(recvbuf, recvcount, recvtype, source, recvtag, comm, status);
|
|---|
| 195 | return 0;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | /* Broadcasts a message from root to everyone else.
|
|---|
| 199 | * Need to use a differnt comm.
|
|---|
| 200 | */
|
|---|
| 201 | int MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root,
|
|---|
| 202 | MPI_Comm comm) {
|
|---|
| 203 | $assert(comm.status == __INIT,
|
|---|
| 204 | "MPI_Bcast() cannot be invoked without MPI_Init() "
|
|---|
| 205 | "being called before.\n");
|
|---|
| 206 | if ($comm_place(comm.col) == root) {
|
|---|
| 207 | int nprocs = $comm_size(comm.col);
|
|---|
| 208 |
|
|---|
| 209 | for (int i=0; i<nprocs; i++)
|
|---|
| 210 | if (i != root)
|
|---|
| 211 | CMPI_Send(buf, count, datatype, i, BCAST_TAG, comm.col);
|
|---|
| 212 | } else {
|
|---|
| 213 | CMPI_Recv(buf, count, datatype, root, BCAST_TAG, comm.col,
|
|---|
| 214 | MPI_STATUS_IGNORE);
|
|---|
| 215 | }
|
|---|
| 216 | return 0;
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /* Reduces values on all processes to a single value */
|
|---|
| 220 | int MPI_Reduce(void* sendbuf, void* recvbuf, int count,
|
|---|
| 221 | MPI_Datatype datatype, MPI_Op op, int root,
|
|---|
| 222 | MPI_Comm comm) {
|
|---|
| 223 | int rank;
|
|---|
| 224 |
|
|---|
| 225 | $assert(comm.status == __INIT,
|
|---|
| 226 | "MPI_Reduce() cannot be invoked without "
|
|---|
| 227 | "MPI_Init() being called before.\n");
|
|---|
| 228 | rank = $comm_place(comm.col);
|
|---|
| 229 | if (rank != root)
|
|---|
| 230 | CMPI_Send(sendbuf, count, datatype, root, REDUCE_TAG, comm.col);
|
|---|
| 231 | else {
|
|---|
| 232 | int nprocs = $comm_size(comm.col);
|
|---|
| 233 | int size;
|
|---|
| 234 |
|
|---|
| 235 | for (int i = 0; i<nprocs; i++) {
|
|---|
| 236 | if(i == root) continue;
|
|---|
| 237 | else{
|
|---|
| 238 | $message in = $comm_dequeue(comm.col, i, REDUCE_TAG);
|
|---|
| 239 | size = count * sizeofDatatype(datatype);
|
|---|
| 240 |
|
|---|
| 241 | /* the third argument "count" indicates the number of cells needs doing the
|
|---|
| 242 | operation. */
|
|---|
| 243 | $bundle_unpack_apply(in.data, sendbuf, count, op);
|
|---|
| 244 | $assert(in.size <= size,
|
|---|
| 245 | "Message of size %d exceeds the specified size %d.",
|
|---|
| 246 | in.size, size);
|
|---|
| 247 | }
|
|---|
| 248 | }
|
|---|
| 249 | size = count * sizeofDatatype(datatype);
|
|---|
| 250 | memcpy(recvbuf, sendbuf, size);
|
|---|
| 251 | }
|
|---|
| 252 | return 0;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | /* Combines values from all processes and distributes the result back to all processes */
|
|---|
| 256 | /* default root is 0 */
|
|---|
| 257 | int MPI_Allreduce(void* sendbuf, void* recvbuf, int count,
|
|---|
| 258 | MPI_Datatype datatype,
|
|---|
| 259 | MPI_Op op, MPI_Comm comm) {
|
|---|
| 260 | int root = 0;
|
|---|
| 261 | MPI_Status status;
|
|---|
| 262 |
|
|---|
| 263 | $assert(comm.status == __INIT,
|
|---|
| 264 | "MPI_Allreduce() cannot be invoked without "
|
|---|
| 265 | "MPI_Init() being called before.\n");
|
|---|
| 266 | MPI_Reduce(sendbuf, recvbuf, count, datatype, op, root, comm);
|
|---|
| 267 | MPI_Bcast(recvbuf, count, datatype, root, comm);
|
|---|
| 268 | return 0;
|
|---|
| 269 | }
|
|---|
| 270 |
|
|---|
| 271 | int MPI_Barrier(MPI_Comm comm){
|
|---|
| 272 |
|
|---|
| 273 | $assert(comm.status == __INIT, "MPI_Allreduce() cannot be invoked without MPI_Init() being called before.\n");
|
|---|
| 274 | $barrier_call(comm.barrier);
|
|---|
| 275 | }
|
|---|
| 276 | #endif
|
|---|