10BC0 BUG: f2py does not generate wrappers for modules when compiling multiple files (regression) · Issue #25337 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: f2py does not generate wrappers for modules when compiling multiple files (regression) #25337

@andrea-bia

Description

@andrea-bia

Describe the issue:

In the latest version (2.0.0.dev0+git20231201.44570a8) of the codebase, I have observed a regression regarding the access to module variables when compiling more fortran files.
Specifically, after compiling two Fortran files:
data.F90

module data
   real(8) :: shift
contains
   subroutine set_shift(in_shift)
      real(8), intent(in) :: in_shift
      shift = in_shift
   end subroutine set_shift
end module data

and use_data.F90

subroutine shift_a(dim_a, a)
    use data, only: shift
    integer, intent(in) :: dim_a
    real(8), intent(inout), dimension(dim_a) :: a
    a = a + shift
end subroutine shift_a

with the command:

f2py -m example -c use_data.F90 data.F90 

we cannot access example.data:

>>> import example
>>> example.data
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'example' has no attribute 'data'
2.0.0.dev0+git20231201.44570a8
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /opt/intel//oneapi/mkl/latest/lib/pkgconfig/../../include
    lib directory: /opt/intel//oneapi/mkl/latest/lib/pkgconfig/../../lib/intel64
    name: mkl-dynamic-lp64-seq
    openblas configuration: unknown
    pc file directory: /usr/local/share/pkgconfig
    version: '2023.1'
  lapack:
    detection method: internal
    found: true
    include directory: unknown
    lib directory: unknown
    name: dep140360165707840
    openblas configuration: unknown
    pc file directory: unknown
    version: 2.0.0.dev0+git20231201.44570a8
Compilers:
  c:
    commands: cc
    linker: ld.bfd
    name: gcc
    version: 13.2.1
  c++:
    commands: c++
    linker: ld.bfd
    name: gcc
    version: 13.2.1
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 3.0.6
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
  host:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
Python Information:
  path: /usr/bin/python3
  version: '3.12'
SIMD Extensions:
  baseline:
  - SSE
  - SSE2
  - SSE3
  found:
  - SSSE3
  - SSE41
  - POPCNT
  - SSE42
  - AVX
  - F16C
  - FMA3
  - AVX2
  not found:
  - AVX512F
  - AVX512CD
  - AVX512_KNL
  - AVX512_KNM
  - AVX512_SKX
  - AVX512_CLX
  - AVX512_CNL
  - AVX512_ICL
  - AVX512_SPR

But we can in version 1.26.2:

>>> import example        
>>> example.data  
<fortran object>
1.26.2
Build Dependencies:
  blas:
    detection method: pkgconfig
    found: true
    include directory: /usr/local/include
    lib directory: /usr/local/lib
    name: openblas64
    openblas configuration: USE_64BITINT=1 DYNAMIC_ARCH=1 DYNAMIC_OLDER= NO_CBLAS=
      NO_LAPACK= NO_LAPACKE= NO_AFFINITY=1 USE_OPENMP= HASWELL MAX_THREADS=2
    pc file directory: /usr/local/lib/pkgconfig
    version: 0.3.23.dev
  lapack:
    detection method: internal
    found: true
    include directory: unknown
    lib directory: unknown
    name: dep140640983012528
    openblas configuration: unknown
    pc file directory: unknown
    version: 1.26.2
Compilers:
  c:
    commands: cc
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  c++:
    commands: c++
    linker: ld.bfd
    name: gcc
    version: 10.2.1
  cython:
    commands: cython
    linker: cython
    name: cython
    version: 3.0.5
Machine Information:
  build:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
  host:
    cpu: x86_64
    endian: little
    family: x86_64
    system: linux
Python Information:
  path: /opt/python/cp310-cp310/bin/python
  version: '3.10'
SIMD Extensions:
  baseline:
  - SSE
  - SSE2
  - SSE3
  found:
  - SSSE3
  - SSE41
  - POPCNT
  - SSE42
  - AVX
  - F16C
  - FMA3
  - AVX2
  not found:
  - AVX512F
  - AVX512CD
  - AVX512_KNL
  - AVX512_KNM
  - AVX512_SKX
  - AVX512_CLX
  - AVX512_CNL
  - AVX512_IC

This appears related to this added to fix this bug regarding common blocks.
I don't think the observed behavior is intentional in general but please let me know if I am missing something.

Reproduce the code example:

data = '''
module data
   real(8) :: shift
contains
   subroutine set_shift(in_shift)
      real(8), intent(in) :: in_shift
      shift = in_shift
   end subroutine set_shift
end module data
'''

use_data = '''
subroutine shift_a(dim_a, a)
    use data, only: shift
    integer, intent(in) :: dim_a
    real(8), intent(inout), dimension(dim_a) :: a
    a = a + shift
end subroutine shift_a
'''

with open('data.F90', 'w') as f:
    f.write(data)

with open('use_data.F90', 'w') as f:
    f.write(use_data)

import subprocess

subprocess.call(['f2py', '-c', '-m', 'example', 'use_data.F90', 'data.F90'])

import importlib

example = importlib.import_module('example')

example.data.set_shift(1.0)

import numpy as np

a = np.array([1.0, 2.0, 3.0], dtype=np.float64)

print(a)

example.shift_a(a)

print(a)

Error message:

No response

Python and NumPy Versions:

2.0.0.dev0+git20231201.44570a8
3.12.0 (main, Oct 2 2023, 00:00:00) [GCC 13.2.1 20230918 (Red Hat 13.2.1-3)]

Runtime Environment:

No response

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0