Re: [Haskell-cafe] Array Binary IO & molecular simulation

2009-05-03 Thread Grigory Sarnitskiy
To sum up here is the example
that can write two arrays in one file and then read this two arrays
back. To restore written data it just reads the file into bytestring,
then splits the bytestring into equal parts. The parts are decoded. I
suppose the method is suitable for decoding files with unboxed arrays
of equal size.

import Data.Array.Unboxed
import Data.Binary
import qualified Data.ByteString.Lazy as BL
import IO

a = listArray ((1,1),(3,2)) [3,4,5,6,7,8] :: UArray (Int, Int) Float
b = listArray ((1,1),(3,2)) [9,10,11,12,13,14] :: UArray (Int, Int) Float

encodeFile2 f = BL.appendFile f . encode

encoder = do
    encodeFile "Results.txt" a
    encodeFile2 "Results.txt" b

decoder = do
    contents <- BL.readFile "Results.txt"
    print $ (show (decode (fst (BL.splitAt 118 contents)) :: UArray (Int, Int) Float))
    print $ (show (decode (snd (BL.splitAt 118 contents)) :: UArray (Int, Int) Float))
P.S. I've already sent this letter to mailist several ours ago, but it wasn't published :-/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Array Binary IO & molecular simulation

2009-05-03 Thread Grigory Sarnitskiy
To sum up here is the example that can write two arrays in one file and then 
read this two arrays back. To restore written data it just reads the file into 
bytestring, then splits the bytestring into equal parts. The parts are decoded. 
I suppose the method is suitable for decoding files with unboxed arrays of 
equal size.

import Data.Array.Unboxed
import Data.Binary
import qualified Data.ByteString.Lazy as BL
import IO

a = listArray ((1,1),(3,2)) [3,4,5,6,7,8] :: UArray (Int, Int) Float
b = listArray ((1,1),(3,2)) [9,10,11,12,13,14] :: UArray (Int, Int) Float

encodeFile2 f = BL.appendFile f . encode

encoder = do
encodeFile "Results.txt" a
encodeFile2 "Results.txt" b

decoder = do
contents <- BL.readFile "Results.txt"
print $ (show (decode (fst (BL.splitAt 118 contents)) :: UArray (Int, Int) 
Float))
print $ (show (decode (snd (BL.splitAt 118 contents)) :: UArray (Int, Int) 
Float))
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Array Binary IO & molecular simulation

2009-05-02 Thread Jason Dusek
2009/05/02 Grigory Sarnitskiy :
> 2009/05/02 Jason Dusek :
>> The original poster should try serializing a tuple of arrays
>> instead of serializing each array individually.
>
> Maybe, but I have some doubts. I have to operate with
> thousands of arrays --- are tuples good in such case?

  No, they can be only 17 elements long. Then you need lists of
  arrays, I guess.

> Moreover it is desirable to write data as it is calculated to
> be sure it wont be lost...

  Would it be too much trouble to write to separate files?

--
Jason Dusek
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Array Binary IO & molecular simulation

2009-05-02 Thread Grigory Sarnitskiy
> 2009/05/02 Jason Dusek :>The original poster should try serializing a tuple of> arrays instead of serializing each array individually.Maybe, but I have some doubts.  I have to operate with thousands of arrays --- are tuples good in such case? Moreover it is desirable to write data as it is calculated to be sure it wont be lost if the program suddenly stops for some reason. Can tuple/array/list of arrays be serialized and written to the file without its whole computation, but just as new elements appear?
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Array Binary IO & molecular simulation

2009-05-02 Thread Jason Dusek
2009/05/02 Krzysztof Skrzętnicki :
> 2009/05/02 Jason Dusek :
>> I believe Data.Binary includes a header with the data it
>> serializes. In consequence, the second and all following
>> arrays will be invisible.
>
> I didn't check the Binary instance for arrays, however I
> belive it's not true. Consider Binary instances for tuples:

  These instances describe serializers for tuples; however,
  there is nothing preventing Data.Binary from tacking on a
  length header or anything else like that when `encode` is
  called. The original poster should try serializing a tuple of
  arrays instead of serializing each array individually.

> If what you are saying would be true, the tuple instances
> would be incorrect for array tuples.

  What is an array tuple?

--
Jason Dusek
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Array Binary IO & molecular simulation

2009-05-02 Thread Krzysztof Skrzętnicki
On Sat, May 2, 2009 at 11:12, Jason Dusek  wrote:
>  I believe Data.Binary includes a header with the data
>  it serializes. In consequence, the second and all following
>  arrays will be invisible.
[Apologies for Jason for sending this twice to him]

I didn't check the Binary instance for arrays, however I belive it's
not true. Consider Binary instances for tuples:

instance (Binary a, Binary b) => Binary (a,b) where
   put (a,b)   = put a >> put b
   get = liftM2 (,) get get

instance (Binary a, Binary b, Binary c) => Binary (a,b,c) where
   put (a,b,c) = put a >> put b >> put c
   get = liftM3 (,,) get get get

If what you are saying would be true, the tuple instances would be
incorrect for array tuples.

Best regards

Christopher Skrzętnicki
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Array Binary IO & molecular simulation

2009-05-02 Thread Jason Dusek
  I believe Data.Binary includes a header with the data
  it serializes. In consequence, the second and all following
  arrays will be invisible.

  Sorry I can not be of more help.

--
Jason Dusek
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Array Binary IO & molecular simulation

2009-05-01 Thread Claus Reinke
So I wonder of existing projects of such type, both Molecular dynamics and Monte Carlo methods. 


The fastest Haskell Monte Carlo code I've seen in action is 
Simon's port of a Monte Carlo Go engine:


http://www.haskell.org/pipermail/haskell-cafe/2009-March/057982.html
http://www.haskell.org/pipermail/haskell-cafe/2009-March/058069.html

That is competitive to lightly optimised non-Haskell versions, though
not competitive with highly optimised versions (the kind that slows
down when you update your compiler, but gives you dramatic boosts
due to detailed attention both to generated assembly code and to 
high-level algorithm shortcuts).


Though there's also specialization as an option

http://www.cse.unsw.edu.au/~chak/papers/KCCSB07.html

and googling for "haskell monte carlo" give a few more hits, such as

http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.47.2981
http://www.citeulike.org/user/dguibert/article/561815

and even some hackage package, though I don't know whether
efficiency was a concern there?

I've got also some technical questions. Now I'm using 2D DiffUArray 
to represent particle positions during the simulation (when there are lots 
of array updates). Is this reasonably fast (I want to use pure external 
interface of DiffArray)?


DiffArray is slow (don't know about DiffUArray):
http://hackage.haskell.org/trac/ghc/ticket/2727

The default Random is also slow (see the mersenne alternatives on
hackage instead, and be careful if you use them through the standard
class interface):

http://hackage.haskell.org/trac/ghc/ticket/2280
http://hackage.haskell.org/trac/ghc/ticket/427

Claus


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe