o-du/phy
Intel O-RAN/X-RAN Generated Doxygen Documentation
Data Structures | Macros | Typedefs | Functions
common.hpp File Reference
#include <exception>
#include <random>
#include <string>
#include <utility>
#include <vector>
#include <immintrin.h>
#include <malloc.h>
#include <rte_config.h>
#include <rte_malloc.h>
#include "gtest/gtest.h"
#include "common_typedef_xran.h"
#include "json.hpp"
Include dependency graph for common.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  BenchmarkParameters
 
struct  missing_config_file_exception
 
struct  reading_input_file_exception
 
class  KernelTests
 

Macros

#define _BBLIB_DPDK_
 
#define ASSERT_ARRAY_NEAR(reference, actual, size, precision)   assert_array_near(reference, actual, size, precision)
 
#define ASSERT_ARRAY_EQ(reference, actual, size)   assert_array_eq(reference, actual, size)
 
#define ASSERT_AVG_GREATER_COMPLEX(reference, actual, size, precision)   assert_avg_greater_complex(reference, actual, size, precision)
 

Typedefs

using json = nlohmann::json
 

Functions

int bind_to_cpu (const unsigned cpu)
 Attach current process to the selected core. More...
 
std::pair< double, double > calculate_statistics (const std::vector< long > values)
 Calculate the mean and variance from the result of the run_benchmark. More...
 
std::vector< unsigned > get_sequence (const unsigned number)
 For a given number return sequence of number from 0 to number - 1. More...
 
json read_json_from_file (const std::string &filename)
 Read JSON from the given file. More...
 
char * read_data_to_aligned_array (const std::string &filename)
 Read binary data from the file. More...
 
unsigned long tsc_recovery ()
 Measure the TSC on the machine. More...
 
unsigned long tsc_tick ()
 Return the current value of the TSC. More...
 
template<typename F , typename ... Args>
std::pair< double, double > run_benchmark (F function, Args ... args)
 Run the given function and return the mean run time and stddev. More...
 
template<typename T >
void assert_array_eq (const T *reference, const T *actual, const int size)
 Assert elements of two arrays. It calls ASSERT_EQ for each element of the array. More...
 
template<typename T >
void assert_array_near (const T *reference, const T *actual, const int size, const double precision)
 Assert elements of two arrays. It calls ASSERT_NEAR for each element of the array. More...
 
template<>
void assert_array_near< complex_float > (const complex_float *reference, const complex_float *actual, const int size, const double precision)
 
template<typename T >
void assert_avg_greater_complex (const T *reference, const T *actual, const int size, const double precision)
 Assert average diff of two arrays. It calls ASSERT_GT to check the average. More...
 
template<typename T >
T * aligned_malloc (const int size, const unsigned alignment)
 Allocates memory of the given size. More...
 
template<typename T >
void aligned_free (T *ptr)
 Frees memory pointed by the given pointer. More...
 
template<typename T , typename U >
T * generate_random_numbers (const long size, const unsigned alignment, U &distribution)
 generate random numbers. More...
 
template<typename T >
T * generate_random_data (const long size, const unsigned alignment)
 generate random data. More...
 
template<typename T >
T * generate_random_int_numbers (const long size, const unsigned alignment, const T lo_range, const T up_range)
 generate integer random numbers. More...
 
template<typename T >
T * generate_random_real_numbers (const long size, const unsigned alignment, const T lo_range, const T up_range)
 generate real random numbers. More...
 

Macro Definition Documentation

◆ _BBLIB_DPDK_

#define _BBLIB_DPDK_

Definition at line 40 of file common.hpp.

◆ ASSERT_ARRAY_EQ

#define ASSERT_ARRAY_EQ (   reference,
  actual,
  size 
)    assert_array_eq(reference, actual, size)

Definition at line 58 of file common.hpp.

◆ ASSERT_ARRAY_NEAR

#define ASSERT_ARRAY_NEAR (   reference,
  actual,
  size,
  precision 
)    assert_array_near(reference, actual, size, precision)

Definition at line 55 of file common.hpp.

◆ ASSERT_AVG_GREATER_COMPLEX

#define ASSERT_AVG_GREATER_COMPLEX (   reference,
  actual,
  size,
  precision 
)    assert_avg_greater_complex(reference, actual, size, precision)

Definition at line 61 of file common.hpp.

Typedef Documentation

◆ json

Definition at line 53 of file common.hpp.

Function Documentation

◆ aligned_free()

void aligned_free ( T *  ptr)

Frees memory pointed by the given pointer.

aligned_free is a wrapper for functions that free memory allocated by aligned_malloc: 'rte_free' from DPDK if hugepages are defined and 'free' otherwise.

Parameters
[in]ptrPointer to the allocated memory.

Definition at line 566 of file common.hpp.

◆ aligned_malloc()

T* aligned_malloc ( const int  size,
const unsigned  alignment 
)

Allocates memory of the given size.

aligned_malloc is wrapper to functions that allocate memory: 'rte_malloc' from DPDK if hugepages are defined, 'memalign' otherwise. Size is defined as a number of variables of given type e.g. floats, rather than bytes. It hides sizeof(T) multiplication and cast hence makes things cleaner.

Parameters
[in]sizeSize of the memory to allocate.
[in]alignmentBytes alignment of the allocated memory. If 0, the return is a pointer that is suitably aligned for any kind of variable (in the same manner as malloc()). Otherwise, the return is a pointer that is a multiple of align. In this case, it must be a power of two. (Minimum alignment is the cacheline size, i.e. 64-bytes)
Returns
Pointer to the allocated memory.

Definition at line 544 of file common.hpp.

◆ assert_array_eq()

void assert_array_eq ( const T *  reference,
const T *  actual,
const int  size 
)

Assert elements of two arrays. It calls ASSERT_EQ for each element of the array.

Parameters
[in]referenceArray with reference values.
[in]actualArray with the actual output.
[in]sizeSize of the array.

Definition at line 448 of file common.hpp.

◆ assert_array_near()

void assert_array_near ( const T *  reference,
const T *  actual,
const int  size,
const double  precision 
)

Assert elements of two arrays. It calls ASSERT_NEAR for each element of the array.

Parameters
[in]referenceArray with reference values.
[in]actualArray with the actual output.
[in]sizeSize of the array.
[in]precisionPrecision fo the comparision used by ASSERT_NEAR.

Definition at line 465 of file common.hpp.

◆ assert_array_near< complex_float >()

void assert_array_near< complex_float > ( const complex_float reference,
const complex_float actual,
const int  size,
const double  precision 
)

Definition at line 475 of file common.hpp.

◆ assert_avg_greater_complex()

void assert_avg_greater_complex ( const T *  reference,
const T *  actual,
const int  size,
const double  precision 
)

Assert average diff of two arrays. It calls ASSERT_GT to check the average.

Parameters
[in]referenceArray with reference values, interleaved IQ inputs.
[in]actualArray with the actual output, interleaved IQ inputs.
[in]sizeSize of the array, based on complex inputs.
[in]precisionPrecision for the comparison used by ASSERT_GT.

Definition at line 494 of file common.hpp.

◆ bind_to_cpu()

int bind_to_cpu ( const unsigned  cpu)

Attach current process to the selected core.

Parameters
[in]cpuCore number.
Returns
0 on success, -1 otherwise.

Definition at line 47 of file common.cpp.

Here is the caller graph for this function:

◆ calculate_statistics()

std::pair<double, double> calculate_statistics ( const std::vector< long >  values)

Calculate the mean and variance from the result of the run_benchmark.

Parameters
[in]valuesVector with result values.
Returns
std::pair where the first element is mean and the second one is standard deviation.
Note
It's not a general mean/stddev function it only works properly when feed with data from the benchmark function.

Definition at line 60 of file common.cpp.

Here is the caller graph for this function:

◆ generate_random_data()

T* generate_random_data ( const long  size,
const unsigned  alignment 
)

generate random data.

