Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile Lapack #1232

Open
Tracked by #1251
certik opened this issue Jan 28, 2023 · 23 comments
Open
Tracked by #1251

Compile Lapack #1232

certik opened this issue Jan 28, 2023 · 23 comments
Assignees
Labels
Code to be Compiled Projects to be compiled using LFortran compiler

Comments

@certik
Copy link
Contributor

certik commented Jan 28, 2023

Here is how to get started:

git clone https://github.com/Reference-LAPACK/lapack
cd lapack
mkdir build
cd build
FC=gfortran cmake -DCBLAS=no -DLAPACKE=no -DBUILD_TESTING=no -DBUILD_DOUBLE=no -DBUILD_COMPLEX=no -DBUILD_COMPLEX16=no ..
make

With LFortran you can do:

FC=lfortran cmake -DCBLAS=no -DLAPACKE=no -DBUILD_TESTING=no -DBUILD_DOUBLE=no -DBUILD_COMPLEX=no -DBUILD_COMPLEX16=no -DLAPACKE_WITH_TMG=no -DCMAKE_Fortran_FLAGS="--fixed-form"  ..

I also had to apply the following patch:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 29e489ffe..e919a8eac 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -444,7 +444,7 @@ endif()
 set(LAPACK_INSTALL_EXPORT_NAME ${LAPACK_INSTALL_EXPORT_NAME_CACHE})
 unset(LAPACK_INSTALL_EXPORT_NAME_CACHE)
 
-add_subdirectory(LAPACKE)
+#add_subdirectory(LAPACKE)
 
 
 #-------------------------------------
@certik
Copy link
Contributor Author

certik commented Jan 30, 2023

The cmake build system seems to be running some diagnostic Fortran compilation at the beginning to determine capabilities and it fails with LFortran (even wiht LAPACKE disabled with the patch above). It would be good to get things working so that one can just use their cmake build system. But if it is too much work, we can write our own build system, as the most important task is to get the actual Lapack Fortran files to compile and run correctly. Others can then help us get all the corner cases working with regards to the build system.

This was referenced Feb 1, 2023
@meow464
Copy link
Contributor

meow464 commented Aug 2, 2023

Lfortran seems to pass the cmake tests now.

@meow464
Copy link
Contributor

meow464 commented Aug 2, 2023

For some reason cmake is not calling lfortran with --fixed-form, as lapack is a mix of 77 and 90 we get some errors.

@meow464
Copy link
Contributor

meow464 commented Aug 3, 2023

I'm working on adding support for lfortran on cmake, this should take care of these problems and #1538

@meow464
Copy link
Contributor

meow464 commented Aug 12, 2023

I'm using cmake .. -DCMAKE_Fortran_COMPILER=lfortran -DCBLAS=no -DLAPACKE=no -DBUILD_TESTING=no -DBUILD_DOUBLE=no -DBUILD_COMPLEX=no -DBUILD_COMPLEX16=no -DLAPACKE_WITH_TMG=no -DCMAKE_Fortran_PREPROCESS=yes -DCMAKE_Fortran_FLAGS="--fixed-form-infer"

@meow464
Copy link
Contributor

meow464 commented Aug 12, 2023

semantic error: Function 'radix' not found or not implemented yet (if it is intrinsic)
   --> /home/tuco/Projects/LFortran/lapack/BLAS/SRC/snrm2.f90:103:39
    |
103 |    real(wp), parameter :: tsml = real(radix(0._wp), wp)**ceiling( &
    |                                       ^^^^^^^^^^^^ 

I will work on implementing radix next, from what I understand it should always return 2.

@meow464
Copy link
Contributor

meow464 commented Nov 10, 2023

cmake .. -DCMAKE_Fortran_COMPILER=lfortran -DCBLAS=no -DLAPACKE=no -DBUILD_TESTING=no -DBUILD_DOUBLE=no -DBUILD_COMPLEX=no -DBUILD_COMPLEX16=no -DLAPACKE_WITH_TMG=no -DCMAKE_Fortran_PREPROCESS=yes -DCMAKE_Fortran_FLAGS="--fixed-form-infer --implicit-interface"

@certik
Copy link
Contributor Author

certik commented Nov 10, 2023

@meow464 can you post exact instructions? Here is what I tried on macOS:

$ git clone https://github.com/Reference-LAPACK/lapack
$ cd lapack
$ mkdir build
$ cd build
$ cmake .. -DCMAKE_Fortran_COMPILER=lfortran -DCBLAS=no -DLAPACKE=no -DBUILD_TESTING=no -DBUILD_DOUBLE=no -DBUILD_COMPLEX=no -DBUILD_COMPLEX16=no -DLAPACKE_WITH_TMG=no -DCMAKE_Fortran_PREPROCESS=yes -DCMAKE_Fortran_FLAGS="--fixed-form-infer --implicit-interface"

-- The C compiler identification is AppleClang 13.0.0.13000029
-- The CXX compiler identification is AppleClang 13.0.0.13000029
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Setting build type to 'Release' as none was specified.
-- Checking if build type is 'Coverage'
-- Checking if build type is 'Coverage': 0
-- Build tests: no
-- Build deprecated routines: OFF
-- Build single precision real: ON
-- Build double precision real: no
-- Build single precision complex: no
-- Build double precision complex: no
-- Using supplied NETLIB BLAS implementation
-- The Fortran compiler identification is unknown
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Check for working Fortran compiler: /Users/ondrej/bin/lfortran - skipped
CMake Warning at CMAKE/CheckLAPACKCompilerFlags.cmake:236 (message):
  Fortran local arrays should be allocated on the stack.  Please use a
  compiler which guarantees that feature.  See
  https://github.com/Reference-LAPACK/lapack/pull/188 and references therein.
Call Stack (most recent call first):
  BLAS/CMakeLists.txt:5 (CheckLAPACKCompilerFlags)


-- Using supplied NETLIB LAPACK implementation
CMake Warning at CMAKE/CheckLAPACKCompilerFlags.cmake:236 (message):
  Fortran local arrays should be allocated on the stack.  Please use a
  compiler which guarantees that feature.  See
  https://github.com/Reference-LAPACK/lapack/pull/188 and references therein.
Call Stack (most recent call first):
  CMakeLists.txt:322 (CheckLAPACKCompilerFlags)


-- Looking for Fortran NONE - not found
-- Looking for Fortran INT_CPU_TIME - not found
-- Looking for Fortran EXT_ETIME - not found
-- Looking for Fortran EXT_ETIME_ - not found
-- Looking for Fortran INT_ETIME - not found
-- --> Will use second_NONE.f and dsecnd_NONE.f as timing function.
-- Detecting Fortran/C Interface
Failed to compile
-- Verifying Fortran/C Compiler Compatibility
Failed to compile
CMake Warning (dev) at /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/share/cmake-3.23/Modules/FortranCInterface.cmake:309 (message):
  No FortranCInterface mangling known for VerifyFortran
Call Stack (most recent call first):
  /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/share/cmake-3.23/Modules/FortranCInterface/Verify/CMakeLists.txt:16 (FortranCInterface_HEADER)
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Verifying Fortran/C Compiler Compatibility - Failed
CMake Error at /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/share/cmake-3.23/Modules/FortranCInterface.cmake:400 (message):
  The Fortran compiler:

    /Users/ondrej/bin/lfortran

  and the C compiler:

    /Library/Developer/CommandLineTools/usr/bin/cc

  failed to compile a simple test project using both languages.  The output
  was:

    Change Dir: /Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC
    
    Run Build Command(s):/usr/bin/make -f Makefile VerifyFortranC && /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/bin/cmake -S/Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/share/cmake-3.23/Modules/FortranCInterface/Verify -B/Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC --check-build-system CMakeFiles/Makefile.cmake 0
    /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/Makefile2 VerifyFortranC
    /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/bin/cmake -S/Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/share/cmake-3.23/Modules/FortranCInterface/Verify -B/Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC --check-build-system CMakeFiles/Makefile.cmake 0
    /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/bin/cmake -E cmake_progress_start /Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC/CMakeFiles 5
    /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/Makefile2 CMakeFiles/VerifyFortranC.dir/all
    /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/VerifyFortran.dir/build.make CMakeFiles/VerifyFortran.dir/depend
    cd /Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC && /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/bin/cmake -E cmake_depends "Unix Makefiles" /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/share/cmake-3.23/Modules/FortranCInterface/Verify /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/share/cmake-3.23/Modules/FortranCInterface/Verify /Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC /Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC /Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC/CMakeFiles/VerifyFortran.dir/DependInfo.cmake
    Scanning dependencies of target VerifyFortran
    /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/VerifyFortran.dir/build.make CMakeFiles/VerifyFortran.dir/build
    [ 20%] Building Fortran object CMakeFiles/VerifyFortran.dir/VerifyFortran.f.o
    /Users/ondrej/bin/lfortran  -I/Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC --fixed-form-infer --implicit-interface -c /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/share/cmake-3.23/Modules/FortranCInterface/Verify/VerifyFortran.f -o CMakeFiles/VerifyFortran.dir/VerifyFortran.f.o
    [ 40%] Linking Fortran static library libVerifyFortran.a
    /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/bin/cmake -P CMakeFiles/VerifyFortran.dir/cmake_clean_target.cmake
    /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/bin/cmake -E cmake_link_script CMakeFiles/VerifyFortran.dir/link.txt --verbose=1
    /Library/Developer/CommandLineTools/usr/bin/ar qc libVerifyFortran.a CMakeFiles/VerifyFortran.dir/VerifyFortran.f.o
    /Library/Developer/CommandLineTools/usr/bin/ranlib libVerifyFortran.a
    warning: /Library/Developer/CommandLineTools/usr/bin/ranlib: archive library: libVerifyFortran.a the table of contents is empty (no object file members in the library define global symbols)
    [ 40%] Built target VerifyFortran
    /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/VerifyFortranC.dir/build.make CMakeFiles/VerifyFortranC.dir/depend
    cd /Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC && /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/bin/cmake -E cmake_depends "Unix Makefiles" /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/share/cmake-3.23/Modules/FortranCInterface/Verify /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/share/cmake-3.23/Modules/FortranCInterface/Verify /Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC /Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC /Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC/CMakeFiles/VerifyFortranC.dir/DependInfo.cmake
    /Library/Developer/CommandLineTools/usr/bin/make  -f CMakeFiles/VerifyFortranC.dir/build.make CMakeFiles/VerifyFortranC.dir/build
    [ 60%] Building C object CMakeFiles/VerifyFortranC.dir/main.c.o
    /Library/Developer/CommandLineTools/usr/bin/cc  -I/Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC -O3 -DNDEBUG -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -MD -MT CMakeFiles/VerifyFortranC.dir/main.c.o -MF CMakeFiles/VerifyFortranC.dir/main.c.o.d -o CMakeFiles/VerifyFortranC.dir/main.c.o -c /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/share/cmake-3.23/Modules/FortranCInterface/Verify/main.c
    [ 80%] Building C object CMakeFiles/VerifyFortranC.dir/VerifyC.c.o
    /Library/Developer/CommandLineTools/usr/bin/cc  -I/Users/ondrej/repos/lapack/build/CMakeFiles/FortranCInterface/VerifyC -O3 -DNDEBUG -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -MD -MT CMakeFiles/VerifyFortranC.dir/VerifyC.c.o -MF CMakeFiles/VerifyFortranC.dir/VerifyC.c.o.d -o CMakeFiles/VerifyFortranC.dir/VerifyC.c.o -c /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/share/cmake-3.23/Modules/FortranCInterface/Verify/VerifyC.c
    [100%] Linking C executable VerifyFortranC
    /Users/ondrej/repos/spack/opt/spack/darwin-monterey-m1/apple-clang-13.0.0/cmake-3.23.1-3geuicgdlylivqqft4pjwrdnmb63dfxr/bin/cmake -E cmake_link_script CMakeFiles/VerifyFortranC.dir/link.txt --verbose=1
    /Library/Developer/CommandLineTools/usr/bin/cc -O3 -DNDEBUG -arch arm64 -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.1.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/VerifyFortranC.dir/main.c.o CMakeFiles/VerifyFortranC.dir/VerifyC.c.o -o VerifyFortranC  libVerifyFortran.a 
    Undefined symbols for architecture arm64:
      "_VerifyFortran", referenced from:
          _main in main.c.o
    ld: symbol(s) not found for architecture arm64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make[3]: *** [VerifyFortranC] Error 1
    make[2]: *** [CMakeFiles/VerifyFortranC.dir/all] Error 2
    make[1]: *** [CMakeFiles/VerifyFortranC.dir/rule] Error 2
    make: *** [VerifyFortranC] Error 2
    
    
Call Stack (most recent call first):
  LAPACKE/include/CMakeLists.txt:17 (FortranCInterface_VERIFY)


-- Configuring incomplete, errors occurred!
See also "/Users/ondrej/repos/lapack/build/CMakeFiles/CMakeOutput.log".
See also "/Users/ondrej/repos/lapack/build/CMakeFiles/CMakeError.log".

@meow464
Copy link
Contributor

meow464 commented Nov 10, 2023

I don't get that. Maybe because I'm on linux amd64?
Made sure I'm using upstream cmake. On Lapack I'm on commit 129637ea6fb3baed0f248c7570b090eee17602bf

@certik
Copy link
Contributor Author

certik commented Nov 11, 2023

Yes, I think that's it. Can you post your full compilation log at https://gist.github.com/? I want to see how far it gets.

@meow464
Copy link
Contributor

meow464 commented Nov 11, 2023

I excluded two files from the compilation process because they use minexponent which is not implemented yet.
I changed BLAS/SRC/CMakeLists.txt to use --cpp instead of -cpp.
https://gist.github.com/meow464/e4da2842ce7367294bdffbff5abb8b63

@certik
Copy link
Contributor Author

certik commented Nov 11, 2023

Ok, so it got to about 12%. That's not bad! I think the remaining issues will be relatively minor, we just have to implement all the intrinsic functions and compile time evaluation.

@meow464
Copy link
Contributor

meow464 commented Nov 11, 2023

Hopefully! I'm currently trying to implement fdefault-integer-8.

@Pranavchiku
Copy link
Contributor

I opened issue to implement minexponent #2905, this looks an easy implementation, I'll open a PR soon.

@Thirumalai-Shaktivel Thirumalai-Shaktivel added the Code to be Compiled Projects to be compiled using LFortran compiler label Nov 24, 2023
@Pranavchiku
Copy link
Contributor

I will work on implementing radix next, from what I understand it should always return 2.

Are there any resources from where we can see underlying mathematics to implement radix, minexponent, exponent and other intrinsics?

@certik
Copy link
Contributor Author

certik commented Nov 28, 2023

You can look at the definitions in the Fortran standard: https://www.fortran90.org/, or the GFortran documentation of these functions.

@meow464
Copy link
Contributor

meow464 commented Nov 28, 2023

The ifort documentation was the best resource I found.

@meow464
Copy link
Contributor

meow464 commented Jan 12, 2024

I'm still working on fdefault-integer-8, I expect to be finished in the following weeks.

@certik
Copy link
Contributor Author

certik commented Jan 12, 2024

Perfect, thanks!

@meow464
Copy link
Contributor

meow464 commented Feb 16, 2024

@certik The error you were having is caused by the cmake check FortranCInterfaceVerify. The test didn't pass because the -c options generates an empty .o file (I know there is an open issue for this but I can't find it).

You can skip the error by commenting out:

447# add_subdirectory(LAPACKE)

In the top CMakeLists.txt.

@meow464
Copy link
Contributor

meow464 commented Feb 16, 2024

--generate-object-code doesn't solve the issue, I need to investigate further.

@Shaikh-Ubaid
Copy link
Member

Shaikh-Ubaid commented Apr 14, 2024

After removing some tests, it seems I am able to build lapack. I pushed my branch here https://github.com/Shaikh-Ubaid/lapack/tree/lf_01. It works on Linux as well as MacOS for me.

I shared my build commands and outputs here https://github.com/Shaikh-Ubaid/lapack/blob/lf_01/LF_README.md.

PS: I just tested building lapack. I am yet to run tests.

@certik
Copy link
Contributor Author

certik commented Apr 15, 2024

Great job. I think you built an empty library --- all the object files are empty. So you tested compiling to ASR, which succeeded, and that's great news. Now we need to test the LLVM backend. Use --generate-object-file option and see.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Code to be Compiled Projects to be compiled using LFortran compiler
Projects
None yet
Development

No branches or pull requests

5 participants