diff --git a/.gitignore b/.gitignore index b0ac550..65d0183 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.o -SM-MaponiA3 -Sherman-Morrison +*.mod +cppSherman-Morrison +fSherman-Morrison .vscode \ No newline at end of file diff --git a/Makefile b/Makefile index 24460e3..b7182d2 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,30 @@ -CC=icc CXX=icpc -CFLAGS=-O0 -debug full CXXFLAGS=-O0 -debug full -traceback +FC=ifort +FFLAGS=-O0 -debug full -traceback # ARCH=-xCORE-AVX2 -DEPS = SM-MaponiA3.cpp -OBJ = SM-MaponiA3.o main.o +## Deps & objs for the C++ stuff +cppDEPS = cppmain.cpp SM_MaponiA3.cpp SM_MaponiA3.hpp Helpers.hpp +cppOBJ = cppmain.o SM_MaponiA3.o +## Deps & objs for the Fortran stuff +fDEPS = fmain.f90 SM_MaponiA3_mod.f90 +fOBJ = SM_MaponiA3_mod.o fmain.o -%.o: %.cpp $(DEPS) - $(CXX) $(ARCH) -c -o $@ $< $(CFLAGS) +%.o: %.cpp $(cppDEPS) + $(CXX) $(ARCH) $(CXXFLAGS) -c -o $@ $< -Sherman-Morrison: $(OBJ) - $(CXX) $(ARCH) -o $@ $^ $(CFLAGS) +%.o: %.f90 $(fDEPS) + $(FC) $(ARCH) $(FFLAGS) -c -o $@ $< + +all: cppSherman-Morrison fSherman-Morrison + +cppSherman-Morrison: $(cppOBJ) + $(CXX) $(ARCH) $(CXXFLAGS) -o $@ $^ + +fSherman-Morrison: $(fOBJ) + $(FC) $(ARCH) $(FFLAGS) -o $@ $^ clean: - @rm -vf *.o + @rm -vf *.o *.mod + \ No newline at end of file diff --git a/SM-MaponiA3.hpp b/SM-MaponiA3.hpp deleted file mode 100644 index 9b5a48f..0000000 --- a/SM-MaponiA3.hpp +++ /dev/null @@ -1,3 +0,0 @@ -// SM-MaponiA3.hpp - -void Sherman_Morrison(int **Slater0, double **Slater_inv, unsigned int *Dim, unsigned int *N_updates, int **Updates, unsigned int *Updates_index); \ No newline at end of file diff --git a/SM-MaponiA3.cpp b/SM_MaponiA3.cpp similarity index 94% rename from SM-MaponiA3.cpp rename to SM_MaponiA3.cpp index df94ef2..a652fe2 100644 --- a/SM-MaponiA3.cpp +++ b/SM_MaponiA3.cpp @@ -1,10 +1,10 @@ // SM-MaponiA3.cpp // Algorithm 3 from P. Maponi, // p. 283, doi:10.1016/j.laa.2006.07.007 -#include "SM-MaponiA3.hpp" +#include "SM_MaponiA3.hpp" #include "Helpers.hpp" -void Sherman_Morrison(int **Slater0, double **Slater_inv, unsigned int *Dim, unsigned int *N_updates, int **Updates, unsigned int *Updates_index) { +void Sherman_Morrison_(int **Slater0, double **Slater_inv, unsigned int *Dim, unsigned int *N_updates, int **Updates, unsigned int *Updates_index) { unsigned int k, l, lbar, i, j, tmp, M = *Dim; unsigned int *p = new unsigned int[M+1]; unsigned int **Id = new unsigned int*[M]; diff --git a/SM_MaponiA3.hpp b/SM_MaponiA3.hpp new file mode 100644 index 0000000..065d662 --- /dev/null +++ b/SM_MaponiA3.hpp @@ -0,0 +1,5 @@ +// SM-MaponiA3.hpp + +extern "C" { + void Sherman_Morrison_(int **Slater0, double **Slater_inv, unsigned int *Dim, unsigned int *N_updates, int **Updates, unsigned int *Updates_index); +} \ No newline at end of file diff --git a/SM_MaponiA3_mod.f90 b/SM_MaponiA3_mod.f90 new file mode 100644 index 0000000..c1d9e91 --- /dev/null +++ b/SM_MaponiA3_mod.f90 @@ -0,0 +1,11 @@ +module MYMODULE + interface + subroutine MYSUBROUTINE(Slater0, Slater_inv, dim, n_updates, Updates, Updates_index) bind(C, name="Sherman_Morrison") + use, intrinsic :: iso_c_binding, only : c_int, c_double + integer(c_int), intent(in) :: dim, n_updates + integer(c_int), dimension(:), allocatable, intent(in) :: Updates_index + integer(c_int), dimension(:,:), allocatable, intent(in) :: Slater0, Updates + real(c_double), dimension(:,:), allocatable, intent(in out) :: Slater_inv + end subroutine MYSUBROUTINE + end interface +end module MYMODULE \ No newline at end of file diff --git a/main.cpp b/cppmain.cpp similarity index 96% rename from main.cpp rename to cppmain.cpp index 55af456..79671a5 100644 --- a/main.cpp +++ b/cppmain.cpp @@ -1,5 +1,5 @@ // main.cpp -#include "SM-MaponiA3.hpp" +#include "SM_MaponiA3.hpp" #include "Helpers.hpp" #include #include @@ -65,7 +65,7 @@ int main() { // Define pointers dim and n_updates to use in Sherman-Morrison(...) function call unsigned int *dim = new unsigned int(M); unsigned int *n_updates = new unsigned int(M); - Sherman_Morrison(A0, A0_inv, dim, n_updates, Ar, Ar_index); + Sherman_Morrison_(A0, A0_inv, dim, n_updates, Ar, Ar_index); showMatrix(A0_inv, M, "A0_inv"); // Deallocate all vectors and matrices diff --git a/fmain.f90 b/fmain.f90 new file mode 100644 index 0000000..44a2a53 --- /dev/null +++ b/fmain.f90 @@ -0,0 +1,35 @@ +program Interface_test + use, intrinsic :: iso_c_binding, only : c_int, c_double + use MYMODULE, only : MYSUBROUTINE + implicit none + + integer i, j !! Iterators + integer(c_int) :: dim, N_updates + integer(c_int), dimension(:), allocatable :: Ar_index + integer(c_int), dimension(:,:), allocatable :: A, A0, Ar + real(c_double), dimension(:,:), allocatable :: A0_inv + + dim = 3 + N_updates = dim + allocate(Ar_index(dim), A(dim,dim), A0(dim,dim), Ar(dim,dim), A0_inv(dim,dim)) + + !! Initialize A with M=3 and fill acc. to Eq. (17) from paper + A(1,1) = 1 + A(1,2) = 1 + A(1,3) = -1 + A(2,1) = 1 + A(2,2) = 1 + A(2,3) = 0 + A(3,1) = -1 + A(3,2) = 0 + A(3,3) = -1 + + do i=1,3 + do j=1,3 + write(*,"(I)", advance="no") A(i,j) + end do + write(*,*) + end do + + deallocate(Ar_index, A, A0, Ar, A0_inv) +end program