Prepared the main Fortran infrastructure for the C++ calls. Updated the makefile.

This commit is contained in:
François Coppens 2021-02-04 11:39:00 +01:00
parent 5531d9eb12
commit 090847247d
8 changed files with 80 additions and 18 deletions

5
.gitignore vendored
View File

@ -1,4 +1,5 @@
*.o
SM-MaponiA3
Sherman-Morrison
*.mod
cppSherman-Morrison
fSherman-Morrison
.vscode

View File

@ -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

View File

@ -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);

View File

@ -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];

5
SM_MaponiA3.hpp Normal file
View File

@ -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);
}

11
SM_MaponiA3_mod.f90 Normal file
View File

@ -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

View File

@ -1,5 +1,5 @@
// main.cpp
#include "SM-MaponiA3.hpp"
#include "SM_MaponiA3.hpp"
#include "Helpers.hpp"
#include <cstdlib>
#include <ctime>
@ -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

35
fmain.f90 Normal file
View File

@ -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