[GHC] #8061: Support for Complex Double in Foreign Function Interface

#8061: Support for Complex Double in Foreign Function Interface ------------------------------------+------------------------------------- Reporter: dsamperi | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Keywords: | Operating System: Unknown/Multiple Architecture: Unknown/Multiple | Type of failure: None/Unknown Difficulty: Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | ------------------------------------+------------------------------------- Currently the FFI only permits BASIC return types like Double, Int, Bool, etc., but NOT Complex Double (only slightly non-basic!). This makes it impossible to import a Fortran function (not subroutine) that returns a complex value. Adding support for Complex Double would make it easier to interface to external libs like BLAS and LAPACK; without this support wrapper C libs must be used, and these are not standardized (the Haskell package hs-linear-algebra currently has some problems related to this). Thus adding Foreign support for Complex Double (not just through Ptr) would open up access to a large collection of external Fortran libs for the Haskell community. Currently I get the message "Unacceptable return type in FFI" when I try to return IO (ComplexType). -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8061 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8061: Support for Complex Double in Foreign Function Interface -------------------------------------+------------------------------------ Reporter: dsamperi | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Comment (by simonpj): Sounds reasonable in principle. Someone would need to look into the FFI interface, which would need to pass and return complex numbers, or perhaps more generally structs. I don't have a clue what C's calling convention looks like for that. I think we use `libffi` to generate code, but I'm pretty hazy about the details. Or maybe it'd be enough to return multiple ''results'', dually with passing multiple arguments. So at the most primitive level you could say {{{ foreign import foo :: Double# -> Double# -> IO (# Double#, Double# #) }}} If someone is up for doing the legwork, I'm sure there are people on `ghc- devs` who can advise. Simon -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8061#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8061: Support for Complex Double in Foreign Function Interface -------------------------------------+------------------------------------ Reporter: dsamperi | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Changes (by carter): * cc: carter.schonwald@… (added) Comment: this is a matter near and dear to me. the best way to work around this in current GHC (and indeed what i'll be doing soon) is to have a wrapper on the C/Fortran side that takes a pointer to a single element storable array for a Complex Floats and Complex Doubles. I'll then pass a pointer to a single elem array via FFI, and read the answer from the storable array on the haskell side. For any interesting numerical algorithms, i believe this trick, which would be internal/private to the library, is not likely to be any performance bottle neck. the complex number case is pretty easy to handle that way. Also you shouldn't use hs-linear-algebra, no ones maintaining it (i spent some time trying to clean it up, decided it wasn't worth it, will be releasing my own alternative in a few weeks) though because complex doubles and floats have a memory layout specified by the ieee (i believe), adding support via a scheme like what Simon is suggesting would be pretty nice I *believe* that for general structs, it might be less clear... aren't C structure padded to satisfy platform specific alignment constraints? Is that specified per platform ABI? @Simon, GHC HEAD in the -fllvm path could pretty cheaply have unboxed complex numbers using the SIMD machinery on supported platforms. (gratned... that wouldn't quite be portable and the native code gen needs some simd love before such a rep would be viable) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8061#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8061: Support for Complex Double in Foreign Function Interface -------------------------------------+------------------------------------ Reporter: dsamperi | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Comment (by dsamperi): Hello Carter, Has support for Complex Double been added to GHC/FFI (7.8.1)? What is the status of your alternative to Pat Perry's hs-linear-algebra? Does it cover BLAS and LAPACK, or just BLAS? Thanks, Dominick -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8061#comment:3 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#8061: Support for Complex Double in Foreign Function Interface -------------------------------------+------------------------------------ Reporter: dsamperi | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.6.3 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Unknown/Multiple Type of failure: None/Unknown | Difficulty: Unknown Test Case: | Blocked By: Blocking: | Related Tickets: -------------------------------------+------------------------------------ Comment (by carter): hey, 1) nope, and wont until 7.10 at the earliest. That said, theres a pretty easy work around 2) theres a very very easy work around, just use storable instances, some tiny storable vectors and do a wee pointer wrapper! I have that for some C code I wrote for complex numbers. heres an example that does that boxing for a scalar complex double result for dot product. You'll have to write your own wee C wrapper for each thing, but its not much work and will work with a wide range of GHC versions (which is never a bad thing) {{{ /// NOTE: complex valued dot products have a different type than the real float dot products /// this is important to remember! void arrayDotProduct_complex_double(int32_t length, complex_double * left, int32_t leftStride ,complex_double * right,int32_t rightStride, complex_double * resultSingleton); void arrayDotProduct_complex_double(int32_t length, complex_double * left, int32_t leftStride ,complex_double * right,int32_t rightStride, complex_double * resultSingleton){ complex_double result = arrayDotProduct_internal_complex_double(length,left,leftStride,right,rightStride); resultSingleton[0] = result ; } void arrayDotProduct_complex_float(int32_t length, complex_float * left, int32_t leftStride ,complex_float * right,int32_t rightStride, complex_float * resultSingleton); void arrayDotProduct_complex_float(int32_t length, complex_float * left, int32_t leftStride ,complex_float * right,int32_t rightStride, complex_float * resultSingleton){ complex_float result = arrayDotProduct_internal_complex_float(length,left,leftStride,right,rightStride); resultSingleton[0] = result ; } }}} 3) asking me on a ticket is probably the most inefficient way to find out, you'll hear about the initial release very soon though :) -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/8061#comment:4 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC