File: binding_c.md

package info (click to toggle)
mpich 5.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 251,828 kB
  • sloc: ansic: 1,323,147; cpp: 82,869; f90: 72,420; javascript: 40,763; perl: 28,296; sh: 19,399; python: 16,191; xml: 14,418; makefile: 9,468; fortran: 8,046; java: 4,635; pascal: 352; asm: 324; ruby: 176; awk: 27; lisp: 19; php: 8; sed: 4
file content (184 lines) | stat: -rw-r--r-- 6,067 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# Binding Generation

This documents how the C interface functions are generated.

## Rationale

We used to manually maintain individual source files for each mpi-level
function. For example, `src/mpi/pt2pt/send.c` for `MPI_Send`. Starting with
[PR #4846](https://github.com/pmodels/mpich/pull/4846),
we are now generating all the code with a python script and a set of
configuration files.

The interface is generated by `python maint/gen_binding_c.py`, executed
inside of `autogen.sh`. It loads the master configuration file, then loads, per
category, custom configuration files. One file is generated for each configured
function in the `src/binding/c` folder. Additionally it generates the
following three files:

- `src/binding/c/Makefile.mk`
- `src/binding/c/errnames.txt`
- `src/include/mpir_impl.h`

## Master API config file

The master configuration file, `maint/mpi_standards_api.txt` is a direct
transcription from the mpi-standard repository. It contains entries as
follows:

```
MPI_Send:
    buf: BUFFER, constant=True, [initial address of send buffer]
    count: POLYXFER_NUM_ELEM_NNI, [number of elements in send buffer]
    datatype: DATATYPE, [datatype of each send buffer element]
    dest: RANK, [rank of destination]
    tag: TAG, [message tag]
    comm: COMMUNICATOR
```

Each entry will have a function name and a list of parameters. Each parameter
starts with the variable name, followed by a "kind", then a set of optional 
attributes, and finally a description inside the square brackets.

## Language specific kind mapping

The mapping from parameter "kind" to language specific type is listed in
`maint/api_mapping.txt` with a simple format:

```
LIS_KIND_MAP:
    ACCESS_MODE: integer
    ...
BASE_C_KIND_MAP:
    ACCESS_MODE: int
    ...
SMALL_C_KIND_MAP:
    .base: BASE_C_KIND_MAP
    POLYDISPLACEMENT: int
    ...
BIG_C_KIND_MAP:
    .base: BASE_C_KIND_MAP
    POLYDISPLACEMENT: MPI_Aint
...
```

Each entry contains a map type heading followed by a set of `kind: type`
listings. `BASE_C_KIND_MAP` is only used to avoid duplication between `SMALL`
and `BIG` versions.

Typically we should use the exact mapping from the mpi standard, meaning
`maint/api_mapping.txt` should be kept in-sync with the upstream branch. But
sometimes, e.g. `MPIX` functions, we may need to define our own kind mappings.
We do that in `src/binding/custom_mapping.txt`.


## Custom API config files

For any function to be generated, it has to be listed in one of the custom
configuration files in the `src/binding/c` folder, such as
`src/binding/c/pt2pt_api.txt`. The filename has to fit the `dir_api.txt`
format. The root name designates the subfolder where the code files will be
generated in. For example, all functions listed in
`src/binding/c/pt2pt_api.txt` will get generated in `src/binding/c/pt2pt/`.

The custom configuration file follows the same format as the master
configuration file plus a few additions. Their formatting as follows:

```
MPI_Example:
    .desc: a short description
/*
    Notes:
    Here we can supply custom man page notes explaining the semantics of the
    function.
*/
/*
    Multiple such "comment" blocks are allowed. The first block adds notes to
    the top of the man page (after the generated parameter info). The rest of
    the blocks are concatenated a follows the generated notes.

    Typical functions will all include .ThreadSafe and .Fortran notes, unless
    one or both of them are excluded by .skip directives.

    .N ExampleNote
    Any text is allowed. Refer to the sowing package manual.
*/
{ -- error_check -- list_of, parameters, that, are, checked
    /* custom error checking code when automatically generated validation is
     * insufficient */
    if (list_of < 0) {
        mpi_errno = ...;
        goto fn_fail;
    }
}
{ -- early_return --
    if (!param1) {
        goto fn_exit;
    }
}
{
    /* without the -- mark, this is the "body of routine" */
    mpi_errno = my_custom_code(...);
    if (mpi_errno) {
        goto fn_fail;
    }
}
```

The above example customizes `MPI_Example` with the following:
- A short description
- Extra man page notes
- Custom error checking code
- `early_return` code
- Custom body of routine

The customizations are place holders to allow flexibility when dealing with
arbitrary complex code that is too complex to deal with within the python
script. However, for most functions, most of the custom parts can be omitted.
For example, the following:

```
MPI_Sendrecv:
    .desc: Sends and receives a message
```

is sufficient to generate `src/binding/c/pt2pt/sendrecv.c` with:
- A default man page
- Default validations
- Default body of routine which simply calls `MPIR_Sendrecv_impl`

When in doubt, double check the generated output and add customizations when
necessary.

For `MPIX` functions, we'll need to supply our own parameter information since
they will be missing from the standard master configuration file. Simply list
the parameters in the custom files. The parameters all start with 

```
param_name: ...
```

while the directives all start with a `.`,

```
.desc: ...
```

Supported directives include:
- `.desc` - A short description
- `.skip` - A list of items that should skip auto-generations:
    - `ThreadSafe, Fortran` - These are standard notes
    - `validate-INDEX` - The `INDEX` kind should be skipped
    - `validate-ANY` - Skip all validations
    - `initcheck` - The function should skip `MPIR_ERRTEST_INITIALIZED_ORDIE`
    - `global_cs` - The function should skip `MPID_THREAD_CS_ENTER/EXIT`

- `.extra` - A list extra items that should be generated:
    - `SignalSafe, NotThreadSafe, collops` - Extra notes 
- `.error` - A list extra error code that should listed in the document.
             The error codes associated with generated validation code are
             always documented automatically.

It is easy to extend with extra special directives. Simply specify it in the
configuration files with a `.name`, then implement it in
`maint/local_python/binding_c.py`. The difficult part is to keep it documented.