Skip to content

Commit

Permalink
Merge branch 'alg_doc'
Browse files Browse the repository at this point in the history
  • Loading branch information
marvelousmonicaaa committed May 30, 2024
2 parents 913e1a7 + d70ccc4 commit a992496
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pySLM2/runtime_benchmark/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ For the same tests as above, we obtained:
| CPU |$437.35 \pm 17.03$ s | $ 468.21 \pm 13.55 $ s | $250.61 \pm 12.39$ s| $0.55 \pm 0.07$ s |
| GPU | $3.51 \pm 0.19 $ s | $4.31 \pm 0.17$ s |$4.06 \pm 0.15$ s| $ 0.21 \pm 0.10$ s|

These findings show that the iterative algorithms can be greatly accelerated by GPU usage.
These findings show that the iterative algorithms can be greatly accelerated by GPU usage.
61 changes: 61 additions & 0 deletions pySLM2/runtime_benchmark/runtime_ifta_cpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
from scipy.constants import micro, nano, milli
import numpy as np
import time

import pySLM2
import tensorflow as tf


def task(method):
dmd = pySLM2.DLP9500(
369 * nano, # wavelength
200 * milli, # effective focal length
4, # periodicity of the grating = 4 pixels
np.pi/4, # The dmd is rotated by 45 degrees
)
x0, y0 = dmd.first_order_origin
# The beam illumilating the DMD is an gaussian beam with a waist of 5 mm
input_profile = pySLM2.HermiteGaussian(0,0,1,5*milli)

# targeted profile at the image plane
# Here we create two gaussian spots separated by 30 microns
output_profile = pySLM2.HermiteGaussian(0,0,1,10*micro, n=0, m=0) - pySLM2.HermiteGaussian(30*micro,0,1,10*micro, n=0, m=0)
signal_window = pySLM2.RectangularWindowRectangle(x0, y0, 100 * micro, 100 * micro)

start = time.time()

kwargs = dict()
kwargs["signal_window"] = signal_window
kwargs["N"] = 2000
dmd.calculate_dmd_state(
input_profile,
output_profile,
method=method,
**kwargs
)
end = time.time()
total_time = end-start
return total_time

num_gpu = len(tf.config.list_physical_devices('GPU'))
print("Num GPUs Available: ", num_gpu)
if num_gpu ==0:
print("No GPU found. Running on CPU.")

else:
print("Found GPU. Exit.")
exit()
# Run the task on CPU
print("Running on CPU")

num_test = 10
result = []
print(f'Total {num_test} Tests Running on CPU')
for i in range(num_test):
ti = task('ifta')
print(f'test {i} runtime: {ti:0.02f}s')
result.append(ti)
print(f'runtime for {num_test} runs: {np.mean(result):0.2f}s +- {np.std(result):0.2f}s')

63 changes: 63 additions & 0 deletions pySLM2/runtime_benchmark/runtime_ifta_gpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from scipy.constants import micro, nano, milli
import numpy as np
import time

import pySLM2
import tensorflow as tf



def task(method):
dmd = pySLM2.DLP9500(
369 * nano, # wavelength
200 * milli, # effective focal length
4, # periodicity of the grating = 4 pixels
np.pi/4, # The dmd is rotated by 45 degrees
)
x0, y0 = dmd.first_order_origin
# The beam illumilating the DMD is an gaussian beam with a waist of 5 mm
input_profile = pySLM2.HermiteGaussian(0,0,1,5*milli)

# targeted profile at the image plane
# Here we create two gaussian spots separated by 30 microns
output_profile = pySLM2.HermiteGaussian(0,0,1,10*micro, n=0, m=0) - pySLM2.HermiteGaussian(30*micro,0,1,10*micro, n=0, m=0)
signal_window = pySLM2.RectangularWindowRectangle(x0, y0, 100 * micro, 100 * micro)

start = time.time()

kwargs = dict()
kwargs["signal_window"] = signal_window
kwargs["N"] = 2000
dmd.calculate_dmd_state(
input_profile,
output_profile,
method=method,
**kwargs
)
end = time.time()
total_time = end-start
return total_time



num_gpu = len(tf.config.list_physical_devices('GPU'))
print("Num GPUs Available: ", num_gpu)
if num_gpu ==0:
print("No GPU found. Exit.")
exit()
else:
print("Is Built with CUDA: ", tf.test.is_built_with_cuda())

