Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: Space leak in a fold even after the usual fixes
(Heinrich Apfelmus)
2. Re: hmatrix, and Float (Alberto Ruiz)
3. Compiling C into Haskell (Hein Hundal)
4. Re: Compiling C into Haskell (Thomas Davie)
5. Re: Compiling C into Haskell (Hein Hundal)
6. Re: Compiling C into Haskell (allan)
----------------------------------------------------------------------
Message: 1
Date: Mon, 12 Apr 2010 10:09:23 +0200
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: Space leak in a fold even after the
usual fixes
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Travis Erdman wrote:
> The driver of my algorithm looks like this:
>
> foldl' processfn nullarray (take arg infinitelist)
>
> where processfn takes an array and the next set of inputs,
> processes, and delivers a new updated array (using the
> Data.Vector library).
>
> Apparently, I have a space leak ... the "maximum residency"
> varies linearly with the size of "arg" supplied, garbage
> collection consumes ~75% of CPU time, and, if the arg is too
> big, the whole thing crashes with an out of memory
> error. The algorithm should operate in constant
> space.
>
> As you can see, I'm using a strict left-fold and also have
> made the accumulating array strict in the processfn
> definition with a bang pattern. So, I'm sort of at a
> loss as to how to resolve this.
I take it that size of the array does not depend on arg ?
Apparently, despite your attempts to force evaluation, the array still
contains many unevaluated expressions. Documentation for the vector
pacakge reveals that Data.Vector is a *boxed* array, i.e. the entries
are only evaluated on demand, so that's where the unevaluated
expressions linger around.
Unless you need the extra laziness - which you probably don't, given how
you've structured your algorithm - you might want to switch to
Data.Vector.Unboxed or Data.Vector.Storable .
Regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 2
Date: Mon, 12 Apr 2010 14:10:57 +0200
From: Alberto Ruiz <[email protected]>
Subject: Re: [Haskell-beginners] hmatrix, and Float
To: MAN <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
MAN wrote:
> I noticed that hmatrix defines the auxiliary type 'Element', and
> provides instances for Double and Complex, but not Float. Then Vector
> and Matrix are defined in terms of Element... is there any reason why
> Float is intentionally left out.
No, it is just that other improvements have higher priority... Float
elements could be included in the future if really required.
> I'd like to use Numeric.LinearAlgebra with both single and double
> precision so as to compare results and times... could I?
I am not an expert, but it looks like Float computations are not faster
than Double in typical cpus. For big arrays, differences are caused by
memory/cache usage.
http://stackoverflow.com/questions/158889/are-doubles-faster-than-floats-in-c/160918#160918
Alberto
------------------------------
Message: 3
Date: Mon, 12 Apr 2010 07:10:12 -0700 (PDT)
From: Hein Hundal <[email protected]>
Subject: [Haskell-beginners] Compiling C into Haskell
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
Hi,
I have been playing around with Haskell for about a month now and reading
the nice book "Real World Haskell." My main reason for learning Haskell is
that I want to code up some machine learning projects (heavy use of matricies).
Normally, I use Mathematica and mix it with C++, but Mathematica is
proprietary, slow, and can't produce executable while C++ is verbose. Learning
Haskell has been fun; however, I have been a little worried that I will
sacrifice too much performance when coding in Haskell.
So, I recently asked one of my friends the following question, "Say you had
a C program. Can you always convert it to Haskell in such a way that the
compiled Haskell is not too slow and does not need too much memory?" Supposing
that too slow means slower than 1/4 the speed of C and too much memory means
twice the memory of C. Hopefully, someone that knows Haskell well can comment
on this question.
I am not sure, but I think the answer is yes, such a conversion can always
be done and creating a C to Haskell compiler with the above performance
constraints is not extremely hard. I started thinking about how a compiler
might convert a simple C program into Haskell. Below I will paste a C program
and the compile-by-hand Haskell code. It seems to me that the ideas I used to
create the Haskell code can be implemented in a compiler that converts a simple
subset of C into Haskell. I was thinking about restricting the C to one data
type 32bit-integers, arithmetic (+-*/%), assignment (=), comparison
(<,>,==,<=,>=), the if-condition-codeblock construct, and the
while-condition-codeblock construct. (I would also like to do integer arrays,
but I have not read about mutable arrays and monads yet.)
Any comments?
Cheers,
Hein H.
--------------------C to Haskell------------------------
module Main where
-- Convert a C-Program line by line into Haskell
--
-- 1-- #include <stdio.h>
-- 2-- void main()
-- 3-- {
-- 4-- long i,j;
-- 5-- i=7;
-- 6-- j=0;
-- 7-- while(j<10000)
-- 8-- {
-- 9-- if ((i % 17)== 11)
--10-- i = i*2;
--11-- if ((i % 35)== 12)
--12-- i = i+13;
--13-- if (i> 1000)
--14-- i = i - 1000;
--15-- i++;
--16-- j++;
--17-- }
--18-- printf("%ld", i);
--19--}
--assignment statements
line5 (i,j) = (7,j)
line6 (i,j) = (i,0)
line10 (i,j) = (i*2,j)
line12 (i,j) = (i+13,j)
line14 (i,j) = (i-1000,j)
line15 (i,j) = (i+1,j)
line16 (i,j) = (i,j+1)
--while statement
line7test (i,j) = j >=100000000
line7 (i,j) = until line7test body9To16 (i,j)
--if statements
line9 (i,j) = if ((mod i 17) == 11)
then line10 (i,j) else (i,j)
line11 (i,j) = if ((mod i 35) == 12)
then line12 (i,j) else (i,j)
line13 (i,j) = if (i>1000)
then line14 (i,j) else (i,j)
-- code blocks
mainprogram :: (Int, Int) -> (Int, Int)
mainprogram = line7 . line6 . line5
body9To16 = line16 . line15 . line13 . line11 . line9
main :: IO ()
main = putStrLn(show(mainprogram (0,0)))
-----------------end of program-------------------------
------------------------------
Message: 4
Date: Mon, 12 Apr 2010 15:18:21 +0100
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] Compiling C into Haskell
To: Hein Hundal <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
One comment,
The question is a dumb one, and makes the standard programmer assumption that
you want one mega-language to kill all other languages.
Haskell has some cases in which it's fast, quick to code, and very useful.
C also has some cases in which it's fast, quick to code, and very useful.
Use Haskell where the first holds, use C where the second holds, use something
else where neither holds.
Bob
On 12 Apr 2010, at 15:10, Hein Hundal wrote:
> Hi,
>
> I have been playing around with Haskell for about a month now and reading
> the nice book "Real World Haskell." My main reason for learning Haskell is
> that I want to code up some machine learning projects (heavy use of
> matricies). Normally, I use Mathematica and mix it with C++, but Mathematica
> is proprietary, slow, and can't produce executable while C++ is verbose.
> Learning Haskell has been fun; however, I have been a little worried that I
> will sacrifice too much performance when coding in Haskell.
>
> So, I recently asked one of my friends the following question, "Say you had
> a C program. Can you always convert it to Haskell in such a way that the
> compiled Haskell is not too slow and does not need too much memory?"
> Supposing that too slow means slower than 1/4 the speed of C and too much
> memory means twice the memory of C. Hopefully, someone that knows Haskell
> well can comment on this question.
>
> I am not sure, but I think the answer is yes, such a conversion can always
> be done and creating a C to Haskell compiler with the above performance
> constraints is not extremely hard. I started thinking about how a compiler
> might convert a simple C program into Haskell. Below I will paste a C
> program and the compile-by-hand Haskell code. It seems to me that the ideas
> I used to create the Haskell code can be implemented in a compiler that
> converts a simple subset of C into Haskell. I was thinking about restricting
> the C to one data type 32bit-integers, arithmetic (+-*/%), assignment (=),
> comparison (<,>,==,<=,>=), the if-condition-codeblock construct, and the
> while-condition-codeblock construct. (I would also like to do integer
> arrays, but I have not read about mutable arrays and monads yet.)
>
> Any comments?
>
> Cheers,
> Hein H.
>
> --------------------C to Haskell------------------------
> module Main where
>
> -- Convert a C-Program line by line into Haskell
> --
> -- 1-- #include <stdio.h>
> -- 2-- void main()
> -- 3-- {
> -- 4-- long i,j;
> -- 5-- i=7;
> -- 6-- j=0;
> -- 7-- while(j<10000)
> -- 8-- {
> -- 9-- if ((i % 17)== 11)
> --10-- i = i*2;
> --11-- if ((i % 35)== 12)
> --12-- i = i+13;
> --13-- if (i> 1000)
> --14-- i = i - 1000;
> --15-- i++;
> --16-- j++;
> --17-- }
> --18-- printf("%ld", i);
> --19--}
>
>
> --assignment statements
> line5 (i,j) = (7,j)
> line6 (i,j) = (i,0)
> line10 (i,j) = (i*2,j)
> line12 (i,j) = (i+13,j)
> line14 (i,j) = (i-1000,j)
> line15 (i,j) = (i+1,j)
> line16 (i,j) = (i,j+1)
>
> --while statement
> line7test (i,j) = j >=100000000
> line7 (i,j) = until line7test body9To16 (i,j)
>
> --if statements
> line9 (i,j) = if ((mod i 17) == 11)
> then line10 (i,j) else (i,j)
> line11 (i,j) = if ((mod i 35) == 12)
> then line12 (i,j) else (i,j)
> line13 (i,j) = if (i>1000)
> then line14 (i,j) else (i,j)
>
> -- code blocks
> mainprogram :: (Int, Int) -> (Int, Int)
> mainprogram = line7 . line6 . line5
> body9To16 = line16 . line15 . line13 . line11 . line9
>
>
> main :: IO ()
> main = putStrLn(show(mainprogram (0,0)))
>
> -----------------end of program-------------------------
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
------------------------------
Message: 5
Date: Mon, 12 Apr 2010 08:29:51 -0700 (PDT)
From: Hein Hundal <[email protected]>
Subject: Re: [Haskell-beginners] Compiling C into Haskell
To: Thomas Davie <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=us-ascii
--- On Mon, 4/12/10, Thomas Davie <[email protected]> wrote:
> ... makes the standard
> programmer assumption that you want one mega-language to
> kill all other languages.
>
> Haskell has some cases in which it's fast, quick to code,
> and very useful.
> C also has some cases in which it's fast, quick to code,
> and very useful.
>
> Use Haskell where the first holds, use C where the second
> holds, use something else where neither holds.
>
Hi Thomas,
I currently don't know enough about Haskell to answer the question "Will
Haskell be fast, quick to code, and very useful for this project?"
For my machine learning project, I will definitely have to do a lot of
complex operations on matricies. I want the abstraction power of Lisp, but all
the compilers for Lisp seem to produce rather slow executables. I was hoping
that Haskell might work for this project or Haskell plus the LAPACK numerical
computation library.
Cheers,
Hein
PS: I feel that I need to comment on your use of the phrase "dumb question".
Some beginners are young and they might be put off by the use of language that
could be interpreted as a put down. Perhaps in the future, if you want to use
that phrase in a beginners forum you could put the word dumb in quotes. That
way, the reader is less likely to interpret it as a put down. :)
------------------------------
Message: 6
Date: Mon, 12 Apr 2010 16:40:18 +0100
From: allan <[email protected]>
Subject: Re: [Haskell-beginners] Compiling C into Haskell
To: Hein Hundal <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Hi
I'm not quite sure I understand why you would wish to compile C into
Haskell?
It seems to me that you would then get the "worst" of both worlds. That
is you have to program in C without using the greater abstraction of
Haskell, but then suffer the performance hit of compiling FROM Haskell
to machine code (yes I know some things are easier to write in C and I
know that some things are faster in Haskell, but that aside ...).
Compilation from C to Haskell would be useful if you THEN wish to edit
the produced Haskell code, in other words if you're hoping to port a
currently available program in C to Haskell. Even then I would think
the resultant Haskell would be pretty much unreadable.
For your specific situation it would seem that you just want to use the
foreign function interface and write some of your code in Haskell, and
the performance heavy matrix routines in C. This can sometimes be quite
a nice work flow;
1) write everything in Haskell
2) spot the performance bottleneck and replace those with
handcoded/optimised C routines
This means as well that you have a back up implementation in Haskell of
the C routines to compare against when debugging.
regards
allan
Hein Hundal wrote:
> Hi,
>
> I have been playing around with Haskell for about a month now and reading
> the nice book "Real World Haskell." My main reason for learning Haskell is
> that I want to code up some machine learning projects (heavy use of
> matricies). Normally, I use Mathematica and mix it with C++, but Mathematica
> is proprietary, slow, and can't produce executable while C++ is verbose.
> Learning Haskell has been fun; however, I have been a little worried that I
> will sacrifice too much performance when coding in Haskell.
>
> So, I recently asked one of my friends the following question, "Say you
> had a C program. Can you always convert it to Haskell in such a way that the
> compiled Haskell is not too slow and does not need too much memory?"
> Supposing that too slow means slower than 1/4 the speed of C and too much
> memory means twice the memory of C. Hopefully, someone that knows Haskell
> well can comment on this question.
>
> I am not sure, but I think the answer is yes, such a conversion can always
> be done and creating a C to Haskell compiler with the above performance
> constraints is not extremely hard. I started thinking about how a compiler
> might convert a simple C program into Haskell. Below I will paste a C
> program and the compile-by-hand Haskell code. It seems to me that the ideas
> I used to create the Haskell code can be implemented in a compiler that
> converts a simple subset of C into Haskell. I was thinking about restricting
> the C to one data type 32bit-integers, arithmetic (+-*/%), assignment (=),
> comparison (<,>,==,<=,>=), the if-condition-codeblock construct, and the
> while-condition-codeblock construct. (I would also like to do integer
> arrays, but I have not read about mutable arrays and monads yet.)
>
> Any comments?
>
> Cheers,
> Hein H.
>
> --------------------C to Haskell------------------------
> module Main where
>
> -- Convert a C-Program line by line into Haskell
> --
> -- 1-- #include <stdio.h>
> -- 2-- void main()
> -- 3-- {
> -- 4-- long i,j;
> -- 5-- i=7;
> -- 6-- j=0;
> -- 7-- while(j<10000)
> -- 8-- {
> -- 9-- if ((i % 17)== 11)
> --10-- i = i*2;
> --11-- if ((i % 35)== 12)
> --12-- i = i+13;
> --13-- if (i> 1000)
> --14-- i = i - 1000;
> --15-- i++;
> --16-- j++;
> --17-- }
> --18-- printf("%ld", i);
> --19--}
>
>
> --assignment statements
> line5 (i,j) = (7,j)
> line6 (i,j) = (i,0)
> line10 (i,j) = (i*2,j)
> line12 (i,j) = (i+13,j)
> line14 (i,j) = (i-1000,j)
> line15 (i,j) = (i+1,j)
> line16 (i,j) = (i,j+1)
>
> --while statement
> line7test (i,j) = j >=100000000
> line7 (i,j) = until line7test body9To16 (i,j)
>
> --if statements
> line9 (i,j) = if ((mod i 17) == 11)
> then line10 (i,j) else (i,j)
> line11 (i,j) = if ((mod i 35) == 12)
> then line12 (i,j) else (i,j)
> line13 (i,j) = if (i>1000)
> then line14 (i,j) else (i,j)
>
> -- code blocks
> mainprogram :: (Int, Int) -> (Int, Int)
> mainprogram = line7 . line6 . line5
> body9To16 = line16 . line15 . line13 . line11 . line9
>
>
> main :: IO ()
> main = putStrLn(show(mainprogram (0,0)))
>
> -----------------end of program-------------------------
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 22, Issue 18
*****************************************