
Hi .. Hope you are doing well . I've just joined this group. Recently, I am struggling to do some simple experiment with haskell language about parallelism and wrong answers that we can get while using a shared variable . I tried to write a simple program, for example calculationg 'n=n+1' few times.And then I tried to do it in parallel by using 'par' and 'pseq' . The aim was to get the wrong answer because we have to share a variable here,and without using 'MVar' function we will get the wrong answer for the calculation .I don't know how to write it in parallel in order to get a wrong answer when we don't use MVar,because we have a shared variable here. I read about MVars as well,but also I don't know how to combine MVar and Par together to get the program to work.I wrote this :module Main where f :: Int -> Int -> Int f i n = g 1 i n where g x i n | x <= i = g (x+1) i (n+1) | otherwise = n main :: IO () main = do putStrLn "starting..." let r = f 10 5 putStrLn (show r) putStrLn "finished" I want to make to work in parallel by using 'Par'.And also use MVar for this simple example to work.All of the example about MVar are a little bit complicated and I couldn't figure it that how can I write one,the same !Can any one help me with this ? I want a simple example that I can feel the need of MVar when I run my program in parallel and while I am using a shared variable.Regards; Mozhgan _________________________________________________________________ News, entertainment and everything you care about at Live.com. Get it now! http://www.live.com/getstarted.aspx

2008/12/16 Mozhgan Kabiri
Hi .. Hope you are doing well . I've just joined this group. Recently, I am struggling to do some simple experiment with haskell language about parallelism and wrong answers that we can get while using a shared variable . I tried to write a simple program, for example calculationg 'n=n+1' few times.And then I tried to do it in parallel by using 'par' and 'pseq' . The aim was to get the wrong answer because we have to share a variable here,and without using 'MVar' function we will get the wrong answer for the calculation .
This is fortunately impossible. par can never change the semantics of a program; it just says "compute this in parallel now because we might need it later", as opposed to just computing it later when it is demanded. Because Haskell is referentially transparent, the answer it will get now vs. later will always be the same. Race conditions cannot happen with par. Perhaps you want to experiment with concurrency rather than parallelism (this is the realm in which MVars lie). In that case, look at the function forkIO, which spawns a new thread, and the MVar operations. Luke
I don't know how to write it in parallel in order to get a wrong answer when we don't use MVar,because we have a shared variable here. I read about MVars as well,but also I don't know how to combine MVar and Par together to get the program to work.
I wrote this :
module Main where f :: Int -> Int -> Int f i n = g 1 i n where g x i n | x <= i = g (x+1) i (n+1) | otherwise = n main :: IO () main = do putStrLn "starting..." let r = f 10 5 putStrLn (show r) putStrLn "finished" I want to make to work in parallel by using 'Par'.And also use MVar for this simple example to work. All of the example about MVar are a little bit complicated and I couldn't figure it that how can I write one,the same !
Can any one help me with this ? I want a simple example that I can feel the need of MVar when I run my program in parallel and while I am using a shared variable.
Regards; Mozhgan
------------------------------ Get news, entertainment and everything you care about at Live.com. Check it out! http://www.live.com/getstarted.aspx
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hi ..Hmm .. maybe I explained it badly.For example I want my two processor to do two tasks while they are sharing a variable . Is it not parallelism ? We don't need MVar, as well ? I completely misunderstood !MozhganDate: Tue, 16 Dec 2008 04:03:59 -0700From: lrpalmer@gmail.comTo: mozhgan_kch@hotmail.comSubject: Re: [Haskell-cafe] MVar and Par ..CC: haskell-cafe@haskell.org2008/12/16 Mozhgan Kabiri

Mozhgan Kabiri wrote:
Hi .. Hope you are doing well . I've just joined this group.
Hi.
Recently, I am struggling to do some simple experiment with haskell language about parallelism and wrong answers that we can get while using a shared variable .
Your goal is still unclear. Are you trying to create an example which shows unexpected answers? Are you trying to create an example which shows the expected answer?
I tried to write a simple program, for example calculationg 'n=n+1' few times.And then I tried to do it in parallel by using 'par' and 'pseq' . The aim was to get the wrong answer because we have to share a variable here,and without using 'MVar' function we will get the wrong answer for the calculation .
MVar is a mutable storage cell. One can use forkIO to create IO threads which race to change MVar, and if this is done badly then you can get unexpected answers. One cannot use the pure "par" and "pseq" to launch IO threads, so you cannot use "par" and "pseq" to create race conditions, so one can only get the expected answer.
I don't know how to write it in parallel in order to get a wrong answer when we don't use MVar,because we have a shared variable here. I read about MVars as well,but also I don't know how to combine MVar and Par together to get the program to work.
I do not immediately see how MVar and "par" can be sensibly combined at all.
I wrote this :
module Main where f :: Int -> Int -> Int f i n = g 1 i n where g x i n | x <= i = g (x+1) i (n+1) | otherwise = n main :: IO () main = do putStrLn "starting..." let r = f 10 5 putStrLn (show r) putStrLn "finished" I want to make to work in parallel by using 'Par'.And also use MVar for this simple example to work. All of the example about MVar are a little bit complicated and I couldn't figure it that how can I write one,the same !
Can any one help me with this ? I want a simple example that I can feel the need of MVar when I run my program in parallel and while I am using a shared variable.
Regards; Mozhgan
I have run out of time, so I leave this part of your question to others.

