File: psm2.h

package info (click to toggle)
libpsm2 11.2.185-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,544 kB
  • sloc: ansic: 36,650; makefile: 660; sh: 216; asm: 59
file content (1767 lines) | stat: -rw-r--r-- 77,766 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
/*

  This file is provided under a dual BSD/GPLv2 license.  When using or
  redistributing this file, you may do so under either license.

  GPL LICENSE SUMMARY

  Copyright(c) 2017 Intel Corporation.

  This program is free software; you can redistribute it and/or modify
  it under the terms of version 2 of the GNU General Public License as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  Contact Information:
  Intel Corporation, www.intel.com

  BSD LICENSE

  Copyright(c) 2017 Intel Corporation.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the
      distribution.
    * Neither the name of Intel Corporation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#ifndef PSM2_H
#define PSM2_H

#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

/*!
 * @file psm2.h
 * @page psm2_main PSM2 API
 *
 * @brief PSM2 OPA Messaging Library
 *
 * The PSM2 OPA Messaging API, or PSM2 API, is Intel's low-level
 * user-level communications interface for the OPA family of products.
 * PSM2 users are enabled with mechanisms necessary to implement higher level
 * communications interfaces in parallel environments.
 *
 * Since PSM2 targets clusters of multicore processors, it internally implements
 * two levels of communication: intra-node shared memory communication and
 * inter-node OPA communication.  Both of these levels are encapsulated
 * below the interface and the user is free to assume that intra-node and
 * inter-node communication is transparently handled within PSM.
 *
 * @section compat Compatibility
 *
 * PSM2 can coexist with other QLogic/Pathscale software distributions, such as
 * OpenIB/OpenFabrics, which allows applications to simultaneously target
 * PSM-based and non PSM-based applications on a single node without changing
 * any system-level configuration.  However, PSM2 does not support running
 * PSM-based and non PSM-based communication within the same user process.
 *
 * Except where noted, PSM2 does not assume an SPMD (single program, multiple
 * data) parallel model and extends to MPMD (multiple program, multiple data)
 * environments in specific areas. However, PSM2 assumes the runtime environment
 * to be homogeneous on all nodes in bit width (32-bit or 64-bit) and endianness
 * (little or big) and will fail at startup if any of these assumptions do not
 * hold.  For homogeneous systems PSM2 can run either in 32-bit or 64-bit
 * environments.  Even though both environments should expect similar
 * performance from the API, PSM2 has chosen to favor 64-bit environments in
 * some minor areas.
 *
 * @section ep_model Endpoint Communication Model
 *
 * PSM2 follows an endpoint communication model where an endpoint is defined as
 * an object (or handle) instantiated to support sending and receiving messages
 * to other endpoints.  In order to prevent PSM2 from being tied to a particular
 * parallel model (such as SPMD), control over the parallel layout of endpoints
 * is retained by the user.  Opening endpoints (@ref psm2_ep_open) and
 * connecting endpoints to enable communication (@ref psm2_ep_connect) are two
 * decoupled mechanisms.  Users that do not dynamically change the number of
 * endpoints beyond parallel startup will probably lump both mechanisms
 * together at startup.  Users that wish to manipulate the location and number
 * of endpoints at runtime can do so by explicitly connecting sets or subsets
 * of endpoints.
 *
 * As a side effect, this greater flexibility forces the user to cope with a
 * two-stage initialization process.  In the first stage of opening an endpoint
 * (@ref psm2_ep_open), a user obtains an opaque handle to the endpoint and a
 * globally distributable endpoint identifier (@ref psm2_epid_t).  Prior to the
 * second stage of connecting endpoints (@ref psm2_ep_connect), a user must
 * distribute all relevent endpoint identifiers through an out-of-band
 * mechanism.  Once the endpoint identifiers are successfully distributed to
 * all processes that wish to communicate, the user
 * connects all endpoint identifiers to the locally opened endpoint
 * (@ref psm2_ep_connect).  In connecting the endpoints, the user obtains an
 * opaque endpoint address (@ref psm2_epaddr_t), which is required for all PSM
 * communication primitives.
 *
 *
 * @section components PSM2 Components
 *
 * PSM2 exposes a single endpoint initialization model, but enables various
 * levels of communication functionality and semantics through @e components.
 * The first major component available in PSM2 is PSM2 Matched Queues
 * (@ref psm2_mq), and the second is PSM2 Active Message (@ref psm2_am).
 *
 * Matched Queues (MQ) present a queue-based communication model with the
 * distinction that queue consumers use a 3-tuple of metadata to match incoming
 * messages against a list of preposted receive buffers.  The MQ semantics are
 * sufficiently akin to MPI to cover the entire MPI-1.2 standard.
 *
 * The Active Message (AM) component presents a request/reply model where
 * the arrival of a message triggers the execution of consumer-provided
 * handler code. This can be used to implement many one-sided and two-sided
 * communications paradigms.
 *
 * With future releases of the PSM2 interface, more components will
 * be exposed to accommodate users that implement parallel communication
 * models that deviate from the Matched Queue semantics.  For example, PSM
 * plans to expose a connection management component to make it easier to
 * handle endpoint management for clients without their own connection
 * managers.
 *
 *
 * @section progress PSM2 Communication Progress Guarantees
 *
 * PSM2 internally ensures progress of both intra-node and inter-node messages,
 * but not autonomously.  This means that while performance does not depend
 * greatly on how the user decides to schedule communication progress,
 * explicit progress calls are required for correctness.  The @ref psm2_poll
 * function is available to make progress over all PSM2 components in a generic
 * manner.  For more information on making progress over many communication
 * operations in the MQ component, see the @ref mq_progress documentation.
 *
 *
 * @section completion PSM2 Completion semantics
 *
 * PSM2 implements the MQ component, which documents its own
 * message completion semantics (@ref mq_completion).
 *
 *
 * @section error_handling PSM2 Error handling
 *
 * PSM2 exposes a list of user and runtime errors enumerated in @ref psm2_error.
 * While most errors are fatal in that the user is not expected to be able to
 * recover from them, PSM2 still allows some level of control.  By
 * default, PSM2 returns all errors to the user but as a convenience, allows
 * users to either defer errors internally to PSM2 or to have PSM2 return all
 * errors to the user (callers to PSM2 functions).  PSM2 attempts to deallocate
 * its resources as a best effort, but exits are always non-collective with
 * respect to endpoints opened in other processes.  The user is expected to be
 * able to handle non-collective exits from any endpoint and in turn cleanly
 * and independently terminate the parallel environment.  Local error handling
 * can be handled in three modes:
 *
 * Errors and error handling can be individually registered either globally or
 * per-endpoint:
 * @li @b Per-endpoint error handling captures errors for functions where the
 * error scoping is determined to be over an endpoint.  This includes all
 * communication functions that include an EP or MQ handle as the first
 * parameter.
 *
 * @li @b Global error handling captures errors for functions where a
 * particular endpoint cannot be identified or for @ref psm2_ep_open, where
 * errors (if any) occur before the endpoint is opened.
 *
 * Error handling is controlled by registering error handlers (@ref
 * psm2_error_register_handler).  The global error handler can
 * be set at any time (even before @ref psm2_init), whereas a per-endpoint error
 * handler can be set as soon as a new endpoint is successfully created.  If a
 * per-endpoint handle is not registered, the per-endpoint handler inherits
 * from the global error handler at time of open.
 *
 * PSM2 predefines two different mechanisms for handling errors:
 *
 * @li PSM-internal error handler (@ref PSM2_ERRHANDLER_PSM_HANDLER)
 * @li No-op PSM2 error handler where errors are returned
 *     (@ref PSM2_ERRHANDLER_NO_HANDLER)
 *
 * The default PSM-internal error handler effectively frees the user from
 * explicitly handling the return values of ever PSM2 function but may not
 * return to the user in a function determined to have caused a fatal error.
 *
 * The No-op PSM2 error handler bypasses all error handling functionality and
 * always returns the error to the user.  The user can then use @ref
 * psm2_error_get_string to obtain a generic string from an error code (compared
 * to a more detailed error message available through registering of error
 * handlers).
 *
 * For even more control, users can register their own error handlers to have
 * access to more precise error strings and selectively control when an when
 * not to return to callers of PSM2 functions.  All error handlers shown defer
 * error handling to PSM2 for errors that are not recognized using @ref
 * psm2_error_defer.  Deferring an error from a custom error handler is
 * equivalent to relying on the default error handler.
 *
 * @section env_var Environment variables
 *
 * Some PSM2 behaviour can be controlled via environment variables.
 *
 * @li @b PSM2_DEVICES. PSM2 implements three devices for communication which
 * are, in order,  @c self, @c shm and @c hfi.  For PSM2 jobs that do not
 * require shared-memory communications, @b PSM2_DEVICES can be specified as @c
 * self, @c hfi.  Similarly, for shared-memory only jobs, the @c hfi device
 * can be disabled.  It is up to the user to ensure that the endpoint ids
 * passed in @ref psm2_ep_connect do not require a device that has been
 * explicitly disabled by the user.  In some instances, enabling only the
 * devices that are required may improve performance.
 *
 * @li @b PSM2_TRACEMASK. Depending on the value of the tracemask, various parts
 * of PSM2 will output debugging information.  With a default value of @c 0x1,
 * informative messages will be printed (this value should be considered a
 * minimum).  At @c 0x101, startup and finalization messages are added to the
 * output.  At @c 0x1c3, every communication event is logged and should hence
 * be used for extreme debugging only.
 *
 * @li @b PSM2_MULTI_EP. By default, only one PSM2 endpoint may be opened in
 * a process. With the correct setting of this environment variable, a process
 * may open more than one PSM2 endpoint. In order to enable multiple endpoint
 * per process support, the value of this environment variable should be set
 * to "1" or "yes".
 *
 * @section thr_sfty Thread safety and reentrancy
 * Unless specifically noted otherwise, all PSM2 functions should not be considered
 * to be thread safe or reentrant.
 */

