Skip to content

Instantly share code, notes, and snippets.

@V0XNIHILI
Created May 7, 2020 10:53
Show Gist options
  • Save V0XNIHILI/4e476a073b0ca59275e442ac58370119 to your computer and use it in GitHub Desktop.
Save V0XNIHILI/4e476a073b0ca59275e442ac58370119 to your computer and use it in GitHub Desktop.

Multicore Python

Below you can find some sample template code which you can use to run multicore Python computations.

# =================================================================================================
# Imports
# =================================================================================================

import multiprocessing as mp
import numpy as np

# =================================================================================================
# Variables to keep track of best performance
# =================================================================================================

# You can add variables, remove them or change their names as long as you use the same name in the
# callback() function
variable_1 = 0
variable_2 = 0

# =================================================================================================
# Function you want to run in parallel
# =================================================================================================

# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Update the contents of this function with what you want to compute
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
def parallel(parameters):
    return parameters * 3

# =================================================================================================
# Functions which will be ran by the multicore implementation
# =================================================================================================

def callback(results):
    """Perform some check on the results of the run function!

    Parameters
    ----------
    results : [number]
        Array of result values that have been generated by run()
    """
    # Update the names of variable_1 and variable_2 accordingly
    global variable_1
    global variable_2

    # Leave as is
    res = results[1]

    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    # Perform your checks here and update your best performance case
    # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    if res[0] > variable_1:
        variable_1 = res[0]
        variable_2 = res[1]

def run(pc, parameters):
    """Edit the contents of this function

    Parameters
    ----------
    pc : integer
        Process count; Ignore it
    parameters : [number]
        Array of parameters that have been provided in the loop below

    Returns
    -------
    [number]
        Results of the calculation
    """
    # Leave this line as is
    return (pc, parallel(parameters))

# =================================================================================================
# Multicore setup code
# =================================================================================================

# Test that has to be ran if this script needs to be executed on Windows. Without this check and without
# mp.freeze_support() following right after this check, Windows will not be able to properly run multicore
# Python functionality
# This check evaluates to True when you are running this file as python make_imgs.py
mp.freeze_support()

pool = mp.Pool(mp.cpu_count())

process_counter = 0

# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Create your own loop here with what you would like to loop over, see example below
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

# Perform some form of looping for variables
for q in np.linspace(-1, -1, 20):
    for p in np.linspace(0, 2, 20):
        # Edit your arguments here for the function in which you want to computer something
        arguments = [p, q]

        # Leave the line below as is
        pool.apply_async(run,
                         args=(process_counter, arguments),
                         callback=callback)

        # Leave the line below as is
        process_counter += 1

# Leave as is
pool.close()
pool.join()

# =================================================================================================
# Final results plot/print here
# =================================================================================================

print(variable_1)
print(variable_2)

print('Finished!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment