Skip to content

Instantly share code, notes, and snippets.

/Main.hs Secret

Created June 20, 2016 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5ff9bb4b9f074df99b3ac9e61175939a to your computer and use it in GitHub Desktop.
Save anonymous/5ff9bb4b9f074df99b3ac9e61175939a to your computer and use it in GitHub Desktop.
module Main where
import Control.Monad
import System.IO
import qualified Data.Vector.Unboxed as V
import qualified MandelV as MV
savePgm w h orbits v filename = do
withFile filename WriteMode $ \f -> do
hPutStrLn f $ "P2"
hPutStrLn f $ show w ++ " " ++ show h
hPutStrLn f $ show orbits
V.imapM_ (elm f) v
where elm f ix e =
if ix `mod` w == 0
then hPutStrLn f $ show e
else hPutStr f $ show e ++ " "
main :: IO ()
main = do
let w = 2560
let h = 1600
let x1 = -2
let y1 = -1.5
let x2 = 1
let y2 = 1.5
let filename = "test_hs.pgm"
let orbits = 63
let radius = 2
let v = MV.mandelbrot orbits radius x1 y1 x2 y2 w h :: V.Vector Int
savePgm w h orbits v filename
#include <stdio.h>
#include <stdlib.h>
int orbits (int max_orbits, double radius, double a, double b) {
double x = 0, y = 0, r2 = radius * radius;
int n = 0;
while (1) {
if (n == max_orbits) { return n; }
double y2 = y * y;
double x2 = x * x;
if (x2 + y2 >= r2) { return n; }
n++;
y = (x * y * 2) + b;
x = x2 - y2 + a;
}
}
void mandelbrot(int max_orbits, double radius,
double x1, double y1, double x2, double y2,
int w, int h, int out[]) {
double mx = (x2 - x1) / ((double)(w - 1));
double my = (y2 - y1) / ((double)(h - 1));
for (int j = 0; j < h; ++j) {
for (int i = 0; i < w; ++i) {
out[(j * w) + i] = orbits(max_orbits, radius,
mx * (double)i + x1, my * (double)j + y1);
}
}
}
void save_pgm(int w, int h, int max_orbits, int out[], char filename[]) {
FILE *f = fopen(filename, "w");
fprintf(f, "P2\n");
fprintf(f, "%d %d\n", w, h);
fprintf(f, "%d\n", max_orbits);
for (int j = 0; j < h; ++j) {
for (int i = 0; i < w; ++i) {
fprintf(f, "%d ", out[(j * w) + i]);
}
fprintf(f, "\n");
}
fclose(f);
}
int main() {
int w = 2560;
int h = 1600;
double x1 = -2;
double y1 = -1.5;
double x2 = 1;
double y2 = 1.5;
char filename[] = "test_c.pgm";
int max_orbits = 63;
double radius = 2;
int* out = malloc(w * h * sizeof(int));;
mandelbrot(max_orbits, radius, x1, y1, x2, y2, w, h, out);
save_pgm(w, h, max_orbits, out, filename);
}
Mon Jun 20 12:54 2016 Time and Allocation Profiling Report (Final)
mandel3 +RTS -p -RTS
total time = 19.68 secs (19675 ticks @ 1000 us, 1 processor)
total alloc = 40,244,709,888 bytes (excludes profiling overheads)
COST CENTRE MODULE %time %alloc
orbits.go MandelV 72.3 71.6
savePgm.elm Main 9.2 12.4
orbits MandelV 3.1 0.8
mandelbrot MandelV 3.0 4.6
mandelbrot.f MandelV 2.2 2.4
savePgm.\ Main 1.9 0.8
mandelbrot.f.x MandelV 1.5 1.6
mandelbrot.f.y MandelV 1.4 1.6
>>= Data.Vector.Fusion.Util 1.1 1.5
individual inherited
COST CENTRE MODULE no. entries %time %alloc %time %alloc
MAIN MAIN 629 0 0.0 0.0 100.0 100.0
main Main 1260 0 0.0 0.0 12.2 14.4
savePgm Main 1261 0 0.0 0.0 12.2 14.4
savePgm.\ Main 1265 1 1.9 0.8 12.2 14.4
savePgm.elm Main 1341 4096000 9.2 12.4 9.2 12.4
basicUnsafeIndexM Data.Vector.Unboxed.Base 1337 4096000 0.4 0.0 0.7 0.3
basicUnsafeIndexM Data.Vector.Primitive 1338 4096000 0.4 0.3 0.4 0.3
indexByteArray# Data.Primitive.Types 1339 4096000 0.0 0.0 0.0 0.0
>>= Data.Vector.Fusion.Util 1336 4096001 0.4 0.9 0.4 0.9
unId Data.Vector.Fusion.Util 1335 4096001 0.0 0.0 0.0 0.0
basicLength Data.Vector.Unboxed.Base 1333 1 0.0 0.0 0.0 0.0
basicLength Data.Vector.Primitive 1334 1 0.0 0.0 0.0 0.0
sElems Data.Vector.Fusion.Bundle.Monadic 1275 1 0.0 0.0 0.0 0.0
CAF:main1 Main 1256 0 0.0 0.0 0.0 0.0
main Main 1258 1 0.0 0.0 0.0 0.0
main.v Main 1276 1 0.0 0.0 0.0 0.0
main.filename Main 1262 1 0.0 0.0 0.0 0.0
savePgm Main 1259 1 0.0 0.0 0.0 0.0
savePgm.\ Main 1268 0 0.0 0.0 0.0 0.0
CAF:main2 Main 1255 0 0.0 0.0 0.0 0.0
main Main 1263 0 0.0 0.0 0.0 0.0
main.filename Main 1264 0 0.0 0.0 0.0 0.0
CAF:main3 Main 1254 0 0.0 0.0 87.8 85.6
main Main 1277 0 0.0 0.0 87.8 85.6
main.v Main 1278 0 0.0 0.0 87.8 85.6
mandelbrot MandelV 1279 1 3.0 4.6 87.8 85.6
basicUnsafeFreeze Data.Vector.Unboxed.Base 1330 1 0.0 0.0 0.0 0.0
basicUnsafeFreeze Data.Vector.Primitive 1331 1 0.0 0.0 0.0 0.0
mandelbrot.mx MandelV 1323 1 0.0 0.0 0.0 0.0
mandelbrot.my MandelV 1315 1 0.0 0.0 0.0 0.0
mandelbrot.f MandelV 1308 4096000 2.2 2.4 81.4 78.8
mandelbrot.f.i MandelV 1328 4096000 0.2 0.0 0.2 0.0
mandelbrot.f.x MandelV 1322 4096000 1.5 1.6 1.5 1.6
mandelbrot.f.(...) MandelV 1321 4096000 0.5 0.6 0.5 0.6
mandelbrot.f.j MandelV 1320 4096000 0.1 0.0 0.1 0.0
mandelbrot.f.y MandelV 1314 4096000 1.4 1.6 1.4 1.6
orbits MandelV 1309 4096000 3.1 0.8 75.7 72.6
orbits.r2 MandelV 1311 4096000 0.4 0.2 0.4 0.2
orbits.go MandelV 1310 63778080 72.3 71.6 72.3 71.6
basicUnsafeSlice Data.Vector.Unboxed.Base 1304 4096001 0.1 0.0 0.6 0.3
basicUnsafeSlice Data.Vector.Primitive.Mutable 1305 4096001 0.4 0.3 0.4 0.3
>>= Data.Vector.Fusion.Util 1299 8192001 0.8 0.7 2.8 1.7
fmap Data.Vector.Fusion.Stream.Monadic 1301 4096001 0.7 0.7 2.0 1.1
basicUnsafeWrite Data.Vector.Unboxed.Base 1302 4096000 0.2 0.0 1.3 0.4
basicUnsafeWrite Data.Vector.Primitive.Mutable 1303 4096000 0.9 0.4 1.1 0.4
writeByteArray# Data.Primitive.Types 1307 4096000 0.1 0.0 0.1 0.0
primitive Control.Monad.Primitive 1306 0 0.0 0.0 0.0 0.0
unId Data.Vector.Fusion.Util 1298 4096001 0.0 0.0 0.0 0.0
sChunks Data.Vector.Fusion.Bundle.Monadic 1297 1 0.0 0.0 0.0 0.0
basicUnsafeNew Data.Vector.Unboxed.Base 1284 1 0.0 0.0 0.0 0.1
basicUnsafeNew Data.Vector.Primitive.Mutable 1285 1 0.0 0.1 0.0 0.1
basicUnsafeNew.size Data.Vector.Primitive.Mutable 1287 1 0.0 0.0 0.0 0.0
basicUnsafeNew.mx Data.Vector.Primitive.Mutable 1286 1 0.0 0.0 0.0 0.0
sSize Data.Vector.Fusion.Bundle.Monadic 1281 1 0.0 0.0 0.0 0.0
upperBound Data.Vector.Fusion.Bundle.Size 1280 1 0.0 0.0 0.0 0.0
CAF:main_radius Main 1253 0 0.0 0.0 0.0 0.0
main Main 1312 0 0.0 0.0 0.0 0.0
main.radius Main 1313 1 0.0 0.0 0.0 0.0
CAF:main_orbits Main 1252 0 0.0 0.0 0.0 0.0
main Main 1273 0 0.0 0.0 0.0 0.0
main.orbits Main 1274 1 0.0 0.0 0.0 0.0
CAF:main_y2 Main 1251 0 0.0 0.0 0.0 0.0
main Main 1316 0 0.0 0.0 0.0 0.0
main.y2 Main 1317 1 0.0 0.0 0.0 0.0
CAF:main_x2 Main 1250 0 0.0 0.0 0.0 0.0
main Main 1324 0 0.0 0.0 0.0 0.0
main.x2 Main 1325 1 0.0 0.0 0.0 0.0
CAF:main_y1 Main 1249 0 0.0 0.0 0.0 0.0
main Main 1318 0 0.0 0.0 0.0 0.0
main.y1 Main 1319 1 0.0 0.0 0.0 0.0
CAF:main_x1 Main 1248 0 0.0 0.0 0.0 0.0
main Main 1326 0 0.0 0.0 0.0 0.0
main.x1 Main 1327 1 0.0 0.0 0.0 0.0
CAF:main_h Main 1247 0 0.0 0.0 0.0 0.0
main Main 1271 0 0.0 0.0 0.0 0.0
main.h Main 1272 1 0.0 0.0 0.0 0.0
CAF:main_w Main 1246 0 0.0 0.0 0.0 0.0
main Main 1269 0 0.0 0.0 0.0 0.0
main.w Main 1270 1 0.0 0.0 0.0 0.0
CAF:lvl5_rbUN Main 1241 0 0.0 0.0 0.0 0.0
savePgm Main 1342 0 0.0 0.0 0.0 0.0
savePgm.\ Main 1343 0 0.0 0.0 0.0 0.0
savePgm.elm Main 1344 0 0.0 0.0 0.0 0.0
CAF:lvl4_rbUM Main 1240 0 0.0 0.0 0.0 0.0
savePgm Main 1266 0 0.0 0.0 0.0 0.0
savePgm.\ Main 1267 0 0.0 0.0 0.0 0.0
CAF:poly_$dPrimMonad_r6tZ MandelV 1233 0 0.0 0.0 0.0 0.0
CAF:poly_a2_r6tY MandelV 1232 0 0.0 0.0 0.0 0.0
CAF:doUnsafeChecks Data.Vector.Internal.Check 1227 0 0.0 0.0 0.0 0.0
doUnsafeChecks Data.Vector.Internal.Check 1283 1 0.0 0.0 0.0 0.0
CAF:doInternalChecks Data.Vector.Internal.Check 1226 0 0.0 0.0 0.0 0.0
doInternalChecks Data.Vector.Internal.Check 1282 1 0.0 0.0 0.0 0.0
CAF:$fMonadBox_$creturn Data.Vector.Fusion.Util 1225 0 0.0 0.0 0.0 0.0
return Data.Vector.Fusion.Util 1340 1 0.0 0.0 0.0 0.0
CAF:$fMonadId1 Data.Vector.Fusion.Util 1220 0 0.0 0.0 0.0 0.0
return Data.Vector.Fusion.Util 1300 1 0.0 0.0 0.0 0.0
CAF:lvl90_r8F0P Data.Vector.Unboxed.Base 760 0 0.0 0.0 0.0 0.0
basicUnsafeNew Data.Vector.Unboxed.Base 1288 0 0.0 0.0 0.0 0.0
basicUnsafeNew Data.Vector.Primitive.Mutable 1289 0 0.0 0.0 0.0 0.0
basicUnsafeNew.size Data.Vector.Primitive.Mutable 1290 0 0.0 0.0 0.0 0.0
sizeOf Data.Primitive 1291 1 0.0 0.0 0.0 0.0
sizeOf# Data.Primitive.Types 1292 1 0.0 0.0 0.0 0.0
unI# Data.Primitive.Types 1293 1 0.0 0.0 0.0 0.0
CAF:lvl_r7He Control.Monad.Primitive 741 0 0.0 0.0 0.0 0.0
primitive Control.Monad.Primitive 1295 1 0.0 0.0 0.0 0.0
CAF:sIZEOF_INT Data.Primitive.MachDeps 737 0 0.0 0.0 0.0 0.0
sIZEOF_INT Data.Primitive.MachDeps 1294 1 0.0 0.0 0.0 0.0
CAF GHC.Conc.Signal 674 0 0.0 0.0 0.0 0.0
CAF GHC.IO.Encoding 667 0 0.0 0.0 0.0 0.0
CAF GHC.IO.Encoding.Iconv 665 0 0.0 0.0 0.0 0.0
CAF GHC.IO.FD 659 0 0.0 0.0 0.0 0.0
CAF GHC.IO.Handle.FD 657 0 0.0 0.0 0.0 0.0
CAF GHC.IO.Handle.Text 655 0 0.0 0.0 0.0 0.0
{-# LANGUAGE BangPatterns #-}
module MandelV where
import qualified Data.Vector.Unboxed as V
orbits limit radius a b =
go 0 0 0
where
r2 = radius * radius
go !n !x !y
| n == limit = n
| x2 + y2 >= r2 = n
| otherwise = go (n + 1) (x2 - y2 + a) (2 * x * y + b)
where
x2 = x * x
y2 = y * y
mandelbrot limit radius x1 y1 x2 y2 w h =
V.generate (w * h) f
where
mx = (x2 - x1) / fromIntegral (w - 1)
my = (y2 - y1) / fromIntegral (h - 1)
f ix =
orbits limit radius x y
where
(j, i) = divMod ix w
x = mx * fromIntegral i + x1
y = my * fromIntegral j + y1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment