Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed improvements #74

Merged
merged 4 commits into from
Aug 8, 2020
Merged

Speed improvements #74

merged 4 commits into from
Aug 8, 2020

Conversation

eSoares
Copy link
Contributor

@eSoares eSoares commented Aug 5, 2020

Hello,

In the past few weeks I have been using the library for some heavy number of runs and notice some bottlenecks.
Here are some contributions to patch some of the bottlenecks found to try to make the simulation faster.

  • Added caching using an LRU cache in some functions that are slow and repeatedly called with same parameters;
  • Change not-equal comparison of numpy arrays to bitwise xor since they are 1's and 0's;
  • Reduce iterations in the _where_c function by taking advantage of numpy function to search for a value;

Between each commit the following test was performed (based on conv_encode_decode):

# Authors: CommPy contributors
# License: BSD 3-Clause

import math
import time
from datetime import timedelta

import numpy as np

import commpy.channelcoding.convcode as cc
import commpy.channels as chan
import commpy.links as lk
import commpy.modulation as mod

# =============================================================================
# Convolutional Code 1: G(D) = [1+D^2, 1+D+D^2]
# Standard code with rate 1/2
# =============================================================================

# Number of delay elements in the convolutional encoder
memory = np.array(6, ndmin=1)

# Generator matrix
g_matrix = np.array((0o5, 0o7), ndmin=2)

# Create trellis data structure
trellis1 = cc.Trellis(memory, g_matrix)

# ==================================================================================================
# Complete example using Commpy features and compare hard and soft demodulation. Example with code 1
# ==================================================================================================

# Modem : QPSK
modem = mod.QAMModem(4)

# AWGN channel
channels = chan.SISOFlatChannel(None, (1 + 0j, 0j))

# SNR range to test
SNRs = np.arange(0, 6) + 10 * math.log10(modem.num_bits_symbol)


# Modulation function
def modulate(bits):
    return modem.modulate(cc.conv_encode(bits, trellis1, 'cont'))


# Receiver function (no process required as there are no fading)
def receiver_soft(y, h, constellation, noise_var):
    return modem.demodulate(y, 'soft', noise_var)


# Decoder function
def decoder_soft(msg):
    return cc.viterbi_decode(msg, trellis1, decoding_type='soft')


# Build model from parameters
code_rate = trellis1.k / trellis1.n
model_soft = lk.LinkModel(modulate, channels, receiver_soft,
                          modem.num_bits_symbol, modem.constellation, modem.Es,
                          decoder_soft, code_rate)

# Test
start = time.time()
BERs_soft = model_soft.link_performance(SNRs, 1000, 600, 5000, code_rate)
print(str(timedelta(seconds=(time.time() - start))))

Rough results for each commit are presented bellow:

base (the repo at master) - 0:00:42.092337
with added cache (commit fc764d5) - 0:00:33.801127
with bitwise xor improvement (commit a9d3aa7) - 0:00:31.678570
improvement where_c (commit c276ede) - 0:00:13.405827

Eduardo Soares added 4 commits August 5, 2020 12:59
Using a LRU cache. For now uses static parameters, in the future some configuration should be added.
Is a bit faster in most cases that I have tested.
Instead of iterate all rows and cols, iterate only over the matching values.
Some times the result for the decoded message would be in floats instead of ints, causing the bitwise operation to fail.
@veeresht veeresht merged commit b104c99 into veeresht:master Aug 8, 2020
@veeresht
Copy link
Owner

veeresht commented Aug 8, 2020

Thank you! @eSoares

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants