From baf48d1987df3a06562b75e9a08936053dcb07ca Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Wed, 14 Oct 2020 01:43:13 +0200 Subject: [PATCH] Added some iso_c_bindings --- src/Makefile | 15 ++++++++---- src/README.org | 15 ++++++------ src/qmckl_context.c.org | 53 +++++++++++++++++++++++++++++++++++------ 3 files changed, 65 insertions(+), 18 deletions(-) diff --git a/src/Makefile b/src/Makefile index aeea3d7..ee1493d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,11 +1,18 @@ CC=gcc CFLAGS= +FC=gfortran +FFLAGS= + SOURCE_FILES=$(wildcard *.org) -%.o: %.c.org - - emacs --quick --no-init-file --batch --eval "(require 'org)" --eval '(org-babel-tangle-file "$^")' - - $(CC) $(CFLAGS) -c $*.c -o $@ - - rm -f $*.c $*.h +%.c %_f.f90 %.h: %.c.org + emacs --quick --no-init-file --batch --eval "(require 'org)" --eval '(org-babel-tangle-file "$^")' + +%.o: %.c + $(CC) $(CFLAGS) -c $*.c -o $*.o + +%.o: %.f90 + $(FC) $(FFLAGS) -c $*.f90 -o $*.o diff --git a/src/README.org b/src/README.org index 2b3c741..5040596 100644 --- a/src/README.org +++ b/src/README.org @@ -2,15 +2,16 @@ ** Introduction -Source files are written in org-mode format, to provide useful comments and -LaTex formulas close to the code. There exists multiple possibilities to convert -org-mode files into different formats such as HTML or pdf. +Source files are written in org-mode format, to provide useful +comments and LaTex formulas close to the code. There exists multiple +possibilities to convert org-mode files into different formats such as +HTML or pdf. -The code is extracted from the org files using Emacs in the =Makefile=, and -then the produced files are compiled. +The code is extracted from the org files using Emacs in the =Makefile=, +and then the produced files are compiled. -For a better experience, editing can be done with Emacs. Note that Emacs can -behave like Vim when switched into ``Evil'' mode. +For a better experience, editing can be done with Emacs. Note that +Emacs can behave like Vim when switched into ``Evil'' mode. ** Documentation diff --git a/src/qmckl_context.c.org b/src/qmckl_context.c.org index c21cfd4..7872c22 100644 --- a/src/qmckl_context.c.org +++ b/src/qmckl_context.c.org @@ -1,12 +1,16 @@ # -*- mode: org -*- +This file is written in C because it is more natural to express the context in +C than in Fortran. This file also produces the Fortran binding. + + #+BEGIN_SRC C :tangle qmckl_context.c #include /* malloc */ #include "qmckl_context.h" #+END_SRC * Context - + The context variable is a handle for the state of the library, and is stored in the following data structure, which can't be seen outside of the library. @@ -15,15 +19,15 @@ #define QMCKL_DEFAULT_PRECISION 53 #define QMCKL_DEFAULT_RANGE 2 -typedef long int qmckl_context ; +typedef long long int qmckl_context ; #+END_SRC #+BEGIN_SRC C :tangle qmckl_context.c typedef struct qmckl_context_struct_ { struct qmckl_context_struct_ * prev; - int precision; - int range; + int precision; + int range; } qmckl_context_struct; #+END_SRC @@ -52,7 +56,23 @@ qmckl_context qmckl_context_create() { return (qmckl_context) context; } #+END_SRC - + + #+BEGIN_SRC fortran :tangle qmckl_context_f.f90 +integer(8) function qmckl_context_create() + use, intrinsic :: iso_c_binding + implicit none + + interface + integer(c_long_long) function c_qmckl_context_create() & + bind(C, name='qmckl_context_create') + use, intrinsic :: iso_c_binding, only : c_long_long + end function + end interface + + qmckl_context_create = c_qmckl_context_create() +end + #+END_SRC + #+BEGIN_SRC C :tangle qmckl_context.h qmckl_context qmckl_context_copy(qmckl_context context); @@ -72,13 +92,32 @@ qmckl_context qmckl_context_copy(qmckl_context context) { old_context = (qmckl_context_struct*) context; new_context->prev = old_context; - new_context->precision = old_context->precision; + new_context->precision = old_context->precision; new_context->range = old_context->range; return (qmckl_context) new_context; } #+END_SRC - + + + #+BEGIN_SRC fortran :tangle qmckl_context_f.f90 +integer(8) function qmckl_context_copy(context) + use, intrinsic :: iso_c_binding + implicit none + integer(8), intent(in) :: context + + interface + integer(c_long_long) function c_qmckl_context_copy(context) & + bind(C, name='qmckl_context_copy') + use, intrinsic :: iso_c_binding, only : c_long_long + integer(c_long_long), intent(in) :: context + end function + end interface + + qmckl_context_copy = c_qmckl_context_copy(context) +end + #+END_SRC + * Precision