Commit c94b5a57 authored by cianciosa's avatar cianciosa
Browse files

Add inital fortran binding and test. Allow derivatives of piecewise constants...

Add inital fortran binding and test. Allow derivatives of piecewise constants to act like piecewise constants.
parent fb4f6c8e
Loading
Loading
Loading
Loading
+17 −10
Original line number Diff line number Diff line
@@ -12,7 +12,8 @@ option (USE_CONSTANT_CACHE "Cache the value of constantants in kernel registers.
option (SHOW_USE_COUNT "Add a comment showing the use count in kernel sources." OFF)
option (USE_INDEX_CACHE "Cache index values instead of computing them every time." OFF)
option (USE_VERBOSE "Verbose jit option." OFF)
option (BUILD_C_BINDING "Build C interface" OFF)
option (BUILD_C_BINDING "Build C interface." OFF)
option (BUILD_Fortran_BINDING "Build Fortran interface." OFF)

#-------------------------------------------------------------------------------
#  Set the cmake module path.
@@ -41,6 +42,9 @@ if (${APPLE})

    if (${USE_METAL})
        enable_language (OBJCXX)
        if (${BUILD_C_BINDING})
            enable_language (OBJC)
        endif ()

        add_library (metal_lib INTERFACE)
        target_link_libraries (metal_lib
@@ -55,7 +59,8 @@ if (${APPLE})
        )
        target_compile_options (metal_lib
                                INTERFACE
                                -fobjc-arc
                                $<$<COMPILE_LANGUAGE:OBJCXX>:-fobjc-arc>
                                $<$<COMPILE_LANGUAGE:OBJC>:-fobjc-arc>
        )
    endif ()
else ()
@@ -312,20 +317,22 @@ endif ()
#-------------------------------------------------------------------------------
#  Setup targets
#-------------------------------------------------------------------------------
add_subdirectory (graph_framework)

if (${USE_METAL})
    enable_language (OBJCXX)
if (${BUILD_Fortran_BINDING})
    set (BUILD_C_BINDING ON CACHE STRING "Build C interface." FORCE)
endif ()

add_subdirectory (graph_framework)
if (${BUILD_C_BINDING})
    enable_language (C)
    if (${USE_METAL})
        enable_language (OBJC)
    endif ()
    add_subdirectory (graph_c_binding)
endif ()

if (${BUILD_Fortran_BINDING})
    enable_language (Fortran)
    add_subdirectory (graph_fortran_binding)
endif ()

#-------------------------------------------------------------------------------
#  Setup testing
#-------------------------------------------------------------------------------
@@ -347,7 +354,7 @@ macro (add_tool_target target lang)
                                         PROPERTIES
                                         LANGUAGE OBJCXX
            )
        else ()
        elseif (${lang} STREQUAL "c")
            set_source_files_properties (${CMAKE_CURRENT_SOURCE_DIR}/${target}.${lang}
                                         PROPERTIES
                                         LANGUAGE OBJC
+15 −0
Original line number Diff line number Diff line
add_library (graph_f)

target_include_directories (graph_f
                            PUBLIC
                            $<TARGET_PROPERTY:graph_f,BINARY_DIR>
)
target_link_libraries (graph_f
                       PUBLIC
                       graph_c
)
target_sources (graph_f
                PRIVATE
                $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/graph_fortran_binding.f90>
)
+250 −0
Original line number Diff line number Diff line
!-------------------------------------------------------------------------------
!>  @file graph_fortran_binding.f90
!>  @brief Implimentation of the Fortran binding library.
!
!  Note separating the Doxygen comment block here so the detailed description is
!  found in the Module not the file.
!
!> Module contains subroutines for calling this from fortran.
!-------------------------------------------------------------------------------
      MODULE graph_fortran
      USE, INTRINSIC :: ISO_C_BINDING

      IMPLICIT NONE

!*******************************************************************************
!  PARAMETERS
!*******************************************************************************
!>  Float type.
    INTEGER(C_INT8_T), PARAMETER :: FLOAT_T = 0
!>  Double type.
    INTEGER(C_INT8_T), PARAMETER :: DOUBLE_T = 1
!>  Complex Float type.
    INTEGER(C_INT8_T), PARAMETER :: COMPLEX_FLOAT_T = 2
!>  Complex Double type.
    INTEGER(C_INT8_T), PARAMETER :: COMPLEX_DOUBLE_T = 3

!-------------------------------------------------------------------------------
!>  @brief Class object for the binding.
!-------------------------------------------------------------------------------
      TYPE :: graph_context
!>  The auto release pool context.
         TYPE(C_PTR) :: arp_context
!>  The graph c context.
         TYPE(C_PTR) :: c_context
      CONTAINS
         FINAL :: graph_destruct
      END TYPE

!*******************************************************************************
!  INTERFACE BLOCKS
!*******************************************************************************
!-------------------------------------------------------------------------------
!>  @brief Interface for the graph_context constructor with float type.
!-------------------------------------------------------------------------------
      INTERFACE graph_float_context
         MODULE PROCEDURE graph_construct_float
      END INTERFACE

!-------------------------------------------------------------------------------
!>  @brief Interface for the graph_context constructor with double type.
!-------------------------------------------------------------------------------
      INTERFACE graph_double_context
         MODULE PROCEDURE graph_construct_double
      END INTERFACE

!-------------------------------------------------------------------------------
!>  @brief Interface for the graph_context constructor with complex float type.
!-------------------------------------------------------------------------------
      INTERFACE graph_complex_float_context
         MODULE PROCEDURE graph_construct_complex_float
      END INTERFACE

!-------------------------------------------------------------------------------
!>  @brief Interface for the graph_context constructor with complex double type.
!-------------------------------------------------------------------------------
      INTERFACE graph_complex_double_context
         MODULE PROCEDURE graph_construct_complex_double
      END INTERFACE

!*******************************************************************************
!  C Binding Interface.
!*******************************************************************************
      INTERFACE
!-------------------------------------------------------------------------------
!>  @brief Auto release pool push interface.
!>
!>  @returns An auto release pool context.
!-------------------------------------------------------------------------------
         TYPE(C_PTR) FUNCTION objc_autoreleasePoolPush()                      &
         BIND(C, NAME='objc_autoreleasePoolPush')
         USE, INTRINSIC :: ISO_C_BINDING
         IMPLICIT NONE
         END FUNCTION

!-------------------------------------------------------------------------------
!>  @brief Auto release pool pop interface.
!>
!>  @param[in,out] ctx Auto Release pool context.
!-------------------------------------------------------------------------------
         SUBROUTINE objc_autoreleasePoolPop(ctx)                               &
         BIND(C, NAME='objc_autoreleasePoolPop')
         USE, INTRINSIC :: ISO_C_BINDING
         IMPLICIT NONE
         TYPE(C_PTR), VALUE :: ctx
         END SUBROUTINE

!-------------------------------------------------------------------------------
!>  @brief Construct a C context.
!>
!>  @param[in] c_type        The type of the context @ref graph_type.
!>  @param[in] use_safe_math C context uses safemath.
!>  @returns The constructed C context.
!-------------------------------------------------------------------------------
         TYPE(C_PTR) FUNCTION graph_construct_context(c_type, use_safe_math)   &
         BIND(C, NAME='graph_construct_context')
         USE, INTRINSIC :: ISO_C_BINDING
         IMPLICIT NONE
         INTEGER(C_INT8_T), VALUE :: c_type
         LOGICAL(C_BOOL), VALUE    :: use_safe_math
         END FUNCTION

!-------------------------------------------------------------------------------
!>  @brief Destroy C context.
!>
!>  @param[in] c The c context to delete.
!-------------------------------------------------------------------------------
         SUBROUTINE graph_destroy_context(c)                                   &
         BIND(C, NAME='graph_destroy_context')
         USE, INTRINSIC :: ISO_C_BINDING
         IMPLICIT NONE
         TYPE(C_PTR), VALUE :: c
         END SUBROUTINE

      END INTERFACE

      CONTAINS

!*******************************************************************************
!  CONSTRUCTION SUBROUTINES
!*******************************************************************************
!-------------------------------------------------------------------------------
!>  @brief Construct a @ref graph_context object with float type.
!>
!>  Allocate memory for the @ref graph_context and initalize the c context with
!>  a double type.
!>
!>  @param[in] use_safe_math Optional use safe math.
!-------------------------------------------------------------------------------
      FUNCTION graph_construct_float(use_safe_math)

      IMPLICIT NONE

!  Declare Arguments
      CLASS (graph_context), POINTER :: graph_construct_float
      LOGICAL(C_BOOL), INTENT(IN)    :: use_safe_math

!  Start of executable code.
      ALLOCATE(graph_construct_float)
      graph_construct_float%arp_context = objc_autoreleasePoolPush()
      graph_construct_float%c_context =                                        &
         graph_construct_context(FLOAT_T, use_safe_math)

      END FUNCTION

!-------------------------------------------------------------------------------
!>  @brief Construct a @ref graph_context object with double type.
!>
!>  Allocate memory for the @ref graph_context and initalize the c context with
!>  a double type.
!>
!>  @param[in] use_safe_math Use safe math.
!-------------------------------------------------------------------------------
      FUNCTION graph_construct_double(use_safe_math)

      IMPLICIT NONE

!  Declare Arguments
      CLASS (graph_context), POINTER :: graph_construct_double
      LOGICAL(C_BOOL), INTENT(IN)    :: use_safe_math

!  Start of executable code.
      ALLOCATE(graph_construct_double)
      graph_construct_double%arp_context = objc_autoreleasePoolPush()
      graph_construct_double%c_context =                                       &
         graph_construct_context(DOUBLE_T, use_safe_math)

      END FUNCTION

!-------------------------------------------------------------------------------
!>  @brief Construct a @ref graph_context object with complex float type.
!>
!>  Allocate memory for the @ref graph_context and initalize the c context with
!>  a complex float type.
!>
!>  @param[in] use_safe_math Use safe math.
!-------------------------------------------------------------------------------
      FUNCTION graph_construct_complex_float(use_safe_math)

      IMPLICIT NONE

!  Declare Arguments
      CLASS (graph_context), POINTER :: graph_construct_complex_float
      LOGICAL(C_BOOL), INTENT(IN)    :: use_safe_math

!  Start of executable code.
      ALLOCATE(graph_construct_complex_float)
      graph_construct_complex_float%arp_context = objc_autoreleasePoolPush()
      graph_construct_complex_float%c_context =                                &
         graph_construct_context(COMPLEX_FLOAT_T, use_safe_math)

      END FUNCTION

!-------------------------------------------------------------------------------
!>  @brief Construct a @ref graph_context object with complex double type.
!>
!>  Allocate memory for the @ref graph_context and initalize the c context with
!>  a complex double type.
!>
!>  @param[in] use_safe_math Use safe math.
!-------------------------------------------------------------------------------
      FUNCTION graph_construct_complex_double(use_safe_math)

      IMPLICIT NONE

!  Declare Arguments
      CLASS (graph_context), POINTER :: graph_construct_complex_double
      LOGICAL(C_BOOL), INTENT(IN)    :: use_safe_math

!  Start of executable code.
      ALLOCATE(graph_construct_complex_double)
      graph_construct_complex_double%arp_context = objc_autoreleasePoolPush()
      graph_construct_complex_double%c_context =                               &
         graph_construct_context(COMPLEX_DOUBLE_T, use_safe_math)

      END FUNCTION

!*******************************************************************************
! DESTRUCTION SUBROUTINES
!*******************************************************************************
!-------------------------------------------------------------------------------
!>  @brief Deconstruct a @ref graph_context object.
!>
!>  Deallocate memory and unitialize a @ref graph_context object.
!>
!>  @param[input] this A @ref graph_context instance.
!-------------------------------------------------------------------------------
      SUBROUTINE graph_destruct(this)

      IMPLICIT NONE

!  Declare Arguments
      TYPE (graph_context), INTENT(INOUT) :: this

!  Start of executable.
      CALL objc_autoreleasePoolPop(this%arp_context)
      CALL graph_destroy_context(this%c_context)

      END SUBROUTINE

      END MODULE
+15 −1
Original line number Diff line number Diff line
@@ -378,6 +378,9 @@
		C7931E7128073BF30033B488 /* CMakeLists.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = "<group>"; };
		C7931E7228073BFC0033B488 /* CMakeLists.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = "<group>"; };
		C7931E7328074F540033B488 /* backend_test.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; path = backend_test.cpp; sourceTree = "<group>"; };
		C7AE06632E3C285000586BCD /* CMakeLists.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = "<group>"; };
		C7AE06642E3C285000586BCD /* graph_fortran_binding.f90 */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.fortran.f90; path = graph_fortran_binding.f90; sourceTree = "<group>"; };
		C7AE06662E3C2AEE00586BCD /* f_binding_test.f90 */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.fortran.f90; path = f_binding_test.f90; sourceTree = "<group>"; };
		C7B676072AA9023F005AB34C /* xrays_bench.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; fileEncoding = 4; path = xrays_bench.cpp; sourceTree = "<group>"; };
		C7B676092AA90243005AB34C /* CMakeLists.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = "<group>"; };
		C7B677D829E45C9500D3ADC6 /* backend.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = backend.hpp; sourceTree = "<group>"; };
@@ -391,7 +394,7 @@
		C7DC9EE22E39768300524F6F /* graph_c_binding.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.objcpp; path = graph_c_binding.cpp; sourceTree = "<group>"; };
		C7DC9EE82E39789900524F6F /* libgraph_c.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libgraph_c.a; sourceTree = BUILT_PRODUCTS_DIR; };
		C7DC9EEF2E397BE600524F6F /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
		C7DC9EF12E3A688F00524F6F /* c_binding_test.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = c_binding_test.c; sourceTree = "<group>"; };
		C7DC9EF12E3A688F00524F6F /* c_binding_test.c */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.objc; path = c_binding_test.c; sourceTree = "<group>"; };
		C7E134492A3CB3EC0083F6A7 /* output.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = output.hpp; sourceTree = "<group>"; };
		C7E5643E28A2A16F000F31A2 /* backend_test */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = backend_test; sourceTree = BUILT_PRODUCTS_DIR; };
		C7E5644A28A2A1C5000F31A2 /* dispersion_test */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = dispersion_test; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -621,6 +624,7 @@
				C79141AD22DA9C0600E0BA0D /* graph_framework */,
				C7931E6928073BCA0033B488 /* graph_tests */,
				C7DC9EE32E39768300524F6F /* graph_c_binding */,
				C7AE06652E3C285000586BCD /* graph_fortran_binding */,
				C79141B422DAAD0C00E0BA0D /* graph_driver */,
				C74DF4582AA8BC7300319113 /* graph_benchmark */,
				C736E6B02C9B52CA00AAE3C0 /* graph_playground */,