Hi , Yeah , first I want to get unexpected answer ! And then I want to prove that when I use MVar I will get the write answer.I want to show that when we have a shared variable, we should use MVar.Besides we use for example two processors to do different tasks.But the point is these processors have to share a variable and I want to show it with MVar.But I don't know how to write a simple code to show it while undrestand it. Mozhgan
To: haskell-cafe@haskell.org From: haskell@list.mightyreason.com Date: Tue, 16 Dec 2008 12:34:23 +0000 Subject: [Haskell-cafe] Re: MVar and Par ..
Mozhgan Kabiri wrote:
Hi .. Hope you are doing well . I've just joined this group.
Hi.
Recently, I am struggling to do some simple experiment with haskell language about parallelism and wrong answers that we can get while using a shared variable .
Your goal is still unclear. Are you trying to create an example which shows unexpected answers? Are you trying to create an example which shows the expected answer?
I tried to write a simple program, for example calculationg 'n=n+1' few times.And then I tried to do it in parallel by using 'par' and 'pseq' . The aim was to get the wrong answer because we have to share a variable here,and without using 'MVar' function we will get the wrong answer for the calculation .
MVar is a mutable storage cell. One can use forkIO to create IO threads which race to change MVar, and if this is done badly then you can get unexpected answers.
One cannot use the pure "par" and "pseq" to launch IO threads, so you cannot use "par" and "pseq" to create race conditions, so one can only get the expected answer.
I don't know how to write it in parallel in order to get a wrong answer when we don't use MVar,because we have a shared variable here. I read about MVars as well,but also I don't know how to combine MVar and Par together to get the program to work.
I do not immediately see how MVar and "par" can be sensibly combined at all.
I wrote this :
module Main where f :: Int -> Int -> Int f i n = g 1 i n where g x i n | x <= i = g (x+1) i (n+1) | otherwise = n main :: IO () main = do putStrLn "starting..." let r = f 10 5 putStrLn (show r) putStrLn "finished" I want to make to work in parallel by using 'Par'.And also use MVar for this simple example to work. All of the example about MVar are a little bit complicated and I couldn't figure it that how can I write one,the same !
Can any one help me with this ? I want a simple example that I can feel the need of MVar when I run my program in parallel and while I am using a shared variable.
Regards; Mozhgan
I have run out of time, so I leave this part of your question to others.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
_________________________________________________________________ Explore the seven wonders of the world http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE

Okay, that's a bit clearer. Control.Parallel is not what you want; that is
for parallelizing pure code (and is very nice inside its little domain).
But you want concurrent code: multiple threads that change state at the same
time. That is in Control.Concurrent.
In particular, the "shared variable" is Data.IORef.
Simple example from which you should be able to extrapolate.
import Control.Concurrent
import Control.Concurrent.MVar
import Data.IORef
threadBody = do
x <- readIORef var
writeIORef var (x+1)
main = do
var <- newIORef 0
-- spawn two threads
forkIO threadBody
forkIO threadBody
-- look at the value of the variable
x <- readIORef var
print x
You can scatter threadDelay throughout this code to mess with the scheduler
and show how a failure could happen.
Conversion to MVars should be pretty straightforward.
Luke
2008/12/16 Mozhgan Kabiri
Hi ,
Yeah , first I want to get unexpected answer ! And then I want to prove that when I use MVar I will get the write answer.I want to show that when we have a shared variable, we should use MVar.Besides we use for example two processors to do different tasks.But the point is these processors have to share a variable and I want to show it with MVar.But I don't know how to write a simple code to show it while undrestand it.
Mozhgan
To: haskell-cafe@haskell.org From: haskell@list.mightyreason.com Date: Tue, 16 Dec 2008 12:34:23 +0000 Subject: [Haskell-cafe] Re: MVar and Par ..
Mozhgan Kabiri wrote:
Hi .. Hope you are doing well . I've just joined this group.
Hi.
Recently, I am struggling to do some simple experiment with haskell language about parallelism and wrong answers that we can get while using a shared variable .
Your goal is still unclear. Are you trying to create an example which shows unexpected answers? Are you trying to create an example which shows the expected answer?
I tried to write a simple program, for example calculationg 'n=n+1' few
times.And then I tried to do it in parallel by using 'par' and 'pseq' .
The aim was to get the wrong answer because we have to share a variable
here,and without using 'MVar' function we will get the wrong answer for
the calculation .
MVar is a mutable storage cell. One can use forkIO to create IO threads which race to change MVar, and if this is done badly then you can get unexpected answers.
One cannot use the pure "par" and "pseq" to launch IO threads, so you cannot use "par" and "pseq" to create race conditions, so one can only get the expected answer.
I don't kno w how to write it in parallel in order to get a wrong answer when we don't use MVar,because we have a shared variable here. I read about MVars as well,but also I don't know how to combine MVar and Par together to get the program to work.
I do not immediately see how MVar and "par" can be sensibly combined at all.
I wrote this :
module Main where f :: Int -> Int -> Int f i n = g 1 i n where g x i n | x <= i = g (x+1) i (n+1) | otherwise = n main :: IO () main = do putStrLn "starting..." let r = f 10 5 putStrLn (show r) putStrLn "finished" I want to make to work in parallel by using 'Par'.And also use MVar for
this simple example to work. All of the example about MVar are a little bit complicated and I couldn't figure it that how can I write one,the same !
> ; Can any one help me with this ? I want a simple example that I can feel
the need of MVar when I run my program in parallel and while I am using
a shared variable.
Regards; Mozhgan
I have run out of time, so I leave this part of your question to others.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
------------------------------ Explore the seven wonders of the world Learn more!http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Tue, Dec 16, 2008 at 5:54 AM, Luke Palmer
Okay, that's a bit clearer. Control.Parallel is not what you want; that is for parallelizing pure code (and is very nice inside its little domain). But you want concurrent code: multiple threads that change state at the same time. That is in Control.Concurrent.
In particular, the "shared variable" is Data.IORef.
Simple example from which you should be able to extrapolate.
Apologies, this code doesn't compile. Here's the fixed version: import Control.Concurrent import Control.Concurrent.MVar import Data.IORef threadBody var = do x <- readIORef var writeIORef var (x+1) main = do var <- newIORef 0 -- spawn two threads forkIO (threadBody var) forkIO (threadBody var) -- look at the value of the variable x <- readIORef var print x

Hi ..
Thanks ..
But I keep get error when I run the program in order to see the result . It says 'var' is not in the scope.
Mozhgan
Date: Tue, 16 Dec 2008 05:54:40 -0700
From: lrpalmer@gmail.com
To: mozhgan_kch@hotmail.com
Subject: Re: [Haskell-cafe] Re: MVar and Par ..
CC: haskell@list.mightyreason.com; haskell-cafe@haskell.org
Okay, that's a bit clearer. Control.Parallel is not what you want; that is for parallelizing pure code (and is very nice inside its little domain). But you want concurrent code: multiple threads that change state at the same time. That is in Control.Concurrent.
In particular, the "shared variable" is Data.IORef.
Simple example from which you should be able to extrapolate.
import Control.Concurrent
import Control.Concurrent.MVar
import Data.IORef
threadBody = do
x <- readIORef var
writeIORef var (x+1)
main = do
var <- newIORef 0
-- spawn two threads
forkIO threadBody
forkIO threadBody
-- look at the value of the variable
x <- readIORef var
print x
You can scatter threadDelay throughout this code to mess with the scheduler and show how a failure could happen.
Conversion to MVars should be pretty straightforward.
Luke
2008/12/16 Mozhgan Kabiri
To: haskell-cafe@haskell.org From: haskell@list.mightyreason.com
Date: Tue, 16 Dec 2008 12:34:23 +0000 Subject: [Haskell-cafe] Re: MVar and Par ..
Mozhgan Kabiri wrote:
Hi .. Hope you are doing well . I've just joined this group.
Hi.
Recently, I am struggling to do some simple experiment with haskell language about parallelism and wrong answers that we can get while using a shared variable .
Your goal is still unclear. Are you trying to create an example which shows unexpected answers? Are you trying to create an example which shows the expected answer?
I tried to write a simple program, for example calculationg 'n=n+1' few
times.And then I tried to do it in parallel by using 'par' and 'pseq' . The aim was to get the wrong answer because we have to share a variable here,and without using 'MVar' function we will get the wrong answer for
the calculation .
MVar is a mutable storage cell. One can use forkIO to create IO threads which race to change MVar, and if this is done badly then you can get unexpected answers.
One cannot use the pure "par" and "pseq" to launch IO threads, so you cannot use "par" and "pseq" to create race conditions, so one can only get the expected answer.
I don't kno
w how to write it in parallel in order to get a wrong answer
when we don't use MVar,because we have a shared variable here. I read about MVars as well,but also I don't know how to combine MVar and Par
together to get the program to work.
I do not immediately see how MVar and "par" can be sensibly combined at all.
I wrote this :
module Main where f :: Int -> Int -> Int f i n = g 1 i n where g x i n |
x <= i = g (x+1) i (n+1) | otherwise = n main :: IO () main = do putStrLn "starting..." let r = f 10 5 putStrLn (show r) putStrLn "finished" I want to make to work in parallel by using 'Par'.And also use MVar for
this simple example to work. All of the example about MVar are a little bit complicated and I couldn't figure it that how can I write one,the same !
> ; Can any one help me with this ? I want a simple example that I can feel
the need of MVar when I run my program in parallel and while I am using a shared variable.
Regards; Mozhgan
I have run out of time, so I leave this part of your question to others.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org
Explore the seven wonders of the world Learn more! _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe _________________________________________________________________ Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy! http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mkt=en-us
participants (3)
-
ChrisK
-
Luke Palmer
-
Mozhgan Kabiri