1[This document is formatted with GitHub-Flavored Markdown. ]:# 2[For better viewing, including hyperlinks, read it online at ]:# 3[https://github.com/sourceryinstitute/OpenCoarrays/blob/master/CAF_API.md]:# 4 5OpenCoarrays Application Binary Interface (ABI) 6=============================================== 7 8[![Download as PDF][pdf img]](http://md2pdf.herokuapp.com/sourceryinstitute/OpenCoarrays/blob/master/CAF_ABI.pdf) 9 10Download this file as a PDF document 11[here](http://md2pdf.herokuapp.com/sourceryinstitute/OpenCoarrays/blob/master/CAF_ABI.pdf). 12 13* [To Do](#to-do) 14* [Implementation status](#implementation-status) 15* [Definitions and types](#definitions-and-types) 16* [Provided functions](#provided-functions) 17 18This document describes the OpenCoarrays application binary interface (ABI) through 19which a compiler accesses coarray functionality. As such, the target audience for 20this document is compiler developers. Most application developers need only write 21standard-conforming Fortran 2008 or 2015 and compile their code with the OpenCoarrays 22`caf` compiler wrapper without knowledge of the ABI. 23 24The actual function names in this document have a PREFIX in the source code to avoid 25name clashes. The prefix can be vendor-specific. 26 27### Warning ### 28 29*This document may be out of date.* 30 31To Do 32----- 33 34* [ ] Discuss the current draft 35* [ ] Add missing functions of the current gfortran implementation 36* [ ] Address the TODO items 37* [ ] Extend the functions to match a sensible set 38* [ ] Update the implementation status, especially for the ARMCI library 39 40Implementation status 41--------------------- 42 43The library implementation in this directory should be ABI-compatible 44with the wording below, except for some `int errmsg_len` vs. `size_t` 45changes that have not yet been implemented. 46 47Definitions and types 48--------------------- 49 50### 2.1 `caf_token_t` ### 51 52Typedef of type `void *` on the compiler side. Can be any data 53type on the library side. 54 55### 2.2 `caf_register_t` ### 56 57Type indicating which kind of coarray variable should be registered. 58 59```c 60typedef enum caf_register_t { 61 CAF_REGTYPE_COARRAY_STATIC, 62 CAF_REGTYPE_COARRAY_ALLOC, 63 CAF_REGTYPE_LOCK_STATIC, 64 CAF_REGTYPE_LOCK_ALLOC, 65 CAF_REGTYPE_CRITICAL, 66 CAF_REGTYPE_EVENT_STATIC, 67 CAF_REGTYPE_EVENT_ALLOC 68 } 69caf_register_t; 70``` 71 72__TODO__: 73 Check whether this set is complete and makes sense 74 75 76### 2.3 `caf_token_t` ### 77 78In terms of the processor, an opaque pointer, which is used to identify a 79coarray. The exact content is implementation-defined by the library. 80 81### 2.4 Stat values ### 82 83```c 84#define STAT_UNLOCKED 0 85#define STAT_LOCKED 1 86#define STAT_LOCKED_OTHER_IMAGE 2 87#define STAT_STOPPED_IMAGE 6000 88``` 89 90__TODO__: 91 Define more, allow room for lib-specific values, update for [TS18508]. 92 Do we need to take care of special vendor choices? 93 94__Note__: 95 Some values have to be such that they differ from certain other 96 values. 97 98 99Provided functions 100------------------ 101 102### 3.1 Initialization function ### 103 104```c 105void caf_init (int *argc, char ***argv) 106``` 107 108This function shall be called at startup of the program before the Fortran main 109program. It takes as arguments the command-line arguments of the program. It is 110permitted to pass to NULL pointers as argument; if non-NULL, the library is 111permitted to modify the arguments. 112 113| Argument | `intent` | description | 114| ------ | ------ | ------ | 115| `argc` | `inout` | An integer pointer with the number of arguments passed to the program or NULL. | 116| `argv` | `inout` | A pointer to an array of strings with the command-line arguments or NULL. | 117 118__Note__: 119 The function is modeled after the initialization function of the 120 Message Passing Interface (MPI) specification. Due to the way coarray 121 registration (3.5) works, it might not be the first call to the libaray. If 122 the main program is not written in Fortran and only a library uses coarrays, 123 it can happen that this function is never called. Therefore, it is 124 recommended that the library does not rely on the passed arguments and whether 125 the call has been done. 126 127__GCC__: 128 In gfortran, the function is generated when the Fortran main program is 129 compiled with -fcoarray=lib; the call happens before the run-time library 130 initialiation such that changes to the command-line arguments will be visible 131 when the command-line intrinsics are invoked. 132 133 134### 3.2 Finalization function ### 135 136```c 137void caf_finish (void) 138``` 139 140This function shall be called at the end of the program to permit a graceful 141shutdown. 142 143__Note__: 144 It is recommended to add this call at the end of the Fortran main program and 145 when invoking `STOP`. To ensure that the shutdown is also performed for 146 programs where this function is not explicitly invoked, for instance 147 non-Fortran programs or calls to the system's `exit()` function, the library can 148 use a destructor function. Note that programs can also be terminated using 149 the `ERROR STOP` statement, which is handled via its own library call. 150 151__GCC__: 152 In gfortran, this function is called at the end of the Fortran main program and 153 when before the program stops with a `STOP` command, the respective file has been 154 compiled with the `-fcoarray=lib` option. 155 156 157### 3.3 Querying the image number ### 158 159```c 160int caf_this_image (int distance) 161``` 162 163This function returns the current image number, which is a positive number. 164 165| Argument | description | 166| ------ | ------ | 167| `distance` | As specified for the `this_image` intrinsic in [TS18508]. Shall be a nonnegative number. | 168 169__Note__: 170 If the Fortran intrinsic `this_image()` is invoked without an argument, which is the only permitted form in Fortran 2008, the processor shall pass 0 as first argument. 171 172__GCC__: 173 (No special note.) 174 175 176 177### 3.4 Querying the maximal number of images ### 178 179```c 180int caf_num_images (int distance, int failed) 181``` 182 183This function returns the number of images in the current team, if distance is 0 184or the number of images in the parent team at the specified distance. If failed 185is -1, the function returns the number of all images at the specified 186distance; if it is 0, the function returns the number of non-failed images, and 187if it is 1, it returns the number of failed images. 188 189| Argument | description | 190| ------ | ------ | 191| `distance` | the distance from this image to the ancestor. Shall be positive. | 192| `failed` | shall be -1, 0, or 1 | 193 194__Note__: 195 This function follows [TS18508]. If the `num_image` intrinsic has no arguments, 196 the processor shall pass `distance = 0` and `failed = -1` to the function. 197 198__GCC__: 199 (No special note.) 200 201 202 203### 3.5 Registering coarrays ### 204 205```c 206void *caf_register (size_t size, caf_register_t type, caf_token_t *token, int *stat, char *errmsg, int errmsg_len) 207``` 208 209Allocates memory for a coarray and creates a token to identify the coarray. The 210function is called for both coarrays with `SAVE` attribute and using an explicit 211`ALLOCATE` statement. If an error occurs and `STAT` is a `NULL` pointer, the function 212shall abort with printing an error message and starting the error termination. 213If no error occurs and `STAT=` is present, it shall be set to zero. Otherwise, it 214shall be set to a positive value and, if not-@code{NULL}, @var{ERRMSG} shall be 215set to a string describing the failure. The function returns a pointer to the 216requested memory for the local image as a call to `malloc` would do. 217 218For `CAF_REGTYPE_COARRAY_STATIC` and `CAF_REGTYPE_COARRAY_ALLOC`, the passed size is 219the byte size requested. For `CAF_REGTYPE_LOCK_STATIC`, `CAF_REGTYPE_LOCK_ALLOC` 220and `CAF_REGTYPE_CRITICAL` it is the array size or one for a scalar. 221 222| Argument | description | 223| ------ | ------ | 224| `size` | For normal coarrays, the byte size of the coarray to be allocated; for lock types, the number of elements. | 225| `type` | one of the `caf_register_t` types. Possible values: `CAF_REGTYPE_COARRAY_STATIC` - for nonallocatable coarrays `CAF_REGTYPE_COARRAY_ALLOC` - for allocatable coarrays `CAF_REGTYPE_LOCK_STATIC` - for nonallocatable lock variables `CAF_REGTYPE_LOCK_ALLOC` - for allocatable lock variables `CAF_REGTYPE_CRITICAL` - for lock variables used for critical sections | 226| `token` | `intent(out)` An opaque pointer identifying the coarray. | 227| `stat` | `intent(out)` For allocatable coarrays, stores the `STAT=`; may be `NULL` | 228| `errmsg` | intent(out) When an error occurs, this will be set to an error message; may be `NULL` | 229| `errmgs_len` | the buffer size of errmsg. | 230 231__TODO__: 232 233 - [ ] Check whether the locking should be handled like that and whether one needs 234 more, e.g. for locking types in DT? 235 - [ ] Check whether one needs an additional function for to register coarrays 236 which are in static memory and used without memory allocation, i.e. just to 237 register the address. 238 - [ ] Check whether we need an explicit `SYNC ALL` at the beginning of the main 239 program or whether we can do without. 240 - [ ] Does [TS18508] require more for `SAVE` within teams or within blocks? 241 242__Note__: 243 Non-allocatable coarrays have to be registered prior use from remote images. 244 In order to guarantee this, they have to be registered before the main 245 program. This can be achieved by creating constructor functions. When using 246 `caf_register`, also non-allocatable coarrays the memory is allocated and no 247 static memory is used. 248 249 For normal coarrays, the returned pointer is used for accesses on the local 250 image. For lock types, the value shall only used for checking the allocation 251 status. Note that for critical blocks, the locking is only required on one 252 image; in the locking statement, the processor shall always pass always an 253 image index of one for critical-section lock variables (`CAF_REGTYPE_CRITICAL`). 254 255__GCC__: 256 (no special notes) 257 258__TODO__: 259 Change `errmsg_len` to `size_t` 260 261 262 263### 3.6 Deregistering coarrays ### 264 265```c 266void caf_deregister (const caf_token_t *token, int *stat, char *errmsg, size_t errmsg_len) 267``` 268 269Called to free the memory of a coarray; the processor calls this function for 270automatic and explicit deallocation. In case of an error, this function shall 271fail with an error message, unless the `STAT=` variable is not null. 272 273| Argument | `intent` | description | 274| ------ | ------ | ----- | 275| `token` | `inout` | An opaque pointer identifying the coarray. | 276| `stat` | `out` | For allocatable coarrays, stores the `STAT=`; may be `NULL` | 277| `errmsg` | `out` | When an error occurs, this will be set to an error message, may be `NULL` | 278| `errmgs_len` | | the buffersize of `errmsg`. | 279 280__Note__: 281 The implementation is permitted to set the token to `NULL`. However, it is not required to do so. 282 For nonalloatable coarrays this function is never called. If a cleanup is required, it has to be handled via the finish, stop and error stop functions, and via destructors. 283 284__GCC__: 285 (no special notes) 286 287__TODO__: 288 Change `errmsg_len` to `size_t` 289 290 291### 3.7 Sending data from a local image to a remote image ### 292 293```c 294void caf_send (caf_token_t token, size_t offset, int image_index, 295 gfc_descriptor_t *dest, caf_vector_t *dst_vector, 296 gfc_descriptor_t *src, int dst_kind, int src_kind) 297``` 298 299Called to send a scalar, an array section or whole array from a local 300to a remote image identified by the `image_index`. 301 302| Argument | description | 303| ------ | ------ | 304| `token` | `intent(in)` An opaque pointer identifying the coarray. | 305| `offset` | By which amount of bytes the actual data is shifted compared to the base address of the coarray. | 306| `image_index` | The ID of the remote image; must be a positive number. | 307| `dest` | `intent(in)` Array descriptor for the remote image for the bounds and the size. The `base_addr` shall not be accessed. | 308| `dst_vector` | `intent(in)` If not `NULL`, it contains the vector subscript of the destination array; the values are relative to the dimension triplet of the dest argument. | 309| `src` | `intent(in)` Array descriptor of the local array to be transferred to the remote image | 310| `dst_kind` | Kind of the destination argument | 311| `src_kind` | Kind of the source argument | 312 313__Note__: 314 It is permitted to have `image_id` equal the current image; the memory of the 315 send-to and the send-from might (partially) overlap in that case. The 316 implementation has to take care that it handles this case. Note that the 317 assignment of a scalar to an array is permitted. In addition, the library has 318 to handle numeric-type conversion and for strings, padding and different 319 `character` kinds. 320 321__GCC__: 322 Currently, it uses gfortran's private array descriptor. A change to [TS29113]'s 323 array descriptor is planned; when that's done, the additional kind arguments 324 will be removed. 325 Note that the kind arguments permit to distiniguish the `character` kinds and 326 `real`/`complex` kinds 10 and 16, which have the same byte size. 327 328 329__TODO__ `FOR SEND*`: 330 331 - [ ] Wait is missing 332 - [ ] Assignment to an address instead of using a token, to handle 333 `caf[i]%allocatable%alloc_array(:,:) = ...` 334 Or some other means to handle those. 335 - [ ] Image index: How to handle references to other TEAMS? 336 337__OTHER TODOs__: 338 339- [ ] 3.x TODO: Handle `GET` and remote-to-remote communication 340- [ ] 3.y TODO: Handle `ATOMIC`, `LOCK`, `CRITICAL` 341- [ ] 3.z TODO Teams and error recovery 342 343### 3.8 Getting data from a remote image ### 344 345```c 346void caf_get_desc (caf_token_t token, size_t offset, 347 int image_index, gfc_descriptor_t *src, 348 caf_vector_t *src_vector, gfc_descriptor_t *dest, 349 int src_kind, int dst_kind) 350``` 351 352Called to get an array section or whole array from a a remote, 353image identified by the `image_index`. 354 355| Argument | description | 356| ------ | ------ | 357| `token` | `intent(in)` An opaque pointer identifying the coarray. | 358| `offset` | By which amount of bytes the actual data is shifted compared to the base address of the coarray. | 359| `image_index` | The ID of the remote image; must be a positive number. | 360| `dest` | `intent(out)` Array descriptor of the local array to which the data will be transferred | 361| `src` | `intent(in)` Array descriptor for the remote image for the bounds and the size. The `base_addr` shall not be accessed. 362| `src_vector` | `intent(int)` If not `NULL`, it contains the vector subscript of the destination array; the values are relative to the dimension triplet of the dest argument. | 363| `dst_kind` | Kind of the destination argument | 364| `src_kind` | Kind of the source argument | 365 366__Note__: 367 It is permitted to have `image_id` equal the current image; the memory of the 368 send-to and the send-from might (partially) overlap in that case. The 369 implementation has to take care that it handles this case. Note that the 370 library has to handle numeric-type conversion and for strings, padding 371 and different `character` kinds. 372 373__GCC__: 374 Currently, it uses gfortran's private array descriptor. A change to [TS29113]'s 375 array descriptor is planned; when that's done, the additional kind arguments 376 will be removed. 377 Note that the kind arguments permit to distinguish the `character` kinds and 378 `real`/`complex` kinds 10 and 16, which have the same byte size. 379 380 381### 3.9 Sending data between remote images ### 382 383```c 384void caf_sendget (caf_token_t dst_token, size_t dst_offset, 385 int dst_image_index, gfc_descriptor_t *dest, 386 caf_vector_t *dst_vector, caf_token_t src_token, 387 size_t src_offset, int src_image_index, 388 gfc_descriptor_t *src, caf_vector_t *src_vector, 389 int dst_kind, int src_kind) 390``` 391 392Called to send a scalar, an array section or whole array from a remote image 393identified by the `src_image_index` to a remote image identified by the 394`dst_image_index`. 395 396| Argument | description | 397| ------ | ------ | 398| `dst_token` | `intent(in)` An opaque pointer identifying the destination coarray. | 399| `dst_offset` | By which amount of bytes the actual data is shifted compared to the base address of the destination coarray. | 400| `dst_image_index` | The ID of the destination remote image; must be a positive number. | 401| `dest` | `intent(in)` Array descriptor for the destination remote image for the bounds and the size. The `base_addr` shall not be accessed. | 402| `dst_vector` | `intent(int)` If not NULL, it contains the vector subscript of the destination array; the values are relative to the dimension triplet of the dest argument. | 403| `src_token` | `intent(in)` An opaque pointer identifying the source coarray. | 404| `src_offset` | By which amount of bytes the actual data is shifted compared to the base address of the source coarray. | 405| `src_image_index` | The ID of the source remote image; must be a positive number. | 406| `src` | `intent(in)` Array descriptor of the local array to be transferred to the remote image | 407| `src_vector` | `intent(in)` Array descriptor of the local array to be transferred to the remote image | 408| `dst_kind` | Kind of the destination argument | 409| `src_kind` | Kind of the source argument | 410 411__Note__: 412 It is permitted to have `image_id` equal the current image; the memory of the 413 send-to and the send-from might (partially) overlap in that case. The 414 implementation has to take care that it handles this case. Note that the 415 assignment of a scalar to an array is permitted. In addition, the library has 416 to handle numeric-type conversion and for strings, padding and different 417 `character` kinds. 418 419__GCC__: 420 Currently, it uses gfortran's private array descriptor. A change to [TS29113]'s 421 array descriptor is planned; when that's done, the additional kind arguments 422 will be removed. 423 Note that the kind arguments permit to distinguish the `character` kinds and 424 `real`/`complex` kinds 10 and 16, which have the same byte size. 425 426 427 428### 3.10 Barriers ### 429 430### 3.10.1 All-Image Barrier ### 431 432```c 433void caf_sync_all (int *stat, char *errmsg, size_t errmsg_len) 434``` 435 436Barrier which waits for all other images, pending asynchronous communication 437and other data transfer. 438 439| Argument | description | 440| ------ | ------ | 441| `stat` | Status variable, if `NULL`, failures are fatal. If non-null, assigned 0 on success, and a stat code (cf. 2.3) in case of an error. | 442| `errmsg` | If not NULL: Ignored unless stat is present; unmodified when successful, otherwise, an error message is copied into the variable. | 443| `errmsg_len` | Maximal length of the error string, which is not '\0' terminated. The string should be padded by blanks. | 444 445__Note__: 446 For portability, consider only using 7bit ASCII characters in the error 447 message. 448 449__GCC__: 450 Implemented in GCC 4.x using an int argument for the length. 451 Currently, `size_t` is not implemented. 452 453 454 455### 3.10.2 Barrier for Selected Images ### 456 457```c 458void sync_images (int count, int images[], int *stat, 459 char *errmsg, size_t errmsg_len) 460``` 461 462| Argument | description | 463| ------ | ------ | 464| `count` | Size of the array "images"; has value -1 for `sync images(*)` and value 0 for a zero-sized array. | 465| `image` | list of images to be synced with. | 466| `stat` | Status variable, if NULL, failures are fatal. If non-null, assigned 0 on success, and a stat code (cf. 2.3) in case of an error. | 467| `errmsg` | If not `NULL`: Ignored unless stat is present; unmodified when successful, otherwise, an error message is copied into the variable. | 468| `errmsg_len` | Maximal length of the error string, which is not `\0` terminated. The string should be padded by blanks. | 469 470__Note__: 471 For portability, consider only using 7bit ASCII characters in the error 472 message. Note that the list can contain also the ID of `this_image` or can be 473 an empty set. Example use is that image 1 syncs with all others (i.e `sync images(*)`) and the others sync only with that image (`sync image(1)`). Or 474 for point-to point communication (`sync image([left_image, right_image]`). 475 476__GCC__: 477 Implemented in GCC 4.x using an int argument for the error-string length. 478 Currently, `size_t` is not implemented. 479 480### 3.11 Error abort ### 481 482```c 483void error_stop_str (const char *string, int32_t str_len); 484void error_stop (int32_t exit_error_code) 485``` 486 487__TODO__ 488 489 - [ ] Fix this description by filling-in the missing bits 490 - [ ] `STOP` vs `ERROR STOP` handling. Currently, `STOP` calls `finalize` and then the 491 normal `STOP` while for `ERROR STOP` directly calls the library 492 - [ ] F2008 requires that one prints the raised exceptions with `STOP` and `ERROR 493 STOP`. libgfortran's `STOP` and `ERROR STOP` do so - the current implementation 494 for `ERROR STOP` does not. 495 496 497### 3.11 Locking and unlocking ### 498 499#### 3.11.1 Locking a lock variable #### 500 501```c 502void caf_lock (caf_token_t token, size_t index, int image_index, 503 int *aquired_lock, int *stat, char *errmsg, 504 int errmsg_len) 505``` 506 507Acquire a lock on the given image on a scalar locking variable or for the 508given array element for an array-valued variable. If the `acquired_lock` 509is `NULL`, the function return after having obtained the lock. If it is 510non-null, the result is is assigned the value true (one) when the lock could be 511obtained and false (zero) otherwise. Locking a lock variable which has already 512been locked by the same image is an error. 513 514| Argument | arguments | 515| ------ | ------ | 516| `token` | `intent(in)` An opaque pointer identifying the coarray. | 517| `index` | Array index; first array index is 0. For scalars, it is always 0. | 518| `image_index` | The ID of the remote image; must be a positive number. | 519| `aquired_lock` | `intent(out)` If not NULL, it returns whether lock could be obtained | 520| `stat` | `intent(out)` For allocatable coarrays, stores the `STAT=`; may be NULL | 521| `errmsg` | intent(out) When an error occurs, this will be set to an error message; may be NULL | 522| `errmsg_len` | the buffer size of errmsg. | 523 524__Note__: 525 This function is also called for critical sections; for those, the array index 526 is always zero and the image index is one. Libraries are permitted to use other 527 images for critical-section locking variables. 528 529__GCC__: 530 (no special notes) 531 532__TODO__: 533 Change `errmsg_len` to `size_t` 534 535 536#### 3.11.2 Unlocking a lock variable #### 537 538```c 539void caf_unlock (caf_token_t token, size_t index, int image_index, 540 int *stat, char *errmsg, int errmsg_len) 541``` 542 543Release a lock on the given image on a scalar locking variable or for the 544given array element for an array-valued variable. Unlocking a lock variable 545which is unlocked or has been locked by a different image is an error. 546 547| Argument | description | 548| ------ | ------ | 549| `token` | `intent(in)` An opaque pointer identifying the coarray. | 550| `index` | Array index; first array index is 0. For scalars, it is always 0. | 551| `image_index` | The ID of the remote image; must be a positive number. | 552| `stat` | `intent(out)` For allocatable coarrays, stores the `STAT=`; may be `NULL` | 553| `errmsg` | `intent(out)` When an error occurs, this will be set to an error message; may be `NULL` | 554| `errmsg_len` | the buffer size of `errmsg`. | 555 556__Note__: 557 This function is also called for critical sections; for those, the array index 558 is always zero and the image index is one. Libraries are permitted to use other 559 images for critical-section locking variables. 560 561__GCC__: 562 (no special notes) 563 564__TODO__: 565 Change `errmsg_len` to `size_t` 566 567--- 568 569[![GitHub forks](https://img.shields.io/github/forks/sourceryinstitute/OpenCoarrays.svg?style=social&label=Fork)](https://github.com/sourceryinstitute/OpenCoarrays/fork) 570[![GitHub stars](https://img.shields.io/github/stars/sourceryinstitute/OpenCoarrays.svg?style=social&label=Star)](https://github.com/sourceryinstitute/OpenCoarrays) 571[![GitHub watchers](https://img.shields.io/github/watchers/sourceryinstitute/OpenCoarrays.svg?style=social&label=Watch)](https://github.com/sourceryinstitute/OpenCoarrays) 572[![Twitter URL](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?hashtags=HPC,Fortran,PGAS&related=zbeekman,gnutools,HPCwire,HPC_Guru,hpcprogrammer,SciNetHPC,DegenerateConic,jeffdotscience,travisci&text=Stop%20programming%20w%2F%20the%20%23MPI%20docs%20in%20your%20lap%2C%20try%20Coarray%20Fortran%20w%2F%20OpenCoarrays%20%26%20GFortran!&url=https%3A//github.com/sourceryinstitute/OpenCoarrays) 573 574 575[Hyperlinks]:# 576 577[TS29113]: ftp://ftp.nag.co.uk/sc22wg5/n1901-n1950/n1942.pdf 578[TS18508]: http://isotc.iso.org/livelink/livelink?func=ll&objId=17288706&objAction=Open 579[To Do]: #to-do 580[Implementation status]: #implementation-status 581[Definitions and types]: #definitions-and-types 582[Provided functions]: #provided-functions 583[pdf img]: https://img.shields.io/badge/PDF-CAF_ABI.md-6C2DC7.svg?style=flat-square "Download as PDF" 584