| [0596330] | 1 | Message passing notes.
|
|---|
| 2 |
|
|---|
| 3 | Scoping goals:
|
|---|
| 4 | - it should be possible to statically determine the scope
|
|---|
| 5 | of any expression. That means the expression only references
|
|---|
| 6 | objects in that scope.
|
|---|
| 7 | - it should be possible to specify/verify the "read scope"
|
|---|
| 8 | and "write scope" of any procedure.
|
|---|
| 9 | - the goals above are complicated by pointers, which can
|
|---|
| 10 | potentially to point to any scope if not controlled.
|
|---|
| 11 | So there must be ways to specify/verify some restrictions
|
|---|
| 12 | on the scopes of pointers.
|
|---|
| 13 |
|
|---|
| 14 | Scopes:
|
|---|
| 15 |
|
|---|
| 16 | First, scopes are declared like variable declarations:
|
|---|
| 17 |
|
|---|
| 18 | { ...
|
|---|
| 19 | $scope s;
|
|---|
| 20 | ...
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | gives a name to that scope. In fact, every scope has a name
|
|---|
| 24 | automatically, this just gives the program a way to refer to it.
|
|---|
| 25 |
|
|---|
| 26 | Scopes cannot be used in expressions except as specified
|
|---|
| 27 | below. In particular they cannot be assigned
|
|---|
| 28 | or passed as arguments to functions.
|
|---|
| 29 |
|
|---|
| 30 | Pointer types:
|
|---|
| 31 |
|
|---|
| 32 | Given any object type T, there is a type "pointer-to-T".
|
|---|
| 33 | Given in addition a scope s, there is a type "pointer-to-T-in-s".
|
|---|
| 34 | The type "pointer-to-T-in-s" is a subtype of "pointer-to-T".
|
|---|
| 35 | The type "pointer-to-T" is identical to the type
|
|---|
| 36 | "pointer-to-T-in-s0", where s0 is the system (most global) scope.
|
|---|
| 37 |
|
|---|
| 38 | Furthermore, if s1 is contained in s2, "pointer-to-T-in-s1"
|
|---|
| 39 | is a subtype of "pointer-to-T-in-s2".
|
|---|
| 40 |
|
|---|
| 41 | Syntax: a variable is declared to have type
|
|---|
| 42 | "pointer-to-int-in-s", for example, as follows:
|
|---|
| 43 |
|
|---|
| 44 | int *<s> t;
|
|---|
| 45 |
|
|---|
| 46 | In general, the scope modifier is always placed after the
|
|---|
| 47 | * and surrounded by angle brackets.
|
|---|
| 48 |
|
|---|
| 49 | Address-of: the address-of operator (&) returns a pointer
|
|---|
| 50 | of the appropriate subtype using the innermost scope
|
|---|
| 51 | in which its lhs argument is declared. For example:
|
|---|
| 52 |
|
|---|
| 53 | {
|
|---|
| 54 | $scope s1;
|
|---|
| 55 | int x;
|
|---|
| 56 | double a[N];
|
|---|
| 57 | int *<s1> p = &x;
|
|---|
| 58 | double *<s1> q = &a[2];
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | is legal (it will type-check) because &x has type
|
|---|
| 62 | pointer-to-int-in-s1, since s1 is the scope in which x
|
|---|
| 63 | is declared. Etc.
|
|---|
| 64 |
|
|---|
| 65 | Pointer addition: if p has type pointer-to-T-in-s,
|
|---|
| 66 | then p+i (for an integer i) has type pointer-to-T-in-s.
|
|---|
| 67 | I.e., pointer addition cannot leave the scope.
|
|---|
| 68 | (in fact, it cannot leave the object).
|
|---|
| 69 |
|
|---|
| 70 | Pointer subtraction: pointer subtraction is
|
|---|
| 71 | defined on two pointers of the same type, where "same"
|
|---|
| 72 | includes the scope.
|
|---|
| 73 |
|
|---|
| 74 | Pointer casts: if scope s1 is contained in scope s2,
|
|---|
| 75 | an expression of type pointer-to-T-in-s1 can always
|
|---|
| 76 | be cast to pointer-to-T-in-s2, because the first
|
|---|
| 77 | is a subtype of the second. This is checked
|
|---|
| 78 | statically. The cast in the other direction is also OK,
|
|---|
| 79 | but will only be checked at runtime. If neither s1 is
|
|---|
| 80 | contained in s2 nor s2 is contained in s1, a cast from
|
|---|
| 81 | one pointer type to the other is illegal and will be
|
|---|
| 82 | reported as a syntax error statically.
|
|---|
| 83 |
|
|---|
| 84 | A type pointer-to-T1-in-s can be cast to a type
|
|---|
| 85 | pointer-to-T2-in-s according to the usual rules
|
|---|
| 86 | of C (or whatever variation is used in CIVL-C).
|
|---|
| 87 | In other words, usual casting rules apply as long
|
|---|
| 88 | as you don't change the scope.
|
|---|
| 89 |
|
|---|
| 90 | Parameterized typedefs:
|
|---|
| 91 |
|
|---|
| 92 | typedefs can be parameterized by scopes, e.g.
|
|---|
| 93 |
|
|---|
| 94 | <s> typedef struct message_struct {
|
|---|
| 95 | int size;
|
|---|
| 96 | void *<s> data;
|
|---|
| 97 | } Message;
|
|---|
| 98 |
|
|---|
| 99 | When the typedef is used, you specify the scope parameters:
|
|---|
| 100 |
|
|---|
| 101 | Message<s1> buffer[N];
|
|---|
| 102 |
|
|---|
| 103 | For each scope s, you get a different type Message<s>.
|
|---|
| 104 | There is no (subtype or other) relaltionship between
|
|---|
| 105 | Message<s1> and Message<s2> and you cannot cast from one
|
|---|
| 106 | to the other.
|
|---|
| 107 |
|
|---|
| 108 | Multiple scope can be used as parameters:
|
|---|
| 109 |
|
|---|
| 110 | <s1,s2,s3> typedef struct ...
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | Prameterized procedures:
|
|---|
| 114 |
|
|---|
| 115 | procedures can be parameterized by scopes, for example, as follows:
|
|---|
| 116 |
|
|---|
| 117 | <s> int *<s> f(int *<s> p);
|
|---|
| 118 |
|
|---|
| 119 | This means: for any scope s, f takes a pointer-to-int-in-s and
|
|---|
| 120 | returns a pointer-to-int-in-s. Multiple scopes can be used
|
|---|
| 121 | as parameters: <s,t> ....
|
|---|
| 122 |
|
|---|
| 123 | The procedure f is invoked by specifying the scope as follows
|
|---|
| 124 | ... f<s1>(...) ...
|
|---|
| 125 | Eventually we can infer the s in most cases.
|
|---|
| 126 |
|
|---|
| 127 | Procedure contracts: Procedure contracts
|
|---|
| 128 | can be optionally associated to procedure declarations
|
|---|
| 129 | or definitions in CIVL-C. There will be typical
|
|---|
| 130 | requires/ensures and other clauses in these contracts.
|
|---|
| 131 | In addition, the following two kinds of clauses
|
|---|
| 132 | can be used:
|
|---|
| 133 |
|
|---|
| 134 | $reads s1;
|
|---|
| 135 | $writes s2;
|
|---|
| 136 |
|
|---|
| 137 | where s1 and s2 are scopes. This means that any object
|
|---|
| 138 | read in any execution of f must lie in scope s1 and any
|
|---|
| 139 | object modified in any execution of f must lie is scope
|
|---|
| 140 | s2. The scopes s1 and s2 can be parameters, as in:
|
|---|
| 141 |
|
|---|
| 142 | <s1,s2> double *<s2> f(int *<s1> p)
|
|---|
| 143 | $reads s1;
|
|---|
| 144 | $writes s2;
|
|---|
| 145 | {
|
|---|
| 146 | ...
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 | Heaps:
|
|---|
| 151 |
|
|---|
| 152 | A heap is declared using the built-in type $heap:
|
|---|
| 153 | { ... $heap h1; ...}
|
|---|
| 154 |
|
|---|
| 155 | However, a heap cannot be assigned or used in expressions,
|
|---|
| 156 | with one exception: it can be the argument of address-of (&).
|
|---|
| 157 | Hence this is fine:
|
|---|
| 158 |
|
|---|
| 159 | { ...
|
|---|
| 160 | $scope s1;
|
|---|
| 161 | $heap h1;
|
|---|
| 162 | $heap *<s1> p = &h1;
|
|---|
| 163 | ...
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | Malloc and free: these built-in functions have the following
|
|---|
| 167 | signatures:
|
|---|
| 168 |
|
|---|
| 169 | <s> void *<s> $malloc($heap *<s> heap, int size)
|
|---|
| 170 | $reads s;
|
|---|
| 171 | $writes s;
|
|---|
| 172 |
|
|---|
| 173 | <s> void $free($heap *<s> heap, void *<s> ptr)
|
|---|
| 174 | $reads s;
|
|---|
| 175 | $writes s;
|
|---|
| 176 |
|
|---|
| 177 | This means:
|
|---|
| 178 | - for any scope s, $malloc takes a pointer-to-heap-in-s,
|
|---|
| 179 | and an integer size, and returns a pointer-to-void-in-s.
|
|---|
| 180 | - $free takes a pointer-to-heap-in-s and a pointer-to-void-in-s,
|
|---|
| 181 | and returns nothing.
|
|---|
| 182 | - both procedures can only read and modify s.
|
|---|
| 183 |
|
|---|
| 184 | A runtime error will be generated if the second argument
|
|---|
| 185 | to $free is not really a pointer to the heap specified in
|
|---|
| 186 | the first argument.
|
|---|
| 187 |
|
|---|
| 188 | It may be that the first argument to $free is unnecessary.
|
|---|
| 189 | However, it helps keep track of what is modified by the procedure
|
|---|
| 190 | (namely, the heap that that arguments points to). At the very
|
|---|
| 191 | least, we need to keep track of the scope of the heap.
|
|---|
| 192 | Possibly, we could get by with:
|
|---|
| 193 |
|
|---|
| 194 | <s> void $free(void *<s> ptr)
|
|---|
| 195 | $reads s;
|
|---|
| 196 | $writes s;
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 | Memcpy: this is another built-in function with signature:
|
|---|
| 200 |
|
|---|
| 201 | <s,t> void memcpy(void *<s> p, void *<t> q, size_t size)
|
|---|
| 202 | $reads t;
|
|---|
| 203 | $writes s;
|
|---|
| 204 |
|
|---|
| 205 | This copies data from q to p as usual.
|
|---|
| 206 |
|
|---|
| 207 |
|
|---|
| 208 | Examples:
|
|---|
| 209 |
|
|---|
| 210 | {
|
|---|
| 211 | $scope shared;
|
|---|
| 212 | $heap shared_heap;
|
|---|
| 213 | double *<shared> p = $malloc<shared>(&shared_heap, n*sizeof(double));
|
|---|
| 214 | double *<shared> q = $malloc<shared>(&shared_heap, n*sizeof(double));
|
|---|
| 215 |
|
|---|
| 216 | for (int i=0; i<n; i++) p[i] = (double)i;
|
|---|
| 217 | $memcpy<shared,shared>(p, q, n*sizeof(double));
|
|---|
| 218 | $free<shared>(&shared_heap, p);
|
|---|
| 219 | $free<shared>(&shared_heap, q);
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | Here is how an MPI program can be modeled with every process
|
|---|
| 223 | getting its own heap:
|
|---|
| 224 |
|
|---|
| 225 | void MPI_proc(int pid) {
|
|---|
| 226 | $scope proc_scope;
|
|---|
| 227 | $heap proc_heap;
|
|---|
| 228 | ...
|
|---|
| 229 | ... $malloc<proc_scope>(&proc_heap, size) ...
|
|---|
| 230 | ...
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | void main(int nprocs) {
|
|---|
| 234 | $proc procs[nprocs];
|
|---|
| 235 |
|
|---|
| 236 | for (int i=0; i<nprocs; i++)
|
|---|
| 237 | procs[i] = $spawn MPI_proc(i);
|
|---|
| 238 | ...
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | ------------------------------------------------------------
|
|---|
| 242 |
|
|---|
| 243 | First attempt
|
|---|
| 244 |
|
|---|
| 245 | $input int NPROCS;
|
|---|
| 246 | $assume NPROCS >= 1;
|
|---|
| 247 | $scope top;
|
|---|
| 248 | $heap mp_heap;
|
|---|
| 249 |
|
|---|
| 250 | typedef struct Message {
|
|---|
| 251 | struct Message *<top> next;
|
|---|
| 252 | struct Message *<top> prev;
|
|---|
| 253 | int tag;
|
|---|
| 254 | int size;
|
|---|
| 255 | void *<top> data;
|
|---|
| 256 | }
|
|---|
| 257 |
|
|---|
| 258 | typedef struct Comm {
|
|---|
| 259 | int nprocs;
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|
| 263 | -------------------------------------------------------------
|
|---|
| 264 |
|
|---|
| 265 | <s> typedef struct _CIVL_Message {
|
|---|
| 266 | struct _CIVL_Message *<s> next; // next message in this queue
|
|---|
| 267 | struct _CIVL_Message *<s> prev; // previous message in this queue
|
|---|
| 268 | int tag; // message tag
|
|---|
| 269 | int size; // number of elements
|
|---|
| 270 | void *<s> data; // pointer to first element
|
|---|
| 271 | } CIVL_Message;
|
|---|
| 272 |
|
|---|
| 273 | <s> typedef struct _CIVL_Comm {
|
|---|
| 274 | $heap heap;
|
|---|
| 275 | int numProcs; // number of procs in this communicator
|
|---|
| 276 | $proc *<s> procs; // array of length numProcs
|
|---|
| 277 | CIVL_Message *<s>*<s>*<s> buf_front; // oldest element of each msg queue
|
|---|
| 278 | CIVL_Message *<s>*<s>*<s> buf_back; // newest element of each msg queue
|
|---|
| 279 | } CIVL_Comm;
|
|---|
| 280 |
|
|---|
| 281 | <s> CIVL_Comm CIVL_Comm_create(int nprocs, $proc *<s> procs)
|
|---|
| 282 | { YOU CAN'T DO THIS: if you want the scope of comm to be s,
|
|---|
| 283 | you need a pointer to a heap in s...............
|
|---|
| 284 | Alternatively, declare this within the scope
|
|---|
| 285 |
|
|---|
| 286 | CIVL_Comm<s> comm;
|
|---|
| 287 |
|
|---|
| 288 | comm.numProcs = nprocs;
|
|---|
| 289 | comm.procs = $malloc(&comm.heap, nprocs*sizeof(int));
|
|---|
| 290 | for (int i=0; i<nprocs; i++) comm.procs[i] = procs[i];
|
|---|
| 291 | comm.buf_front =
|
|---|
| 292 | (CIVL_Message *<s>*<s>*<s>)
|
|---|
| 293 | $malloc(&comm.heap, nprocs*sizeof(CIVL_Message *<s>*<s>));
|
|---|
| 294 | for (int i=0; i<nprocs; i++) {
|
|---|
| 295 | comm.buf_front[i] =
|
|---|
| 296 | (CIVL_Message *<s>*<s>)
|
|---|
| 297 | $malloc(&comm.heap, nprocs*sizeof(CIVL_Message *<s>));
|
|---|
| 298 | for (int j=0; j<nprocs; j++)
|
|---|
| 299 | comm.buf_front[i][j] = NULL;
|
|---|
| 300 | }
|
|---|
| 301 | // ditto for comm.buf_back
|
|---|
| 302 | return comm;
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | <s> send(int source, void *<s> buf, int size, int dest, int tag,
|
|---|
| 306 | CIVL_Comm<s>)
|
|---|
| 307 | $reads s;
|
|---|
| 308 | $writes s;
|
|---|
| 309 | {
|
|---|
| 310 |
|
|---|
| 311 | }
|
|---|