It allocates memory and populate it with random data using C++11 default engine and uniform integer distribution (bytes not floats are uniformly distributed). Don't forget to free allocated memory!

Parameters
[in]sizeSize of the memory to be filled with random data.
[in]alignmentBytes alignment of the memory.
Returns
Pointer to the allocated memory with random data.

Definition at line 618 of file common.hpp.

◆ generate_random_int_numbers()

T* generate_random_int_numbers ( const long  size,
const unsigned  alignment,
const T  lo_range,
const T  up_range 
)

generate integer random numbers.

It allocates memory and populate it with random numbers using C++11 default engine and uniform integer distribution (where lo_range <= x < up_range). Don't forget to free allocated memory! The result type generated by the generator should be one of int types.

Parameters
[in]sizeSize of the memory to be filled with random data.
[in]alignmentBytes alignment of the memory.
[in]lo_rangeLower bound of range of values returned by random generator.
[in]up_rangeUpper bound of range of values returned by random generator.
Returns
Pointer to the allocated memory with random data.

Definition at line 640 of file common.hpp.

◆ generate_random_numbers()

T* generate_random_numbers ( const long  size,
const unsigned  alignment,
U &  distribution 
)

generate random numbers.

It allocates memory and populate it with random numbers using C++11 default engine and uniform real / int distribution (where lo_range <= x <up_range). Don't forget to free allocated memory!

Parameters
[in]sizeSize of the memory to be filled with random data.
[in]alignmentBytes alignment of the memory.
[in]distributionDistribuiton for random generator.
Returns
Pointer to the allocated memory with random data.

Definition at line 593 of file common.hpp.

◆ generate_random_real_numbers()

T* generate_random_real_numbers ( const long  size,
const unsigned  alignment,
const T  lo_range,
const T  up_range 
)

generate real random numbers.

It allocates memory and populate it with random numbers using C++11 default engine and uniform real distribution (where lo_range <= x <up_range). Don't forget to free allocated memory! The result type generated by the generator should be one of real types: float, double or long double.

Parameters
[in]sizeSize of the memory to be filled with random data.
[in]alignmentBytes alignment of the memory.
[in]lo_rangeLower bound of range of values returned by random generator.
[in]up_rangeUpper bound of range of values returned by random generator.
Returns
Pointer to the allocated memory with random data.

Definition at line 663 of file common.hpp.

◆ get_sequence()

std::vector<unsigned> get_sequence ( const unsigned  number)

For a given number return sequence of number from 0 to number - 1.

Parameters
[in]numberPositive integer value.
Returns
Vector with the sorted integer numbers between 0 and number - 1.

Definition at line 78 of file common.cpp.

Here is the caller graph for this function:

◆ read_data_to_aligned_array()

char* read_data_to_aligned_array ( const std::string &  filename)

Read binary data from the file.

Parameters
[in]filenamename of the binary file.
Returns
Pointer to the allocated memory with data from the file.
Exceptions
std::runtime_errorwhen memory cannot be allocated.

Definition at line 86 of file common.cpp.

Here is the caller graph for this function:

◆ read_json_from_file()

json read_json_from_file ( const std::string &  filename)

Read JSON from the given file.

Parameters
[in]filenamename of the .json file.
Returns
JSON object with data.
Exceptions
missing_config_file_exceptionwhen file cannot be opened.

Definition at line 106 of file common.cpp.

Here is the caller graph for this function:

◆ run_benchmark()

std::pair<double, double> run_benchmark ( function,
Args ...  args 
)

Run the given function and return the mean run time and stddev.

Parameters
[in]functionFunction to benchmark.
[in]argsFunction's arguments.
Returns
std::pair where the first element is mean and the second one is standard deviation.

Definition at line 425 of file common.hpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tsc_recovery()

unsigned long tsc_recovery ( )

Measure the TSC on the machine.

Returns
Number of ticks per us

Definition at line 119 of file common.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ tsc_tick()

unsigned long tsc_tick ( )

Return the current value of the TSC.

Returns
Current TSC value

Definition at line 162 of file common.cpp.

Here is the caller graph for this function: