python multiprocessing for mandelbrot set












0














please help speed this up. (I am a multiprocessing noob):



from multiprocessing import Pool
from numpy import real, imag, abs
from PIL import Image
from threading import Thread
import os
resolution = 15000 # not direct resolution, but proportional to it
resolution100=int(resolution*0.01)
size = tuple([100, resolution*2])
color = tuple([1])
start_r = 0
stop_r= 0
"""r is real and i is imaginary where practical"""


def process_job(r):
"""creates a slice of the set"""
img = Image.new('1', size, color) # B&W image
print(int(r + 2*resolution100+1), 'of', int(resolution100 *3))
r = r * 100
for rval in range(100):
for i in range(-resolution, resolution, 1):
if abrot(((r + rval) / resolution), (i / resolution * 1j)):
try:
img.putpixel((rval, i + resolution,), 0)
except:
print(r, rval, i + resolution)
img.save(('z_images/' + str(resolution) + '/' + str(int(r/100)+int(resolution*.02)) + '.png'))


def abrot(x, y):
"""tests a point"""
c = (x + y)
z = 0 + 0j
for _ in range(5):
z = z * z + c
if abs(real(z*z+c))>=2and abs(imag(z*z+c))>=2:
return False
for _ in range(int(resolution / 10)):
if real(z + 0.0001) > float(real(z*z+c)) > real(z - 0.0001):
return True
z = z * z + c
if abs(real(z)) >= 2:
return False
return True


def doItAll(resolution):
with Pool(4, maxtasksperchild=10) as p:
p.map(process_job, range((-2 * resolution100+start_r), resolution100-stop_r, 1))


if __name__ == '__main__':
try:
os.mkdir('/Users/milo/PycharmProjects/mandlebrot/venv/z_images/'+str(resolution)+'/') # I know I misspelled it
except:pass
doItAll(resolution)
# image names must be sorted, but are strings like '1.png', so dict is used
beta_dirs=(os.listdir('/Users/milo/PycharmProjects/mandlebrot/venv/z_images/'+str(resolution)+'/'))
dic={}
for dir in beta_dirs:
key=''
for character in dir:
if character in '1234567890-':
key+=(character)
dic[int(key)]=dir

img = Image.new('1', (resolution*3, resolution*2))
r_offset = 0
for dir in sorted(dic.items()):
im=Image.open('/Users/milo/PycharmProjects/mandlebrot/venv/z_images/'+str(resolution)+'/'+dir[1])
img.paste(im, (r_offset, 0))
r_offset += 100

img.save(('z_images/brot' + str(resolution) + '.png'))
print('saved as brot' + str(resolution) + '.png')


this is being run on a mac fyi, also it runs fine, just slow. mostly looking to speed it up, and allow for restarts more easily. I am running pool with more cores than I think I have, but activity monitor is showing best cpu utilization with 4 processes, so I don't know what I should do









share







New contributor




