/**************************************************************************************************** * generic.c - source file for all generic functions * * * * Currently supporting the following functionalities: * * - Provide common function for input creation * * - Provide functionalitites to generate both real and complex vectors * * - Provide functionalities to generate both real and complex matrices * * * * ***************************************************************************************************** * modification history: * * 31.03.2021, created by ThinkPalm Technologies Pvt Ltd * ****************************************************************************************************/ #include #include "generic.h" #include "utils.h" char* MKLExecMessage[MKL_OI_MAXE] = { "real vector vector addition", "real vector vector multiplication", "real vector maximum index operation", "real vector maximum every operation", "real vector sort operation", "real vector convolution operation", "real vector 1D fft operation", "complex vector vector addition", "complex vector vector multiplication", "complex vector absolute value squared operation", "complex vector 1D fft operation", "complex vector convolution operation", "real matrix vector multiplication", "real matrix matrix multiplication", "real inverse matrix vector multiplication", "real inverse matrix matrix multiplication", "real sparse inverse matrix vector multiplication", "real sparse inverse matrix matrix multiplication", "real matrix exponent multiplication operation", "real sparse matrix exponent multiplication operation", "complex matrix vector multiplication", "complex matrix matrix multiplication", "complex inverse matrix vector multiplication", "complex inverse matrix matrix multiplication", "complex sparse inverse matrix vector multiplication", "complex sparse inverse matrix matrix multiplication" }; /******************************************************************************* * - name: generateRealVector * * - title: generate a vector with real values * * - input: weight - the size required for the vector * * - output: generates a new vector with real values * * - returns: the pointer of the newly generated vector * * - description: this function generates a new vector with real values * *******************************************************************************/ float* generateRealVector(int weight) { size_t size; float* pRealVtr; int i, random; size = weight * sizeof(float); pRealVtr = (float*)malloc(size); if (pRealVtr == NULL) { printf("NULL"); return NULL; } for (i = 0; i < weight; i++) { random = rand(); pRealVtr[i] = (float) rand(); } return pRealVtr; } /******************************************************************************* * - name: generateComplexVector * * - title: generate a vector with complex values * * - input: weight - the size required for the vector * * - output: generates a new vector with complex values * * - returns: the pointer of newly generated vector * * - description: this function generates a new vector with complex values * *******************************************************************************/ MKL_Complex8* generateComplexVector(int weight) { MKL_Complex8* pComplexVtrptr; int i; pComplexVtrptr = (MKL_Complex8*)malloc(weight * sizeof(MKL_Complex8)); if (pComplexVtrptr == NULL) { return NULL; } for (i = 0; i < weight; i++) { MKL_Complex8 complexNo = { 0 }; complexNo.real = (float)rand(); complexNo.imag = (float)rand(); pComplexVtrptr[i] = complexNo; } return pComplexVtrptr; } /******************************************************************************* * - name: generateRealMatrix * * - title: generate a matrix with real values * * - input: weight - the size required for the matrix * * - output: generates a new matrix with real values * * - returns: the pointer of the newly generated matrix * * - description: this function generates a new matrix with real values * *******************************************************************************/ float* generateRealMatrix(int weight) { float* pRealMtx; int i, j; pRealMtx = (float*)malloc(weight * weight * sizeof(float)); if (pRealMtx == NULL) { return NULL; } for (i = 0; i < weight; i++) { for (j = 0; j < weight; j++) { *(pRealMtx + i * weight + j) = (float) rand(); } } return pRealMtx; } /******************************************************************************* * - name: generateComplexMatrix * * - title: generate a matrix with complex values * * - input: weight - the size required for the matrix * * - output: generates a new matrix with complex values * * - returns: the pointer of newly generated matrix * * - description: this function generates a new matrix with complex values * *******************************************************************************/ MKL_Complex8* generateComplexMatrix(int weight) { MKL_Complex8* pComplexMtx; int i, j; pComplexMtx = (MKL_Complex8*)malloc(weight * weight * sizeof(MKL_Complex8)); if (pComplexMtx == NULL) { return NULL; } for (i = 0; i < weight; i++) { for (j = 0; j < weight; j++) { MKL_Complex8 complexNo = { 0 }; complexNo.real = (float) rand(); complexNo.imag = (float) rand(); *(pComplexMtx + i * weight + j) = complexNo; } } return pComplexMtx; } /******************************************************************************* * - name: generateRealSparseMatrix * * - title: generate a sparse matrix with real values * * - input: weight - the size required for the sparse matrix * * - output: generates a new sparse matrix with real values * * - returns: the pointer of the newly generated sparse matrix * * - description: this function generates a new sparse matrix with real values * *******************************************************************************/ float* generateRealSparseMatrix(int weight) { float* pRealSparseMtx; int i, j; pRealSparseMtx = (float*)malloc(weight * weight * sizeof(float)); if (pRealSparseMtx == NULL) { return NULL; } for (i = 0; i < weight; i++) { for (j = 0; j < weight; j++) { if (i == j) *(pRealSparseMtx + i * weight + j) =(float) (i + j + 1); else *(pRealSparseMtx + i * weight + j) = 0; } } return pRealSparseMtx; } /******************************************************************************* * - name: generateComplexSparseMatrix * * - title: generate a sparse matrix with complex values * * - input: weight - the size required for the sparse matrix * * - output: generates a new sparse matrix with complex values * * - returns: the pointer of newly generated sparse matrix * * - description: this function generates a new sparse matrix with complex values * *******************************************************************************/ MKL_Complex8* generateComplexSparseMatrix(int weight) { MKL_Complex8* pCompleSparsexMtx; int i, j; pCompleSparsexMtx = (MKL_Complex8*)malloc(weight * weight * sizeof(MKL_Complex8)); if (pCompleSparsexMtx == NULL) { return NULL; } for (i = 0; i < weight; i++) { for (j = 0; j < weight; j++) { MKL_Complex8 complexNo = { 0 }; if (i == j) { complexNo.real = (float) (i + j + 1); complexNo.imag = (float) (i + j + 1); *(pCompleSparsexMtx + i * weight + j) = complexNo; } else complexNo.real = 0; complexNo.imag = 0; *(pCompleSparsexMtx + i * weight + j) = complexNo; } } return pCompleSparsexMtx; } /******************************************************************************* * - name: logExecTime * * - title: generic function to log execution time for all operations * * - input: operation - operation specific array item * range - total number of elements to iterate * indexList - array to a pointer containing the index list * message - operation specific message * * - output: logs the execution time for operations * * * - description: this function creates generic function to log execution time * for all operations * *******************************************************************************/ void logExecTime(uint32_t* operation, int range, int* indexList, MKL_OP_INDEX index) { int i = 0; printf("\nAverage execution time for %s\n", MKLExecMessage[index]); for (i; i < range; i++) { double average = (double)operation[i] / (double)ITER_COUNT; printf("index %d execution time .2%f microseconds\n", indexList[i], average); } printf("\n\n"); }