Skip to content

Commit

Permalink
Merge pull request #7 from DavidDiazGuerra/mixed_precision
Browse files Browse the repository at this point in the history
Mixed precision
  • Loading branch information
DavidDiazGuerra authored Sep 24, 2019
2 parents df4d93e + 801b9b5 commit 84ea4bf
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 109 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ Thumbs.db
###########
build/
dist/
gpurRIR.egg-info/
gpuRIR.egg-info/
20 changes: 12 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
# https://stackoverflow.com/questions/51907755/building-a-pybind11-module-with-cpp-and-cuda-sources-using-cmake

cmake_minimum_required(VERSION 3.12)
#set(CMAKE_CXX_COMPILER "/usr/local/gcc55/bin/g++55")

project(gpuRIR LANGUAGES CXX CUDA)
find_package(CUDA)

set(PYBIND11_CPP_STANDARD -std=c++11)
add_subdirectory(third_party/pybind11)
# Select several CUDA architectures
include(FindCUDA)
cuda_select_nvcc_arch_flags(CUDA_ARCH_FLAGS Common) # Change Common by Auto for autodetect
string(REPLACE ";" " " CUDA_ARCH_FLAGS "${CUDA_ARCH_FLAGS}")
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${CUDA_ARCH_FLAGS}")

set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -D_FORCE_INLINES")
# set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -lineinfo")

add_library(gpuRIRcu STATIC
src/gpuRIR_cuda.cu
)