/** @brief Local endpoint handle (opaque)
 *  @ingroup ep
 *
 * Handle returned to the user when a new local endpoint is created.  The
 * handle is a local handle to be used in all communication functions and is
 * not intended to globally identify the opened endpoint in any way.
 *
 * All open endpoint handles can be globally identified using the endpoint id
 * integral type (@ref psm2_epid_t) and all communication must use an endpoint
 * address (@ref psm2_epaddr_t) that can be obtained by connecting a local
 * endpoint to one or more endpoint identifiers.
 *
 * @remark The local endpoint handle is opaque to the user.  */
typedef struct psm2_ep *psm2_ep_t;

/** @brief MQ handle (opaque)
 * @ingroup mq
 *
 * Handle returned to the user when a new Matched queue is created (@ref
 * psm2_mq_init).  */
typedef struct psm2_mq *psm2_mq_t;

/*! @defgroup init PSM2 Initialization and Maintenance
 * @{
 */
#define PSM2_VERNO       0x0202	/*!< Header-defined Version number */
#define PSM2_VERNO_MAJOR 0x02	/*!< Header-defined Major Version Number */
#define PSM2_VERNO_MINOR 0x02	/*!< Header-defined Minor Version Number */
#define PSM2_VERNO_COMPAT_MAJOR 0x01    /*!<Minimum PSM1 Major Version Number for Compatibility */

/*! @brief PSM2 Error type
 */
enum psm2_error {
	/*! Interface-wide "ok", guaranteed to be 0. */
	PSM2_OK = 0,
	/*! No events progressed on @ref psm2_poll (not fatal) */
	PSM2_OK_NO_PROGRESS = 1,
	/*! Error in a function parameter */
	PSM2_PARAM_ERR = 3,
	/*! PSM2 ran out of memory */
	PSM2_NO_MEMORY = 4,
	/*! PSM2 has not been initialized by @ref psm2_init */
	PSM2_INIT_NOT_INIT = 5,
	/*! API version passed in @ref psm2_init is incompatible */
	PSM2_INIT_BAD_API_VERSION = 6,
	/*! PSM2 Could not set affinity */
	PSM2_NO_AFFINITY = 7,
	/*! PSM2 Unresolved internal error */
	PSM2_INTERNAL_ERR = 8,
	/*! PSM2 could not set up shared memory segment */
	PSM2_SHMEM_SEGMENT_ERR = 9,
	/*! PSM2 option is a read-only option */
	PSM2_OPT_READONLY = 10,
	/*! PSM2 operation timed out */
	PSM2_TIMEOUT = 11,
	/*! Too many endpoints */
	PSM2_TOO_MANY_ENDPOINTS = 12,

	/*! PSM2 is finalized */
	PSM2_IS_FINALIZED = 13,

	/*! Endpoint was closed */
	PSM2_EP_WAS_CLOSED = 20,
	/*! PSM2 Could not find an OPA Unit */
	PSM2_EP_NO_DEVICE = 21,
	/*! User passed a bad unit or port number */
	PSM2_EP_UNIT_NOT_FOUND = 22,
	/*! Failure in initializing endpoint */
	PSM2_EP_DEVICE_FAILURE = 23,
	/*! Error closing the endpoing error */
	PSM2_EP_CLOSE_TIMEOUT = 24,
	/*! No free ports could be obtained */
	PSM2_EP_NO_PORTS_AVAIL = 25,
	/*! Could not detect network connectivity */
	PSM2_EP_NO_NETWORK = 26,
	/*! Invalid Unique job-wide UUID Key */
	PSM2_EP_INVALID_UUID_KEY = 27,
	/*! Internal out of resources */
	PSM2_EP_NO_RESOURCES = 28,

	/*! Endpoint connect status unknown (because of other failures or if
	 * connect attempt timed out) */
	PSM2_EPID_UNKNOWN = 40,
	/*! Endpoint could not be reached by any PSM2 component */
	PSM2_EPID_UNREACHABLE = 41,
	/*! At least one of the connecting nodes was incompatible in endianess */
	PSM2_EPID_INVALID_NODE = 43,
	/*! At least one of the connecting nodes provided an invalid MTU */
	PSM2_EPID_INVALID_MTU = 44,
	/*! At least one of the connecting nodes provided a bad key */
	PSM2_EPID_INVALID_UUID_KEY = 45,
	/*! At least one of the connecting nodes is running an incompatible
	 * PSM2 protocol version */
	PSM2_EPID_INVALID_VERSION = 46,
	/*! At least one node provided garbled information */
	PSM2_EPID_INVALID_CONNECT = 47,
	/*! EPID was already connected */
	PSM2_EPID_ALREADY_CONNECTED = 48,
	/*! EPID is duplicated, network connectivity problem */
	PSM2_EPID_NETWORK_ERROR = 49,
	/*! EPID incompatible partition keys */
	PSM2_EPID_INVALID_PKEY = 50,
	/*! Unable to resolve path for endpoint */
	PSM2_EPID_PATH_RESOLUTION = 51,

	/*! MQ Non-blocking request is incomplete */
	PSM2_MQ_NO_COMPLETIONS = 60,
	/*! MQ Message has been truncated at the receiver */
	PSM2_MQ_TRUNCATION = 61,

	/*! AM reply error */
	PSM2_AM_INVALID_REPLY = 70,

	/*! Info query invalid query error */
	PSM2_IQ_INVALID_QUERY = 71,

    /*! Reserved Value to indicate highest ENUM value */
    PSM2_ERROR_LAST = 80
};

/*! Backwards header compatibility for a confusing error return name */
#define PSM2_MQ_INCOMPLETE PSM2_MQ_NO_COMPLETIONS

/*! @see psm2_error */
typedef enum psm2_error psm2_error_t;

/*! @brief PSM2 Error type
 */
enum psm2_component {
	/*! PSM2 core library */
	PSM2_COMPONENT_CORE = 0,
	/*! MQ component */
	PSM2_COMPONENT_MQ = 1,
	/*! AM component */
	PSM2_COMPONENT_AM = 2,
	/*! IB component */
	PSM2_COMPONENT_IB = 3
};

/*! @see psm2_component */
typedef enum psm2_component psm2_component_t;

/*! @brief PSM2 Path resolution mechanism
 */
enum psm2_path_res {
	/*! PSM2 no path resolution */
	PSM2_PATH_RES_NONE = 0,
	/*! Use OFED Plus for path resolution */
	PSM2_PATH_RES_OPP = 1,
	/*! Use OFED UMAD for path resolution */
	PSM2_PATH_RES_UMAD = 2
};

/*! @see psm2_path_resolution */
typedef enum psm2_path_res psm2_path_res_t;

/** @brief Initialize PSM2 interface
 *
 * Call to initialize the PSM2 library for a desired API revision number.
 *
 * @param[in,out] api_verno_major As input a pointer to an integer that holds
 *                                @ref PSM2_VERNO_MAJOR. As output, the pointer
 *                                is updated with the major revision number of
 *                                the loaded library.
 * @param[in,out] api_verno_minor As input, a pointer to an integer that holds
 *                                @ref PSM2_VERNO_MINOR.  As output, the pointer
 *                                is updated with the minor revision number of
 *                                the loaded library.
 *
 * @pre The user has not called any other PSM2 library call except @ref
 *      psm2_error_register_handler to register a global error handler.
 *
 * @post Depending on the environment variable @ref PSM2_MULTI_EP being set and
 * 	 its contents, support for opening multiple endpoints is either enabled
 * 	 or disabled.
 *
 * @warning PSM2 initialization is a precondition for all functions used in the
 *          PSM2 library.
 *
 * @returns PSM2_OK The PSM2 interface could be opened and the desired API
 *                 revision can be provided.
 * @returns PSM2_INIT_BAD_API_VERSION The PSM2 library cannot compatibility for
 *                                   the desired API version.
 *
 * @code{.c}
   	// In this example, we want to handle our own errors before doing init,
   	// since we don't want a fatal error if OPA is not found.
   	// Note that @ref psm2_error_register_handler
   	// (and @ref psm2_uuid_generate and @ref psm2_get_capability_mask)
   	// are the only function that can be called before @ref psm2_init

   	int try_to_initialize_psm() {
   	    int verno_major = PSM2_VERNO_MAJOR;
   	    int verno_minor = PSM2_VERNO_MINOR;

   	    int err = psm2_error_register_handler(NULL,  // Global handler
   	                                 PSM2_ERRHANDLER_NO_HANDLER); // return errors
   	    if (err) {
   	       fprintf(stderr, "Couldn't register global handler: %s\n",
   	   	          psm2_error_get_string(err));
   	       return -1;
   	    }

   	    err = psm2_init(&verno_major, &verno_minor);
   	    if (err || verno_major > PSM2_VERNO_MAJOR) {
   	       if (err)
   	         fprintf(stderr, "PSM2 initialization failure: %s\n",
   	                 psm2_error_get_string(err));
   	     else
   	         fprintf(stderr, "PSM2 loaded an unexpected/unsupported "
   	                         "version (%d.%d)\n", verno_major, verno_minor);
   	     return -1;
   	    }

   	    // We were able to initialize PSM2 but will defer all further error
   	    // handling since most of the errors beyond this point will be fatal.
   	    int err = psm2_error_register_handler(NULL,  // Global handler
   	                                          PSM2_ERRHANDLER_PSM_HANDLER);
   	    if (err) {
   	       fprintf(stderr, "Couldn't register global errhandler: %s\n",
   	   	          psm2_error_get_string(err));
   	       return -1;
   	    }
   	    return 1;
   	}
   @endcode
 */
