Skip to content

Commit

Permalink
Initial import.
Browse files Browse the repository at this point in the history
  • Loading branch information
judah committed Oct 9, 2010
0 parents commit 68429b5
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
30 changes: 30 additions & 0 deletions LICENSE
@@ -0,0 +1,30 @@
Copyright (c)2010, Judah Jacobson

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

* Neither the name of Judah Jacobson nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
103 changes: 103 additions & 0 deletions Math/FFT/Vector/Base.hsc
@@ -0,0 +1,103 @@
-- | A basic interface between Vectors and the fftw library.
module Math.FFT.Vector.Base where

import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Generic as V
import Control.Monad.Primitive
import Data.Complex as DC
import Foreign
import Foreign.C
import Data.Bits


#include <fftw3.h>

---------------------
-- Creating FFTW plans

-- First, the planner flags:
data PlanType = Estimate | Measure | Patient | Exhaustive
data Preservation = PreserveInput | DestroyInput

type CFlags = CUInt

-- | Marshal the planner flags for use by fftw.
planInitFlags :: PlanType -> Preservation -> CFlags
planInitFlags pt pr = planTypeInt .&. preservationInt
where
planTypeInt = case pt of
Estimate -> #const FFTW_ESTIMATE
Measure -> #const FFTW_MEASURE
Patient -> #const FFTW_PATIENT
Exhaustive -> #const FFTW_EXHAUSTIVE
preservationInt = case pr of
PreserveInput -> #const FFTW_PRESERVE_INPUT
DestroyInput -> #const FFTW_DESTROY_INPUT

newtype CPlan = CPlan {unCPlan :: ForeignPtr CPlan}

withPlan :: CPlan -> (Ptr CPlan -> IO a) -> IO a
withPlan = withForeignPtr . unCPlan

foreign import ccall unsafe fftw_execute :: Ptr CPlan -> IO ()
foreign import ccall "&" fftw_destroy_plan :: FunPtr (Ptr CPlan -> IO ())

newPlan :: Ptr CPlan -> IO CPlan
newPlan = fmap CPlan . newForeignPtr fftw_destroy_plan

----------------------------------------
-- vector-fftw plans

data Plan sh a b = Plan {
planInput :: VS.MVector RealWorld a,
planOutput :: VS.MVector RealWorld b,
planExecute :: IO ()
}

{-
-- TODO: Allow arbitrary Shape sh
execute :: (Storable a, Elt a, Storable b, Elt b)
=> Plan (Z:.Int) a b -> Array (Z:.Int) a -> Array (Z:.Int) b
execute p a
| extent a /= planInputSize p = error "execute: shape mismatch: expected "
++ show (planInputSize p)
++ ", got " ++ show (extent a)
| otherwise = unsafePerformIO
$ withForeignPtr (planInputArray p) $ \p_in ->
$ withForeignPtr (planOutputArray p) $ \p_out -> do
forM_ [0..n-1] $ \k -> pokeElemOff p_in k $ a :! (Z:.k)
planExecute p
-- TODO: Hacky McHackerstein!
return $! force $ fromFunction (Z:.n)
(\_:.k -> unsafeInlineIO
$ peekElemOff p_out k)
where
_:.n = extent a
-----------------------
-- Planners: methods of plan creation.
data Planner sh a b = Planner {
plannerSizes :: sh -> (sh,sh) -- (input,output)
creationSizeFromInput :: sh -> sh,
makePlan :: sh -> Ptr a -> Ptr b -> CFlags -> IO (Ptr CPlan),
normalizeInput :: sh -> Plan sh a b -> Plan sh a b
}
planOfType :: (Storable a, Storable b) => PlanType
-> Planner Int a b -> Int -> Plan Int a b
planOfType ptype Planner{..} n
| inputSize n <= 0 || outputSize n <= 0 = error "Can't (yet) plan for empty arrays!"
| otherwise = unsafePerformIO $ do
planInput@(MArray _ inFP) <- newFFTWArr_ $ inputSize n
planOutput@(MArray _ outFP) <- newFFTWArr_ $ outputSize n
withForeignPtr inFP $ \inP -> withForeignPtr outFP $ \outP -> do
pPlan <- makePlan (toEnum n) inP outP $ flagsInt ptype DestroyInput
cPlan <- newPlan pPlan
let planExecute = withPlan cPlan fftw_execute
return $ normalization n $ Plan {..}
plan :: (Storable a, Storable b) => Planner a b -> Int -> Plan a b
plan = planOfType Estimate
-}
2 changes: 2 additions & 0 deletions Setup.hs
@@ -0,0 +1,2 @@
import Distribution.Simple
main = defaultMain
3 changes: 3 additions & 0 deletions TODO
@@ -0,0 +1,3 @@
- Thread safety of plans.
- Explicit versions in build-depends.
- Remove the /opt/local additions.
27 changes: 27 additions & 0 deletions vector-fftw.cabal
@@ -0,0 +1,27 @@
Name: vector-fftw

Version: 0.1
Synopsis: TODO
Description: TODO
License: BSD3
License-file: LICENSE
Author: Judah Jacobson
Maintainer: Judah Jacobson <judah.jacobson@gmail.com>
Copyright: (c) Judah Jacobson, 2010
Category: Math
Build-type: Simple
Cabal-version: >=1.2


Library
Exposed-modules:
Math.FFT.Vector.Base

Build-depends: base==4.* && < 4.4, vector==0.7.*, primitive==0.3.*
Extra-libraries: fftw3

Extensions: ForeignFunctionInterface

-- XXX MacPorts-only
include-dirs: /opt/local/include
Extra-lib-dirs: /opt/local/lib

0 comments on commit 68429b5

Please sign in to comment.