SET(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -D_FORCE_INLINES")
# SET(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -lineinfo")

#target_link_libraries(gpuRIRcu PRIVATE -lcurand)

set_target_properties(gpuRIRcu PROPERTIES
POSITION_INDEPENDENT_CODE ON
CUDA_VISIBILITY_PRESET "hidden"
Expand All @@ -27,6 +28,9 @@ add_library(gpuRIR_bind MODULE
src/python_bind.cpp
)

set(PYBIND11_CPP_STANDARD -std=c++11)
add_subdirectory(third_party/pybind11)

set_target_properties(gpuRIR_bind PROPERTIES
CXX_VISIBILITY_PRESET "hidden"
PREFIX "${PYTHON_MODULE_PREFIX}"
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- [Documentation](#documentation)
* [`simulateRIR`](#simulaterir)
* [`simulateTrajectory`](#simulatetrajectory)
* [`activateMixedPrecision`](#activateMixedPrecision)
* [`beta_SabineEstimation`](#beta_sabineestimation)
* [`att2t_SabineEstimator`](#att2t_sabineestimator)
* [`t2n`](#t2n)
Expand Down Expand Up @@ -101,6 +102,15 @@ Filter an audio signal by the RIRs of a motion trajectory recorded with a microp
*2D ndarray*
Matrix with the signals captured by each microphone in each column.

### `activateMixedPrecision`

Activate the mixed precision mode, only for Pascal GPU architecture or superior.

#### Parameters

* **activate** : *bool, optional.*
True for activate and Flase for deactivate. True by default.

### `beta_SabineEstimation`

Estimation of the reflection coefficients needed to have the desired reverberation time.
Expand Down
2 changes: 2 additions & 0 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from math import ceil

import gpuRIR
gpuRIR.activateMixedPrecision(False)

room_sz = [3,3,2.5] # Size of the room [m]
nb_src = 2 # Number of sources
Expand All @@ -31,3 +32,4 @@

t = np.arange(int(ceil(Tmax * fs))) / fs
plt.plot(t, RIRs.reshape(nb_src*nb_rcv, -1).transpose())
plt.show()
2 changes: 2 additions & 0 deletions examples/simulate_trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from scipy.io import wavfile

import gpuRIR
gpuRIR.activateMixedPrecision(False)

fs, source_signal = wavfile.read('source_signal.wav')

Expand All @@ -33,3 +34,4 @@
filtered_signal = gpuRIR.simulateTrajectory(source_signal, RIRs)
wavfile.write('filtered_signal.wav', fs, filtered_signal)
plt.plot(filtered_signal)
plt.show()
6 changes: 4 additions & 2 deletions examples/time_vs_T60.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import numpy as np
import matplotlib.pyplot as plt
import time
import time

import gpuRIR
gpuRIR.activateMixedPrecision(False)

T60_vec = np.arange(0.1, 2.2, 0.2) # Reverberation times to measure
nb_test_per_point = 10 # Number of simulations per T60 to average the runtime
Expand Down Expand Up @@ -45,4 +46,5 @@

plt.semilogy(T60_vec, times)
plt.ylabel("time [s]")
plt.xlabel("T60 [s]")
plt.xlabel("T60 [s]")
plt.show()
4 changes: 3 additions & 1 deletion examples/time_vs_nbRIRs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

import numpy as np
import matplotlib.pyplot as plt
import time
import time

import gpuRIR
gpuRIR.activateMixedPrecision(False)

nb_src_vec = np.concatenate([2**np.arange(12), [4094]]) # Number of RIRs to measure
nb_test_per_point = 10 # Number of simulations per T60 to average the runtime
Expand Down Expand Up @@ -46,3 +47,4 @@
plt.loglog(nb_src_vec, times)
plt.ylabel("time [s]")
plt.xlabel("number of RIRs")
plt.show()
23 changes: 17 additions & 6 deletions gpuRIR/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from scipy.optimize import minimize
from scipy.signal import convolve

from gpuRIR_bind import simulateRIR_bind, gpu_conv, cuda_warmup
from gpuRIR_bind import gpuRIR_bind

__all__ = ["mic_patterns", "beta_SabineEstimation", "att2t_SabineEstimator", "t2n", "simulateRIR", "simulateTrajectory"]
__all__ = ["mic_patterns", "beta_SabineEstimation", "att2t_SabineEstimator", "t2n", "simulateRIR", "simulateTrajectory", "activate_mixed_precision"]

mic_patterns = {
"omni": 0,
Expand Down Expand Up @@ -153,7 +153,7 @@ def simulateRIR(room_sz, beta, pos_src, pos_rcv, nb_img, Tmax, fs, Tdiff=None, m
if orV_rcv is None: orV_rcv = np.zeros_like(pos_rcv)
else: orV_rcv = orV_rcv.astype('float32', order='C', copy=False)

return simulateRIR_bind(room_sz, beta, pos_src, pos_rcv, orV_rcv, mic_patterns[mic_pattern], nb_img, Tdiff, Tmax, fs, c)
return gpuRIR_bind_simulator.simulateRIR_bind(room_sz, beta, pos_src, pos_rcv, orV_rcv, mic_patterns[mic_pattern], nb_img, Tdiff, Tmax, fs, c)

def simulateTrajectory(source_signal, RIRs, timestamps=None, fs=None):
''' Filter an audio signal by the RIRs of a motion trajectory recorded with a microphone array.
Expand Down Expand Up @@ -192,7 +192,7 @@ def simulateTrajectory(source_signal, RIRs, timestamps=None, fs=None):
for n in range(nPts):
segments[n,0:w_len[n]] = source_signal[w_ini[n]:w_ini[n+1]]
segments = segments.astype('float32', order='C', copy=False)
convolution = gpu_conv(segments, RIRs)
convolution = gpuRIR_bind_simulator.gpu_conv(segments, RIRs)

filtered_signal = np.zeros((nSamples+lenRIR-1, nRcv))
for m in range(nRcv):
Expand All @@ -201,5 +201,16 @@ def simulateTrajectory(source_signal, RIRs, timestamps=None, fs=None):

return filtered_signal

# Initialize the CUDA runtime API and the cuFFT library when the module is loaded
cuda_warmup()
def activateMixedPrecision(activate=True):
''' Activate the mixed precision mode, only for Pascal GPU architecture or superior.
Parameters
----------
activate : bool, optional
True for activate and Flase for deactivate. True by default.
'''
gpuRIR_bind_simulator.activate_mixed_precision_bind(activate)

# Create the simulator object when the module is loaded
gpuRIR_bind_simulator = gpuRIR_bind()
Binary file added gpuRIR/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def build_extension(self, ext):

setup(
name='gpuRIR',
version='1.1.0',
version='1.2.0',
author='David Diaz-Guerra',
author_email='[email protected]',
url='',
Expand Down
Loading

0 comments on commit 84ea4bf

Please sign in to comment.