-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.hs
executable file
·104 lines (90 loc) · 3.7 KB
/
main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
-- PRUEBA 2 INFO188 - 2021
--
-- Q1) (1pt) Compile (make) y ejecute el programa, por ejemplo asi:
-- ./prog 64 4000
-- - ¿Que es lo que genera? Puede buscar bibliografia (citarla si es asi)
-- - ¿Que rol juegan los parametros?
-- - ¿Que impacto tienen n y k en el rendimiento?
-- - Proponga dos inputs (Los llamaremos CONF1 y CONF2):
-- - Una config de n,k donde la grilla es extensa, pero no hay tanto trabajo por celda.
-- - Una config de n,k donde la grilla es relativamente pequena, pero hay mucho trabajo por celda.
--
--
-- Q2) (2.5pts) Paralelize el programa usando `parListChunk`, (Haga una copia de este archivo, con nombre Q2.hs)
-- - Busque en la literatura como se usa y explique que es lo que hace
-- - Encuentre el parametro de parListChunk que le da mayor acceleracion paralela.
-- - Reporte cual fue el tiempo de ejecucion al usar 1, 2, 4, 8 threads. (una tabla para cada CONF)
-- - Comente sus resultados en relacion a CONF1 y CONF2, fueron los que esperaba? si? no? argumente por que.
--
--
--
import System.Environment
import System.IO
import Control.Exception
import Control.DeepSeq
import Control.Parallel
import Control.Parallel.Strategies
import Data.Time.Clock
import Data.Complex
import Data.List
import Text.Printf
-- calculo
calc :: Complex Float -> Complex Float -> Int -> Char
calc z c 0 = if (magnitude z) <= 2.0
then '*'
else ' '
calc z c k = let znext = z*z + c
in calc znext c (k-1)
-- proceso
proceso :: Int -> Int -> [Char]
proceso n k = let r1 = -1.5
dr = 3.0/(fromIntegral n) :: Float
im1 = 1.0
dim = 2.0/(fromIntegral n) :: Float
in
barrido n k n n r1 dr im1 dim r1 im1
barrido :: Int -> Int -> Int -> Int -> Float -> Float -> Float -> Float -> Float -> Float ->[Char]
barrido n k 0 0 r1 dr i1 di x y = []
barrido n k 0 ny r1 dr i1 di x y = let
z = (0.0 :+ 0.0)
c = (x :+ y) :: (Complex Float)
xnext = r1
ynext = y-di
in (calc z c k) : barrido n k n (ny-1) r1 dr i1 di xnext ynext
barrido n k nx ny r1 dr i1 di x y = let z = (0.0 :+ 0.0)
c = (x :+ y)
xnext = x+dr
in (calc z c k) : barrido n k (nx-1) (ny) r1 dr i1 di xnext y
printSpace :: (Show a) => [a] -> Int -> String
printSpace [] _ = ""
printSpace cs n = (intersperse ' ' (show $ take (n+1) cs)) ++ "\n" ++ printSpace (drop (n+1) cs) n
printTimeSince t0 = do
t1 <- getCurrentTime
printf "time: %.3fs\n" (realToFrac (diffUTCTime t1 t0) :: Double)
main:: IO ()
main = do
-- I) ARGS
args <- getArgs
if (length args) /= 3
then error $ "run as ./prog n k j\nn = diminio de n x n\nk = iteraciones\nj= chunck size"
else return ()
let n = read (args !! 0) :: Int
let k = read (args !! 1) :: Int
let j = read (args !! 2) :: Int
printf "Setup n=%i k=%i\n" n k
-- II) CALCULO
printf ("Calculando.........................\n")
hFlush stdout
t1 <- getCurrentTime
normal <- evaluate (proceso n k)
normal `deepseq` printf "done normal: "
printTimeSince t1
t0 <- getCurrentTime
r <- evaluate (proceso n k `using` parListChunk j rpar)
r `deepseq` printf "done paralelo: "
printTimeSince t0
-- III) resultado (imprimir solo si n es relativamente pequeno, para evitar flood de print)
if n <= 256
then printf $ printSpace r n
else return ()
return ()