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

29 fortran implementation #30

Merged
merged 5 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions fortran_implementation/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Specify minimum CMake version
cmake_minimum_required(VERSION 3.12)

# Project name and language
project(squishyplanet Fortran)

# Find BLAS source files
file(GLOB BLAS
"${CMAKE_CURRENT_SOURCE_DIR}/external_libraries/BLAS/SRC/*.f"
)
add_library(blas STATIC ${BLAS})

file(GLOB_RECURSE LAPACK_ROUTINES
"${CMAKE_CURRENT_SOURCE_DIR}/external_libraries/lapack_routines/*.f"
"${CMAKE_CURRENT_SOURCE_DIR}/external_libraries/lapack_routines/*.f90"
)
add_library(local_lapack_routines STATIC ${LAPACK_ROUTINES})

file(GLOB QUADPACK
"${CMAKE_CURRENT_SOURCE_DIR}/external_libraries/QUADPACK/*.F90"
)
add_library(local_quadpack STATIC ${QUADPACK})

# Add executable and specify source files
add_executable(squishyplanet
main.f90
read_in_files.f90
model_types.f90
squishyplanet_2d.f90
constants.f90
keplerian.f90
parametric_ellipse.f90
intersection_pts.f90
integrals.f90
planet_3d.f90
squishyplanet_3d.f90
)

target_link_libraries(local_lapack_routines PUBLIC blas)
target_link_libraries(squishyplanet PRIVATE
local_lapack_routines # This already includes blas
local_quadpack
)

if(CMAKE_BUILD_TYPE MATCHES Debug)
set(CMAKE_Fortran_FLAGS_DEBUG "${CMAKE_Fortran_FLAGS_DEBUG} -fcheck=all -fbacktrace -g -O0 -Wall -Wextra -fimplicit-none")
endif()
12 changes: 12 additions & 0 deletions fortran_implementation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
This is a standalone implementation of the squishyplanet code but in Fortran instead of JAX. The main JAX package does not rely on these routines, and these routines don't rely on the JAX package- this exists only to interface with other Fortran codebases like the original MultiNest and Luna. Though during development we verified it produces outputs for a reasonable set of parameters that match squishyplanet, it is not thoroughly tested or maintained.

It should, fingers crossed, require no external dependencies beyond CMake and a Fortran compiler. It contains local copies of relevant BLAS, LINPACK, and QUADPACK routines.

To run an example version, run:
```bash
mkdir build
cd build
cmake ..
cmake --build .
./squishyplanet
```
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions fortran_implementation/change_of_basis_matricies/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import numpy as np

from squishyplanet.engine.greens_basis_transform import generate_change_of_basis_matrix

for i in range(2, 11):
m = np.array(generate_change_of_basis_matrix(i))
# m = np.asfortranarray(m, dtype=np.float64)
# m.tofile(f"g_matrix_{i}.bin")
with open(f"g_matrix_{i}.bin", "wb") as f:
f.write(m.tobytes("F")) # 'F' ensures Fortran ordering
10 changes: 10 additions & 0 deletions fortran_implementation/constants.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module constants
use, intrinsic :: iso_fortran_env, only: dp => real64
implicit none
real(dp), parameter :: PI = 3.14159265358979323846_dp
real(dp), parameter :: TWO_PI = 2.0_dp*PI
real(dp), parameter :: HALF_PI = 0.5_dp*PI
real(dp), parameter :: THREE_HALF_PI = 1.5_dp*PI
real(dp), parameter :: DEG_TO_RAD = PI/180.0_dp
real(dp), parameter :: RAD_TO_DEG = 180.0_dp/PI
end module constants
161 changes: 161 additions & 0 deletions fortran_implementation/external_libraries/BLAS/SRC/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#######################################################################
# This is the makefile to create a library for the BLAS.
# The files are grouped as follows:
#
# SBLAS1 -- Single precision real BLAS routines
# CBLAS1 -- Single precision complex BLAS routines
# DBLAS1 -- Double precision real BLAS routines
# ZBLAS1 -- Double precision complex BLAS routines
#
# CB1AUX -- Real BLAS routines called by complex routines
# ZB1AUX -- D.P. real BLAS routines called by d.p. complex
# routines
#
# ALLBLAS -- Auxiliary routines for Level 2 and 3 BLAS
#
# SBLAS2 -- Single precision real BLAS2 routines
# CBLAS2 -- Single precision complex BLAS2 routines
# DBLAS2 -- Double precision real BLAS2 routines
# ZBLAS2 -- Double precision complex BLAS2 routines
#
# SBLAS3 -- Single precision real BLAS3 routines
# CBLAS3 -- Single precision complex BLAS3 routines
# DBLAS3 -- Double precision real BLAS3 routines
# ZBLAS3 -- Double precision complex BLAS3 routines
#
#######################################################################

