From 090847247d45c78d12b3dc30a1fd0adfb0f04d77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Coppens?= Date: Thu, 4 Feb 2021 11:39:00 +0100 Subject: [PATCH 1/6] Prepared the main Fortran infrastructure for the C++ calls. Updated the makefile. --- .gitignore | 5 +++-- Makefile | 31 ++++++++++++++++++-------- SM-MaponiA3.hpp | 3 --- SM-MaponiA3.cpp => SM_MaponiA3.cpp | 4 ++-- SM_MaponiA3.hpp | 5 +++++ SM_MaponiA3_mod.f90 | 11 ++++++++++ main.cpp => cppmain.cpp | 4 ++-- fmain.f90 | 35 ++++++++++++++++++++++++++++++ 8 files changed, 80 insertions(+), 18 deletions(-) delete mode 100644 SM-MaponiA3.hpp rename SM-MaponiA3.cpp => SM_MaponiA3.cpp (94%) create mode 100644 SM_MaponiA3.hpp create mode 100644 SM_MaponiA3_mod.f90 rename main.cpp => cppmain.cpp (96%) create mode 100644 fmain.f90 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 From 84fffdb7fa7240b85211c8c1c920f83767937599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Coppens?= Date: Thu, 4 Feb 2021 13:12:34 +0100 Subject: [PATCH 2/6] Prepared the example matrix of Example 8 of the paper and its decomposition in the Fortran code and made a basic call to the subroutine MYSUBROUTINE, which is bound to the C++ void function 'Sherman-Morrison();. For now compilation fails with lots of undefined references.' --- Makefile | 25 +++++++++++++++++-------- SM_MaponiA3.cpp | 2 +- SM_MaponiA3.hpp | 6 +++--- cppmain.cpp | 2 +- fmain.f90 | 25 +++++++++++++++++++++---- 5 files changed, 43 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index b7182d2..cf37720 100644 --- a/Makefile +++ b/Makefile @@ -7,24 +7,33 @@ FFLAGS=-O0 -debug full -traceback ## 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 +fOBJ = SM_MaponiA3.o SM_MaponiA3_mod.o fmain.o +## Compile recipes for C++ stuff %.o: %.cpp $(cppDEPS) $(CXX) $(ARCH) $(CXXFLAGS) -c -o $@ $< +## Compile recepies for Fortran stuff %.o: %.f90 $(fDEPS) $(FC) $(ARCH) $(FFLAGS) -c -o $@ $< +.PHONY: all clean distclean + all: cppSherman-Morrison fSherman-Morrison -cppSherman-Morrison: $(cppOBJ) - $(CXX) $(ARCH) $(CXXFLAGS) -o $@ $^ - -fSherman-Morrison: $(fOBJ) - $(FC) $(ARCH) $(FFLAGS) -o $@ $^ - clean: @rm -vf *.o *.mod - \ No newline at end of file + +distclean: clean + @rm -vf cppSherman-Morrison fSherman-Morrison + +## Linking the C++ example program +cppSherman-Morrison: $(cppOBJ) + $(CXX) $(ARCH) $(CXXFLAGS) -o $@ $^ + +## Linking Fortran example program calling the C++ function 'Sherman_Morrison()' +fSherman-Morrison: $(fOBJ) + $(FC) $(ARCH) $(FFLAGS) -o $@ $^ diff --git a/SM_MaponiA3.cpp b/SM_MaponiA3.cpp index a652fe2..9fc477c 100644 --- a/SM_MaponiA3.cpp +++ b/SM_MaponiA3.cpp @@ -4,7 +4,7 @@ #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 index 065d662..0a53741 100644 --- a/SM_MaponiA3.hpp +++ b/SM_MaponiA3.hpp @@ -1,5 +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 + void Sherman_Morrison(int **Slater0, double **Slater_inv, unsigned int *Dim, unsigned int *N_updates, int **Updates, unsigned int *Updates_index); +} + diff --git a/cppmain.cpp b/cppmain.cpp index 79671a5..2592370 100644 --- a/cppmain.cpp +++ b/cppmain.cpp @@ -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 index 44a2a53..fe6447f 100644 --- a/fmain.f90 +++ b/fmain.f90 @@ -24,12 +24,29 @@ program Interface_test A(3,2) = 0 A(3,3) = -1 - do i=1,3 - do j=1,3 - write(*,"(I)", advance="no") A(i,j) + !! Prepare the diagonal matrix A0 and the update matrix Ar + do i=1,dim + Ar_index(i) = i + do j=1,dim + if (i == j) then + A0(i,j) = A(i,j) + A0_inv(i,j) = 1.0d0 / A0(i,j) + else + A0(i,j) = 0 + A0_inv(i,j) = 0.0d0 + end if + Ar(i,j) = A(i,j) - A0(i,j) end do - write(*,*) end do + ! do i=1,dim + ! do j=1,dim + ! write(*,"(I)", advance="no") Ar_index(i) + ! end do + ! write(*,*) + ! end do + + call MYSUBROUTINE(A0, A0_inv, dim, n_updates, Ar, Ar_index) + deallocate(Ar_index, A, A0, Ar, A0_inv) end program From f1641dd4e4178da0fb6aac2a6a41fe848b11ba51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Coppens?= Date: Thu, 4 Feb 2021 17:05:55 +0100 Subject: [PATCH 3/6] Solved linking problem by including C++ standard library '-lstdc++'. Also added a minimal working example of Fortran-C++ interfacing in dir 'mwe'. --- Makefile | 14 ++++++++------ mwe/compile.sh | 1 + mwe/main.f90 | 12 ++++++++++++ mwe/worker.cpp | 7 +++++++ mwe/worker.h | 6 ++++++ 5 files changed, 34 insertions(+), 6 deletions(-) create mode 100755 mwe/compile.sh create mode 100644 mwe/main.f90 create mode 100644 mwe/worker.cpp create mode 100644 mwe/worker.h diff --git a/Makefile b/Makefile index cf37720..cb48a63 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ -CXX=icpc -CXXFLAGS=-O0 -debug full -traceback -FC=ifort -FFLAGS=-O0 -debug full -traceback -# ARCH=-xCORE-AVX2 +CXX = icpc +CXXFLAGS = -O0 -debug full -traceback +FC = ifort +FFLAGS = -O0 -debug full -traceback +# ARCH = -xCORE-AVX2 ## Deps & objs for the C++ stuff cppDEPS = cppmain.cpp SM_MaponiA3.cpp SM_MaponiA3.hpp Helpers.hpp @@ -11,6 +11,8 @@ cppOBJ = cppmain.o SM_MaponiA3.o ## Deps & objs for the Fortran stuff fDEPS = fmain.f90 SM_MaponiA3_mod.f90 fOBJ = SM_MaponiA3.o SM_MaponiA3_mod.o fmain.o +fLIBS = -lstdc++ + ## Compile recipes for C++ stuff %.o: %.cpp $(cppDEPS) @@ -36,4 +38,4 @@ cppSherman-Morrison: $(cppOBJ) ## Linking Fortran example program calling the C++ function 'Sherman_Morrison()' fSherman-Morrison: $(fOBJ) - $(FC) $(ARCH) $(FFLAGS) -o $@ $^ + $(FC) $(ARCH) $(FFLAGS) $(fLIBS) -o $@ $^ diff --git a/mwe/compile.sh b/mwe/compile.sh new file mode 100755 index 0000000..72b5010 --- /dev/null +++ b/mwe/compile.sh @@ -0,0 +1 @@ +icpc -c worker.cpp && ifort -c main.f90 && ifort -lstdc++ worker.o main.o -o test diff --git a/mwe/main.f90 b/mwe/main.f90 new file mode 100644 index 0000000..4d9a8e1 --- /dev/null +++ b/mwe/main.f90 @@ -0,0 +1,12 @@ +program test + use iso_c_binding + implicit none + + interface + subroutine hello() bind(C, name="worker") + end subroutine + end interface + + call hello() + +end program test diff --git a/mwe/worker.cpp b/mwe/worker.cpp new file mode 100644 index 0000000..1b678b8 --- /dev/null +++ b/mwe/worker.cpp @@ -0,0 +1,7 @@ +#include "worker.h" + +void worker() +{ + std::cout << "Hello, World!" << std::endl; +} + diff --git a/mwe/worker.h b/mwe/worker.h new file mode 100644 index 0000000..70ff209 --- /dev/null +++ b/mwe/worker.h @@ -0,0 +1,6 @@ +#include + +extern "C" +{ + void worker(); +} From 7f7b23f7c4ffb60b7950d3f7b95f045708a1d404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Coppens?= Date: Thu, 4 Feb 2021 18:52:26 +0100 Subject: [PATCH 4/6] Put more comments in Makefile. --- Makefile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index cb48a63..842dbbe 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,9 @@ +## Used compilers CXX = icpc -CXXFLAGS = -O0 -debug full -traceback FC = ifort + +## Compiler flags +CXXFLAGS = -O0 -debug full -traceback FFLAGS = -O0 -debug full -traceback # ARCH = -xCORE-AVX2 @@ -13,7 +16,6 @@ fDEPS = fmain.f90 SM_MaponiA3_mod.f90 fOBJ = SM_MaponiA3.o SM_MaponiA3_mod.o fmain.o fLIBS = -lstdc++ - ## Compile recipes for C++ stuff %.o: %.cpp $(cppDEPS) $(CXX) $(ARCH) $(CXXFLAGS) -c -o $@ $< @@ -22,6 +24,7 @@ fLIBS = -lstdc++ %.o: %.f90 $(fDEPS) $(FC) $(ARCH) $(FFLAGS) -c -o $@ $< +## Build tagets .PHONY: all clean distclean all: cppSherman-Morrison fSherman-Morrison From 3f6bca2b049df7844e8a75ffe7ef98dc40db4a60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Coppens?= Date: Sat, 6 Feb 2021 18:59:07 +0100 Subject: [PATCH 5/6] The Fortran interface to C++ fuction MaponiA3() works but the mechanism of passing the 2D arrays from Fortran to C++ must be improved. Now the passed 'linear' 2D arrays are copied and reshaped into usable 2D arrays. This needs to be replaced with some suitable casting mechanism. --- Helpers.hpp | 17 ++++++ Makefile | 2 +- SM_MaponiA3.hpp | 5 +- SM_MaponiA3_f.cpp | 145 ++++++++++++++++++++++++++++++++++++++++++++ SM_MaponiA3_f.hpp | 4 ++ SM_MaponiA3_mod.f90 | 8 +-- fmain.f90 | 18 +++--- mwe/main.cpp | 23 +++++++ 8 files changed, 204 insertions(+), 18 deletions(-) create mode 100644 SM_MaponiA3_f.cpp create mode 100644 SM_MaponiA3_f.hpp create mode 100644 mwe/main.cpp diff --git a/Helpers.hpp b/Helpers.hpp index 646ba6e..23bf812 100644 --- a/Helpers.hpp +++ b/Helpers.hpp @@ -75,6 +75,23 @@ T **matMul(T **A, T **B, unsigned int size) { return C; } +template +T **matMul2(T **A, T (*B)[], unsigned int size) { + T **C = new T*[size]; + for (unsigned int i = 0; i < size; i++) { + C[i] = new T[size]; + } + for (unsigned int i = 0; i < size; i++) { + for (unsigned int j = 0; j < size; j++) { + for (unsigned int k = 0; k < size; k++) { + C[i][j] += A[i][k] * B[k][j]; + } + } + } + return C; +} + + template T1 **outProd(T1 *vec1, T2 *vec2, unsigned int size) { T1 **C = new T1*[size]; diff --git a/Makefile b/Makefile index 842dbbe..1a51821 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ cppOBJ = cppmain.o SM_MaponiA3.o ## Deps & objs for the Fortran stuff fDEPS = fmain.f90 SM_MaponiA3_mod.f90 -fOBJ = SM_MaponiA3.o SM_MaponiA3_mod.o fmain.o +fOBJ = SM_MaponiA3_f.o SM_MaponiA3_mod.o fmain.o fLIBS = -lstdc++ ## Compile recipes for C++ stuff diff --git a/SM_MaponiA3.hpp b/SM_MaponiA3.hpp index 0a53741..5e330ba 100644 --- a/SM_MaponiA3.hpp +++ b/SM_MaponiA3.hpp @@ -1,5 +1,2 @@ // 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); -} - +void Sherman_Morrison(int **Slater0, double **Slater_inv, unsigned int *Dim, unsigned int *N_updates, int **Updates, unsigned int *Updates_index); diff --git a/SM_MaponiA3_f.cpp b/SM_MaponiA3_f.cpp new file mode 100644 index 0000000..e56e1fb --- /dev/null +++ b/SM_MaponiA3_f.cpp @@ -0,0 +1,145 @@ +// SM-MaponiA3_f.cpp +// Algorithm 3 from P. Maponi, +// p. 283, doi:10.1016/j.laa.2006.07.007 +#include "SM_MaponiA3_f.hpp" +#include "Helpers.hpp" + +void MaponiA3(int **linSlater0, double **linSlater_inv, unsigned int *Dim, unsigned int *N_updates, int **linUpdates, unsigned int *Updates_index) { + + // Define new 2D arrays and copy the elements of the + // linear passed Fortran arrays. This block needs to + // be replaced with some casting mechanism to avoid + // copying of arrays. + int **Slater0 = new int*[*Dim]; + int **Updates = new int*[*Dim]; + double **Slater_inv = new double*[*Dim]; + for (int i = 0; i < *Dim; i++) { + Slater0[i] = new int[*Dim]; + Updates[i] = new int[*Dim]; + Slater_inv[i] = new double[*Dim]; + } + for (unsigned int i = 0; i < *Dim; i++) { + for (unsigned int j = 0; j < *Dim; j++) { + Slater0[i][j] = linSlater0[0][i+*Dim*j]; + Slater_inv[i][j] = linSlater_inv[0][i+*Dim*j]; + Updates[i][j] = linUpdates[0][i+*Dim*j]; + } + } + + // Possible casting candidates + // int (*Slater0)[*Dim] = (int(*)[*Dim])linSlater0[0]; + // double (*Slater_inv)[*Dim] = (double(*)[*Dim])linSlater_inv[0]; + // int (*Updates)[*Dim] = (int(*)[*Dim])linUpdates[0]; + //////////////////////////////////////////////////////////////////////// + + 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]; + double alpha, beta; + double **U, *breakdown = new double[M+1]; + double **Al = new double*[M]; + p[0] = 0; + for (i = 0; i < M; i++) { + p[i+1] = i + 1; + Id[i] = new unsigned int[M]; + Al[i] = new double[M]; + } + + // Declare auxiliary solution matrix ylk + double ***ylk = new double**[M]; + for (l = 0; l < M; l++) { + ylk[l] = new double*[M+1]; + for (k = 0; k < M+1; k++) { + ylk[l][k] = new double[M+1]; + } + } + + // Initialize identity matrix + for (i = 0; i < M; i++) { + for (j = 0; j < M; j++) { + if (i != j) Id[i][j] = 0; + else Id[i][j] = 1; + } + } + + // Initialize ylk with zeros + for (l = 0; l < M; l++) { + for (k = 0; k < M+1; k++) { + for (i = 0; i < M+1; i++) { + ylk[l][k][i] = 0; + } + } + } + + // Calculate all the y0k in M^2 multiplications instead of M^3 + for (k = 1; k < M+1; k++) { + for (i = 1; i < M+1; i++) { + ylk[0][k][i] = Slater_inv[i-1][i-1] * Updates[i-1][k-1]; + } + } + + // Calculate all the ylk from the y0k + for (l = 1; l < M; l++) { + for (j = l; j < M+1; j++) { + breakdown[j] = abs( 1 + ylk[l-1][p[j]][p[j]] ); + } + lbar = getMaxIndex(breakdown, M+1); + for (i = 0; i < M; i++) { + breakdown[i] = 0; + } + tmp = p[l]; + p[l] = p[lbar]; + p[lbar] = tmp; + for (k = l+1; k < M+1; k++) { + beta = 1 + ylk[l-1][p[l]][p[l]]; + if (beta == 0) { + cout << "Break-down condition occured. Exiting..." << endl; + exit; + } + for (i = 1; i < M+1; i++) { + alpha = ylk[l-1][p[k]][p[l]] / beta; + ylk[l][p[k]][i] = ylk[l-1][p[k]][i] - alpha * ylk[l-1][p[l]][i]; + } + } + } + + // Construct A-inverse from A0-inverse and the ylk + // Keep the memory location of the passed array 'Slater_inv' before 'Slater_inv' + // gets reassigned by 'matMul(...)' in the next line, by creating a new + // pointer 'copy' that points to whereever 'Slater_inv' points to now. + // double **copy = Slater_inv; + + for (l = 0; l < M; l++) { + k = l+1; + U = outProd(ylk[l][p[k]], Id[p[k]-1], M); + beta = 1 + ylk[l][p[k]][p[k]]; + for (i = 0; i < M; i++) { + for (j = 0; j < M; j++) { + Al[i][j] = Id[i][j] - U[i][j] / beta; + } + } + Slater_inv = matMul(Al, Slater_inv, M); + } + + // // Assign the new values of 'Slater_inv' to the old values in 'copy[][]' + // for (i = 0; i < M; i++) { + // for (j = 0; j < M; j++) { + // copy[i][j] = Slater_inv[i][j]; + // } + // } + + // Assign the new values of 'Slater_inv' to the old values in 'copy[][]' + for (i = 0; i < M; i++) { + for (j = 0; j < M; j++) { + linSlater_inv[0][i+*Dim*j] = Slater_inv[i][j]; + } + } + + for (l = 0; l < M; l++) { + for (k = 0; k < M+1; k++) { + delete [] ylk[l][k]; + } + delete [] ylk[l], Id[l], U[l], Al[l], Slater_inv[l]; + } + delete [] p, breakdown; +} \ No newline at end of file diff --git a/SM_MaponiA3_f.hpp b/SM_MaponiA3_f.hpp new file mode 100644 index 0000000..1537a65 --- /dev/null +++ b/SM_MaponiA3_f.hpp @@ -0,0 +1,4 @@ +// SM-MaponiA3_f.hpp +extern "C" { + void MaponiA3(int **linSlater0, double **linSlater_inv, unsigned int *Dim, unsigned int *N_updates, int **linUpdates, unsigned int *Updates_index); +} diff --git a/SM_MaponiA3_mod.f90 b/SM_MaponiA3_mod.f90 index c1d9e91..1708fd0 100644 --- a/SM_MaponiA3_mod.f90 +++ b/SM_MaponiA3_mod.f90 @@ -1,11 +1,11 @@ -module MYMODULE +module Sherman_Morrison interface - subroutine MYSUBROUTINE(Slater0, Slater_inv, dim, n_updates, Updates, Updates_index) bind(C, name="Sherman_Morrison") + subroutine MaponiA3(Slater0, Slater_inv, dim, n_updates, Updates, Updates_index) bind(C, name="MaponiA3") 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 subroutine MaponiA3 end interface -end module MYMODULE \ No newline at end of file +end module Sherman_Morrison \ No newline at end of file diff --git a/fmain.f90 b/fmain.f90 index fe6447f..447ee46 100644 --- a/fmain.f90 +++ b/fmain.f90 @@ -1,6 +1,6 @@ program Interface_test + use Sherman_Morrison, only : MaponiA3 use, intrinsic :: iso_c_binding, only : c_int, c_double - use MYMODULE, only : MYSUBROUTINE implicit none integer i, j !! Iterators @@ -38,15 +38,15 @@ program Interface_test Ar(i,j) = A(i,j) - A0(i,j) end do end do + + call MaponiA3(A0, A0_inv, dim, n_updates, Ar, Ar_index) - ! do i=1,dim - ! do j=1,dim - ! write(*,"(I)", advance="no") Ar_index(i) - ! end do - ! write(*,*) - ! end do - - call MYSUBROUTINE(A0, A0_inv, dim, n_updates, Ar, Ar_index) + do i=1,dim + do j=1,dim + write(*,"(F3.0,3X)", advance="no") A0_inv(i,j) + end do + write(*,*) + end do deallocate(Ar_index, A, A0, Ar, A0_inv) end program diff --git a/mwe/main.cpp b/mwe/main.cpp new file mode 100644 index 0000000..cdf7867 --- /dev/null +++ b/mwe/main.cpp @@ -0,0 +1,23 @@ +#include + + +int main() +{ + + typedef int (*to2D)[3]; //pint2 is a pointer to an array of 2 ints + int linArray[9] = {0,1,2,3,4,5,6,7,8}; + + to2D dArray = (to2D)linArray; + + std::cout << dArray[0][0] << std::endl; + std::cout << dArray[0][1] << std::endl; + std::cout << dArray[0][2] << std::endl; + std::cout << dArray[1][0] << std::endl; + std::cout << dArray[1][1] << std::endl; + std::cout << dArray[1][2] << std::endl; + std::cout << dArray[2][0] << std::endl; + std::cout << dArray[2][1] << std::endl; + std::cout << dArray[2][2] << std::endl; + + return 0; +} From b0140cbc07c40a5a42974b537bf1b2d728530223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Coppens?= Date: Sat, 6 Feb 2021 19:11:02 +0100 Subject: [PATCH 6/6] Changed some comments. --- SM_MaponiA3_f.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/SM_MaponiA3_f.cpp b/SM_MaponiA3_f.cpp index e56e1fb..4ef5ab4 100644 --- a/SM_MaponiA3_f.cpp +++ b/SM_MaponiA3_f.cpp @@ -8,8 +8,8 @@ void MaponiA3(int **linSlater0, double **linSlater_inv, unsigned int *Dim, unsig // Define new 2D arrays and copy the elements of the // linear passed Fortran arrays. This block needs to - // be replaced with some casting mechanism to avoid - // copying of arrays. + // be replaced with a suitable casting mechanism to + // avoid copying of memory. int **Slater0 = new int*[*Dim]; int **Updates = new int*[*Dim]; double **Slater_inv = new double*[*Dim]; @@ -25,7 +25,6 @@ void MaponiA3(int **linSlater0, double **linSlater_inv, unsigned int *Dim, unsig Updates[i][j] = linUpdates[0][i+*Dim*j]; } } - // Possible casting candidates // int (*Slater0)[*Dim] = (int(*)[*Dim])linSlater0[0]; // double (*Slater_inv)[*Dim] = (double(*)[*Dim])linSlater_inv[0]; @@ -103,12 +102,12 @@ void MaponiA3(int **linSlater0, double **linSlater_inv, unsigned int *Dim, unsig } } - // Construct A-inverse from A0-inverse and the ylk // Keep the memory location of the passed array 'Slater_inv' before 'Slater_inv' // gets reassigned by 'matMul(...)' in the next line, by creating a new // pointer 'copy' that points to whereever 'Slater_inv' points to now. // double **copy = Slater_inv; + // Construct A-inverse from A0-inverse and the ylk for (l = 0; l < M; l++) { k = l+1; U = outProd(ylk[l][p[k]], Id[p[k]-1], M); @@ -121,14 +120,14 @@ void MaponiA3(int **linSlater0, double **linSlater_inv, unsigned int *Dim, unsig Slater_inv = matMul(Al, Slater_inv, M); } - // // Assign the new values of 'Slater_inv' to the old values in 'copy[][]' + // Overwrite the old values in 'copy' with the new ones in Slater_inv // for (i = 0; i < M; i++) { // for (j = 0; j < M; j++) { // copy[i][j] = Slater_inv[i][j]; // } // } - // Assign the new values of 'Slater_inv' to the old values in 'copy[][]' + // Overwrite the old values in 'linSlater_inv' with the new values in Slater_inv for (i = 0; i < M; i++) { for (j = 0; j < M; j++) { linSlater_inv[0][i+*Dim*j] = Slater_inv[i][j]; @@ -142,4 +141,4 @@ void MaponiA3(int **linSlater0, double **linSlater_inv, unsigned int *Dim, unsig delete [] ylk[l], Id[l], U[l], Al[l], Slater_inv[l]; } delete [] p, breakdown; -} \ No newline at end of file +}