| 1 | /* This file completes the definitions of some types and functions
|
|---|
| 2 | * for communication, which are declared in comm-common.cvh.
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | #ifndef __CIVLC_COMM__
|
|---|
| 6 | #define __CIVLC_COMM__
|
|---|
| 7 | #include <civlc.cvh>
|
|---|
| 8 | #include<comm.cvh>
|
|---|
| 9 | #include<seq.cvh>
|
|---|
| 10 | #include<pointer.cvh>
|
|---|
| 11 | /* *********************** Types *********************** */
|
|---|
| 12 |
|
|---|
| 13 | /* A datatype representing a queue of messages. All message
|
|---|
| 14 | * data is encapsulated inside this value; no external allocation
|
|---|
| 15 | * is used. Completes the declaration of this structure type in
|
|---|
| 16 | * civlc-common.h */
|
|---|
| 17 | struct _queue {
|
|---|
| 18 | int length;
|
|---|
| 19 | $message messages[];
|
|---|
| 20 | };
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 | /* A global communicator datatype which must be operated by local communicators.
|
|---|
| 24 | * This communicator type has the same meaning as the communicator type
|
|---|
| 25 | * in MPI. Completes the declaration of this type in civlc-common.h */
|
|---|
| 26 | struct _gcomm {
|
|---|
| 27 | int nprocs; // number of processes
|
|---|
| 28 | $proc procs[]; // process references
|
|---|
| 29 | _Bool isInit[]; // if the local comm has been initiated
|
|---|
| 30 | $queue buf[][]; // message buffers
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | /* A datatype representing a local communicator which is used for
|
|---|
| 34 | * operating global communicators. The local communicator type has
|
|---|
| 35 | * a handle of a global communicator. This type represents for
|
|---|
| 36 | * a set of processes which have ranks in common.
|
|---|
| 37 | * Completes the declaration of this type in civlc-common.h.
|
|---|
| 38 | */
|
|---|
| 39 | struct _comm {
|
|---|
| 40 | int place;
|
|---|
| 41 | $gcomm gcomm;
|
|---|
| 42 | };
|
|---|
| 43 |
|
|---|
| 44 | /* *********************** Functions *********************** */
|
|---|
| 45 |
|
|---|
| 46 | /* creates a new message, copying data from the specified buffer */
|
|---|
| 47 | /*@ depends_on \access(data); */
|
|---|
| 48 | $atomic_f $message $message_pack(int source, int dest, int tag,
|
|---|
| 49 | void *data, int size) {
|
|---|
| 50 | $message result;
|
|---|
| 51 |
|
|---|
| 52 | result.source = source;
|
|---|
| 53 | result.dest = dest;
|
|---|
| 54 | result.tag = tag;
|
|---|
| 55 | result.data = $bundle_pack(data, size);
|
|---|
| 56 | result.size = size;
|
|---|
| 57 | return result;
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | /* returns the message source */
|
|---|
| 61 | int $message_source($message message) {
|
|---|
| 62 | return message.source;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /* returns the message tag */
|
|---|
| 66 | int $message_tag($message message) {
|
|---|
| 67 | return message.tag;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | /* returns the message destination */
|
|---|
| 71 | int $message_dest($message message) {
|
|---|
| 72 | return message.dest;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /* returns the message size */
|
|---|
| 76 | int $message_size($message message) {
|
|---|
| 77 | return message.size;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /* transfers message data to buf, throwing exception if message
|
|---|
| 81 | * size exceeds specified size */
|
|---|
| 82 | /*@ depends_on \access(buf); */
|
|---|
| 83 | $atomic_f void $message_unpack($message message, void *buf, int size) {
|
|---|
| 84 | $bundle_unpack(message.data, buf);
|
|---|
| 85 | $assert(message.size <= size, "Message of size %d exceeds the specified size %d.", message.size, size);
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /*@ depends_on \nothing;
|
|---|
| 89 | @ assigns \nothing;
|
|---|
| 90 | @ reads \nothing;
|
|---|
| 91 | @*/
|
|---|
| 92 | $atomic_f $gcomm $gcomm_create($scope scope, int size){
|
|---|
| 93 | $gcomm gcomm=($gcomm)$malloc(scope, sizeof(struct _gcomm));
|
|---|
| 94 | $queue empty;
|
|---|
| 95 |
|
|---|
| 96 | empty.length=0;
|
|---|
| 97 | $seq_init(&empty.messages, 0, NULL);
|
|---|
| 98 | gcomm->nprocs=size;
|
|---|
| 99 | gcomm->procs=(($proc[size])$lambda(int i)$proc_null);
|
|---|
| 100 | gcomm->isInit=((_Bool[size])$lambda(int i)$false);
|
|---|
| 101 | gcomm->buf=(($queue[size][size])$lambda(int i,j) empty);
|
|---|
| 102 | return gcomm;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /*@ depends_on \access(junkMsgs), \access(gcomm);
|
|---|
| 106 | @ assigns junkMsgs, gcomm;
|
|---|
| 107 | @ reads \nothing;
|
|---|
| 108 | @*/
|
|---|
| 109 | $atomic_f int $gcomm_destroy($gcomm gcomm, void * junkMsgs){
|
|---|
| 110 | int nprocs = gcomm->nprocs;
|
|---|
| 111 | int numJunks = 0;
|
|---|
| 112 |
|
|---|
| 113 | if(junkMsgs != NULL) {
|
|---|
| 114 | for (int i = 0; i < nprocs; i++)
|
|---|
| 115 | for (int j = 0; j < nprocs; j++) {
|
|---|
| 116 | $queue queue = gcomm->buf[i][j];
|
|---|
| 117 |
|
|---|
| 118 | if (queue.length > 0)
|
|---|
| 119 | $seq_append(junkMsgs, queue.messages, queue.length);
|
|---|
| 120 | }
|
|---|
| 121 | numJunks = $seq_length(junkMsgs);
|
|---|
| 122 | }
|
|---|
| 123 | $free(gcomm);
|
|---|
| 124 | return numJunks;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /*@ depends_on \access(comm->gcomm, newcomm->gcomm); */
|
|---|
| 128 | $atomic_f void $gcomm_dup($comm comm, $comm newcomm){
|
|---|
| 129 | $copy(newcomm->gcomm, comm->gcomm);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /*@ depends_on \nothing;
|
|---|
| 133 | @ reads gcomm;
|
|---|
| 134 | @ assigns gcomm;
|
|---|
| 135 | @*/
|
|---|
| 136 | $atomic_f $comm $comm_create($scope scope, $gcomm gcomm, int place){
|
|---|
| 137 | $assert(!gcomm->isInit[place], "the place %d is already occupied in the global communicator!", place);
|
|---|
| 138 |
|
|---|
| 139 | $comm comm=($comm)$malloc(scope, sizeof(struct _comm));
|
|---|
| 140 |
|
|---|
| 141 | gcomm->procs[place]=$self;
|
|---|
| 142 | gcomm->isInit[place]=$true;
|
|---|
| 143 | comm->gcomm=gcomm;
|
|---|
| 144 | comm->place=place;
|
|---|
| 145 | return comm;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | /*@ depends_on \access(comm);
|
|---|
| 149 | @ assigns comm;
|
|---|
| 150 | @ reads \nothing;
|
|---|
| 151 | @*/
|
|---|
| 152 | $atomic_f void $comm_destroy($comm comm){
|
|---|
| 153 | $free(comm);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /* Returns the place of the local communicator. This is the same as the
|
|---|
| 157 | * place argument used to create the local communicator. */
|
|---|
| 158 | /*@ pure;
|
|---|
| 159 | @ depends_on \nothing;
|
|---|
| 160 | @*/
|
|---|
| 161 | $atomic_f int $comm_place($comm comm){
|
|---|
| 162 | return comm->place;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /*@ pure;
|
|---|
| 166 | @ depends_on \nothing;
|
|---|
| 167 | @*/
|
|---|
| 168 | $atomic_f int $comm_size($comm comm){
|
|---|
| 169 | return comm->gcomm->nprocs;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | #endif
|
|---|