<s> import logging <EOL> import math <EOL> import os <EOL> import sys <EOL> top_dir = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , <EOL> os . pardir , <EOL> os . pardir ) ) <EOL> sys . path . insert ( <NUM_LIT:0> , top_dir ) <EOL> from six . moves import range as compat_range <EOL> from taskflow import engines <EOL> from taskflow . engines . worker_based import worker <EOL> from taskflow . patterns import unordered_flow as uf <EOL> from taskflow import task <EOL> from taskflow . utils import threading_utils <EOL> BASE_SHARED_CONF = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> WORKERS = <NUM_LIT:2> <EOL> WORKER_CONF = { <EOL> '<STR_LIT>' : [ <EOL> '<STR_LIT>' % ( __name__ ) , <EOL> ] , <EOL> } <EOL> ENGINE_CONF = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> IMAGE_SIZE = ( <NUM_LIT> , <NUM_LIT> ) <EOL> CHUNK_COUNT = <NUM_LIT:8> <EOL> MAX_ITERATIONS = <NUM_LIT> <EOL> class MandelCalculator ( task . Task ) : <EOL> def execute ( self , image_config , mandelbrot_config , chunk ) : <EOL> """<STR_LIT>""" <EOL> def mandelbrot ( x , y , max_iters ) : <EOL> c = complex ( x , y ) <EOL> z = <NUM_LIT> <EOL> for i in compat_range ( max_iters ) : <EOL> z = z * z + c <EOL> if ( z . real * z . real + z . imag * z . imag ) >= <NUM_LIT:4> : <EOL> return i <EOL> return max_iters <EOL> min_x , max_x , min_y , max_y , max_iters = mandelbrot_config <EOL> height , width = image_config [ '<STR_LIT:size>' ] <EOL> pixel_size_x = ( max_x - min_x ) / width <EOL> pixel_size_y = ( max_y - min_y ) / height <EOL> block = [ ] <EOL> for y in compat_range ( chunk [ <NUM_LIT:0> ] , chunk [ <NUM_LIT:1> ] ) : <EOL> row = [ ] <EOL> imag = min_y + y * pixel_size_y <EOL> for x in compat_range ( <NUM_LIT:0> , width ) : <EOL> real = min_x + x * pixel_size_x <EOL> row . append ( mandelbrot ( real , imag , max_iters ) ) <EOL> block . append ( row ) <EOL> return block <EOL> def calculate ( engine_conf ) : <EOL> flow = uf . Flow ( "<STR_LIT>" ) <EOL> store = { <EOL> '<STR_LIT>' : [ - <NUM_LIT> , <NUM_LIT:1.0> , - <NUM_LIT:1.0> , <NUM_LIT:1.0> , MAX_ITERATIONS ] , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT:size>' : IMAGE_SIZE , <EOL> } <EOL> } <EOL> task_names = [ ] <EOL> height , _width = IMAGE_SIZE <EOL> chunk_size = int ( math . ceil ( height / float ( CHUNK_COUNT ) ) ) <EOL> for i in compat_range ( <NUM_LIT:0> , CHUNK_COUNT ) : <EOL> chunk_name = '<STR_LIT>' % i <EOL> task_name = "<STR_LIT>" % i <EOL> rows = [ i * chunk_size , i * chunk_size + chunk_size ] <EOL> flow . add ( <EOL> MandelCalculator ( task_name , <EOL> rebind = { '<STR_LIT>' : chunk_name } ) ) <EOL> store [ chunk_name ] = rows <EOL> task_names . append ( task_name ) <EOL> eng = engines . load ( flow , store = store , engine_conf = engine_conf ) <EOL> eng . run ( ) <EOL> gather = [ ] <EOL> for name in task_names : <EOL> gather . extend ( eng . storage . get ( name ) ) <EOL> points = [ ] <EOL> for y , row in enumerate ( gather ) : <EOL> for x , color in enumerate ( row ) : <EOL> points . append ( ( ( x , y ) , color ) ) <EOL> return points <EOL> def write_image ( results , output_filename = None ) : <EOL> print ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( len ( results ) , CHUNK_COUNT , WORKERS ) ) <EOL> if not output_filename : <EOL> return <EOL> try : <EOL> from PIL import Image <EOL> except ImportError as e : <EOL> raise RuntimeError ( "<STR_LIT>" % e ) <EOL> color_max = <NUM_LIT:0> <EOL> for _point , color in results : <EOL> color_max = max ( color , color_max ) <EOL> img = Image . new ( '<STR_LIT:L>' , IMAGE_SIZE , "<STR_LIT>" ) <EOL> pixels = img . load ( ) <EOL> for ( x , y ) , color in results : <EOL> if color_max == <NUM_LIT:0> : <EOL> color = <NUM_LIT:0> <EOL> else : <EOL> color = int ( ( float ( color ) / color_max ) * <NUM_LIT> ) <EOL> pixels [ x , y ] = color <EOL> img . save ( output_filename ) <EOL> def create_fractal ( ) : <EOL> logging . basicConfig ( level = logging . ERROR ) <EOL> shared_conf = dict ( BASE_SHARED_CONF ) <EOL> shared_conf . update ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : <NUM_LIT:0.1> , <EOL> } , <EOL> } ) <EOL> if len ( sys . argv ) >= <NUM_LIT:2> : <EOL> output_filename = sys . argv [ <NUM_LIT:1> ] <EOL> else : <EOL> output_filename = None <EOL> worker_conf = dict ( WORKER_CONF ) <EOL> worker_conf . update ( shared_conf ) <EOL> engine_conf = dict ( ENGINE_CONF ) <EOL> engine_conf . update ( shared_conf ) <EOL> workers = [ ] <EOL> worker_topics = [ ] <EOL> print ( '<STR_LIT>' % IMAGE_SIZE ) <EOL> try : <EOL> print ( '<STR_LIT>' % ( WORKERS ) ) <EOL> for i in compat_range ( <NUM_LIT:0> , WORKERS ) : <EOL> worker_conf [ '<STR_LIT>' ] = '<STR_LIT>' % ( i + <NUM_LIT:1> ) <EOL> worker_topics . append ( worker_conf [ '<STR_LIT>' ] ) <EOL> w = worker . Worker ( ** worker_conf ) <EOL> runner = threading_utils . daemon_thread ( w . run ) <EOL> runner . start ( ) <EOL> w . wait ( ) <EOL> workers . append ( ( runner , w . stop ) ) <EOL> engine_conf [ '<STR_LIT>' ] = worker_topics <EOL> results = calculate ( engine_conf ) <EOL> print ( '<STR_LIT>' ) <EOL> finally : <EOL> print ( '<STR_LIT>' ) <EOL> while workers : <EOL> r , stopper = workers . pop ( ) <EOL> stopper ( ) <EOL> r . join ( ) <EOL> print ( "<STR_LIT>" ) <EOL> write_image ( results , output_filename = output_filename ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> create_fractal ( ) </s>
