Skip to content

Commit 4dc1cb4

Browse files
committed
mpi/cxx: isolate internal headers from C++ bindings
This commit adds some glue code to support the C++ bindings and updates the bindings to use the new glue code. This protects our internal headers (which are C99) from C++. This is done as a quick workaround to compilation errors when the legacy C++ bindings are requested. Fixes #2055 Signed-off-by: Nathan Hjelm <[email protected]> (cherry picked from commit c6464ca)
1 parent 5cdbb7d commit 4dc1cb4

12 files changed

+418
-264
lines changed

ompi/errhandler/errhandler.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* -*- Mode: C; c-basic-offset:4 ; -*- */
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
22
/*
33
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
44
* University Research and Technology
@@ -12,7 +12,9 @@
1212
* All rights reserved.
1313
* Copyright (c) 2008-2012 Cisco Systems, Inc. All rights reserved.
1414
* Copyright (c) 2008-2009 Sun Microsystems, Inc. All rights reserved.
15-
* Copyright (c) 2015 Los Alamos National Security, LLC. All rights reserved.
15+
* Copyright (c) 2015-2016 Intel, Inc. All rights reserved.
16+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
17+
* reserved.
1618
* $COPYRIGHT$
1719
*
1820
* Additional copyrights may follow
@@ -91,9 +93,8 @@ struct ompi_errhandler_t;
9193
/**
9294
* C++ invocation function signature
9395
*/
94-
typedef void (ompi_errhandler_cxx_dispatch_fn_t)(struct ompi_errhandler_t *errhandler,
95-
void *handle, int *err_code,
96-
const char *message);
96+
typedef void (ompi_errhandler_cxx_dispatch_fn_t)(void *handle, int *err_code,
97+
const char *message, void *fn);
9798

