#ifndef __MPI_CVL__ #define __MPI_CVL__ //TODO make a Datatype struct, which has a field "int size;" Define one of these objects for MPI_INT, MPI_DOUBLE, etc. //TODO Then provide methods like MPI provides for creating new ones. //TODO then support MPI_Type_contig(datatype, int n). #define BCAST_TAG 999 #define REDUCE_TAG 998 /* Completed definition for mpi-common.h */ struct MPI_Status{ int MPI_SOURCE; int MPI_TAG; int MPI_ERROR; int size; }; /* Definition of CIVL-MPI */ typedef enum { __UNINIT, __INIT, __FINALIZED }__MPI_Sys_status__; struct MPI_Request{ int id; }; /* Definition of CMPI_Gcomm and MPI_Comm */ typedef struct CMPI_Gcomm { $gcomm p2p; // point-to-point communication $gcomm col; // collective communication $gbarrier gbarrier; } CMPI_Gcomm; struct MPI_Comm { $comm p2p; // point-to-point communication $comm col; // collective communication $barrier barrier; __MPI_Sys_status__ status; }; /********************************** State **************************************/ /* The number of times the MPI_Wtime function has been called */ int CMPI_time_count = 0; /****************************** Helper Functions **********************************/ int sizeofDatatype(MPI_Datatype datatype) { switch (datatype) { case MPI_INT: return sizeof(int); case MPI_FLOAT: return sizeof(float); case MPI_DOUBLE: return sizeof(double); case MPI_CHAR: return sizeof(char); default: $assert(0, "Unreachable"); } } /* Helpers for MPI_Reduce and MPI_Allreduce */ void sumInt(int result[], int buf[], int real_count, int nprocs){ //init result array $atomic{ for(int i=0; i result[j]) result[j] = buf[i * real_count + j]; } } } void maxFloat(float result[], float buf[], int real_count, int nprocs){ //init result array $atom{ for(int i=0; i result[j]) result[j] = buf[i * real_count + j]; } } } void maxDouble(double result[], double buf[], int real_count, int nprocs){ //init result array $atom{ for(int i=0; i result[j]) result[j] = buf[i * real_count + j]; } } } void minInt(int result[], int buf[], int real_count, int nprocs){ //init result array $atom{ for(int i=0; istatus = __INIT; return 0; } int __MPI_Finalize(MPI_Comm *comm) { comm->status = __FINALIZED; return 0; } int MPI_Comm_size(MPI_Comm comm, int *size) { $assert(comm.status == __INIT, "MPI_Comm_size() cannot be invoked without MPI_Init() being called before.\n"); *size = $comm_size(comm.p2p); return 0; } int MPI_Comm_rank(MPI_Comm comm, int *rank) { $assert(comm.status == __INIT, "MPI_Comm_rank() cannot be invoked without MPI_Init() being called before.\n"); *rank = $comm_place(comm.p2p); return 0; } int CMPI_Send(void *buf, int count, MPI_Datatype datatype, int dest, int tag, $comm comm) { if (dest >= 0) { int size = count*sizeofDatatype(datatype); int place = $comm_place(comm); $message out = $message_pack(place, dest, tag, buf, size); $comm_enqueue(comm, out); } return 0; } int MPI_Send(void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm) { $assert(comm.status == __INIT, "MPI_Send() cannot be invoked without MPI_Init() being called before.\n"); return CMPI_Send(buf, count, datatype, dest, tag, comm.p2p); } int CMPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, $comm comm, MPI_Status *status) { if (source >= 0 || source == -1) { $message in = $comm_dequeue(comm, source, tag); int size = count*sizeofDatatype(datatype); $message_unpack(in, buf, size); if (status != MPI_STATUS_IGNORE) { status->size = $message_size(in); status->MPI_SOURCE = $message_source(in); status->MPI_TAG = $message_tag(in); status->MPI_ERROR = 0; } } return 0; } int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status) { $assert(comm.status == __INIT, "MPI_Recv() cannot be invoked without MPI_Init() being called before.\n"); return CMPI_Recv(buf, count, datatype, source, tag, comm.p2p, status); } int MPI_Get_count(MPI_Status *status, MPI_Datatype datatype, int *count) { //$assert(__my_status == __INIT, "MPI status is not INIT.\n"); *count = status->size/sizeofDatatype(datatype); return 0; } int MPI_Sendrecv(void *sendbuf, int sendcount, MPI_Datatype sendtype, int dest, int sendtag, void *recvbuf, int recvcount, MPI_Datatype recvtype, int source, int recvtag, MPI_Comm comm, MPI_Status *status) { $assert(comm.status == __INIT, "MPI_Sendrecv() cannot be invoked without MPI_Init() being called before.\n"); MPI_Send(sendbuf, sendcount, sendtype, dest, sendtag, comm); MPI_Recv(recvbuf, recvcount, recvtype, source, recvtag, comm, status); return 0; } /* Broadcasts a message from root to everyone else. * Need to use a differnt comm. */ int MPI_Bcast(void *buf, int count, MPI_Datatype datatype, int root, MPI_Comm comm) { int place = $comm_place(comm.col); $assert(comm.status == __INIT, "MPI_Bcast() cannot be invoked without MPI_Init() being called before.\n"); place = $comm_place(comm.col); if (place == root) { int nprocs = $comm_size(comm.col); for (int i=0; i