GreenCard: list marshalling

Alastair Reid reid@cs.utah.edu
03 Jun 2002 22:21:49 +0100


wferi@afavant.elte.hu writes:
> Now I'm going to check whether the garbage collector works: both the
> Haskell and the C representation won't fit in memory...

If you're using a foreign library to process very large datasets
(e.g., image processing, large matrix operations, etc.) it's usual to
keep the data on the C side and never transfer the whole dataset into
Haskell at the one time.

If you want to do this, you'd change an interface like this:

  type Matrix a = [[a]]  -- or any other Haskell representation

  %dis invert :: Matrix Double -> Matrix Double
  %dis add :: Matrix Double -> Matrix Double -> Matrix Double
  ...

  instance Show a => Show (Matrix a) where ...

to one like this:

  data CMatrix a   -- declares a type but no representation
  type Matrix a = ForeignPtr (CMatrix a)

  %dis invert :: Matrix Double -> Matrix Double
  %dis add    :: Matrix Double -> Matrix Double -> Matrix Double
  ...

  %dis printMatrix :: Matrix Double -> IO ()

That is, we change the representation to use a ForeignPtr (a pointer
into the C heap) and we try to replace functions which access the
representation directly with C functions.

-- 
Alastair Reid        reid@cs.utah.edu        http://www.cs.utah.edu/~reid/