@@ -719,10 +723,20 @@
				C73BBE7D29F816E60027BB7F /* piecewise_test.cpp */,
				C78F3D8A2DC122C7002E3D94 /* random_test.cpp */,
				C7DC9EF12E3A688F00524F6F /* c_binding_test.c */,
				C7AE06662E3C2AEE00586BCD /* f_binding_test.f90 */,
			);
			path = graph_tests;
			sourceTree = "<group>";
		};
		C7AE06652E3C285000586BCD /* graph_fortran_binding */ = {
			isa = PBXGroup;
			children = (
				C7AE06632E3C285000586BCD /* CMakeLists.txt */,
				C7AE06642E3C285000586BCD /* graph_fortran_binding.f90 */,
			);
			path = graph_fortran_binding;
			sourceTree = "<group>";
		};
		C7DC9EE32E39768300524F6F /* graph_c_binding */ = {
			isa = PBXGroup;
			children = (
+2 −2
Original line number Diff line number Diff line
@@ -203,7 +203,7 @@ void compile_index(std::ostringstream &stream,
///  @returns The derivative of the node.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> df(shared_leaf<T, SAFE_MATH> x) {
            return zero<T, SAFE_MATH> ();
            return constant<T, SAFE_MATH> (static_cast<T> (this->is_match(x)));
        }

//------------------------------------------------------------------------------
@@ -838,7 +838,7 @@ void compile_index(std::ostringstream &stream,
///  @returns The derivative of the node.
//------------------------------------------------------------------------------
        virtual shared_leaf<T, SAFE_MATH> df(shared_leaf<T, SAFE_MATH> x) {
            return zero<T, SAFE_MATH> ();
            return constant<T, SAFE_MATH> (static_cast<T> (this->is_match(x)));
        }

//------------------------------------------------------------------------------
Loading