9899
/**
99100
* Back-end type for MPI_Errorhandler.

ompi/errhandler/errhandler_invoke.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
12
/*
23
* Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
34
* University Research and Technology
@@ -11,6 +12,8 @@
1112
* All rights reserved.
1213
* Copyright (c) 2006-2012 Cisco Systems, Inc. All rights reserved.
1314
* Copyright (c) 2012 Oak Ridge National Labs. All rights reserved.
15+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
16+
* reserved.
1417
* $COPYRIGHT$
1518
*
1619
* Additional copyrights may follow
@@ -54,8 +57,8 @@ int ompi_errhandler_invoke(ompi_errhandler_t *errhandler, void *mpi_object,
5457
break;
5558

5659
case OMPI_ERRHANDLER_LANG_CXX:
57-
errhandler->eh_cxx_dispatch_fn(errhandler, &comm,
58-
&err_code, message);
60+
errhandler->eh_cxx_dispatch_fn(&comm, &err_code, message,
61+
errhandler->eh_comm_fn);
5962
break;
6063

6164
case OMPI_ERRHANDLER_LANG_FORTRAN:
@@ -74,8 +77,8 @@ int ompi_errhandler_invoke(ompi_errhandler_t *errhandler, void *mpi_object,
7477
break;
7578

7679
case OMPI_ERRHANDLER_LANG_CXX:
77-
errhandler->eh_cxx_dispatch_fn(errhandler, &win,
78-
&err_code, message);
80+
errhandler->eh_cxx_dispatch_fn(&win, &err_code, message,
81+
errhandler->eh_win_fn);
7982
break;
8083

8184
case OMPI_ERRHANDLER_LANG_FORTRAN:
@@ -94,8 +97,8 @@ int ompi_errhandler_invoke(ompi_errhandler_t *errhandler, void *mpi_object,
9497
break;
9598

9699
case OMPI_ERRHANDLER_LANG_CXX:
97-
errhandler->eh_cxx_dispatch_fn(errhandler, &file,
98-
&err_code, message);
100+
errhandler->eh_cxx_dispatch_fn(&file, &err_code, message,
101+
errhandler->eh_file_fn);
99102
break;
100103

101104
case OMPI_ERRHANDLER_LANG_FORTRAN:

ompi/mpi/cxx/Makefile.am

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ lib@OMPI_LIBMPI_NAME@_cxx_la_SOURCES = \
3636
intercepts.cc \
3737
comm.cc \
3838
datatype.cc \
39-
win.cc
39+
win.cc \
40+
cxx_glue.c
4041

4142
if OMPI_PROVIDE_MPI_FILE_INTERFACE
4243
lib@OMPI_LIBMPI_NAME@_cxx_la_SOURCES += \
@@ -77,7 +78,8 @@ headers = \
7778
group_inln.h \
7879
op_inln.h \
7980
errhandler_inln.h \
80-
status_inln.h
81+
status_inln.h \
82+
cxx_glue.h
8183

8284
ompidir = $(ompiincludedir)/ompi/mpi/cxx
8385
ompi_HEADERS = \

ompi/mpi/cxx/comm.cc

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// Copyright (c) 2004-2005 The Regents of the University of California.
1212
// All rights reserved.
1313
// Copyright (c) 2007-2008 Cisco Systems, Inc. All rights reserved.
14+
// Copyright (c) 2016 Los Alamos National Security, LLC. All rights
15+
// reserved.
1416
// $COPYRIGHT$
1517
//
1618
// Additional copyrights may follow
@@ -20,16 +22,9 @@
2022

2123
// do not include ompi_config.h because it kills the free/malloc defines
2224
#include "mpi.h"
25+
#include "ompi/constants.h"
2326
#include "ompi/mpi/cxx/mpicxx.h"
24-
25-
#ifdef HAVE_SCHED_H
26-
#include <sched.h>
27-
#endif
28-
29-
30-
#include "ompi/communicator/communicator.h"
31-
#include "ompi/attribute/attribute.h"
32-
#include "ompi/errhandler/errhandler.h"
27+
#include "cxx_glue.h"
3328

3429

3530
//
@@ -56,14 +51,7 @@ MPI::Comm::Comm(const Comm_Null& data) : Comm_Null(data)
5651
MPI::Errhandler
5752
MPI::Comm::Create_errhandler(MPI::Comm::_MPI2CPP_ERRHANDLERFN_* function)
5853
{
59-
MPI_Errhandler c_errhandler =
60-
ompi_errhandler_create(OMPI_ERRHANDLER_TYPE_COMM,
61-
(ompi_errhandler_generic_handler_fn_t*) function,
62-
OMPI_ERRHANDLER_LANG_CXX);
63-
c_errhandler->eh_cxx_dispatch_fn =
64-
(ompi_errhandler_cxx_dispatch_fn_t*)
65-
ompi_mpi_cxx_comm_errhandler_invoke;
66-
return c_errhandler;
54+
return ompi_cxx_errhandler_create_comm ((void *) function);
6755
}
6856

6957

@@ -77,20 +65,15 @@ MPI::Comm::do_create_keyval(MPI_Comm_copy_attr_function* c_copy_fn,
7765
void* extra_state, int &keyval)
7866
{
7967
int ret, count = 0;
80-
ompi_attribute_fn_ptr_union_t copy_fn;
81-
ompi_attribute_fn_ptr_union_t delete_fn;
8268
keyval_intercept_data_t *cxx_extra_state;
8369

8470
// If both the callbacks are C, then do the simple thing -- no
8571
// need for all the C++ machinery.
8672
if (NULL != c_copy_fn && NULL != c_delete_fn) {
87-
copy_fn.attr_communicator_copy_fn =
88-
(MPI_Comm_internal_copy_attr_function*) c_copy_fn;
89-
delete_fn.attr_communicator_delete_fn = c_delete_fn;
90-
ret = ompi_attr_create_keyval(COMM_ATTR, copy_fn, delete_fn,
91-
&keyval, extra_state, 0, NULL);
73+
ret = ompi_cxx_attr_create_keyval_comm (c_copy_fn, c_delete_fn, &keyval,
74+
extra_state, 0, NULL);
9275
if (MPI_SUCCESS != ret) {
93-
return OMPI_ERRHANDLER_INVOKE(MPI_COMM_WORLD, ret,
76+
return ompi_cxx_errhandler_invoke_comm(MPI_COMM_WORLD, ret,
9477
"MPI::Comm::Create_keyval");
9578
}
9679
}
@@ -107,8 +90,8 @@ MPI::Comm::do_create_keyval(MPI_Comm_copy_attr_function* c_copy_fn,
10790
cxx_extra_state = (keyval_intercept_data_t*)
10891
malloc(sizeof(keyval_intercept_data_t));
10992
if (NULL == cxx_extra_state) {
110-
return OMPI_ERRHANDLER_INVOKE(MPI_COMM_WORLD, MPI_ERR_NO_MEM,
111-
"MPI::Comm::Create_keyval");
93+
return ompi_cxx_errhandler_invoke_comm (MPI_COMM_WORLD, MPI_ERR_NO_MEM,
94+
"MPI::Comm::Create_keyval");
11295
}
11396
cxx_extra_state->c_copy_fn = c_copy_fn;
11497
cxx_extra_state->cxx_copy_fn = cxx_copy_fn;
@@ -131,26 +114,20 @@ MPI::Comm::do_create_keyval(MPI_Comm_copy_attr_function* c_copy_fn,
131114
}
132115
if (2 != count) {
133116
free(cxx_extra_state);
134-
return OMPI_ERRHANDLER_INVOKE(MPI_COMM_WORLD, MPI_ERR_ARG,
135-
"MPI::Comm::Create_keyval");
117+
return ompi_cxx_errhandler_invoke_comm (MPI_COMM_WORLD, MPI_ERR_ARG,
118+
"MPI::Comm::Create_keyval");
136119
}
137120

138121
// We do not call MPI_Comm_create_keyval() here because we need to
139122
// pass in the cxx_extra_state to the backend keyval creation so
140123
// that when the keyval is destroyed (i.e., when its refcount goes
141124
// to 0), the cxx_extra_state is free()'ed.
142-
143-
copy_fn.attr_communicator_copy_fn =
144-
(MPI_Comm_internal_copy_attr_function*)
145-
ompi_mpi_cxx_comm_copy_attr_intercept;
146-
delete_fn.attr_communicator_delete_fn =
147-
ompi_mpi_cxx_comm_delete_attr_intercept;
148-
ret = ompi_attr_create_keyval(COMM_ATTR, copy_fn, delete_fn,
149-
&keyval, cxx_extra_state, 0,
150-
cxx_extra_state);
125+
ret = ompi_cxx_attr_create_keyval_comm ((MPI_Comm_copy_attr_function *) ompi_mpi_cxx_comm_copy_attr_intercept,
126+
ompi_mpi_cxx_comm_delete_attr_intercept,
127+
&keyval, cxx_extra_state, 0, cxx_extra_state);
151128
if (OMPI_SUCCESS != ret) {
152-
return OMPI_ERRHANDLER_INVOKE(MPI_COMM_WORLD, ret,
153-
"MPI::Comm::Create_keyval");
129+
return ompi_cxx_errhandler_invoke_comm (MPI_COMM_WORLD, ret,
130+
"MPI::Comm::Create_keyval");
154131
}
155132

156133
return MPI_SUCCESS;

ompi/mpi/cxx/cxx_glue.c

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
2+
/*
3+
* Copyright (c) 2016 Los Alamos National Security, LLC. All rights
4+
* reserved.
5+
* $COPYRIGHT$
6+
*
7+
* Additional copyrights may follow
8+
*
9+
* $HEADER$
10+
*/
11+
12+
#include "ompi_config.h"
13+
14+
#include "ompi/communicator/communicator.h"
15+
#include "ompi/attribute/attribute.h"
16+
#include "ompi/errhandler/errhandler.h"
17+
#include "ompi/file/file.h"
18+
#include "opal/class/opal_list.h"
19+
#include "cxx_glue.h"
20+
21+
typedef struct ompi_cxx_intercept_file_extra_state_item_t {
22+
opal_list_item_t super;
23+
ompi_cxx_intercept_file_extra_state_t state;
24+
} ompi_cxx_intercept_file_extra_state_item_t;
25+
26+
OBJ_CLASS_DECLARATION(ompi_cxx_intercept_file_extra_state_item_t);
27+
OBJ_CLASS_INSTANCE(ompi_cxx_intercept_file_extra_state_item_t, opal_list_item_t,
28+
NULL, NULL);
29+
30+
ompi_cxx_communicator_type_t ompi_cxx_comm_get_type (MPI_Comm comm)
31+
{
32+
if (OMPI_COMM_IS_GRAPH(comm)) {
33+
return OMPI_CXX_COMM_TYPE_GRAPH;
34+
} else if (OMPI_COMM_IS_CART(comm)) {
35+
return OMPI_CXX_COMM_TYPE_CART;
36+
} else if (OMPI_COMM_IS_INTRA(comm)) {
37+
return OMPI_CXX_COMM_TYPE_INTRACOMM;
38+
} else if (OMPI_COMM_IS_INTER(comm)) {
39+
return OMPI_CXX_COMM_TYPE_INTERCOMM;
40+
}
41+
42+
return OMPI_CXX_COMM_TYPE_UNKNOWN;
43+
}
44+
45+
int ompi_cxx_errhandler_invoke_comm (MPI_Comm comm, int ret, const char *message)
46+
{
47+
return OMPI_ERRHANDLER_INVOKE (comm, ret, message);
48+
}
49+
50+
int ompi_cxx_errhandler_invoke_file (MPI_File file, int ret, const char *message)
51+
{
52+
return OMPI_ERRHANDLER_INVOKE (file, ret, message);
53+
}
54+
55+
int ompi_cxx_attr_create_keyval_comm (MPI_Comm_copy_attr_function *copy_fn,
56+
MPI_Comm_delete_attr_function* delete_fn, int *keyval, void *extra_state,
57+
int flags, void *bindings_extra_state)
58+
{
59+
ompi_attribute_fn_ptr_union_t copy_fn_u = {.attr_communicator_copy_fn =
60+
(MPI_Comm_internal_copy_attr_function *) copy_fn};
61+
ompi_attribute_fn_ptr_union_t delete_fn_u = {.attr_communicator_delete_fn =
62+
(MPI_Comm_delete_attr_function *) delete_fn};
63+
64+
return ompi_attr_create_keyval (COMM_ATTR, copy_fn_u, delete_fn_u, keyval, extra_state, 0, bindings_extra_state);
65+
}
66+
67+
int ompi_cxx_attr_create_keyval_win (MPI_Win_copy_attr_function *copy_fn,
68+
MPI_Win_delete_attr_function* delete_fn, int *keyval, void *extra_state,
69+
int flags, void *bindings_extra_state)
70+
{
71+
ompi_attribute_fn_ptr_union_t copy_fn_u = {.attr_win_copy_fn =
72+
(MPI_Win_internal_copy_attr_function *) copy_fn};
73+
ompi_attribute_fn_ptr_union_t delete_fn_u = {.attr_win_delete_fn =
74+
(MPI_Win_delete_attr_function *) delete_fn};
75+
76+
return ompi_attr_create_keyval (WIN_ATTR, copy_fn_u, delete_fn_u, keyval, extra_state, 0, NULL);
77+
}
78+
79+
int ompi_cxx_attr_create_keyval_type (MPI_Type_copy_attr_function *copy_fn,
80+
MPI_Type_delete_attr_function* delete_fn, int *keyval, void *extra_state,
81+
int flags, void *bindings_extra_state)
82+
{
83+
ompi_attribute_fn_ptr_union_t copy_fn_u = {.attr_datatype_copy_fn =
84+
(MPI_Type_internal_copy_attr_function *) copy_fn};
85+
ompi_attribute_fn_ptr_union_t delete_fn_u = {.attr_datatype_delete_fn =
86+
(MPI_Type_delete_attr_function *) delete_fn};
87+
88+
return ompi_attr_create_keyval (TYPE_ATTR, copy_fn_u, delete_fn_u, keyval, extra_state, 0, NULL);
89+
}
90+
91+
MPI_Errhandler ompi_cxx_errhandler_create_comm (void *fn)
92+
{
93+
ompi_errhandler_t *errhandler;
94+
errhandler = ompi_errhandler_create(OMPI_ERRHANDLER_TYPE_COMM,
95+
(ompi_errhandler_generic_handler_fn_t *) fn,
96+
OMPI_ERRHANDLER_LANG_CXX);
97+
errhandler->eh_cxx_dispatch_fn =
98+
(ompi_errhandler_cxx_dispatch_fn_t *) ompi_mpi_cxx_comm_errhandler_invoke;
99+
return errhandler;
100+
}
101+
102+
MPI_Errhandler ompi_cxx_errhandler_create_win (void *fn)
103+
{
104+
ompi_errhandler_t *errhandler;
105+
errhandler = ompi_errhandler_create(OMPI_ERRHANDLER_TYPE_WIN,
106+
(ompi_errhandler_generic_handler_fn_t *) fn,
107+
OMPI_ERRHANDLER_LANG_CXX);
108+
errhandler->eh_cxx_dispatch_fn =
109+
(ompi_errhandler_cxx_dispatch_fn_t *) ompi_mpi_cxx_win_errhandler_invoke;
110+
return errhandler;
111+
}
112+
113+
MPI_Errhandler ompi_cxx_errhandler_create_file (void *fn)
114+
{
115+
ompi_errhandler_t *errhandler;
116+
errhandler = ompi_errhandler_create(OMPI_ERRHANDLER_TYPE_FILE,
117+
(ompi_errhandler_generic_handler_fn_t *) fn,
118+
OMPI_ERRHANDLER_LANG_CXX);
119+
errhandler->eh_cxx_dispatch_fn =
120+
(ompi_errhandler_cxx_dispatch_fn_t *) ompi_mpi_cxx_file_errhandler_invoke;
121+
return errhandler;
122+
}
123+
124+
ompi_cxx_intercept_file_extra_state_t
125+
*ompi_cxx_new_intercept_state (void *read_fn_cxx, void *write_fn_cxx, void *extent_fn_cxx,
126+
void *extra_state_cxx)
127+
{
128+
ompi_cxx_intercept_file_extra_state_item_t *intercept;
129+
130+
intercept = OBJ_NEW(ompi_cxx_intercept_file_extra_state_item_t);
131+
if (NULL == intercept) {
132+
return NULL;
133+
}
134+
135+
opal_list_append(&ompi_registered_datareps, &intercept->super);
136+
intercept->state.read_fn_cxx = read_fn_cxx;
137+
intercept->state.write_fn_cxx = write_fn_cxx;
138+
intercept->state.extent_fn_cxx = extent_fn_cxx;
139+
intercept->state.extra_state_cxx = extra_state_cxx;
140+
141+
return &intercept->state;
142+
}
143+
144+
void ompi_cxx_errhandler_set_dispatch_fn (ompi_errhandler_t *errhandler,
145+
ompi_errhandler_cxx_dispatch_fn_t *dispatch_fn)
146+
{
147+
errhandler->eh_cxx_dispatch_fn = dispatch_fn;
148+
}
149+
150+
void ompi_cxx_errhandler_set_callbacks (struct ompi_errhandler_t *errhandler, MPI_Comm_errhandler_function *eh_comm_fn,
151+
ompi_file_errhandler_fn *eh_file_fn, MPI_Win_errhandler_function *eh_win_fn)
152+
{
153+
errhandler->eh_comm_fn = eh_comm_fn;
154+
#if OMPI_PROVIDE_MPI_FILE_INTERFACE
155+
errhandler->eh_file_fn = eh_file_fn;
156+
#endif
157+
errhandler->eh_win_fn = eh_win_fn;
158+
}

0 commit comments

Comments
 (0)