Skip to content

Instantly share code, notes, and snippets.

@NikosAlexandris
Last active January 22, 2024 10:58
Show Gist options
  • Save NikosAlexandris/9cf8c3209519208e24385448ebcfca21 to your computer and use it in GitHub Desktop.
Save NikosAlexandris/9cf8c3209519208e24385448ebcfca21 to your computer and use it in GitHub Desktop.
Example using DaCe
import dace
import numpy as np
import time as timer
from rich import print
REPETITIONS_DEFAULT = 10
@dace
def myprogram_daced(a):
for i in range(a.shape[0]):
a[i] += i
return np.sum(a)
def myprogram(a):
for i in range(a.shape[0]):
a[i] += i
return np.sum(a)
def time_the_loop_over_array_elements(
an_array,
repetitions: int = REPETITIONS_DEFAULT,
use_dace: bool = False
):
"""
"""
timings = []
for _ in range(repetitions):
if use_dace:
start_time = timer.perf_counter()
myprogram_daced(an_array)
timings.append(timer.perf_counter() - start_time)
else:
start_time = timer.perf_counter()
myprogram(an_array)
timings.append(timer.perf_counter() - start_time)
median_timing = np.median(timings)
return f"{median_timing:.6f}"
# array sizes
array_sizes = [100, 500, 1000, 5000, 10000, 50000, 100000, 500000, 1000000, 5000000]
# timings for each size
timings_without_dace = []
timings_with_dace = []
for size in array_sizes:
an_array = np.random.rand(size)
timings_without_dace.append(time_the_loop_over_array_elements(an_array, use_dace=False))
timings_with_dace.append(time_the_loop_over_array_elements(an_array, use_dace=True))
print(f"Sizes : {array_sizes}")
print(f"Median timing")
print(f"Without DaCe : {timings_without_dace}")
print(f"With DaCe : {timings_with_dace}")
# # Plotting the results
# plt.figure(figsize=(10, 6))
# plt.plot(array_sizes, timings_without_dace, label='Without DaCe')
# plt.plot(array_sizes, timings_with_dace, label='With DaCe')
# plt.xlabel('Array Size')
# plt.ylabel('Median Time (seconds)')
# plt.title('Running Time vs Array Size')
# plt.legend()
# plt.show()
❯ python example_from_readme.py
/software/dace/.dace_virtual_environment/lib/python3.11/site-packages/dace/sdfg/sdfg.py:2191: UserWarning: SDFG "myprogram_daced" is already loaded by another object, recompiling under a different name.
warnings.warn('SDFG "%s" is already loaded by another object, '
Sizes : [100, 500, 1000, 5000, 10000, 50000, 100000, 500000, 1000000, 5000000]
Median timing
Without DaCe : ['0.000020', '0.000104', '0.000203', '0.001048', '0.001878', '0.007718', '0.014757', '0.074528', '0.149014', '0.759548']
With DaCe : ['0.000470', '0.000454', '0.000492', '0.000769', '0.001095', '0.003783', '0.007155', '0.034161', '0.067394', '0.331376']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment