Skip to content

Commit 63dddbc

Browse files
ggouaillardetjsquyres
authored andcommitted
fortran: fix common symbol sizes and alignments
Refs open-mpi#13043 Thanks MJ Rutter for the report Signed-off-by: Gilles Gouaillardet <gilles@rist.or.jp>
1 parent 81207f5 commit 63dddbc

File tree

4 files changed

+75
-20
lines changed

4 files changed

+75
-20
lines changed

config/ompi_fortran_get_alignment.m4

+40
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ dnl Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
1010
dnl University of Stuttgart. All rights reserved.
1111
dnl Copyright (c) 2004-2005 The Regents of the University of California.
1212
dnl All rights reserved.
13+
dnl Copyright (c) 2025 Research Organization for Information Science
14+
dnl and Technology (RIST). All rights reserved.
1315
dnl Copyright (c) 2010-2012 Cisco Systems, Inc. All rights reserved.
1416
dnl $COPYRIGHT$
1517
dnl
@@ -178,3 +180,41 @@ end program]])],
178180
AS_VAR_COPY([$2], [type_var])
179181
AS_VAR_POPDEF([type_var])dnl
180182
])dnl
183+
184+
# OMPI_FORTRAN_GET_COMMON_ALIGNMENT(variable to set)
185+
# ------------------------------------------
186+
AC_DEFUN([OMPI_FORTRAN_GET_COMMON_ALIGNMENT],[
187+
AS_IF([test $OMPI_TRY_FORTRAN_BINDINGS -gt $OMPI_FORTRAN_NO_BINDINGS],
188+
[AC_CACHE_CHECK([alignment of Fortran common], ompi_cv_fortran_common_alignment,
189+
[AC_LANG_PUSH([Fortran])
190+
AC_LINK_IFELSE([AC_LANG_SOURCE([[ program falignment
191+
CHARACTER A,B
192+
COMMON /AA/A
193+
COMMON /BB/B
194+
OPEN(UNIT=10, FILE="conftestval")
195+
if (LOC(A) > LOC(B)) then
196+
write (10,'(I5)') LOC(A)-LOC(B)
197+
else
198+
write (10,'(I5)') LOC(B)-LOC(A)
199+
endif
200+
CLOSE(10)
201+
202+
end program]])],
203+
[AS_IF([test "$cross_compiling" = "yes"],
204+
[AC_MSG_ERROR([Can not determine common alignment when cross-compiling])],
205+
[OPAL_LOG_COMMAND([./conftest],
206+
[AS_VAR_SET(ompi_cv_fortran_common_alignment, [`cat conftestval`])],
207+
[AC_MSG_ERROR([Could not determine common alignment])])])],
208+
209+
[AC_MSG_WARN([Could not determine common alignment])
210+
AC_MSG_WARN([See config.log for details])
211+
AC_MSG_ERROR([Cannot continue])])
212+
rm -rf conftest* *.mod 2> /dev/null
213+
AC_LANG_POP([Fortran])])
214+
215+
AS_VAR_COPY([$1], [ompi_cv_fortran_common_alignment])],
216+
[AC_MSG_CHECKING([Fortran common alignment])
217+
$1=0
218+
AC_MSG_RESULT([skipped])])
219+
220+
])dnl

config/ompi_setup_mpi_fortran.m4

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ dnl Copyright (c) 2006-2008 Sun Microsystems, Inc. All rights reserved.
1515
dnl Copyright (c) 2006-2007 Los Alamos National Security, LLC. All rights
1616
dnl reserved.
1717
dnl Copyright (c) 2009 Oak Ridge National Labs. All rights reserved.
18-
dnl Copyright (c) 2014-2021 Research Organization for Information Science
18+
dnl Copyright (c) 2014-2025 Research Organization for Information Science
1919
dnl and Technology (RIST). All rights reserved.
2020
dnl Copyright (c) 2016-2022 IBM Corporation. All rights reserved.
2121
dnl Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
@@ -138,6 +138,9 @@ AC_DEFUN([OMPI_SETUP_MPI_FORTRAN],[
138138
AC_DEFINE([ompi_fortran_bogus_type_t], [int],
139139
[A bogus type that allows us to have sentinel type values that are still valid])
140140

141+
OMPI_FORTRAN_GET_COMMON_ALIGNMENT([OMPI_FORTRAN_COMMON_ALIGNMENT])
142+
AC_SUBST([OMPI_FORTRAN_COMMON_ALIGNMENT])
143+
141144
# We want to set the #define's for all of these, so invoke the macros
142145
# regardless of whether we have F77 support or not.
143146
OMPI_FORTRAN_CHECK([CHARACTER], [yes],

ompi/include/Makefile.am

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# All rights reserved.
1212
# Copyright (c) 2006-2015 Cisco Systems, Inc. All rights reserved.
1313
# Copyright (c) 2009-2011 Oak Ridge National Labs. All rights reserved.
14-
# Copyright (c) 2014-2021 Research Organization for Information Science
14+
# Copyright (c) 2014-2025 Research Organization for Information Science
1515
# and Technology (RIST). All rights reserved.
1616
# Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
1717
# Copyright (c) 2022 IBM Corporation. All rights reserved.
@@ -111,7 +111,10 @@ mpif-c-constants-decl.h:
111111
--caps $(OMPI_FORTRAN_CAPS) \
112112
--plain $(OMPI_FORTRAN_PLAIN) \
113113
--single $(OMPI_FORTRAN_SINGLE_UNDERSCORE) \
114-
--double $(OMPI_FORTRAN_DOUBLE_UNDERSCORE)
114+
--double $(OMPI_FORTRAN_DOUBLE_UNDERSCORE) \
115+
--status-size $(OMPI_FORTRAN_STATUS_SIZE) \
116+
--align $(OMPI_FORTRAN_COMMON_ALIGNMENT)
117+
115118

116119
if WANT_INSTALL_HEADERS
117120
ompidir = $(ompiincludedir)

ompi/mpi/fortran/base/gen-mpi-mangling.py

+26-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22
#
3-
# Copyright (c) 2015 Research Organization for Information Science
3+
# Copyright (c) 2015-2025 Research Organization for Information Science
44
# and Technology (RIST). All rights reserved.
55
# Copyright (c) 2015-2020 Cisco Systems, Inc. All rights reserved.
66
# Copyright (c) 2025 Jeffrey M. Squyres. All rights reserved.
@@ -22,7 +22,7 @@
2222

2323
# Header comment block
2424
header_comment = """/*
25-
* Copyright (c) 2015 Research Organization for Information Science
25+
* Copyright (c) 2015-2025 Research Organization for Information Science
2626
* and Technology (RIST). All rights reserved.
2727
* Copyright (c) 2015-2020 Cisco Systems, Inc. All rights reserved.
2828
* $COPYRIGHT$
@@ -32,7 +32,7 @@
3232
"""
3333

3434
fortran_header_comment = """!
35-
! Copyright (c) 2015 Research Organization for Information Science
35+
! Copyright (c) 2015-2025 Research Organization for Information Science
3636
! and Technology (RIST). All rights reserved.
3737
! Copyright (c) 2015-2020 Cisco Systems, Inc. All rights reserved.
3838
! $COPYRIGHT$
@@ -43,25 +43,25 @@
4343
def get_fortran_constants(args):
4444
return {
4545
'bottom': {
46-
'c_type': "int",
46+
'c_type': "MPI_Fint",
4747
'c_name': "mpi_fortran_bottom",
4848
'f_type': "integer",
4949
'f_name': "MPI_BOTTOM",
5050
},
5151
'in_place': {
52-
'c_type': "int",
52+
'c_type': "MPI_Fint",
5353
'c_name': "mpi_fortran_in_place",
5454
'f_type': "integer",
5555
'f_name': "MPI_IN_PLACE",
5656
},
5757
'unweighted': {
58-
'c_type': "int",
58+
'c_type': "MPI_Fint",
5959
'c_name': "mpi_fortran_unweighted",
6060
'f_type': "integer, dimension(1)",
6161
'f_name': "MPI_UNWEIGHTED",
6262
},
6363
'weights_empty': {
64-
'c_type': "int",
64+
'c_type': "MPI_Fint",
6565
'c_name': "mpi_fortran_weights_empty",
6666
'f_type': "integer, dimension(1)",
6767
'f_name': "MPI_WEIGHTS_EMPTY",
@@ -79,19 +79,21 @@ def get_fortran_constants(args):
7979
'f_name': "MPI_ARGVS_NULL",
8080
},
8181
'errcodes_ignore': {
82-
'c_type': "int",
82+
'c_type': "MPI_Fint",
8383
'c_name': "mpi_fortran_errcodes_ignore",
8484
'f_type': "integer, dimension(1)",
8585
'f_name': "MPI_ERRCODES_IGNORE",
8686
},
8787
'status_ignore': {
88-
'c_type': "int",
88+
'c_type': "MPI_Fint",
89+
'c_dim' : f"[{args.status_size}]",
8990
'c_name': "mpi_fortran_status_ignore",
9091
'f_type': "type(MPI_STATUS)",
9192
'f_name': "MPI_STATUS_IGNORE",
9293
},
9394
'statuses_ignore': {
94-
'c_type': "int",
95+
'c_type': "MPI_Fint",
96+
'c_dim' : f"[{args.status_size}]",
9597
'c_name': "mpi_fortran_statuses_ignore",
9698
'f_type': "type(MPI_STATUS)",
9799
'f_name': "MPI_STATUSES_IGNORE(1)",
@@ -111,7 +113,7 @@ def mangle(name, mangling_type):
111113
else:
112114
raise ValueError("Unknown name mangling type")
113115

114-
def gen_c_constants_decl(mangling_type, fortran_constants):
116+
def gen_c_constants_decl(mangling_type, fortran_constants, args):
115117
# Generates the mpif-c-constants-decl.h file
116118
with open(file_c_constants_decl, "w") as f:
117119
f.write("/* WARNING: This is a generated file! Edits will be lost! */\n")
@@ -124,11 +126,12 @@ def gen_c_constants_decl(mangling_type, fortran_constants):
124126
for key in sorted(fortran_constants.keys()):
125127
const = fortran_constants[key]
126128
mangled_name = mangle(const['c_name'], mangling_type)
127-
f.write(f"extern {const['c_type']} {mangled_name};\n")
129+
dim = const.get('c_dim', '')
130+
f.write(f"extern {const['c_type']} {mangled_name}{dim};\n")
128131
f.write(f"#define OMPI_IS_FORTRAN_{key.upper()}(addr) \\\n")
129132
f.write(f" (addr == (void*) &{mangled_name})\n\n")
130133

131-
def gen_c_constants(mangling_type, fortran_constants):
134+
def gen_c_constants(mangling_type, fortran_constants, args):
132135
# Generates the mpif-c-constants.h file
133136
with open(file_c_constants, "w") as f:
134137
f.write("/* WARNING: This is a generated file! Edits will be lost! */\n")
@@ -137,8 +140,10 @@ def gen_c_constants(mangling_type, fortran_constants):
137140

138141
for key in sorted(fortran_constants.keys()):
139142
const = fortran_constants[key]
143+
align = f" __opal_attribute_aligned__({args.align}) " if args.align else ' '
144+
dim = const.get('c_dim', '')
140145
mangled_name = mangle(const['c_name'], mangling_type)
141-
f.write(f"{const['c_type']} {mangled_name};\n")
146+
f.write(f"{const['c_type']}{align}{mangled_name}{dim};\n")
142147

143148
def gen_f08_types(mangling_type, fortran_constants):
144149
# Generates the mpif-f08-types.h file
@@ -163,6 +168,10 @@ def main():
163168
help='Use single underscore suffix mangling')
164169
parser.add_argument('--double', type=int, default=0,
165170
help='Use double underscore suffix mangling')
171+
parser.add_argument('--status-size', type=int, default=0,
172+
help='Length of the Fortran MPI_Status array')
173+
parser.add_argument('--align', type=int, default=0,
174+
help='Alignment of Fortran intengers')
166175

167176
args = parser.parse_args()
168177

@@ -190,9 +199,9 @@ def main():
190199

191200
# Generate the files based on the selected mangling type
192201
try:
193-
fortran_constants = get_fortran_constants()
194-
gen_c_constants_decl(mangling_type, fortran_constants)
195-
gen_c_constants(mangling_type, fortran_constants)
202+
fortran_constants = get_fortran_constants(args)
203+
gen_c_constants_decl(mangling_type, fortran_constants, args)
204+
gen_c_constants(mangling_type, fortran_constants, args)
196205
gen_f08_types(mangling_type, fortran_constants)
197206
print(f"Generated files with '{mangling_type}' mangling.")
198207
sys.exit(0)

0 commit comments

Comments
 (0)