psm2_error_t psm2_init(int *api_verno_major, int *api_verno_minor);

/*! @brief PSM2 capabilities definitions
 *
 * Each capability is defined as a separate bit,
 * i.e. next capabilities must be defined as
 * consecutive bits : 0x2, 0x4 ... and so on.
 */
#define PSM2_MULTI_EP_CAP 0x1	/* Multiple Endpoints capability */
#define PSM2_LIB_REFCOUNT_CAP 0x2	/* Library finalization is managed with reference count */

/** @brief PSM2 capabilities provider
 *
 * @param[in] req_cap_mask Requested capabilities are given as bit field.
 *
 * @returns internal capabilities bit field ANDed with a requested bit mask */
uint64_t psm2_get_capability_mask(uint64_t req_cap_mask);

/** @brief Finalize PSM2 interface
 *
 * Single call to finalize PSM2 and close all unclosed endpoints
 *
 * @post The user guarantees not to make any further PSM2 calls, including @ref
 * psm2_init.
 *
 * @returns PSM2_OK Always returns @c PSM2_OK */
psm2_error_t psm2_finalize(void);

/** @brief Error handling opaque token
 *
 * A token is required for users that register their own handlers and wish to
 * defer further error handling to PSM. */
typedef struct psm2_error_token *psm2_error_token_t;

/** @brief Error handling function
 *
 * Users can handle errors explicitly instead of relying on PSM's own error
 * handler.  There is one global error handler and error handlers that can be
 * individually set for each opened endpoint.  By default, endpoints will
 * inherit the global handler registered at the time of open.
 *
 * @param[in] ep Handle associated to the endpoint over which the error occurred
 *               or @c NULL if the error is being handled by the global error
 *               handler.
 * @param[in] error PSM2 error identifier
 * @param[in] error_string A descriptive error string of maximum length @ref
 *                         PSM2_ERRSTRING_MAXLEN.
 * @param[in] token Opaque PSM2 token associated with the particular event that
 *		    generated the error.  The token can be used to extract the
 *		    error string and can be passed to @ref psm2_error_defer to
 *		    defer any remaining or unhandled error handling to PSM.
 *
 * @post If the error handler returns, the error returned is propagated to the
 *       caller.  */
typedef psm2_error_t(*psm2_ep_errhandler_t) (psm2_ep_t ep,
					   const psm2_error_t error,
					   const char *error_string,
					   psm2_error_token_t token);

#define PSM2_ERRHANDLER_DEFAULT	((psm2_ep_errhandler_t)-1)
/**< Obsolete names, only here for backwards compatibility */
#define PSM2_ERRHANDLER_NOP	((psm2_ep_errhandler_t)-2)
/**< Obsolete names, only here for backwards compatibility */

#define PSM2_ERRHANDLER_PSM_HANDLER  ((psm2_ep_errhandler_t)-1)
/**< PSM2 error handler as explained in @ref error_handling */

#define PSM2_ERRHANDLER_NO_HANDLER   ((psm2_ep_errhandler_t)-2)
/**< Bypasses the default PSM2 error handler and returns all errors to the user
 * (this is the default) */

#define PSM2_ERRSTRING_MAXLEN	512 /**< Maximum error string length. */

/** @brief PSM2 error handler registration
 *
 * Function to register error handlers on a global basis and on a per-endpoint
 * basis.  PSM2_ERRHANDLER_PSM_HANDLER and PSM2_ERRHANDLER_NO_HANDLER are special
 * pre-defined handlers to respectively enable use of the default PSM-internal
 * handler or the no-handler that disables registered error handling and
 * returns all errors to the caller (both are documented in @ref
 * error_handling).
 *
 * @param[in] ep Handle of the endpoint over which the error handler should be
 *               registered.  With ep set to @c NULL, the behavior of the
 *               global error handler can be controlled.
 * @param[in] errhandler Handler to register.  Can be a user-specific error
 *                       handling function or PSM2_ERRHANDLER_PSM_HANDLER or
 *                       PSM2_ERRHANDLER_NO_HANDLER.
 *
 * @remark When ep is set to @c NULL, this is the only function that can be
 * called before @ref psm2_init
 */
psm2_error_t
psm2_error_register_handler(psm2_ep_t ep, const psm2_ep_errhandler_t errhandler);

/** @brief PSM2 deferred error handler
 *
 * Function to handle fatal PSM2 errors if no error handler is installed or if
 * the user wishes to defer further error handling to PSM.  Depending on the
 * type of error, PSM2 may or may not return from the function call.
 *
 * @param[in] err_token Error token initially passed to error handler
 *
 * @pre The user is calling into the function because it has decided that PSM
 *      should handle an error case.
 *
 * @post The function may or may not return depending on the error
 */
psm2_error_t psm2_error_defer(psm2_error_token_t err_token);

/** @brief Get generic error string from error
 *
 * Function to return the default error string associated to a PSM2 error.
 *
 * While a more detailed and precise error string is usually available within
 * error handlers, this function is available to obtain an error string out of
 * an error handler context or when a no-op error handler is registered.
 *
 * @param[in] error PSM2 error
 */
const char *psm2_error_get_string(psm2_error_t error);

/** @brief Option key/pair structure
 *
 * Currently only used in MQ.
 */
struct psm2_optkey {
	uint32_t key;	/**< Option key */
	void *value;	/**< Option value */
};

/*! @} */

/*! @defgroup ep PSM2 Device Endpoint Management
 * @{
 */

/** @brief Endpoint ID
 *
 * Integral type of size 8 bytes that can be used by the user to globally
 * identify a successfully opened endpoint.  Although the contents of the
 * endpoint id integral type remains opaque to the user, unique network id and
 * OPA port number can be extracted using @ref psm2_epid_nid and @ref
 * psm2_epid_context.
 */
typedef uint64_t psm2_epid_t;

/** @brief Endpoint Address (opaque)
 *
 * Remote endpoint addresses are created when the user binds an endpoint ID
 * to a particular endpoint handle using @ref psm2_ep_connect.  A given endpoint
 * address is only guaranteed to be valid over a single endpoint.
 */
typedef struct psm2_epaddr *psm2_epaddr_t;

/** @brief PSM2 Unique UID
 *
 * PSM2 type equivalent to the DCE-1 uuid_t, used to uniquely identify an
 * endpoint within a particular job.  Since PSM2 does not participate in job
 * allocation and management, users are expected to generate a unique ID to
 * associate endpoints to a particular parallel or collective job.
 * @see psm2_uuid_generate
 */
typedef uint8_t psm2_uuid_t[16];

/** @brief Get Endpoint identifier's Unique Network ID */
uint64_t psm2_epid_nid(psm2_epid_t epid);

/** @brief Get Endpoint identifier's OPA context number */
uint64_t psm2_epid_context(psm2_epid_t epid);

/** @brief Get Endpoint identifier's OPA port (deprecated, use
 * @ref psm2_epid_context instead) */
uint64_t psm2_epid_port(psm2_epid_t epid);

/** @brief List the number of available OPA units
 *
 * Function used to determine the number of locally available OPA units.
 * For @c N units, valid unit numbers in @ref psm2_ep_open are @c 0 to @c N-1.
 *
 * @returns PSM2_OK unless the user has not called @ref psm2_init
 */
psm2_error_t psm2_ep_num_devunits(uint32_t *num_units);

/** @brief Utility to generate UUIDs for @ref psm2_ep_open
 *
 * This function is available as a utility for generating unique job-wide ids.
 * See discussion in @ref psm2_ep_open for further information.
 *
 * @remark This function does not require PSM2 to be initialized.
 */
void psm2_uuid_generate(psm2_uuid_t uuid_out);

/* Affinity modes for the affinity member of struct psm2_ep_open_opts */
#define PSM2_EP_OPEN_AFFINITY_SKIP     0	/**< Disable setting affinity */
#define PSM2_EP_OPEN_AFFINITY_SET      1	/**< Enable setting affinity unless
					  already set */
#define PSM2_EP_OPEN_AFFINITY_FORCE    2	/**< Enable setting affinity regardless
					  of current affinity setting */

/* Default values for some constants */
#define PSM2_EP_OPEN_PKEY_DEFAULT    0xffffffffffffffffULL
				    /**< Default protection key */

/** @brief Endpoint Open Options
 *
 * These options are available for opening a PSM2 endpoint.  Each is
 * individually documented and setting each option to -1 or passing NULL as the
 * options parameter in @ref psm2_ep_open instructs PSM2 to use
 * implementation-defined defaults.
 *
 * Each option is documented in @ref psm2_ep_open
 */
struct psm2_ep_open_opts {
	int64_t timeout;	/**< timeout in nanoseconds to open device */
	int unit;		/**< OPA Unit ID to open on */
	int affinity;		/**< How PSM2 should set affinity */
	int shm_mbytes;	/**< Megabytes used for intra-node, deprecated */
	int sendbufs_num;	/**< Preallocated send buffers */
	uint64_t network_pkey;	/**< Network Protection Key (v1.01) */
	int port;		/**< IB port to use (1 to N) */
	int outsl;		/**< IB SL to use when sending pkts */
	uint64_t service_id;	/* IB Service ID to use for endpoint */
	psm2_path_res_t path_res_type;	/* Path resolution type */
	int senddesc_num;	/* Preallocated send descriptors */
	int imm_size;		/* Immediate data size for endpoint */
};

/** @brief OPA endpoint creation
 *
 * Function used to create a new local communication endpoint on an OPA
 * adapter.  The returned endpoint handle is required in all PSM2 communication
 * operations, as PSM2 can manage communication over multiple endpoints.  An
 * opened endpoint has no global context until the user connects the endpoint
 * to other global endpoints by way of @ref psm2_ep_connect.  All local endpoint
 * handles are globally identified by endpoint IDs (@ref psm2_epid_t) which are
 * also returned when an endpoint is opened.  It is assumed that the user can
 * provide an out-of-band mechanism to distribute the endpoint IDs in order to
 * establish connections between endpoints (@ref psm2_ep_connect for more
 * information).
 *
 * @param[in] unique_job_key Endpoint key, to uniquely identify the endpoint in
 *                           a parallel job.  It is up to the user to ensure
 *                           that the key is globally unique over a period long
 *                           enough to prevent duplicate keys over the same set
 *                           of endpoints (see comments below).
 *
 * @param[in] opts Open options of type @ref psm2_ep_open_opts
 *                 (see @ref psm2_ep_open_opts_get_defaults).
 *
 * @param[out] ep User-supplied storage to return a pointer to the newly
 *                created endpoint.  The returned pointer of type @ref psm2_ep_t
 *                is a local handle and cannot be used to globally identify the
 *                endpoint.
 * @param[out] epid User-supplied storage to return the endpoint ID associated
 *                  to the newly created local endpoint returned in the @c ep
 *                  handle.  The endpoint ID is an integral type suitable for
 *                  uniquely identifying the local endpoint.
 *
 * PSM2 does not internally verify the consistency of the uuid, it is up to the
 * user to ensure that the uid is unique enough not to collide with other
 * currently-running jobs.  Users can employ three mechanisms to obtain a uuid.
 *
 * 1. Use the supplied @ref psm2_uuid_generate utility
 *
 * 2. Use an OS or library-specific uuid generation utility, that complies with
 *    OSF DCE 1.1, such as @c uuid_generate on Linux or @c uuid_create on
 *    FreeBSD.
 *    (see http://www.opengroup.org/onlinepubs/009629399/uuid_create.htm)
 *
 * 3. Manually pack a 16-byte string using a utility such as /dev/random or
 *    other source with enough entropy and proper seeding to prevent two nodes
 *    from generating the same uuid_t.
 *
 * The following options are relevent when opening an endpoint:
 *   @li @c timeout establishes the number of nanoseconds to wait before
 *                  failing to open a port (with -1, defaults to 15 secs).
 *   @li @c unit sets the OPA unit number to use to open a port (with
 *               -1, PSM2 determines the best unit to open the port).  If @c
 *               HFI_UNIT is set in the environment, this setting is ignored.
 *   @li @c affinity enables or disables PSM2 setting processor affinity.  The
 *                   option can be controlled to either disable (@ref
 *                   PSM2_EP_OPEN_AFFINITY_SKIP) or enable the affinity setting
 *                   only if it is already unset (@ref
 *                   PSM2_EP_OPEN_AFFINITY_SET) or regardless of affinity being
 *                   set or not (@ref PSM2_EP_OPEN_AFFINITY_FORCE).
 *                   If @c HFI_NO_CPUAFFINITY is set in the environment, this
 *                   setting is ignored.
 *   @li @c shm_mbytes sets a maximum number of megabytes that can be allocated
 *		       to each local endpoint ID connected through this
 *		       endpoint (with -1, defaults to 10 MB).
 *   @li @c sendbufs_num sets the number of send buffers that can be
 *                       pre-allocated for communication (with -1, defaults to
 *                       512 buffers of MTU size).
 *   @li @c network_pkey sets the protection key to employ for point-to-point
 *                       PSM2 communication.  Unless a specific value is used,
 *                       this parameter should be set to
 *                       PSM2_EP_OPEN_PKEY_DEFAULT.
 *
 * @warning By default, PSM2 limits the user to calling @ref psm2_ep_open only
 * once per process and subsequent calls will fail. In order to enable creation
 * of multiple endoints per process, one must properly set the environment variable
 * @ref PSM2_MULTI_EP before calling @ref psm2_init.
 *
 * @code{.c}
    	// In order to open an endpoint and participate in a job, each endpoint has
    	// to be distributed a unique 16-byte UUID key from an out-of-band source.
    	// Presumably this can come from the parallel spawning utility either
    	// indirectly through an implementors own spawning interface or as in this
    	// example, the UUID is set as a string in an environment variable
    	// propagated to all endpoints in the job.

    	int try_to_open_psm2_endpoint(psm2_ep_t *ep, // output endpoint handle
    	                             psm2_epid_t *epid, // output endpoint identifier
    	                             int unit)  // unit of our choice
    	{
    	   struct psm2_ep_open_opts epopts;
    	   psm2_uuid_t job_uuid;
    	   char *c;

    	   // Let PSM2 assign its default values to the endpoint options.
    	   psm2_ep_open_opts_get_defaults(&epopts);

    	   // We want a stricter timeout and a specific unit
    	   epopts.timeout = 15*1e9;  // 15 second timeout
    	   epopts.unit = unit;	// We want a specific unit, -1 would let PSM
    	                             // choose the unit for us.
    	   epopts.port = port;	// We want a specific unit, <= 0 would let PSM
    	                             // choose the port for us.
    	   // We've already set affinity, don't let PSM2 do so if it wants to.
    	   if (epopts.affinity == PSM2_EP_OPEN_AFFINITY_SET)
    	      epopts.affinity = PSM2_EP_OPEN_AFFINITY_SKIP;

    	   // ENDPOINT_UUID is set to the same value in the environment of all the
    	   // processes that wish to communicate over PSM2 and was generated by
    	   // the process spawning utility
    	   c = getenv("ENDPOINT_UUID");
    	   if (c && *c)
    	      implementor_string_to_16byte_packing(c, job_uuid);
    	   else {
    	      fprintf(stderr, "Can't find UUID for endpoint\n);
    	      return -1;
    	   }

    	   // Assume we don't want to handle errors here.
    	   psm2_ep_open(job_uuid, &epopts, ep, epid);
    	   return 1;
    	}
   @endcode
 */
psm2_error_t
psm2_ep_open(const psm2_uuid_t unique_job_key,
	    const struct psm2_ep_open_opts *opts, psm2_ep_t *ep,
	    psm2_epid_t *epid);

/** @brief Endpoint open default options.
 *
 * Function used to initialize the set of endpoint options to their default
 * values for use in @ref psm2_ep_open.
 *
 * @param[out] opts Endpoint Open options.
 *
 * @warning For portable operation, users should always call this function
 * prior to calling @ref psm2_ep_open.
 *
 * @return PSM2_OK If result could be updated
 * @return PSM2_INIT_NOT_INIT If psm has not been initialized.
 */
psm2_error_t
psm2_ep_open_opts_get_defaults(struct psm2_ep_open_opts *opts);

/** @brief Endpoint shared memory query
 *
 * Function used to determine if a remote endpoint shares memory with a
 * currently opened local endpiont.
 *
 * @param[in] ep Endpoint handle
 * @param[in] epid Endpoint ID
 *
 * @param[out] result Result is non-zero if the remote endpoint shares memory with the local
 * endpoint @c ep, or zero otherwise.
 *
 * @return PSM2_OK If result could be updated
 * @return PSM2_EPID_UNKNOWN If the epid is not recognized
 */
psm2_error_t
psm2_ep_epid_share_memory(psm2_ep_t ep, psm2_epid_t epid, int *result);

/** @brief Close endpoint
 * @param[in] ep PSM2 endpoint handle
 * @param[in] mode One of @ref PSM2_EP_CLOSE_GRACEFUL or @ref PSM2_EP_CLOSE_FORCE
 * @param[in] timeout How long to wait in nanoseconds if mode is
 *			PSM2_EP_CLOSE_GRACEFUL, 0 waits forever.  If @c mode is
 *			@ref PSM2_EP_CLOSE_FORCE, this parameter is ignored.
 *
 * The following errors are returned, others are handled by the per-endpoint
 * error handler:
 *
 * @return PSM2_OK  Endpoint was successfully closed without force or
 *                 successfully closed with force within the supplied timeout.
 * @return PSM2_EP_CLOSE_TIMEOUT Endpoint could not be successfully closed
 *                              within timeout.
 */
psm2_error_t psm2_ep_close(psm2_ep_t ep, int mode, int64_t timeout);

#define PSM2_EP_CLOSE_GRACEFUL	0	/**< Graceful mode in @ref psm2_ep_close */
#define PSM2_EP_CLOSE_FORCE	1	/**< Forceful mode in @ref psm2_ep_close */

/** @brief Provide mappings for network id to hostname
 *
 * Since PSM2 does not assume or rely on the availability of an external
 * networkid-to-hostname mapping service, users can provide one or more of
 * these mappings.  The @ref psm2_map_nid_hostname function allows a list of
 * network ids to be associated to hostnames.
 *
 * This function is not mandatory for correct operation but may allow PSM2 to
 * provide better diagnostics when remote endpoints are unavailable and can
 * otherwise only be identified by their network id.
 *
 * @param[in] num Number elements in @c nid and @c hostnames arrays
 * @param[in] nids User-provided array of network ids (i.e. OPA LIDs),
 *                 should be obtained by calling @ref psm2_epid_nid on each
 *                 epid.
 * @param[in] hostnames User-provided array of hostnames (array of
 *                      NUL-terimated strings) where each hostname index
 *                      maps to the provided nid hostname.
 *
 * @warning Duplicate nids may be provided in the input @c nids array, only
 *          the first corresponding hostname will be remembered.
 *
 * @pre The user may or may not have already provided a hostname mappings.
 * @post The user may free any dynamically allocated memory passed to the
 *       function.
 *
 */
psm2_error_t
psm2_map_nid_hostname(int num, const uint64_t *nids, const char **hostnames);

/** @brief Connect one or more remote endpoints to a local endpoint
 *
 * Function to non-collectively establish a connection to a set of endpoint IDs
 * and translate endpoint IDs into endpoint addresses.  Establishing a remote
 * connection with a set of remote endpoint IDs does not imply a collective
 * operation and the user is free to connect unequal sets on each process.
 * Similarly, a given endpoint address does not imply that a pairwise
 * communication context exists between the local endpoint and remote endpoint.
 *
 * @param[in] ep PSM2 endpoint handle
 *
 * @param[in] num_of_epid The number of endpoints to connect to, which
 *                        also establishes the number of elements contained in
 *                        all of the function's array-based parameters.
 *
 * @param[in] array_of_epid User-allocated array that contains @c num_of_epid
 *                          valid endpoint identifiers.  Each endpoint id (or
 *                          epid) has been obtained through an out-of-band
 *                          mechanism and each endpoint must have been opened
 *                          with the same uuid key.
 *
 * @param[in] array_of_epid_mask User-allocated array that contains
 *                          @c num_of_epid integers.  This array of masks
 *                          allows users to select which of the epids in @c
 *                          array_of_epid should be connected.  If the integer
 *                          at index i is zero, psm does not attempt to connect
 *                          to the epid at index i in @c array_of_epid.  If
 *                          this parameter is NULL, psm will try to connect to
 *                          each epid.
 *
 * @param[out] array_of_errors User-allocated array of at least @c num_of_epid
 *                             elements. If the function does not return
 *                             PSM2_OK, this array can be consulted for each
 *                             endpoint not masked off by @c array_of_epid_mask
 *                             to know why the endpoint could not be connected.
 *                             Endpoints that could not be connected because of
 *                             an unrelated failure will be marked as @ref
 *                             PSM2_EPID_UNKNOWN.  If the function returns
 *                             PSM2_OK, the errors for all endpoints will also
 *                             contain PSM2_OK.
 *
 * @param[out] array_of_epaddr User-allocated array of at least @c num_of_epid
 *                             elements of type psm2_epaddr_t.  Each
 *                             successfully connected endpoint is updated with
 *                             an endpoint address handle that corresponds to
 *                             the endpoint id at the same index in @c
 *                             array_of_epid.  Handles are only updated if the
 *                             endpoint could be connected and if its error in
 *                             array_of_errors is PSM2_OK.
 *
 * @param[in] timeout Timeout in nanoseconds after which connection attempts
 *                    will be abandoned.  Setting this value to 0 disables
 *                    timeout and waits until all endpoints have been
 *                    successfully connected or until an error is detected.
 *
 * @pre The user has opened a local endpoint and obtained a list of endpoint
 *      IDs to connect to a given endpoint handle using an out-of-band
 *      mechanism not provided by PSM.
 *
 * @post If the connect is successful, @c array_of_epaddr is updated with valid
 *       endpoint addresses.
 *
 * @post If unsuccessful, the user can query the return status of each
 *       individual remote endpoint in @c array_of_errors.
 *
 * @post The user can call into @ref psm2_ep_connect many times with the same
 *       endpoint ID and the function is guaranteed to return the same output
 *       parameters.
 *
 * @post PSM2 does not keep any reference to the arrays passed into the
 *       function and the caller is free to deallocate them.
 *
 * The error value with the highest importance is returned by
 * the function if some portion of the communication failed.  Users should
 * always refer to individual errors in @c array_of_errors whenever the
 * function cannot return PSM2_OK.
 *
 * @returns PSM2_OK  The entire set of endpoint IDs were successfully connected
 *                  and endpoint addresses are available for all endpoint IDs.
 *
 * @code{.c}
   	int connect_endpoints(psm2_ep_t ep, int numep,
   	                      const psm2_epid_t *array_of_epid,
   	                      psm2_epaddr_t **array_of_epaddr_out)
   	{
   	    psm2_error_t *errors = (psm2_error_t *) calloc(numep, sizeof(psm2_error_t));
   	    if (errors == NULL)
   	        return -1;

   	    psm2_epaddr_t *all_epaddrs =
   	             (psm2_epaddr_t *) calloc(numep, sizeof(psm2_epaddr_t));

   	    if (all_epaddrs == NULL)
   	        return -1;

   	    psm2_ep_connect(ep, numep, array_of_epid,
   	                   NULL, // We want to connect all epids, no mask needed
   	                   errors,
   	                   all_epaddrs,
   	                   30*e9); // 30 second timeout, <1 ns is forever
   	    *array_of_epaddr_out = all_epaddrs;
   	    free(errors);
   	    return 1;
   	}
   @endcode
 */
psm2_error_t
psm2_ep_connect(psm2_ep_t ep, int num_of_epid, const psm2_epid_t *array_of_epid,
		   const int *array_of_epid_mask, psm2_error_t *array_of_errors,
		   psm2_epaddr_t *array_of_epaddr, int64_t timeout);

/* @brief Disconnect one or more remote endpoints from a local endpoint.
*
* Function to non-collectively disconnect a connection to a set of endpoint
* addresses and free the endpoint addresses. After disconnecting, the
* application cannot send messages to the remote processes and PSM2 is
* restored back to the state before calling psm2_ep_connect. The application
* must call psm2_ep_connect to establish the connections again.
*
* This function is equivalent to calling psm2_ep_disconnect2() with mode ==
* PSM2_EP_DISCONNECT_GRACEFUL.
*
* @param[in] ep PSM2 endpoint handle
*
* @param[in] num_of_epaddr The number of endpoint addresses to disconnect from,
*                          which also indicates the number of elements contained
*                          in all of the function’s array-based parameters.
*
* @param[in] array_of_epaddr User-allocated array that contains num_of_epaddr
*                            valid endpoint addresses. Each endpoint address (or
*                            epaddr) has been obtained through a previous
*                            psm2_ep_connect call.
*
* @param[in] array_of_epaddr_mask User-allocated array that contains
*                                 num_of_epaddr integers. This array of masks
*                                 allows users to select which of the
*                                 epaddresses in array_of_epaddr should be
*                                 disconnected. If the integer at index i is
*                                 zero, PSM2 does not attempt to disconnect to
*                                 the epaddr at index i in array_of_epaddr. If
*                                 this parameter is NULL, PSM2 tries to
*                                 disconnect all epaddr in array_of_epaddr.
*
* @param[out] array_of_errors User-allocated array of at least num_of_epaddr
*                             elements. If the function does not return PSM2_OK,
*                             this array can be consulted for each endpoint
*                             address not masked off by array_of_epaddr_mask to
*                             know why the endpoint could not be disconnected.
*                             Any endpoint address that could not be
*                             disconnected because of an unrelated failure is
*                             marked as PSM2_EPID_UNKNOWN. If the function
*                             returns PSM2_OK, the errors for all endpoint
*                             addresses also contain PSM2_OK.
*
* @param[in] timeout Timeout in nanoseconds after which disconnection attempts
*                    are abandoned. Setting this value to 0 disables timeout and
*                    waits until all endpoints have been successfully
*                    disconnected or until an error is detected.
*
* @pre You have established the connections with previous psm2_ep_connect calls.
*
* @post If the disconnect is successful, the corresponding epaddr in
*       array_of_epaddr is reset to NULL pointer.
*
* @post If unsuccessful, you can query the return status of each individual
*       remote endpoint in array_of_errors.
*
* @post PSM2 does not keep any reference to the arrays passed into the function
*       and the caller is free to deallocate them.
*
* @post The error value with the highest importance is returned by the function
*       if some portion of the communication failed. Refer to individual errors
*       in array_of_errors whenever the function cannot return PSM2_OK.
*
* @returns PSM2_OK The entire set of endpoint IDs were successfully disconnected
*          and endpoint addresses are freed by PSM2.
*
* @code{.c}
int disconnect_endpoints(psm2_ep_t ep, int num_epaddr,
             const psm2_epaddr_t *array_of_epaddr)
{
    psm2_error_t *errors =
        (psm2_error_t *)calloc(num_epaddr, sizeof(psm2_error_t));
    if (errors == NULL)
        return -1;
    psm2_ep_disconnect(
        ep, num_epaddr, array_of_epaddr,
        NULL, // We want to disconnect all epaddrs, no mask needed,
        errors,
        30 * e9); // 30 second timeout, <1 ns is forever
    free(errors);
    return 1;
}
@endcode
*/
psm2_error_t psm2_ep_disconnect(psm2_ep_t ep, int num_of_epaddr,
				psm2_epaddr_t *array_of_epaddr,
				const int *array_of_epaddr_mask,
				psm2_error_t *array_of_errors, int64_t timeout);

/* @brief Disconnect one or more remote endpoints from a local endpoint.
*
* Function to non-collectively disconnect a connection to a set of endpoint
* addresses and free the endpoint addresses. After disconnecting, the
* application cannot send messages to the remote processes and PSM2 is
* restored back to the state before calling psm2_ep_connect. The application
* must call psm2_ep_connect to establish the connections again.
*
* @param[in] ep PSM2 endpoint handle
*
* @param[in] num_of_epaddr The number of endpoint addresses to disconnect from,
*                          which also indicates the number of elements contained
*                          in all of the function’s array-based parameters.
*
* @param[in] array_of_epaddr User-allocated array that contains num_of_epaddr
*                            valid endpoint addresses. Each endpoint address (or
*                            epaddr) has been obtained through a previous
*                            psm2_ep_connect call.
*
* @param[in] array_of_epaddr_mask User-allocated array that contains
*                                 num_of_epaddr integers. This array of masks
*                                 allows users to select which of the
*                                 epaddresses in array_of_epaddr should be
*                                 disconnected. If the integer at index i is
*                                 zero, PSM2 does not attempt to disconnect to
*                                 the epaddr at index i in array_of_epaddr. If
*                                 this parameter is NULL, PSM2 tries to
*                                 disconnect all epaddr in array_of_epaddr.
*
* @param[out] array_of_errors User-allocated array of at least num_of_epaddr
*                             elements. If the function does not return PSM2_OK,
*                             this array can be consulted for each endpoint
*                             address not masked off by array_of_epaddr_mask to
*                             know why the endpoint could not be disconnected.
*                             Any endpoint address that could not be
*                             disconnected because of an unrelated failure is
*                             marked as PSM2_EPID_UNKNOWN. If the function
*                             returns PSM2_OK, the errors for all endpoint
*                             addresses also contain PSM2_OK.
*
* @param[in] mode One of @ref PSM2_EP_DISCONECT_GRACEFUL or @ref PSM2_EP_DISCONECT_FORCE
*
* @param[in] timeout Timeout in nanoseconds after which disconnection attempts
*                    are abandoned. Setting this value to 0 disables timeout and
*                    waits until all endpoints have been successfully
*                    disconnected or until an error is detected. Supplying a
*                    negative value here sets the disconnection mode to "force".
*
* @pre You have established the connections with previous psm2_ep_connect calls.
*
* @post If the disconnect is successful, the corresponding epaddr in
*       array_of_epaddr is reset to NULL pointer.
*
* @post If unsuccessful, you can query the return status of each individual
*       remote endpoint in array_of_errors.
*
* @post PSM2 does not keep any reference to the arrays passed into the function
*       and the caller is free to deallocate them.
*
* @post The error value with the highest importance is returned by the function
*       if some portion of the communication failed. Refer to individual errors
*       in array_of_errors whenever the function cannot return PSM2_OK.
*
* @returns PSM2_OK The entire set of endpoint IDs were successfully disconnected
*          and endpoint addresses are freed by PSM2.
*
* @code{.c}
int disconnect_endpoints(psm2_ep_t ep, int num_epaddr,
             const psm2_epaddr_t *array_of_epaddr)
{
    psm2_error_t *errors =
        (psm2_error_t *)calloc(num_epaddr, sizeof(psm2_error_t));
    if (errors == NULL)
        return -1;
    psm2_ep_disconnect2(
        ep, num_epaddr, array_of_epaddr,
        NULL, // We want to disconnect all epaddrs, no mask needed,
        errors,
	PSM2_EP_DISCONECT_GRACEFUL,
        30 * e9); // 30 second timeout, 0 ns is forever
    free(errors);
    return 1;
}
@endcode
*/
psm2_error_t psm2_ep_disconnect2(psm2_ep_t ep, int num_of_epaddr,
				psm2_epaddr_t *array_of_epaddr,
				const int *array_of_epaddr_mask,
				psm2_error_t *array_of_errors,
				int mode, int64_t timeout);

#define PSM2_EP_DISCONNECT_GRACEFUL	PSM2_EP_CLOSE_GRACEFUL   /**< Graceful mode in @ref psm2_ep_disconnect2 */
#define PSM2_EP_DISCONNECT_FORCE	PSM2_EP_CLOSE_FORCE   /**< Forceful mode in @ref psm2_ep_disconnect2 */

/** @brief Ensure endpoint communication progress
 *
 * Function to ensure progress for all PSM2 components instantiated on an
 * endpoint (currently, this only includes the MQ component).  The function
 * never blocks and is typically required in two cases:
 *
 * @li Allowing all PSM2 components instantiated over a given endpoint to make
 *     communication progress. Refer to @ref mq_progress for a detailed
 *     discussion on MQ-level progress issues.
 *
 * @li Cases where users write their own synchronization primitives that
 *     depend on remote communication (such as spinning on a memory location
 *     which's new value depends on ongoing communication).
 *
 * The poll function doesn't block, but the user can rely on the @ref
 * PSM2_OK_NO_PROGRESS return value to control polling behaviour in terms of
 * frequency (poll until an event happens) or execution environment (poll for a
 * while but yield to other threads of CPUs are oversubscribed).
 *
 * @returns PSM2_OK             Some communication events were progressed
 * @returns PSM2_OK_NO_PROGRESS Polling did not yield any communication progress
 *
 */
psm2_error_t psm2_poll(psm2_ep_t ep);

/** @brief Set a user-determined ep address label.
 *
 * @param[in] epaddr Endpoint address, obtained from @ref psm2_ep_connect
 * @param[in] epaddr_label_string User-allocated string to print when
 *                   identifying endpoint in error handling or other verbose
 *                   printing.  The NULL-terminated string must be allocated by
 *                   the user since PSM2 only keeps a pointer to the label.  If
 *                   users do not explicitly set a label for each endpoint,
 *                   endpoints will identify themselves as hostname:port.
 */
void psm2_epaddr_setlabel(psm2_epaddr_t epaddr,
			 const char *epaddr_label_string);

/** @brief Set a user-determined ep address context.
 *
 * @param[in] epaddr Endpoint address, obtained from @ref psm2_ep_connect
 * @param[in] ctxt   Opaque user defined state to associate with an endpoint
 *                   address. This state can be retrieved via
 *                   @ref psm2_epaddr_getctxt.
 */
void
psm2_epaddr_setctxt(psm2_epaddr_t epaddr, void *ctxt);

/** @brief Get the user-determined ep address context. Users can associate an
 *  opaque context with each endpoint via @ref psm2_epaddr_setctxt.
 *
 * @param[in] epaddr Endpoint address, obtained from @ref psm2_ep_connect.
 */
void *psm2_epaddr_getctxt(psm2_epaddr_t epaddr);

/* Below are all component specific options. The component object for each of
 * the options is also specified.
 */

/* PSM2_COMPONENT_CORE options */
/* PSM2 debug level */
#define PSM2_CORE_OPT_DEBUG     0x101
  /**< [@b uint32_t ] Set/Get the PSM2 debug level. This option can be set
   * before initializing the PSM2 library.
   *
   * component object: (null)
   * option value: PSM2 Debug mask to set or currently active debug level.
   */

/* PSM2 endpoint address context */
#define PSM2_CORE_OPT_EP_CTXT   0x102
  /**< [@b uint32_t ] Set/Get the context associated with a PSM2 endpoint
   * address (psm2_epaddr_t).
   *
   * component object: PSM2 endpoint (@ref psm2_epaddr_t) address.
   * option value: Context associated with PSM2 endpoint address.
   */

/* PSM2_COMPONENT_IB options */
/* Default service level to use to communicate with remote endpoints */
#define PSM2_IB_OPT_DF_SL 0x201
  /**< [@b uint32_t ] Default OPA SL to use for all remote communication.
   * If unset defaults to Service Level 0.
   *
   * component object: Opened PSM2 endpoint id (@ref psm2_ep_t).
   * option value: Default IB SL to use for endpoint. (0 <= SL < 15)
   */

/* Set IB service level to use for communication to an endpoint */
#define PSM2_IB_OPT_EP_SL 0x202
  /**< [@b uint32_t ] OPA SL to use for communication to specified
   * remote endpoint.
   *
   * component object: PSM2 endpoint (@ ref psm2_epaddr_t) address.
   * option value: SL used to communicate with remote endpoint. (0 <= SL < 15)
   */

/* PSM2_COMPONENT_MQ options (deprecates psm2_mq_set|getopt) */
/* MQ options that can be set in psm2_mq_init and psm2_{set,get}_opt */
#define PSM2_MQ_OPT_RNDV_IB_SZ       0x301
  /**< [@b uint32_t ] Size at which to start enabling rendezvous
   * messaging for OPA messages (if unset, defaults to values
   * between 56000 and 72000 depending on the system configuration)
   *
   * component object: PSM2 Matched Queue (@ref psm2_mq_t).
   * option value: Size at which to switch to rendezvous protocol.
   */
#define PSM2_MQ_RNDV_HFI_SZ          PSM2_MQ_OPT_RNDV_IB_SZ
#define PSM2_MQ_RNDV_IPATH_SZ        PSM2_MQ_OPT_RNDV_IB_SZ

