Skip to content

Commit afe5119

Browse files
committed
add ctypes, cffi
1 parent 93d5eb4 commit afe5119

File tree

7 files changed

+3495
-181
lines changed

7 files changed

+3495
-181
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
*.swp
22
.ipynb_checkpoints/
3+
*.png
4+
*.pgm
5+
*.pyc
6+
main
7+
*.so
8+
.gdb_history

create_fractal.c

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

3-
void create_fractal(unsigned int * image, int width, int height,
4-
int iters) {
5-
float pixel_size_x = 3.0 / width;
6-
float pixel_size_y = 2.0 / height;
7-
for (int y=0; y<height; y++) {
3+
typedef struct _Img{
4+
int width;
5+
int height;
6+
int * data;
7+
} Img;
8+
9+
10+
void create_fractal(Img img, int iters) {
11+
float pixel_size_x = 3.0 / img.width;
12+
float pixel_size_y = 2.0 / img.height;
13+
for (int y=0; y < img.height; y++) {
814
float imag = y * pixel_size_y - 1;
9-
int yy = y * width;
10-
for (int x=0; x<width; x++) {
15+
int yy = y * img.width;
16+
for (int x=0; x < img.width; x++) {
1117
float real = x * pixel_size_x - 2;
1218
unsigned int color;
1319
int ret = mandel(real, imag, iters, &color);
14-
image[yy + x] = color;
20+
img.data[yy + x] = color;
1521
}
1622
}
1723
}

create_fractal.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import print_function, division
2-
31
from mandel import mandel
42

53
def create_fractal(image, width, height, iters):
@@ -11,18 +9,4 @@ def create_fractal(image, width, height, iters):
119
for x in range(width):
1210
real = x * pixel_size_x - 2
1311
color = mandel(real, imag, iters)
14-
image[yy+x] = color
15-
16-
if __name__ == '__main__':
17-
from timeit import default_timer as timer
18-
import numpy as np
19-
from PIL import Image
20-
width = 1500
21-
height = 1000
22-
image = np.empty(width*height, dtype=np.uint8)
23-
s = timer()
24-
create_fractal(image, width, height, 20)
25-
e = timer()
26-
print('pure python required {}f.2 secs'.format(e - s))
27-
im = Image.frombuffer('L', (width, height), image, 'raw', 'L', 0, 1)
28-
im.save('python_numpy.png')
12+
image[yy+x] = color

main.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,34 @@ long timer_end(struct timespec start_time){
1717
return diffInNanos;
1818
}
1919

20-
int mandel(int x, int y, int max_iters, unsigned int * val);
21-
22-
void create_fractal(unsigned int * image, int width, int height,
23-
int iters);
20+
#include "create_fractal.c"
2421

2522
int main(int argc, const char *argv[], const char * env[])
2623
{
2724
int width = 1500;
2825
int height = 1000;
2926
int iters = 20;
3027
FILE * fid = NULL;
28+
Img img;
3129
struct timespec vartime;
3230
long time_elapsed_nanos;
33-
unsigned int * image = (unsigned int*)malloc(width * height * sizeof(unsigned int));
34-
if (NULL == image)
31+
img.width = width;
32+
img.height = height;
33+
img.data = (unsigned int*)malloc(width * height * sizeof(unsigned int));
34+
if (NULL == img.data)
3535
return -1;
36-
fid = fopen("c.pgm", "wb");
37-
if (NULL == fid)
38-
return -2;
36+
3937
vartime = timer_start();
40-
create_fractal(image, width, height, iters);
38+
create_fractal(img, iters);
4139
time_elapsed_nanos = timer_end(vartime);
4240
fprintf(stdout, "create_fractal required %ld millisecs\n", time_elapsed_nanos / 1000000);
41+
42+
fid = fopen("c.pgm", "wb");
43+
if (NULL == fid)
44+
return -2;
4345
pgm_writepgminit(fid, width, height, 255, 0);
4446
for (int i=0; i<height; i++)
45-
pgm_writepgmrow(fid, image + width*i, width, 255, 0);
47+
pgm_writepgmrow(fid, img.data + width*i, width, 255, 0);
4648
fclose(fid);
4749
return 0;
4850
}

mandel.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
1-
/* Python
2-
def mandel(x, y, max_iters):
3-
"""
4-
Given the real and imaginary parts of a complex number,
5-
determine if it is a candidate for membership in the Mandelbrot
6-
set given a fixed number of iterations.
7-
"""
8-
i = 0
9-
c = complex(x,y)
10-
z = 0.0j
11-
for i in range(max_iters):
12-
z = z*z + c
13-
if (z.real*z.real + z.imag*z.imag) >= 4:
14-
return i
15-
16-
return 255
17-
*/
18-
191
#include <complex.h>
202

21-
int mandel(int x, int y, int max_iters, unsigned int * val)
3+
int mandel(float x, float y, int max_iters, unsigned int * val)
224
{
235
int i = 0;
246
_Complex float c = CMPLX(x, y);
@@ -32,6 +14,6 @@ int mandel(int x, int y, int max_iters, unsigned int * val)
3214
return 0;
3315
}
3416
}
35-
*val = 255;
17+
*val = max_iters;
3618
return 1;
3719
}

mandel.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
def mandel(x, y, max_iters, value):
32
"""
43
Given the real and imaginary parts of a complex number,
@@ -14,5 +13,4 @@ def mandel(x, y, max_iters, value):
1413
value[0] = i
1514
return 0
1615
value[i] = 255
17-
return 1
18-
16+
return 1

pycon2017_cffi.ipynb

Lines changed: 3457 additions & 121 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)