#---------------------------------------------------------
# Level 1 BLAS
#---------------------------------------------------------

set(SBLAS1 isamax.f sasum.f saxpy.f scopy.f sdot.f snrm2.f90
srot.f srotg.f90 sscal.f sswap.f sdsdot.f srotmg.f srotm.f)

set(CBLAS1 scabs1.f scasum.f scnrm2.f90 icamax.f caxpy.f ccopy.f
cdotc.f cdotu.f csscal.f crotg.f90 cscal.f cswap.f csrot.f)

set(DBLAS1 idamax.f dasum.f daxpy.f dcopy.f ddot.f dnrm2.f90
drot.f drotg.f90 dscal.f dsdot.f dswap.f drotmg.f drotm.f)

set(DB1AUX sscal.f isamax.f)

set(ZBLAS1 dcabs1.f dzasum.f dznrm2.f90 izamax.f zaxpy.f zcopy.f
zdotc.f zdotu.f zdscal.f zrotg.f90 zscal.f zswap.f zdrot.f)

set(CB1AUX
isamax.f idamax.f
sasum.f saxpy.f scopy.f sdot.f sgemm.f sgemv.f snrm2.f90 srot.f sscal.f
sswap.f)

set(ZB1AUX
icamax.f idamax.f
cgemm.f cherk.f cscal.f ctrsm.f
dasum.f daxpy.f dcopy.f ddot.f dgemm.f dgemv.f dnrm2.f90 drot.f dscal.f
dswap.f
scabs1.f)

#---------------------------------------------------------------------
# Auxiliary routines needed by both the Level 2 and Level 3 BLAS
#---------------------------------------------------------------------
set(ALLBLAS lsame.f xerbla.f xerbla_array.f)

#---------------------------------------------------------
# Level 2 BLAS
#---------------------------------------------------------
set(SBLAS2 sgemv.f sgbmv.f ssymv.f ssbmv.f sspmv.f
strmv.f stbmv.f stpmv.f strsv.f stbsv.f stpsv.f
sger.f ssyr.f sspr.f ssyr2.f sspr2.f)

set(CBLAS2 cgemv.f cgbmv.f chemv.f chbmv.f chpmv.f
ctrmv.f ctbmv.f ctpmv.f ctrsv.f ctbsv.f ctpsv.f
cgerc.f cgeru.f cher.f chpr.f cher2.f chpr2.f)

set(DBLAS2 dgemv.f dgbmv.f dsymv.f dsbmv.f dspmv.f
dtrmv.f dtbmv.f dtpmv.f dtrsv.f dtbsv.f dtpsv.f
dger.f dsyr.f dspr.f dsyr2.f dspr2.f)

set(ZBLAS2 zgemv.f zgbmv.f zhemv.f zhbmv.f zhpmv.f
ztrmv.f ztbmv.f ztpmv.f ztrsv.f ztbsv.f ztpsv.f
zgerc.f zgeru.f zher.f zhpr.f zher2.f zhpr2.f)

#---------------------------------------------------------
# Level 3 BLAS
#---------------------------------------------------------
set(SBLAS3 sgemm.f ssymm.f ssyrk.f ssyr2k.f strmm.f strsm.f sgemmtr.f)

set(CBLAS3 cgemm.f csymm.f csyrk.f csyr2k.f ctrmm.f ctrsm.f
chemm.f cherk.f cher2k.f cgemmtr.f)

set(DBLAS3 dgemm.f dsymm.f dsyrk.f dsyr2k.f dtrmm.f dtrsm.f dgemmtr.f)

set(ZBLAS3 zgemm.f zsymm.f zsyrk.f zsyr2k.f ztrmm.f ztrsm.f
zhemm.f zherk.f zher2k.f zgemmtr.f)


set(SOURCES)
if(BUILD_SINGLE)
list(APPEND SOURCES ${SBLAS1} ${ALLBLAS} ${SBLAS2} ${SBLAS3})
endif()
if(BUILD_DOUBLE)
list(APPEND SOURCES
${DBLAS1} ${DB1AUX} ${ALLBLAS} ${DBLAS2} ${DBLAS3} ${SBLAS3})
endif()
if(BUILD_COMPLEX)
list(APPEND SOURCES ${CBLAS1} ${CB1AUX} ${ALLBLAS} ${CBLAS2} ${CBLAS3})
endif()
if(BUILD_COMPLEX16)
list(APPEND SOURCES ${ZBLAS1} ${ZB1AUX} ${ALLBLAS} ${ZBLAS2} ${ZBLAS3})
endif()
list(REMOVE_DUPLICATES SOURCES)

add_library(${BLASLIB}_obj OBJECT ${SOURCES})
set_target_properties(${BLASLIB}_obj PROPERTIES POSITION_INDEPENDENT_CODE ON)

if(BUILD_INDEX64_EXT_API)
set(SOURCES_64_F)
# Copy files so we can set source property specific to /${BLASLIB}_64_obj target
file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${BLASLIB}_64_obj)
file(COPY ${SOURCES} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/${BLASLIB}_64_obj)
file(GLOB SOURCES_64_F ${CMAKE_CURRENT_BINARY_DIR}/${BLASLIB}_64_obj/*.f*)
add_library(${BLASLIB}_64_obj OBJECT ${SOURCES_64_F})
target_compile_options(${BLASLIB}_64_obj PRIVATE ${FOPT_ILP64})
set_target_properties(${BLASLIB}_64_obj PROPERTIES POSITION_INDEPENDENT_CODE ON)
#Add _64 suffix to all Fortran functions via macros
foreach(F IN LISTS SOURCES_64_F)
if(CMAKE_Fortran_COMPILER_ID STREQUAL "NAG")
set_source_files_properties(${F} PROPERTIES COMPILE_FLAGS "-fpp")
else()
set_source_files_properties(${F} PROPERTIES COMPILE_FLAGS "-cpp")
endif()
file(STRINGS ${F} ${F}.lst)
list(FILTER ${F}.lst INCLUDE REGEX "subroutine|SUBROUTINE|external|EXTERNAL|function|FUNCTION")
list(FILTER ${F}.lst EXCLUDE REGEX "^!.*")
list(FILTER ${F}.lst EXCLUDE REGEX "^[*].*")
list(FILTER ${F}.lst EXCLUDE REGEX "end|END")
foreach(FUNC IN LISTS ${F}.lst)
string(REGEX REPLACE "^[a-zA-Z0-9_ *]*(subroutine|SUBROUTINE|external|EXTERNAL|function|FUNCTION)[ ]*[*]?" "" FUNC ${FUNC})
string(REGEX REPLACE "[(][a-zA-Z0-9_, )]*$" "" FUNC ${FUNC})
string(STRIP ${FUNC} FUNC)
list(APPEND COPT_64_F "${FUNC}=${FUNC}_64")
endforeach()
list(REMOVE_DUPLICATES COPT_64_F)
set_source_files_properties(${F} PROPERTIES COMPILE_DEFINITIONS "${COPT_64_F}")
endforeach()
endif()

add_library(${BLASLIB}
$<TARGET_OBJECTS:${BLASLIB}_obj>
$<$<BOOL:${BUILD_INDEX64_EXT_API}>: $<TARGET_OBJECTS:${BLASLIB}_64_obj>>)

set_target_properties(
${BLASLIB} PROPERTIES
VERSION ${LAPACK_VERSION}
SOVERSION ${LAPACK_MAJOR_VERSION}
POSITION_INDEPENDENT_CODE ON
)
lapack_install_library(${BLASLIB})

if( TEST_FORTRAN_COMPILER )
add_dependencies( ${BLASLIB} run_test_zcomplexabs run_test_zcomplexdiv run_test_zcomplexmult run_test_zminMax )
endif()
Loading