num_test = 10
result = []
print(f'Total {num_test} Tests Running on GPU')
for i in range(num_test):
ti = task('ifta')
print(f'test {i} runtime: {ti:0.02f}s')
result.append(ti)
print(f'runtime for {num_test} runs: {np.mean(result):0.2f}s +- {np.std(result):0.2f}s')



56 changes: 56 additions & 0 deletions pySLM2/runtime_benchmark/slm_hologram_gs_cpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
from scipy.constants import micro, nano, milli
import numpy as np
import time

import pySLM2
import tensorflow as tf


def task(method):
lcos_slm = pySLM2.PLUTO_2(
369 * nano, # wavelength
200 * milli, # effective focal length
)

# The beam illumilating the DMD is an gaussian beam with a waist of 5 mm
input_profile = pySLM2.HermiteGaussian(0,0,1,5*milli)

# targeted profile at the image plane
output_profile = pySLM2.HermiteGaussian(0,0,1,30*micro, n=1, m=1)

start = time.time()
lcos_slm.calculate_hologram(
input_profile,
output_profile,
method=method,
N=200,
)
end = time.time()
total_time = end-start
return total_time



num_gpu = len(tf.config.list_physical_devices('GPU'))
print("Num GPUs Available: ", num_gpu)
if num_gpu ==0:
print("No GPU found. Running on CPU.")

else:
print("Found GPU. Exit.")
exit()

num_test = 10
result = []

print(f'Total {num_test} Tests Running on CPU')
for i in range(num_test):
ti = task('gs')
print(f'test {i} runtime: {ti:0.02f}s')
result.append(ti)
print(f'runtime for {num_test} runs: {np.mean(result):0.2f}s +- {np.std(result):0.2f}s')



54 changes: 54 additions & 0 deletions pySLM2/runtime_benchmark/slm_hologram_gs_gpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
from scipy.constants import micro, nano, milli
import numpy as np
import time

import pySLM2
import tensorflow as tf


def task(method):
lcos_slm = pySLM2.PLUTO_2(
369 * nano, # wavelength
200 * milli, # effective focal length
)

# The beam illumilating the DMD is an gaussian beam with a waist of 5 mm
input_profile = pySLM2.HermiteGaussian(0,0,1,5*milli)

# targeted profile at the image plane
output_profile = pySLM2.HermiteGaussian(0,0,1,30*micro, n=1, m=1)

start = time.time()
lcos_slm.calculate_hologram(
input_profile,
output_profile,
method=method,
N=200,
)
end = time.time()
total_time = end-start
return total_time



num_gpu = len(tf.config.list_physical_devices('GPU'))
print("Num GPUs Available: ", num_gpu)
if num_gpu ==0:
print("No GPU found. Exit.")
exit()
else:
print("Is Built with CUDA: ", tf.test.is_built_with_cuda())

num_test = 10
result = []
print(f'Total {num_test} Tests Running on GPU')
for i in range(num_test):
ti = task('gs')
print(f'test {i} runtime: {ti:0.02f}s')
result.append(ti)
print(f'runtime for {num_test} runs: {np.mean(result):0.2f}s +- {np.std(result):0.2f}s')



8 changes: 5 additions & 3 deletions pySLM2/slm.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,16 +486,18 @@ def _state_tensor(self):

def calculate_hologram(self, input_profile, target_amp_profile, method="gs", **kwargs):
'''
Calculate the hologram for the LCOS SLM.
Calculate the hologram displayed on the LCOS SLM.
Parameters
----------
input_profile: FunctionProfile or numpy.ndarray or tensorflow.Tensor or float or int or complex
The input profile of the beam at Fourier plane.
target_amp_profile: FunctionProfile or numpy.ndarray or tensorflow.Tensor or float or int or complex
The target amplitude profile of the beam at image plane.
The target profile of the beam at image plane.
method: str
The method to calculate the hologram. Available methods are "gs", "mraf".
The method to calculate the hologram.
'gs': Gerchberg-Saxton algorithm.
'mraf': Modified Random Amplitude Fourier Transform Algorithm.
kwargs: dict
Additional arguments for the method.
'''
Expand Down

0 comments on commit a992496

Please sign in to comment.