Skip to content

Commit ad7adec

Browse files
committed
cffi: typedef shmem_{ctx|team}_t as an opaque struct type
This allows for OpenSHMEM implementations declaring ctx/team types as either a pointer types or an integral types. Declaring `shmem_{ctx|team}_t` as an opaque struct type has an annoying side-effect: now ctx/team handles can no longer be compared for equality. Therefore, add a couple auxiliary functions `eq_{ctx|team}` to compare handles for equality. Additionally, in case we need them in the future, add functions to convert to/from integer values.
1 parent 1400715 commit ad7adec

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

src/ffibuilder.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
def build_api(
1212
module="api",
1313
shmem_h="shmem.h",
14-
shmem_ctx_t='...*',
15-
shmem_team_t='...*',
14+
shmem_ctx_t='struct{...;}',
15+
shmem_team_t='struct{...;}',
1616
):
1717
from apicodegen import generate
1818
ffi = cffi.FFI()
@@ -23,6 +23,12 @@ def build_api(
2323
ffi.cdef(code)
2424
for code in generate():
2525
ffi.cdef(code)
26+
for hdl in ('ctx', 'team'):
27+
ffi.cdef(f"""
28+
bool eq_{hdl}(shmem_{hdl}_t, shmem_{hdl}_t);
29+
uintptr_t {hdl}2id(shmem_{hdl}_t);
30+
shmem_{hdl}_t id2{hdl}(uintptr_t);
31+
""")
2632
ffi.cdef("""
2733
int shmem_alltoallsmem_x(
2834
shmem_team_t team,

src/libshmem.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
/* --- */
5252

53+
#include "libshmem/hdltypes.h"
5354
#include "libshmem/fallback.h"
5455
#include "libshmem/initfini.h"
5556
#include "libshmem/memalloc.h"

src/libshmem/hdltypes.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#define eq_ctx(a, b) ((a) == (b))
2+
#define ctx2id(c) ((uintptr_t)(c))
3+
#define id2ctx(i) ((shmem_ctx_t)(i))
4+
5+
#define eq_team(a, b) ((a) == (b))
6+
#define team2id(t) ((uintptr_t)(t))
7+
#define id2team(i) ((shmem_team_t)i)

src/shmem4py/shmem.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,15 +281,15 @@ def __new__(
281281
def __eq__(self, other: Any) -> bool:
282282
if not isinstance(other, Ctx):
283283
return NotImplemented
284-
return self.ob_ctx == other.ob_ctx
284+
return lib.eq_ctx(self.ob_ctx, other.ob_ctx)
285285

286286
def __ne__(self, other: Any) -> bool:
287287
if not isinstance(other, Ctx):
288288
return NotImplemented
289-
return self.ob_ctx != other.ob_ctx
289+
return not lib.eq_ctx(self.ob_ctx, other.ob_ctx)
290290

291291
def __bool__(self) -> bool:
292-
return self.ob_ctx != lib.SHMEM_CTX_INVALID
292+
return not lib.eq_ctx(self.ob_ctx, lib.SHMEM_CTX_INVALID)
293293

294294
def __enter__(self) -> Ctx:
295295
return self
@@ -331,9 +331,9 @@ def destroy(self) -> None:
331331
return
332332
ctx = self.ob_ctx
333333
self.ob_ctx = lib.SHMEM_CTX_INVALID
334-
if ctx == lib.SHMEM_CTX_DEFAULT:
334+
if lib.eq_ctx(ctx, lib.SHMEM_CTX_DEFAULT):
335335
return
336-
if ctx == lib.SHMEM_CTX_INVALID:
336+
if lib.eq_ctx(ctx, lib.SHMEM_CTX_INVALID):
337337
return
338338
lib.shmem_ctx_destroy(ctx)
339339

@@ -400,15 +400,15 @@ def __new__(
400400
def __eq__(self, other: Any) -> bool:
401401
if not isinstance(other, Team):
402402
return NotImplemented
403-
return self.ob_team == other.ob_team
403+
return lib.eq_team(self.ob_team, other.ob_team)
404404

405405
def __ne__(self, other: Any) -> bool:
406406
if not isinstance(other, Team):
407407
return NotImplemented
408-
return self.ob_team != other.ob_team
408+
return not lib.eq_team(self.ob_team, other.ob_team)
409409

410410
def __bool__(self) -> bool:
411-
return self.ob_team != lib.SHMEM_TEAM_INVALID
411+
return not lib.eq_team(self.ob_team, lib.SHMEM_TEAM_INVALID)
412412

413413
def __enter__(self) -> Team:
414414
return self
@@ -426,11 +426,11 @@ def destroy(self) -> None:
426426
return
427427
team = self.ob_team
428428
self.ob_team = lib.SHMEM_TEAM_INVALID
429-
if team == lib.SHMEM_TEAM_WORLD:
429+
if lib.eq_team(team, lib.SHMEM_TEAM_WORLD):
430430
return
431-
if team == lib.SHMEM_TEAM_SHARED:
431+
if lib.eq_team(team, lib.SHMEM_TEAM_SHARED):
432432
return
433-
if team == lib.SHMEM_TEAM_INVALID:
433+
if lib.eq_team(team, lib.SHMEM_TEAM_INVALID):
434434
return
435435
lib.shmem_team_destroy(team)
436436

0 commit comments

Comments
 (0)