miloLovesCamelCase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0














    please help speed this up. (I am a multiprocessing noob):



    from multiprocessing import Pool
    from numpy import real, imag, abs
    from PIL import Image
    from threading import Thread
    import os
    resolution = 15000 # not direct resolution, but proportional to it
    resolution100=int(resolution*0.01)
    size = tuple([100, resolution*2])
    color = tuple([1])
    start_r = 0
    stop_r= 0
    """r is real and i is imaginary where practical"""


    def process_job(r):
    """creates a slice of the set"""
    img = Image.new('1', size, color) # B&W image
    print(int(r + 2*resolution100+1), 'of', int(resolution100 *3))
    r = r * 100
    for rval in range(100):
    for i in range(-resolution, resolution, 1):
    if abrot(((r + rval) / resolution), (i / resolution * 1j)):
    try:
    img.putpixel((rval, i + resolution,), 0)
    except:
    print(r, rval, i + resolution)
    img.save(('z_images/' + str(resolution) + '/' + str(int(r/100)+int(resolution*.02)) + '.png'))


    def abrot(x, y):
    """tests a point"""
    c = (x + y)
    z = 0 + 0j
    for _ in range(5):
    z = z * z + c
    if abs(real(z*z+c))>=2and abs(imag(z*z+c))>=2:
    return False
    for _ in range(int(resolution / 10)):
    if real(z + 0.0001) > float(real(z*z+c)) > real(z - 0.0001):
    return True
    z = z * z + c
    if abs(real(z)) >= 2:
    return False
    return True


    def doItAll(resolution):
    with Pool(4, maxtasksperchild=10) as p:
    p.map(process_job, range((-2 * resolution100+start_r), resolution100-stop_r, 1))


    if __name__ == '__main__':
    try:
    os.mkdir('/Users/milo/PycharmProjects/mandlebrot/venv/z_images/'+str(resolution)+'/') # I know I misspelled it
    except:pass
    doItAll(resolution)
    # image names must be sorted, but are strings like '1.png', so dict is used
    beta_dirs=(os.listdir('/Users/milo/PycharmProjects/mandlebrot/venv/z_images/'+str(resolution)+'/'))
    dic={}
    for dir in beta_dirs:
    key=''
    for character in dir:
    if character in '1234567890-':
    key+=(character)
    dic[int(key)]=dir

    img = Image.new('1', (resolution*3, resolution*2))
    r_offset = 0
    for dir in sorted(dic.items()):
    im=Image.open('/Users/milo/PycharmProjects/mandlebrot/venv/z_images/'+str(resolution)+'/'+dir[1])
    img.paste(im, (r_offset, 0))
    r_offset += 100

    img.save(('z_images/brot' + str(resolution) + '.png'))
    print('saved as brot' + str(resolution) + '.png')


    this is being run on a mac fyi, also it runs fine, just slow. mostly looking to speed it up, and allow for restarts more easily. I am running pool with more cores than I think I have, but activity monitor is showing best cpu utilization with 4 processes, so I don't know what I should do









    share







    New contributor




    miloLovesCamelCase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0







      please help speed this up. (I am a multiprocessing noob):



      from multiprocessing import Pool
      from numpy import real, imag, abs
      from PIL import Image
      from threading import Thread
      import os
      resolution = 15000 # not direct resolution, but proportional to it
      resolution100=int(resolution*0.01)
      size = tuple([100, resolution*2])
      color = tuple([1])
      start_r = 0
      stop_r= 0
      """r is real and i is imaginary where practical"""


      def process_job(r):
      """creates a slice of the set"""
      img = Image.new('1', size, color) # B&W image
      print(int(r + 2*resolution100+1), 'of', int(resolution100 *3))
      r = r * 100
      for rval in range(100):
      for i in range(-resolution, resolution, 1):
      if abrot(((r + rval) / resolution), (i / resolution * 1j)):
      try:
      img.putpixel((rval, i + resolution,), 0)
      except:
      print(r, rval, i + resolution)
      img.save(('z_images/' + str(resolution) + '/' + str(int(r/100)+int(resolution*.02)) + '.png'))


      def abrot(x, y):
      """tests a point"""
      c = (x + y)
      z = 0 + 0j
      for _ in range(5):
      z = z * z + c
      if abs(real(z*z+c))>=2and abs(imag(z*z+c))>=2:
      return False
      for _ in range(int(resolution / 10)):
      if real(z + 0.0001) > float(real(z*z+c)) > real(z - 0.0001):
      return True
      z = z * z + c
      if abs(real(z)) >= 2:
      return False
      return True


      def doItAll(resolution):
      with Pool(4, maxtasksperchild=10) as p:
      p.map(process_job, range((-2 * resolution100+start_r), resolution100-stop_r, 1))


      if __name__ == '__main__':
      try:
      os.mkdir('/Users/milo/PycharmProjects/mandlebrot/venv/z_images/'+str(resolution)+'/') # I know I misspelled it
      except:pass
      doItAll(resolution)
      # image names must be sorted, but are strings like '1.png', so dict is used
      beta_dirs=(os.listdir('/Users/milo/PycharmProjects/mandlebrot/venv/z_images/'+str(resolution)+'/'))
      dic={}
      for dir in beta_dirs:
      key=''
      for character in dir:
      if character in '1234567890-':
      key+=(character)
      dic[int(key)]=dir

      img = Image.new('1', (resolution*3, resolution*2))
      r_offset = 0
      for dir in sorted(dic.items()):
      im=Image.open('/Users/milo/PycharmProjects/mandlebrot/venv/z_images/'+str(resolution)+'/'+dir[1])
      img.paste(im, (r_offset, 0))
      r_offset += 100

      img.save(('z_images/brot' + str(resolution) + '.png'))
      print('saved as brot' + str(resolution) + '.png')


      this is being run on a mac fyi, also it runs fine, just slow. mostly looking to speed it up, and allow for restarts more easily. I am running pool with more cores than I think I have, but activity monitor is showing best cpu utilization with 4 processes, so I don't know what I should do









      share







      New contributor




      miloLovesCamelCase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      please help speed this up. (I am a multiprocessing noob):



      from multiprocessing import Pool
      from numpy import real, imag, abs
      from PIL import Image
      from threading import Thread
      import os
      resolution = 15000 # not direct resolution, but proportional to it
      resolution100=int(resolution*0.01)
      size = tuple([100, resolution*2])
      color = tuple([1])
      start_r = 0
      stop_r= 0
      """r is real and i is imaginary where practical"""


      def process_job(r):
      """creates a slice of the set"""
      img = Image.new('1', size, color) # B&W image
      print(int(r + 2*resolution100+1), 'of', int(resolution100 *3))
      r = r * 100
      for rval in range(100):
      for i in range(-resolution, resolution, 1):
      if abrot(((r + rval) / resolution), (i / resolution * 1j)):
      try:
      img.putpixel((rval, i + resolution,), 0)
      except:
      print(r, rval, i + resolution)
      img.save(('z_images/' + str(resolution) + '/' + str(int(r/100)+int(resolution*.02)) + '.png'))


      def abrot(x, y):
      """tests a point"""
      c = (x + y)
      z = 0 + 0j
      for _ in range(5):
      z = z * z + c
      if abs(real(z*z+c))>=2and abs(imag(z*z+c))>=2:
      return False
      for _ in range(int(resolution / 10)):
      if real(z + 0.0001) > float(real(z*z+c)) > real(z - 0.0001):
      return True
      z = z * z + c
      if abs(real(z)) >= 2:
      return False
      return True


      def doItAll(resolution):
      with Pool(4, maxtasksperchild=10) as p:
      p.map(process_job, range((-2 * resolution100+start_r), resolution100-stop_r, 1))


      if __name__ == '__main__':
      try:
      os.mkdir('/Users/milo/PycharmProjects/mandlebrot/venv/z_images/'+str(resolution)+'/') # I know I misspelled it
      except:pass
      doItAll(resolution)
      # image names must be sorted, but are strings like '1.png', so dict is used
      beta_dirs=(os.listdir('/Users/milo/PycharmProjects/mandlebrot/venv/z_images/'+str(resolution)+'/'))
      dic={}
      for dir in beta_dirs:
      key=''
      for character in dir:
      if character in '1234567890-':
      key+=(character)
      dic[int(key)]=dir

      img = Image.new('1', (resolution*3, resolution*2))
      r_offset = 0
      for dir in sorted(dic.items()):
      im=Image.open('/Users/milo/PycharmProjects/mandlebrot/venv/z_images/'+str(resolution)+'/'+dir[1])
      img.paste(im, (r_offset, 0))
      r_offset += 100

      img.save(('z_images/brot' + str(resolution) + '.png'))
      print('saved as brot' + str(resolution) + '.png')


      this is being run on a mac fyi, also it runs fine, just slow. mostly looking to speed it up, and allow for restarts more easily. I am running pool with more cores than I think I have, but activity monitor is showing best cpu utilization with 4 processes, so I don't know what I should do







      python-3.x





      share







      New contributor




      miloLovesCamelCase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      miloLovesCamelCase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      miloLovesCamelCase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 7 mins ago









      miloLovesCamelCasemiloLovesCamelCase

      1




      1




      New contributor




      miloLovesCamelCase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      miloLovesCamelCase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      miloLovesCamelCase is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          miloLovesCamelCase is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211080%2fpython-multiprocessing-for-mandelbrot-set%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          miloLovesCamelCase is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          miloLovesCamelCase is a new contributor. Be nice, and check out our Code of Conduct.













          miloLovesCamelCase is a new contributor. Be nice, and check out our Code of Conduct.












          miloLovesCamelCase is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211080%2fpython-multiprocessing-for-mandelbrot-set%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Costa Masnaga

          Fotorealismo

          Sidney Franklin