<jupyter_start><jupyter_text>Lab 09 - High Performance Computing Module Imports<jupyter_code>import multiprocessing as mproc, numba, numpy, time
from numba import njit<jupyter_output><empty_output><jupyter_text>Exercise 01 - Number of Processors<jupyter_code>print("Number of processors:", mproc.cpu_count())<jupyter_output>Number of processors: 8<jupyter_text>Exercise 02 - Array Normalization<jupyter_code>def create_list(rows, cols, min, max):
    return numpy.random.randint(min, max, size=[rows, cols])
def normalize_row(row):
    COLS = len(row)
    norm_row = numpy.zeros(shape=(COLS))
    ROW_MIN = min(row)
    ROW_MAX = max(row)

    for col in range(COLS):
        norm_row[col] = (row[col] - ROW_MIN) / (ROW_MAX - ROW_MIN)

    return norm_row
def normalize_by_row(lst):
    ROWS, COLS = lst.shape
    norm_list = numpy.zeros((ROWS, COLS))

    for row in range(ROWS):
        norm_list[row] = normalize_row(lst[row][:])

    return norm_list
MIN = 0
MAX = 10
SIZES = [ 100, 500, 1000, 2000, 3000 ]

for size in SIZES:
    lst = create_list(size, size, MIN, MAX)
    START = time.time()
    norm_list = normalize_by_row(lst)
    print("Sequential normalization by row for size", size, "took", time.time() - START, "seconds")<jupyter_output>Sequential normalization by row for size 100 took 0.005998849868774414 seconds
Sequential normalization by row for size 500 took 0.13302087783813477 seconds
Sequential normalization by row for size 1000 took 0.5031018257141113 seconds
Sequential normalization by row for size 2000 took 2.0174059867858887 seconds
Sequential normalization by row for size 3000 took 4.352947950363159 seconds<jupyter_text>Exercise 03 - Parallelization Using pool.apply Constant Definitions<jupyter_code>CPU_COUNT = mproc.cpu_count()<jupyter_output><empty_output><jupyter_text>pool = mproc.Pool(CPU_COUNT)for size in SIZES:    START = time.time()    results = [pool.apply(normalize_row, args=(row,)) for row in lst]    print("Parallel normalization by row for size", size, "took", time.time() - START, "seconds")pool.close()  Exercise 04 - Parallelization Using pool.apply async<jupyter_code>pool = mproc.Pool(CPU_COUNT)

for size in SIZES:
    lst = create_list(size, size, MIN, MAX)
    START = time.time()
    tasks = [pool.apply_async(normalize_row, args=(row,)) for row in lst]
    print("Async parallel normalization by row for size", size, "took", time.time() - START, "seconds")

pool.close()<jupyter_output>Async parallel normalization by row for size 100 took 0.0409998893737793 seconds
Async parallel normalization by row for size 500 took 0.07799983024597168 seconds
Async parallel normalization by row for size 1000 took 0.043001651763916016 seconds
Async parallel normalization by row for size 2000 took 0.059998273849487305 seconds
Async parallel normalization by row for size 3000 took 0.024000167846679688 seconds<jupyter_text>Exercise 05 - Numba and JIT<jupyter_code>@njit(parallel=True)
def jit_trace(lst, rows, cols):
    trace = 0

    for row in range(rows):
        for col in range(cols):
            trace += numpy.tanh(lst[row][col])

    return trace
for size in SIZES:
    lst = numpy.arange(size * size).reshape(size, size)
    START = time.time()
    jit_trace(lst, size, size)
    print("JITted parallel tracing for size", size, "took", time.time() - START, "seconds")<jupyter_output>C:\ProgramData\Anaconda3\lib\site-packages\numba\core\typed_passes.py:313: NumbaPerformanceWarning: [1m
The keyword argument 'parallel=True' was specified but no transformation for parallel execution was possible.

To find out why, try turning on parallel diagnostics, see https://numba.pydata.org/numba-doc/latest/user/parallel.html#diagnostics for help.
[1m
File "<ipython-input-17-6ea592bbd34b>", line 2:[0m
[1m@njit(parallel=True)
[1mdef jit_trace(lst, rows, cols):
[0m[1m^[0m[0m
[0m
  warnings.warn(errors.NumbaPerformanceWarning(msg,
JITted parallel normalization by row for size 100 took 0.2651340961456299 seconds
JITted parallel normalization by row for size 500 took 0.0019996166229248047 seconds
JITted parallel normalization by row for size 1000 took 0.009998559951782227 seconds
JITted parallel normalization by row for size 2000 took 0.045000553131103516 seconds
JITted parallel normalization by row for size 3000 took 0.08295917510986328 seconds<jupyter_text>Exercise 06 - Normalization Using JIT<jupyter_code>@njit(parallel=True)
def normalize_row_jit(row):
    COLS = len(row)
    norm_row = numpy.zeros(shape=(COLS))
    ROW_MIN = min(row)
    ROW_MAX = max(row)

    for col in numba.prange(COLS):
        norm_row[col] = (row[col] - ROW_MIN) / (ROW_MAX - ROW_MIN)

    return norm_row
@njit(parallel=True)
def normalize_by_row_jit(lst):
    ROWS, COLS = lst.shape
    norm_list = numpy.zeros((ROWS, COLS))

    for row in numba.prange(ROWS):
        norm_list[row] = normalize_row_jit(lst[row][:])

    return norm_list
for size in SIZES:
    lst = create_list(size, size, MIN, MAX)
    START = time.time()
    normalize_by_row_jit(lst)
    print("JIT parallel normalization by row for size", size, "took", time.time() - START, "seconds")<jupyter_output>JIT parallel normalization by row for size 100 took 1.8604621887207031 seconds
JIT parallel normalization by row for size 500 took 0.015000581741333008 seconds
JIT parallel normalization by row for size 1000 took 0.02200031280517578 seconds
JIT parallel normalization by row for size 2000 took 0.045999765396118164 seconds
JIT parallel normalization by row for size 3000 took 0.08199381828308105 seconds
{'fraction_non_alphanumeric': 0.09522949042284062, 'fraction_numerical': 0.08944705457173835, 'mean_word_length': 4.913461538461538, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '545452', 'license': 'MIT', 'n_tokens_mistral': 2243, 'n_tokens_neox': 1826, 'n_words': 540, 'path': 'Lab 09.ipynb', 'repo_name': 'shipstone98/AdvCompLabs', 'size': '5534'}