Skip to content

Commit 067bea4

Browse files
committed
fix cython, add pure pypy
1 parent 70f68f0 commit 067bea4

File tree

7 files changed

+1289
-340
lines changed

7 files changed

+1289
-340
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@
66
main
77
*.so
88
.gdb_history
9+
build/
10+
c.raw
11+
pure_c.txt

create_fractal.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
int mandel(float x, float y, int max_iters, unsigned int * val);
1+
int mandel(float x, float y, int max_iters, unsigned char * val);
22

33
typedef struct _Img{
44
int width;
55
int height;
6-
int * data;
6+
unsigned char * data;
77
} Img;
88

99

@@ -15,7 +15,7 @@ void create_fractal(Img img, int iters) {
1515
int yy = y * img.width;
1616
for (int x=0; x < img.width; x++) {
1717
float real = x * pixel_size_x - 2;
18-
unsigned int color;
18+
unsigned char color;
1919
int ret = mandel(real, imag, iters, &color);
2020
img.data[yy + x] = color;
2121
}

create_fractal.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
typedef struct _Img{
2+
int width;
3+
int height;
4+
int * data;
5+
} cImg;
6+
7+
8+
9+
int create_fractal(cImg img, int iters);
10+
int mandel(float real, float imag, int max_iters, unsigned char * val);
11+

create_fractal.py

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,59 @@
1-
from mandel import mandel
1+
#And for desert:
2+
import sys
3+
print(sys.executable)
4+
from timeit import default_timer as timer
5+
import cffi
6+
from PIL import Image
27

3-
def create_fractal(image, width, height, iters):
4-
pixel_size_x = 3.0 / width
5-
pixel_size_y = 2.0 / height
6-
for y in range(height):
8+
ffi = cffi.FFI()
9+
10+
class Img(object):
11+
def __init__(self, width, height):
12+
self.width = width
13+
self.height = height
14+
self.data = ffi.new('uint8_t[%d]' % (width*height,))
15+
16+
def create_fractal(image, iters, func, oneval):
17+
''' Call a function for each pixel in the image, where
18+
-2 < real < 1 over the columns and
19+
-1 < imag < 1 over the rows
20+
'''
21+
pixel_size_x = 3.0 / image.width
22+
pixel_size_y = 2.0 / image.height
23+
for y in range(image.height):
724
imag = y * pixel_size_y - 1
8-
yy = y*width
9-
for x in range(width):
25+
yy = y * image.width
26+
for x in range(image.width):
1027
real = x * pixel_size_x - 2
11-
color = mandel(real, imag, iters)
12-
image[yy+x] = color
28+
func(real, imag, iters, oneval)
29+
image.data[yy + x] = oneval[0]
30+
31+
def mandel(x, y, max_iters, value):
32+
"""
33+
Given the real and imaginary parts of a complex number,
34+
determine if it is a candidate for membership in the Mandelbrot
35+
set given a fixed number of iterations.
36+
"""
37+
i = 0
38+
c = complex(x,y)
39+
z = 0.0j
40+
for i in range(max_iters):
41+
z = z*z + c
42+
if (z.real*z.real + z.imag*z.imag) >= 4:
43+
value[0] = i
44+
return 0
45+
value[0] = max_iters
46+
return max_iters
47+
48+
# Pure python
49+
width = 1500
50+
height = 1000
51+
image = Img(width, height)
52+
s = timer()
53+
oneval = ffi.new('uint8_t[1]')
54+
create_fractal(image, 20, mandel, oneval)
55+
e = timer()
56+
pure_pypy = e - s
57+
print('pure pypy required {:.2f} millisecs'.format(pure_pypy))
58+
im = Image.fromarray(image.data.reshape(height, width))
59+
im.save('pypyy.png')

main.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <stdio.h>
2-
#include <pgm.h>
2+
#include <stdlib.h>
33
#include <time.h>
44

55
// call this function to start a nanosecond-resolution timer
@@ -30,7 +30,8 @@ int main(int argc, const char *argv[], const char * env[])
3030
long time_elapsed_nanos;
3131
img.width = width;
3232
img.height = height;
33-
img.data = (unsigned int*)malloc(width * height * sizeof(unsigned int));
33+
size_t written;
34+
img.data = (unsigned char*)malloc(width * height * sizeof(unsigned char));
3435
if (NULL == img.data)
3536
return -1;
3637

@@ -39,12 +40,10 @@ int main(int argc, const char *argv[], const char * env[])
3940
time_elapsed_nanos = timer_end(vartime);
4041
fprintf(stdout, "create_fractal required %ld millisecs\n", time_elapsed_nanos / 1000000);
4142

42-
fid = fopen("c.pgm", "wb");
43+
fid = fopen("c.raw", "wb");
4344
if (NULL == fid)
4445
return -2;
45-
pgm_writepgminit(fid, width, height, 255, 0);
46-
for (int i=0; i<height; i++)
47-
pgm_writepgmrow(fid, img.data + width*i, width, 255, 0);
46+
written = fwrite(img.data, sizeof(unsigned char), width * height, fid);
4847
fclose(fid);
4948
return 0;
5049
}

mandel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <complex.h>
22

3-
int mandel(float x, float y, int max_iters, unsigned int * val)
3+
int mandel(float x, float y, int max_iters, unsigned char * val)
44
{
55
int i = 0;
66
_Complex float c = CMPLX(x, y);

0 commit comments

Comments
 (0)