#define PSM2_MQ_OPT_RNDV_SHM_SZ      0x302
#define PSM2_MQ_RNDV_SHM_SZ          PSM2_MQ_OPT_RNDV_SHM_SZ
  /**< [@b uint32_t ] Size at which to start enabling
   * rendezvous messaging for shared memory (intra-node) messages (If
   * unset, defaults to 64000 bytes).
   *
   * component object: PSM2 Matched Queue (@ref psm2_mq_t).
   * option value: Size at which to switch to rendezvous protocol.
   */

#define PSM2_MQ_OPT_SYSBUF_MYBYTES   0x303
#define PSM2_MQ_MAX_SYSBUF_MBYTES    PSM2_MQ_OPT_SYSBUF_MYBYTES
  /**< [@b uint32_t ] Maximum number of bytes to allocate for unexpected
   * messages.
   *
   * component object: PSM2 Matched Queue (@ref psm2_mq_t).
   * option value: Deprecated; this option has no effect.
   */

/* PSM2_COMPONENT_AM options */
#define PSM2_AM_OPT_FRAG_SZ          0x401
#define PSM2_AM_MAX_FRAG_SZ          PSM2_AM_OPT_FRAG_SZ
/*!< [@b uint32_t ] Maximum active message fragment size that can be sent
 * for a given endpoint or across all endpoints. This value can only be
 * queried.
 *
 * component object: PSM2 endpoint (@ref psm2_epaddr_t) address. If NULL then
 *                   option value is the smalles fragment size across all
 *                   active endpoints.
 * option value: Maximum active message fragment size in bytes.
 */

#define PSM2_AM_OPT_NARGS 0x402
#define PSM2_AM_MAX_NARGS PSM2_AM_OPT_NARGS

/*!< [@b uint32_t ] Maximum number of message arguments that can be sent
 * for a given endpoint or across all endpoints. This value can only be
 * queried.
 *
 * component object: PSM2 endpoint (@ref psm2_epaddr_t) address. If NULL then
 *                   option value is the smalles fragment size across all
 *                   active endpoints.
 * option value: Maximum number of active message arguments.
 */

#define PSM2_AM_OPT_HANDLERS 0x403
#define PSM2_AM_MAX_HANDLERS PSM2_AM_OPT_HANDLERS
/*!< [@b uint32_t ] Maximum number of message handlers that can be registered
 * for a given endpoint or across all endpoints. This value can only be
 * queried.
 *
 * component object: PSM2 endpoint (@ref psm2_epaddr_t) address. If NULL then
 *                   option value is the smalles fragment size across all
 *                   active endpoints.
 * option value: Maximum number of active message handlers.
 */

/** @brief Set an option for a PSM2 component
 *
 * Function to set the value of a PSM2 component option
 *
 * @param[in] component Type of PSM2 component for which to set the option
 * @param[in] component_obj Opaque component specify object to apply the set
 *                          operation on. These are passed uninterpreted to the
 *                          appropriate component for interpretation.
 * @param[in] optname Name of component option to set. These are component
 *                    specific and passed uninterpreted to the appropriate
 *                    component for interpretation.
 * @param[in] optval Pointer to storage that contains the value to be updated
 *                   for the supplied option.  It is up to the user to
 *                   ensure that the pointer points to a memory location with a
 *                   correct size and format.
 * @param[in] optlen Size of the memory region pointed to by optval.
 *
 * @returns PSM2_OK if option could be set.
 * @returns PSM2_PARAM_ERR if the component or optname are not valid.
 * @returns PSM2_OPT_READONLY if the option to be set is a read-only option.
 *
 */
psm2_error_t
psm2_setopt(psm2_component_t component, const void *component_obj,
	   int optname, const void *optval, uint64_t optlen);

/** @brief Get an option for a PSM2 component
 *
 * Function to get the value of a PSM2 component option
 *
 * @param[in] component Type of PSM2 component for which to get the option
 * @param[in] component_obj Opaque component specify object to apply the get
 *                          operation on. These are passed uninterpreted to the
 *                          appropriate component for interpretation.
 * @param[in] optname Name of component option to get. These are component
 *                    specific and passed uninterpreted to the appropriate
 *                    component for interpretation.
 * @param[out] optval Pointer to storage that contains the value to be updated
 *                    for the supplied option.  It is up to the user to
 *                    ensure that the pointer points to a valid memory region.
 * @param[in,out] optlen This is a value result parameter initially containing
 *                      the size of the memory region pointed to by optval and
 *                      modified to return the actual size of optval.
 *
 * @returns PSM2_OK if option value could be retrieved successfully.
 * @returns PSM2_PARAM_ERR if the component or optname are not valid.
 * @returns PSM2_NO_MEMORY if the memory region optval is of insufficient size.
 *                         optlen contains the required memory region size for
 *                         optname value.
 *
 */
psm2_error_t
psm2_getopt(psm2_component_t component, const void *component_obj,
	   int optname, void *optval, uint64_t *optlen);

/** @brief Datatype for end-point information */
typedef struct psm2_epinfo {
	psm2_ep_t ep;		/**< The ep for this end-point*/
	psm2_epid_t epid;	/**< The epid for this end-point */
	psm2_uuid_t uuid;	/**< The UUID for this end-point */
	uint16_t jkey;		/**< The job key for this end-point */
	char uuid_str[64];	/**< String representation of the UUID for this end-point */
} psm2_epinfo_t;

/** @brief Datatype for end-point connection */
typedef struct psm2_epconn {
	psm2_epaddr_t addr;	/**< The epaddr for this connection */
	psm2_ep_t ep;		/**< The ep for this connection */
	psm2_mq_t mq;		/**< The mq for this connection */
} psm2_epconn_t;

/** @brief Query PSM2 for end-point information.
 *
 * Function to query PSM2 for end-point information. This allows retrieval of
 * end-point information in cases where the caller does not have access to the
 * results of psm2_ep_open().  In the default single-rail mode PSM2 will use
 * a single endpoint. If either multi-rail mode or multi-endpoint mode is
 * enabled, PSM2 will use multiple endpoints.
 *
 * @param[in,out] num_of_epinfo On input, sizes the available number of entries
 *                              in array_of_epinfo.  On output, specifies the
 *                              returned number of entries in array_of_epinfo.
 * @param[out] array_of_epinfo Returns end-point information structures.
 *
 * @pre PSM2 is initialized and the end-point has been opened.
 *
 * @returns PSM2_OK indicates success.
 * @returns PSM2_PARAM_ERR if input num_if_epinfo is less than or equal to zero.
 * @returns PSM2_EP_WAS_CLOSED if PSM2 end-point is closed or does not exist.
 */
psm2_error_t psm2_ep_query(int *num_of_epinfo, psm2_epinfo_t *array_of_epinfo);

/** @brief Query PSM2 for end-point connections.
 *
 * Function to query PSM2 for end-point connections. This allows retrieval of
 * end-point connections in cases where the caller does not have access to the
 * results of psm2_ep_connect().  The epid values can be found using
 * psm2_ep_query() so that each PSM2 process can determine its own epid. These
 * values can then be distributed across the PSM2 process so that each PSM
 * process knows the epid for all other PSM2 processes.
 *
 * @param[in] epid The epid of a PSM2 process.
 * @param[out] epconn The connection information for that PSM2 process.
 *
 * @pre PSM2 is initialized and the end-point has been connected to this epid.
 *
 * @returns PSM2_OK indicates success.
 * @returns PSM2_EP_WAS_CLOSED if PSM2 end-point is closed or does not exist.
 * @returns PSM2_EPID_UNKNOWN if the epid value is not known to PSM.
 */
psm2_error_t psm2_ep_epid_lookup(psm2_epid_t epid, psm2_epconn_t *epconn);

/** @brief Query given PSM2 end-point for its connections.
 *
 * The need for this function comes with 'multi-ep' feature.
 * Function is similar to (@ref psm2_ep_epid_lookup).
 * It differs in that an extra parameter which identifies
 * the end-point [ep] must be provided which limits the lookup to that single ep.
 *
 * @returns PSM2_OK indicates success.
 * @returns PSM2_EP_WAS_CLOSED if PSM2 end-point [ep] is closed or does not exist.
 * @returns PSM2_EPID_UNKNOWN if the [epid] value is not known to PSM.
 * @returns PSM2_PARAM_ERR if output [epconn] is NULL.
 */
psm2_error_t psm2_ep_epid_lookup2(psm2_ep_t ep, psm2_epid_t epid, psm2_epconn_t *epconn);

/** @brief Get PSM2 epid for given epaddr.
 *
 * @param[in] epaddr The endpoint address.
 * @param[out] epid The epid of a PSM2 process.
 *
 * @returns PSM2_OK indicates success.
 * @returns PSM2_PARAM_ERR if input [epaddr] or output [epid] is NULL.
 */
psm2_error_t psm2_epaddr_to_epid(psm2_epaddr_t epaddr, psm2_epid_t *epid);

/*! @} */

/*! @addtogroup init PSM2 Information Query
 * @{
 */

/** @brief Enumeration for info query APIs
 *
 * Note that calling the function:
 *
 @code{.c}
 psm2_error_t psm2_info_query(psm2_info_query_t, void *out,
                              size_t nargs, psm2_info_query_arg_t []);
 @endcode
 *
 * Takes a variable number of input arguments, per the initial psm2_info_query_t
 *
 * Below, there is an explanation of the number, type and order of the
 * required input arguments, as well as a definition of the type of the output.
 */
typedef enum psm2_info_query_et
{
/*! Required input arguments 0
   Output parameter: uint32_t*, description: the number of units */
	PSM2_INFO_QUERY_NUM_UNITS,

/*! Required input arguments: 0
   Output parameter: uint32_t*, description: the number of ports */
	PSM2_INFO_QUERY_NUM_PORTS,

/*! Required input arguments: 1
   1.  type: uint32_t, description: the unit for which status is
       desired (use: psm2_info_query_arg_t.unit).
   Output parameter: uint32_t, description: zero, when the unit
                     is not active, non-zero when the unit is
                     active.  */
	PSM2_INFO_QUERY_UNIT_STATUS,

/*! Required input arguments: 2
   1.  type: uint32_t, description: the unit for which status is
       desired (use: psm2_info_query_arg_t.unit).
   2.  type: uint32_t, description: the port for which status is
       desired (use: psm2_info_query_arg_t.port).
   Output parameter: uint32_t, description: zero, when the unit
                     is not active, non-zero when the unit is
                     active.  */
	PSM2_INFO_QUERY_UNIT_PORT_STATUS,

/*! Required input arguments: 1
   1.  type: uint32_t, description: the unit for which the number of
       free contexts is desired (use: psm2_info_query_arg_t.unit).
   Output parameter: uint32_t, description: the number of free
                     contexts..  */
	PSM2_INFO_QUERY_NUM_FREE_CONTEXTS,

/*! Required input arguments: 1
   1.  type: uint32_t, description: the unit for which the number of
       contexts is desired (use: psm2_info_query_arg_t.unit).
   Output parameter: uint32_t, description: the number of
                     contexts..  */
	PSM2_INFO_QUERY_NUM_CONTEXTS,

/*! Required input arguments: 2
   1.  type: psm2_mq_t, description: the mq that is associated with the
       connection for which configuration information is wanted.
       (use: psm2_info_query_arg_t.mq).
   2.  type: psm2_epaddr_t, description: the ep address that is
       associated with the connection for which configuration
       information is wanted (use: psm2_info_query_arg_t.epaddr).
   Output parameter: uint32_t, description: a bit mask containing bits defining the configuration.
   see psm2_info_query_config for a description of the bits. */
	PSM2_INFO_QUERY_CONFIG,

/*! Required input arguments: 3
   1.  type: psm2_mq_t, description: the mq that is associated with the
       connection for which the msg size query information is wanted.
       (use: psm2_info_query_arg_t.mq).
   2.  type: psm2_epaddr_t, description: the ep address that is
       associated with the connection for which the msg size query
       information is wanted (use: psm2_info_query_arg_t.epaddr).
   3.  type: enum psm2_info_query_thresh_et, the specific msg size query.
       (use: psm2_info_query_arg_t.mstq).

       Output parameter: uint32_t, description: the message size threshold. */
	PSM2_INFO_QUERY_THRESH,

/*! Required input arguments: 3
   1.  type: psm2_mq_t, description: the mq that is associated with the
       connection for which the device name is wanted.
       (use: psm2_info_query_arg_t.mq).
   2.  type: psm2_epaddr_t, description: the ep address that is
       associated with the connection for which device name is wanted.
       (use: psm2_info_query_arg_t.epaddr).
   3.  type: size_t, the length of the output buffer that will recieve
       the device name (use: psm2_info_query_arg_t.length).
       Output parameter: char *, description: the device name. */
	PSM2_INFO_QUERY_DEVICE_NAME,

/*! Required input arguments: 2
   1.  type: psm2_mq_t, description: the mq that is associated with the
       connection for which the mtu is wanted (use: psm2_info_query_arg_t.mq).
   2.  type: psm2_epaddr_t, description: the ep address that is
       associated with the connection for which mtu is wanted.
       (use: psm2_info_query_arg_t.epaddr).
       Output parameter: uint32_t, description: the mtu. */

	PSM2_INFO_QUERY_MTU,

/*! Required input arguments: 2
   1.  type: psm2_mq_t, description: the mq that is associated with the
       connection for which the link speed is wanted (use:
       psm2_info_query_arg_t.mq).
   2.  type: psm2_epaddr_t, description: the ep address that is
       associated with the connection for which link speed is wanted.
       (use: psm2_info_query_arg_t.epaddr).
       Output parameter: uint32_t, description: the link speed. */
	PSM2_INFO_QUERY_LINK_SPEED,

/*! Required input arguments: 1
   1.  type: size_t, description: the length of the output buffer to receive
       the network type (use: psm2_info_query_arg_t.length).
       Output parameter: char*, description: the network type. */
	PSM2_INFO_QUERY_NETWORK_TYPE,

/*! Required input arguments 0
    Output parameter: uint32_t*, description: a bit mask of the features in libpsm2.
    See psm2_info_query_feature_mask below for bit mask definition. */
	PSM2_INFO_QUERY_FEATURE_MASK,

	PSM2_INFO_QUERY_LAST, /* must appear last, and the info query
				 constants are used as an index. */
} psm2_info_query_t;

/** @brief Enumeration for info query config
 */
enum psm2_info_query_config
{
	/*! The following three are 'main configs': */
	PSM2_INFO_QUERY_CONFIG_IPS      = (1 << 0),
	PSM2_INFO_QUERY_CONFIG_AMSH     = (1 << 1),
	PSM2_INFO_QUERY_CONFIG_SELF     = (1 << 2),

	/*! The following three are sub-configs of
           the IPS main config: */

	PSM2_INFO_QUERY_CONFIG_CUDA     = (1 << 3),
	PSM2_INFO_QUERY_CONFIG_PIO      = (1 << 4),
	PSM2_INFO_QUERY_CONFIG_DMA      = (1 << 5),

	/*! The following is a sub-config of IPS & CUDA
           main config: */

	PSM2_INFO_QUERY_CONFIG_GDR_COPY = (1 << 6),
};

/** @brief Enumeration info query thresholds
 */
enum psm2_info_query_thresh_et
{
/*! This is the start of the thresh queries for IPS config: */
	PSM2_INFO_QUERY_THRESH_IPS_START,

/*! Not shown here are the specific queries supported by the CUDA
   and GDR_COPY, sub-configs.

   But, those configs will need to include threshold queries in case the
   config includes them.

   Note that for the case of gdr_copy the thresholds varies for the case
   of the memory is gpu memory or not. */

/*! The following threshold queres are supported for the IPS config
   only. */

/*! The PSM2_INFO_QUERY_THRESH_IPS_PIO_DMA threshold query indicates at
   what message size the send transport transitions from PIO to DMA.

   Note that this threshold query may be meaningless if PIO or DMA is
   disabled. */
	PSM2_INFO_QUERY_THRESH_IPS_PIO_DMA = PSM2_INFO_QUERY_THRESH_IPS_START,
/*! Messages with messages sizes less than or equal to the tiny threshold
   will be sent by tiny message. */
	PSM2_INFO_QUERY_THRESH_IPS_TINY,
/*! Messages with messages sizes greater than tiny, but less than or equal
   to frag size will be sent by short message. */
	PSM2_INFO_QUERY_THRESH_IPS_PIO_FRAG_SIZE,
	PSM2_INFO_QUERY_THRESH_IPS_DMA_FRAG_SIZE,
/*! Messages that are greater than the frag_size, but less than RNDV will
   be sent by eager message.
   Messages with messages sizes greater than or equal to RNDV will be
   sent by the rendezvous protocol message. */
	PSM2_INFO_QUERY_THRESH_IPS_RNDV,
	PSM2_INFO_QUERY_THRESH_IPS_END = PSM2_INFO_QUERY_THRESH_IPS_RNDV,

/*! Not shown here are the specific thresh queries supported by AMSH and
   SELF configs: */
	PSM2_INFO_QUERY_THRESH_AMSH_START,
	PSM2_INFO_QUERY_THRESH_AMSH_END = PSM2_INFO_QUERY_THRESH_AMSH_START,

	PSM2_INFO_QUERY_THRESH_SELF_START,
	PSM2_INFO_QUERY_THRESH_SELF_END = PSM2_INFO_QUERY_THRESH_SELF_START,
};

enum psm2_info_query_feature_mask
{
	/*! The following bit means that the libpsm2 _can_ support cuda.
	    If the PSM2_INFO_QUERY_FEATURE_MASK request is made and
	    the PSM2_INFO_QUERY_FEATURE_CUDA bit is not present, thne cuda
            is not supported. */
	PSM2_INFO_QUERY_FEATURE_CUDA      = (1 << 0),
};

/** @brief Union for info query arg type
 */
typedef union psm2_info_query_arg
{
	uint32_t                       unit;
	uint32_t                       port;
	size_t                         length;
	psm2_mq_t                      mq;
	psm2_epaddr_t                  epaddr;
	enum psm2_info_query_thresh_et mstq;
} psm2_info_query_arg_t;

/** @brief PSM2 info query
 *
 * Function that allows a client to interrogate PSM2 for various information.
 *
 * @param[in] psm2_info_query_t  What information is requested.
 * @param[out] void * out, where the information will be delivered on a
 *                         PSM2_OK return.
 * @param[in] size_t nargs, the number of following arguments.
 * @param[in] psm2_info_query_arg_t [], The arguments that are required for
 *                                      certain queries.  See documentation
 *                                      at @ref psm2_info_query_t for what
 *                                      arguments are required for what
 *                                      queries as well as what the type
 *                                      the output is expected to be.
 *
 * @retval PSM2_OK The out buffer has successfully been written with the
 * result of the query.
 */
psm2_error_t psm2_info_query(psm2_info_query_t, void *out,
			     size_t nargs, psm2_info_query_arg_t []);

/*! @} */

#ifdef __cplusplus
} /* extern "C" */
#endif
#endif