From d17b33131c14864bd1eae275f49a3f148e21cf29 Mon Sep 17 00:00:00 2001 From: Leo Chan Date: Thu, 22 Oct 2020 01:53:21 -0400 Subject: Squashed commit of the sb-vbs branch. Includes the SD-VBS benchmarks modified to: - Use libextra to loop as realtime jobs - Preallocate memory before starting their main computation - Accept input via stdin instead of via argc Does not include the SD-VBS matlab code. Fixes libextra execution in LITMUS^RT. --- SD-VBS/common/c/calcSobel_dX.c | 77 ++++++ SD-VBS/common/c/calcSobel_dY.c | 79 ++++++ SD-VBS/common/c/extra.h | 479 +++++++++++++++++++++++++++++++++++ SD-VBS/common/c/fCopy.c | 24 ++ SD-VBS/common/c/fDeepCopy.c | 24 ++ SD-VBS/common/c/fDeepCopyRange.c | 24 ++ SD-VBS/common/c/fDivide.c | 23 ++ SD-VBS/common/c/fFind3.c | 46 ++++ SD-VBS/common/c/fFreeHandle.c | 16 ++ SD-VBS/common/c/fHorzcat.c | 40 +++ SD-VBS/common/c/fMallocHandle.c | 18 ++ SD-VBS/common/c/fMdivide.c | 27 ++ SD-VBS/common/c/fMinus.c | 21 ++ SD-VBS/common/c/fMtimes.c | 38 +++ SD-VBS/common/c/fPlus.c | 21 ++ SD-VBS/common/c/fResetArray.c | 19 ++ SD-VBS/common/c/fResetHandle.c | 19 ++ SD-VBS/common/c/fReshape.c | 27 ++ SD-VBS/common/c/fResortIndices.c | 77 ++++++ SD-VBS/common/c/fSelfCheck.c | 59 +++++ SD-VBS/common/c/fSetArray.c | 22 ++ SD-VBS/common/c/fSort.c | 43 ++++ SD-VBS/common/c/fSortIndices.c | 77 ++++++ SD-VBS/common/c/fSum.c | 55 ++++ SD-VBS/common/c/fSum2.c | 56 ++++ SD-VBS/common/c/fTimes.c | 21 ++ SD-VBS/common/c/fTranspose.c | 27 ++ SD-VBS/common/c/fWriteMatrix.c | 34 +++ SD-VBS/common/c/ffConv2.c | 47 ++++ SD-VBS/common/c/ffConv2_dY.c | 52 ++++ SD-VBS/common/c/ffDivide.c | 21 ++ SD-VBS/common/c/ffTimes.c | 21 ++ SD-VBS/common/c/ffVertcat.c | 36 +++ SD-VBS/common/c/ffiConv2.c | 54 ++++ SD-VBS/common/c/fiConv2.c | 54 ++++ SD-VBS/common/c/fiCopy.c | 23 ++ SD-VBS/common/c/fiDeepCopy.c | 23 ++ SD-VBS/common/c/horzcat.c | 48 ++++ SD-VBS/common/c/iCheck.c | 18 ++ SD-VBS/common/c/iDeepCopy.c | 23 ++ SD-VBS/common/c/iDeepCopyRange.c | 23 ++ SD-VBS/common/c/iFreeHandle.c | 16 ++ SD-VBS/common/c/iHorzcat.c | 41 +++ SD-VBS/common/c/iMallocHandle.c | 20 ++ SD-VBS/common/c/iMinus.c | 21 ++ SD-VBS/common/c/iResetArray.c | 22 ++ SD-VBS/common/c/iReshape.c | 25 ++ SD-VBS/common/c/iSetArray.c | 22 ++ SD-VBS/common/c/iSort.c | 43 ++++ SD-VBS/common/c/iSortIndices.c | 47 ++++ SD-VBS/common/c/iTimes.c | 22 ++ SD-VBS/common/c/iTranspose.c | 26 ++ SD-VBS/common/c/iVertcat.c | 34 +++ SD-VBS/common/c/ifDeepCopy.c | 25 ++ SD-VBS/common/c/ifMtimes.c | 38 +++ SD-VBS/common/c/iiConv2.c | 55 ++++ SD-VBS/common/c/imageBlur.c | 67 +++++ SD-VBS/common/c/imageReblur.c | 67 +++++ SD-VBS/common/c/imageResize.c | 78 ++++++ SD-VBS/common/c/isMinus.c | 23 ++ SD-VBS/common/c/isPlus.c | 22 ++ SD-VBS/common/c/photonEndTiming.c | 22 ++ SD-VBS/common/c/photonPrintTiming.c | 22 ++ SD-VBS/common/c/photonReportTiming.c | 28 ++ SD-VBS/common/c/photonStartTiming.c | 23 ++ SD-VBS/common/c/randWrapper.c | 30 +++ SD-VBS/common/c/randnWrapper.c | 40 +++ SD-VBS/common/c/readFile.c | 42 +++ SD-VBS/common/c/readImage.c | 112 ++++++++ SD-VBS/common/c/sdvbs_common.h | 139 ++++++++++ SD-VBS/common/c/selfCheck.c | 65 +++++ SD-VBS/common/c/timingUtils.h | 99 ++++++++ SD-VBS/common/c/uiFreeHandle.c | 15 ++ SD-VBS/common/c/uiMallocHandle.c | 20 ++ SD-VBS/common/c/uiResetArray.c | 19 ++ SD-VBS/common/c/uiSetArray.c | 21 ++ SD-VBS/common/c/writeMatrix.c | 34 +++ 77 files changed, 3381 insertions(+) create mode 100644 SD-VBS/common/c/calcSobel_dX.c create mode 100644 SD-VBS/common/c/calcSobel_dY.c create mode 100644 SD-VBS/common/c/extra.h create mode 100644 SD-VBS/common/c/fCopy.c create mode 100644 SD-VBS/common/c/fDeepCopy.c create mode 100644 SD-VBS/common/c/fDeepCopyRange.c create mode 100644 SD-VBS/common/c/fDivide.c create mode 100644 SD-VBS/common/c/fFind3.c create mode 100644 SD-VBS/common/c/fFreeHandle.c create mode 100644 SD-VBS/common/c/fHorzcat.c create mode 100644 SD-VBS/common/c/fMallocHandle.c create mode 100644 SD-VBS/common/c/fMdivide.c create mode 100644 SD-VBS/common/c/fMinus.c create mode 100644 SD-VBS/common/c/fMtimes.c create mode 100644 SD-VBS/common/c/fPlus.c create mode 100644 SD-VBS/common/c/fResetArray.c create mode 100644 SD-VBS/common/c/fResetHandle.c create mode 100644 SD-VBS/common/c/fReshape.c create mode 100644 SD-VBS/common/c/fResortIndices.c create mode 100644 SD-VBS/common/c/fSelfCheck.c create mode 100644 SD-VBS/common/c/fSetArray.c create mode 100644 SD-VBS/common/c/fSort.c create mode 100644 SD-VBS/common/c/fSortIndices.c create mode 100644 SD-VBS/common/c/fSum.c create mode 100644 SD-VBS/common/c/fSum2.c create mode 100644 SD-VBS/common/c/fTimes.c create mode 100644 SD-VBS/common/c/fTranspose.c create mode 100644 SD-VBS/common/c/fWriteMatrix.c create mode 100644 SD-VBS/common/c/ffConv2.c create mode 100644 SD-VBS/common/c/ffConv2_dY.c create mode 100644 SD-VBS/common/c/ffDivide.c create mode 100644 SD-VBS/common/c/ffTimes.c create mode 100644 SD-VBS/common/c/ffVertcat.c create mode 100644 SD-VBS/common/c/ffiConv2.c create mode 100644 SD-VBS/common/c/fiConv2.c create mode 100644 SD-VBS/common/c/fiCopy.c create mode 100644 SD-VBS/common/c/fiDeepCopy.c create mode 100644 SD-VBS/common/c/horzcat.c create mode 100644 SD-VBS/common/c/iCheck.c create mode 100644 SD-VBS/common/c/iDeepCopy.c create mode 100644 SD-VBS/common/c/iDeepCopyRange.c create mode 100644 SD-VBS/common/c/iFreeHandle.c create mode 100644 SD-VBS/common/c/iHorzcat.c create mode 100644 SD-VBS/common/c/iMallocHandle.c create mode 100644 SD-VBS/common/c/iMinus.c create mode 100644 SD-VBS/common/c/iResetArray.c create mode 100644 SD-VBS/common/c/iReshape.c create mode 100644 SD-VBS/common/c/iSetArray.c create mode 100644 SD-VBS/common/c/iSort.c create mode 100644 SD-VBS/common/c/iSortIndices.c create mode 100644 SD-VBS/common/c/iTimes.c create mode 100644 SD-VBS/common/c/iTranspose.c create mode 100644 SD-VBS/common/c/iVertcat.c create mode 100644 SD-VBS/common/c/ifDeepCopy.c create mode 100644 SD-VBS/common/c/ifMtimes.c create mode 100644 SD-VBS/common/c/iiConv2.c create mode 100644 SD-VBS/common/c/imageBlur.c create mode 100644 SD-VBS/common/c/imageReblur.c create mode 100644 SD-VBS/common/c/imageResize.c create mode 100644 SD-VBS/common/c/isMinus.c create mode 100644 SD-VBS/common/c/isPlus.c create mode 100644 SD-VBS/common/c/photonEndTiming.c create mode 100644 SD-VBS/common/c/photonPrintTiming.c create mode 100644 SD-VBS/common/c/photonReportTiming.c create mode 100644 SD-VBS/common/c/photonStartTiming.c create mode 100644 SD-VBS/common/c/randWrapper.c create mode 100644 SD-VBS/common/c/randnWrapper.c create mode 100644 SD-VBS/common/c/readFile.c create mode 100644 SD-VBS/common/c/readImage.c create mode 100644 SD-VBS/common/c/sdvbs_common.h create mode 100644 SD-VBS/common/c/selfCheck.c create mode 100644 SD-VBS/common/c/timingUtils.h create mode 100644 SD-VBS/common/c/uiFreeHandle.c create mode 100644 SD-VBS/common/c/uiMallocHandle.c create mode 100644 SD-VBS/common/c/uiResetArray.c create mode 100644 SD-VBS/common/c/uiSetArray.c create mode 100644 SD-VBS/common/c/writeMatrix.c (limited to 'SD-VBS/common/c') diff --git a/SD-VBS/common/c/calcSobel_dX.c b/SD-VBS/common/c/calcSobel_dX.c new file mode 100644 index 0000000..4be5845 --- /dev/null +++ b/SD-VBS/common/c/calcSobel_dX.c @@ -0,0 +1,77 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include +#include +#include "sdvbs_common.h" + +F2D* calcSobel_dX(F2D* imageIn) +{ + int rows, cols; + F2D *kernel_1, *kernel_2; + float temp; + int kernelSize, startCol, endCol, halfKernel, startRow, endRow, i, j, kernelSum; + int k, kernelSum_1, kernelSum_2; + F2D *imageOut, *tempOut; + + rows = imageIn->height; + cols = imageIn->width; + + imageOut = fSetArray(rows, cols, 0); + tempOut = fSetArray(rows, cols, 0); + kernel_1 = fMallocHandle(1, 3); + kernel_2 = fMallocHandle(1, 3); + + asubsref(kernel_1,0) = 1; + asubsref(kernel_1,1) = 2; + asubsref(kernel_1,2) = 1; + + kernelSize = 3; + kernelSum_1 = 4; + + asubsref(kernel_2,0) = 1; + asubsref(kernel_2,1) = 0; + asubsref(kernel_2,2) = -1; + + kernelSum_2 = 2; + + startCol = 1; + endCol = cols - 1; + halfKernel = 1; + + startRow = 1; + endRow = rows - 1; + + for(i=startRow; iheight; + cols = imageIn->width; + + // level 1 is the base image. + + outputRows = rows; + outputCols = cols; + + imageOut = fSetArray(outputRows, outputCols, 0); + tempOut = fSetArray(outputRows, outputCols, 0); + kernel_1 = iMallocHandle(1, 3); + kernel_2 = iMallocHandle(1, 3); + + asubsref(kernel_1,0) = 1; + asubsref(kernel_1,1) = 0; + asubsref(kernel_1,2) = -1; + kernelSize = 3; + kernelSum_1 = 2.0; + + asubsref(kernel_2,0) = 1; + asubsref(kernel_2,1) = 2; + asubsref(kernel_2,2) = 1; + kernelSum_2 = 4; + + startCol = 1; + endCol = cols - 1; + halfKernel = 1; + + startRow = 1; + endRow = rows - 1; + + for(i=startRow; i // For O_CREAT and O_RDWR +#include // For sched_yield() +#include // For sem_{open, post, wait}() +#include +#include // For exit() +#include // For strlen() +#include // For mlockall() +#include // For ftruncate() +#include + +// This is only visible if _GNU_SOURCE is defined, and that define does not +// come along to places where this file is included. Address this by manually +// forcing it into the global namespace. +extern int sched_getcpu(); + +// These constants correspond to the imx6q-sabredb platform +#define LINE_SIZE 32 +#define L2_SIZE 16*2048*32 + +#if __arm__ +#include +#include +#endif + +#define LITMUS 1 +#define MC2 0 +#define MMDC_PROF 0 + +#if LITMUS +#include +#endif + +#if MMDC_PROF +#include "/media/speedy/litmus/tools/mmdc/mmdc.h" +#endif + +#if LITMUS +#define SET_UP LOAD_PARAMS SETUP_LITMUS +#else +#define SET_UP LOAD_PARAMS +#endif + +#if MMDC_PROF +#define LOAD_PARAMS LOAD_PARAMS_ITRL SETUP_MMDC +#else +#define LOAD_PARAMS LOAD_PARAMS_ITRL +#endif + +// Store state globally so that the job can be outside main() +// Arrays use float as a comprimise between overflow and size +// Paired arrays use long longs as precision is more important for those times +#ifdef PAIRED +long long *_rt_start_time; +long long *_rt_end_time; +#else +float *_rt_exec_time; +#endif +#if MMDC_PERF +float *_rt_mmdc_read; +float *_rt_mmdc_write; +#endif +long _rt_jobs_complete; +long _rt_max_jobs; +int _rt_core; +int _rt_will_output; +struct timespec _rt_start, _rt_end; + +char *_rt_run_id; +char *_rt_our_prog_name; +char *_rt_other_prog_name; +char *_rt_other_core; +#define _RT_FILENAME_LEN 64 +#define _BILLION (1000*1000*1000) +#ifdef PAIRED +char *_rt_barrier; +sem_t *_rt_first_sem, *_rt_second_sem; +int _rt_lock_id; +#endif + +static void _rt_load_params_itrl(int argc, char **argv) { +#ifdef PAIRED + if (argc != 8) { + fprintf(stderr, "Usage: %s ", argv[0]); + fprintf(stderr, " string for logging. Name of this task.\n"); + fprintf(stderr, " integer number of iterations. -1 for infinite.\n"); + fprintf(stderr, " UNUSED. Core is now auto-detected.\n"); + fprintf(stderr, " integer for logging. Core of paired task.\n"); + fprintf(stderr, " string for logging. Name of paired task.\n"); + fprintf(stderr, " string to append with .txt to yield output file name.\n"); + fprintf(stderr, " 1 to indicate this is pair member 1, otherwise pair member 2.\n"); + exit(1); + } +#else + if (argc != 6) { + fprintf(stderr, "Usage: %s \n", argv[0]); + fprintf(stderr, " string for logging. Name of this task.\n"); + fprintf(stderr, " integer number of iterations. -1 for infinite.\n"); + fprintf(stderr, " UNUSED. Core is now auto-detected.\n"); + fprintf(stderr, " string to append with .txt to yield output file name.\n"); + fprintf(stderr, " 1 to save results, 0 to discard.\n"); + exit(1); + } +#endif + _rt_our_prog_name = argv[1]; + _rt_max_jobs = atol(argv[2]); + _rt_core = sched_getcpu(); +#ifdef PAIRED + _rt_other_core = argv[4]; + _rt_other_prog_name = argv[5]; + _rt_run_id = argv[6]; + _rt_lock_id = atoi(argv[7]); + // The paired version doesn't support disabling output (legacy compatibility) + _rt_will_output = 1; +#else + _rt_other_core = "none"; + _rt_other_prog_name = "none"; + _rt_run_id = argv[4]; + _rt_will_output = atoi(argv[5]); +#endif /* PAIRED */ + if (_rt_max_jobs < 0 && _rt_will_output != 0) { + fprintf(stderr, "Infinite loops only supported when _rt_will_output is disabled!\n"); + exit(1); + } + if (strlen(_rt_run_id) + 5 > _RT_FILENAME_LEN) { + fprintf(stderr, "Run ID is too large! Keep it to less than %d characters.\n", _RT_FILENAME_LEN); + exit(1); + } +#ifdef PAIRED + _rt_start_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(long long)); + _rt_end_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(long long)); + if (!_rt_end_time || !_rt_start_time) { + perror("Unable to allocate buffers for execution times"); + exit(1); + } + _rt_first_sem = sem_open("/_libextra_first_sem", O_CREAT, 644, 0); + _rt_second_sem = sem_open("/_libextra_second_sem", O_CREAT, 644, 0); + if (_rt_first_sem == SEM_FAILED || _rt_second_sem == SEM_FAILED) { + perror("Error while creating semaphores"); + exit(1); + } + int barrier_file = shm_open("/_libextra_barrier", O_CREAT | O_RDWR, 644); + if (barrier_file == -1) { + perror("Error while creating shared memory for barrier synchronization"); + exit(1); + } + if (ftruncate(barrier_file, 1) == -1) { + perror("Error while setting size of shared memory for barrier synchronization"); + exit(1); + } + _rt_barrier = mmap(NULL, 1, PROT_WRITE, MAP_SHARED, barrier_file, 0); + if (_rt_barrier == MAP_FAILED) { + perror("Error while mapping shared memory for barrier synchronization"); + exit(1); + } + *_rt_barrier = 0; +#else + _rt_exec_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(float)); + if (!_rt_exec_time) { + perror("Unable to allocate buffer for execution times"); + exit(1); + } +#endif /* PAIRED */ + _rt_jobs_complete = 0; + mlockall(MCL_CURRENT || MCL_FUTURE); +} +#define LOAD_PARAMS_ITRL _rt_load_params_itrl(argc, argv); + +#define SETUP_MMDC \ + _rt_mmdc_read = calloc(_rt_max_jobs * _rt_will_output, sizeof(float));\ + _rt_mmdc_write = calloc(_rt_max_jobs * _rt_will_output, sizeof(float));\ + if (!_rt_mmdc_read || !_rt_mmdc_write) {\ + perror("Unable to allocate buffer for MMDC data");\ + exit(1);\ + }\ + MMDC_PROFILE_RES_t mmdc_res;\ + memset(&mmdc_res, 0, sizeof(MMDC_PROFILE_RES_t));\ + int fd = open("/dev/mem", O_RDWR, 0);\ + if (fd < 0) {\ + perror("Unable to open /dev/mem");\ + exit(1);\ + }\ + pMMDC_t mmdc = mmap(NULL, 0x4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, MMDC_P0_IPS_BASE_ADDR);\ + if (mmdc == MAP_FAILED) {\ + perror("Unable to map MMDC address space");\ + exit(1);\ + }\ + mmdc->madpcr1 = axi_arm1;\ + msync(&(mmdc->madpcr1),4,MS_SYNC); + +#define SETUP_LITMUS \ + unsigned int wait = 0; \ + if (be_migrate_to_domain(_rt_core) < 0) { \ + perror("Unable to migrate to specified CPU"); \ + exit(1); \ + } \ + struct rt_task rt_param; \ + init_rt_task_param(&rt_param); \ + /* Supposedly the next two parameters are irrelevant when reservations are enabled, but I'm leaving them anyway... */ \ + rt_param.exec_cost = ms2ns(999); \ + rt_param.period = ms2ns(1000); \ + rt_param.priority = LITMUS_HIGHEST_PRIORITY; \ + rt_param.cls = RT_CLASS_HARD; \ + rt_param.release_policy = TASK_PERIODIC; \ + rt_param.budget_policy = NO_ENFORCEMENT; \ + rt_param.cpu = _rt_core; \ + if (set_rt_task_param(gettid(), &rt_param) < 0) { \ + perror("Unable to set real-time parameters"); \ + exit(1); \ + } \ + if (init_litmus() != 0) { \ + perror("init_litmus failed"); \ + exit(1); \ + } \ + MC2_SETUP \ + if (task_mode(LITMUS_RT_TASK) != 0) { \ + perror("Unable to become real-time task"); \ + exit(1); \ + } \ + if (wait && wait_for_ts_release() != 0) { \ + perror("Unable to wait for taskset release"); \ + exit(1); \ + } + +#if MC2 +#define MC2_SETUP \ + + set_page_color(rt_param.cpu); +#else +#define MC2_SETUP +#endif + +#define CLEANUP_LITMUS \ + if (task_mode(BACKGROUND_TASK) != 0) { \ + perror("Unable to become a real-time task"); \ + exit(1); \ + } \ + +#if __arm__ +// On ARM, manually flush the cache +#define FLUSH_CACHES \ + volatile uint8_t buffer[L2_SIZE * 4]; \ + for (uint32_t j = 0; j < 4; j++) \ + for (uint32_t i = 0; i < L2_SIZE * 4; i += LINE_SIZE) \ + buffer[i]++; +#else +// On x86 call the wbinvld instruction (it's in a kernel module due to it being ring-0) +#define FLUSH_CACHES \ + FILE *fp = fopen("/proc/wbinvd", "r");\ + if (fp == NULL) {\ + perror("Cache flush module interface cannot be opened");\ + exit(1);\ + }\ + char dummy;\ + if (fread(&dummy, 1, 1, fp) == 0) {\ + perror("Unable to access cache flush module interface");\ + exit(1);\ + }\ + fclose(fp); +#endif + +// This semaphore-based synchronization is from Sims +#define FIRST_UNLOCK \ + if (_rt_lock_id == 1) {\ + if (sem_post(_rt_second_sem) != 0) {\ + perror("Unable to unlock second semaphore");\ + exit(1);\ + }\ + } \ + else {\ + if (sem_post(_rt_first_sem) != 0) {\ + perror("Unable to unlock first semaphore");\ + exit(1);\ + }\ + } \ + +#define FIRST_LOCK \ + if (_rt_lock_id == 1) {\ + if (sem_wait(_rt_first_sem) != 0) {\ + perror("Unable to wait on first semaphore");\ + exit(1);\ + }\ + }\ + else {\ + if (sem_wait(_rt_second_sem) != 0) {\ + perror("Unable to wait on second semaphore");\ + exit(1);\ + }\ + } + +// This ensures a very low difference between pair member start times +#define BARRIER_SYNC \ + if (__sync_bool_compare_and_swap(_rt_barrier, 0, 1)) {\ + while (!__sync_bool_compare_and_swap(_rt_barrier, 0, 0)) {};\ + }\ + else {\ + __sync_bool_compare_and_swap(_rt_barrier, 1, 0);\ + } + +// Buffer timing result from a single job +static void _rt_save_job_result() { + if (_rt_jobs_complete >= _rt_max_jobs) { + fprintf(stderr, "Max jobs setting too small! Trying to record job #%ld when we only have space for %ld jobs. Exiting...\n", _rt_jobs_complete, _rt_max_jobs); + exit(1); + } + if (_rt_jobs_complete > -1 && _rt_will_output) { +#ifdef PAIRED + _rt_start_time[_rt_jobs_complete] = _rt_start.tv_sec; + _rt_start_time[_rt_jobs_complete] *= _BILLION; + _rt_start_time[_rt_jobs_complete] += _rt_start.tv_nsec; + _rt_end_time[_rt_jobs_complete] = _rt_end.tv_sec; + _rt_end_time[_rt_jobs_complete] *= _BILLION; + _rt_end_time[_rt_jobs_complete] += _rt_end.tv_nsec; +#else + _rt_exec_time[_rt_jobs_complete] = _rt_end.tv_sec - _rt_start.tv_sec; + _rt_exec_time[_rt_jobs_complete] *= _BILLION; + _rt_exec_time[_rt_jobs_complete] += _rt_end.tv_nsec - _rt_start.tv_nsec; +#endif /* PAIRED */ +#if MMDC_PROF + _rt_mmdc_read[_rt_jobs_complete] = mmdc_res.read_bytes; + _rt_mmdc_write[_rt_jobs_complete] = mmdc_res.write_bytes; +#endif + } +} + +// Save all buffered timing results to disk +static void _rt_write_to_file() { + char fileName[_RT_FILENAME_LEN]; + FILE *fp; + munlockall(); + if (!_rt_will_output) + goto out; + strcpy(fileName, _rt_run_id); + strcat(fileName, ".txt"); + fp = fopen(fileName, "a"); + if (fp == NULL) { + perror("Unable to open output file"); + exit(1); + } + // Baseline output uses a similar format with "none" for unused fields + for (int i = 0; i < _rt_jobs_complete; i++){ + fprintf(fp, "%s %s %u %s %ld", _rt_our_prog_name, _rt_other_prog_name, + _rt_core, _rt_other_core, _rt_max_jobs); +#ifdef PAIRED + // For unclear legacy reasons, paired tasks emit sec and ns separately + fprintf(fp, " %lld %lld %lld %lld", + _rt_start_time[i] / _BILLION, _rt_start_time[i] % _BILLION, + _rt_end_time[i] / _BILLION, _rt_end_time[i] % _BILLION); +#else + fprintf(fp, " %.f", _rt_exec_time[i]); +#endif /* PAIRED */ + fprintf(fp, " %s %d %.f %.f\n", _rt_run_id, i, +#if MMDC_PROF + _rt_mmdc_read[i], _rt_mmdc_write[i]); +#else + 0.0, 0.0); +#endif /* MMDC_PROF */ + } + fclose(fp); +out: +#if LITMUS + CLEANUP_LITMUS +#endif /* LITMUS */ +#ifdef PAIRED + munmap(_rt_barrier, 1); + shm_unlink("/_libextra_barrier"); + sem_unlink("/_libextra_first_sem"); + sem_unlink("/_libextra_second_sem"); + free(_rt_start_time); + free(_rt_end_time); +#else + free(_rt_exec_time); +#endif /* PAIRED */ +#if MMDC_PROF + free(_rt_mmdc_read); + free(_rt_mmdc_write); +#endif /* MMDC_PROF */ +} + +// Start a job +static void _rt_start_loop() { +#if LITMUS + if (sleep_next_period() != 0) { + perror("Unable to sleep for next period"); + } +#else + sched_yield(); +#endif /* LITMUS */ +#ifdef PAIRED + FIRST_UNLOCK + FIRST_LOCK +#endif /* PAIRED */ + FLUSH_CACHES +#ifdef PAIRED + BARRIER_SYNC +#endif /* PAIRED */ +#if MMDC_PROF + /* This disables profiling, resets the counters, clears the overflow bit, and enables profiling */ + start_mmdc_profiling(mmdc); +#endif /* MMDC_PROF */ + clock_gettime(CLOCK_MONOTONIC, &_rt_start); +} + +// Complete a job +static void _rt_stop_loop() { + clock_gettime(CLOCK_MONOTONIC, &_rt_end); +#if MMDC_PROF + /* This freezes the profiling and makes results available */ + pause_mmdc_profiling(mmdc); + get_mmdc_profiling_results(mmdc, &mmdc_res); +#endif /* MMDC_PROF */ + _rt_save_job_result(); + _rt_jobs_complete++; +} + +/****** New API ****** + * Intended structure: + * + * |int main(int argc, char **argv) { + * | SET_UP + * | ... + * | for_each_job { + * | tacleInit(); + * | tacleMain(); + * | } + * | WRITE_TO_FILE + * |} + * + * The main() function must call its parameters argc and argv for SET_UP to be + * able to read them. + * Only SET_UP necessarily has to be in main(). + * + * We use some niche C features, here's a quick explaination: + * 1. The && operator doesn't evaluate the right-hand side of the expression + * unless the left side evaluated to true. We use this to only execute + * _rt_start_loop() when the loop will actually run. + * 2. The comma operator executes the first expression and then throws away the + * result. We use this to call our void function from inside a comparison. + */ +#define for_each_job \ + for (; _rt_jobs_complete < _rt_max_jobs && (_rt_start_loop(),1); \ + _rt_stop_loop()) + +/****** Legacy API ****** + * Intended structure: + * + * |int main(int argc, char **argv) { + * | SET_UP + * | for (jobsComplete=0; jobsCompleteheight; + cols = in->width; + + //out = fMallocHandle(rows, cols); + + for(i=0; iheight; + cols = in->width; + + out = fMallocHandle(rows, cols); + + for(i=0; iheight; + cols = a->width; + + c = fMallocHandle(rows, cols); + + for(i=0; i<(rows*cols); i++) + { + asubsref(c,i) = asubsref(a,i) / b; + } + + return c; +} diff --git a/SD-VBS/common/c/fFind3.c b/SD-VBS/common/c/fFind3.c new file mode 100644 index 0000000..a783bae --- /dev/null +++ b/SD-VBS/common/c/fFind3.c @@ -0,0 +1,46 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +F2D* fFind3(F2D* in) +{ + int r, k, y, x, i, j; + F2D *points; + + y = in->height; + x = in->width; + + r = 0; + for(i=0; i +#include +#include "sdvbs_common.h" + +void fFreeHandle(F2D* out) +{ + if(out != NULL) + free(out); + + return; +} + diff --git a/SD-VBS/common/c/fHorzcat.c b/SD-VBS/common/c/fHorzcat.c new file mode 100644 index 0000000..9845e7c --- /dev/null +++ b/SD-VBS/common/c/fHorzcat.c @@ -0,0 +1,40 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +F2D* fHorzcat(F2D* a, F2D* b) +{ + F2D* out_, *out, *c; + int rows=0, cols=0, i, j, k, c_1, c_2, r_3, c_3; + int r_1; + + r_1 = a->height; + c_1 = a->width; + cols += c_1; + c_2 = b->width; + cols += c_2; + rows = r_1; + + out = fMallocHandle(rows, cols); + + for(i=0; i +#include +#include "sdvbs_common.h" + +F2D* fMallocHandle(int rows, int cols) +{ + int i, j; + F2D* out; + + out = (F2D*)malloc(sizeof(F2D) + sizeof(float)*rows*cols); + out->height = rows; + out->width = cols; + return out; +} diff --git a/SD-VBS/common/c/fMdivide.c b/SD-VBS/common/c/fMdivide.c new file mode 100644 index 0000000..671e7d1 --- /dev/null +++ b/SD-VBS/common/c/fMdivide.c @@ -0,0 +1,27 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +F2D* fMdivide(F2D* a, F2D* b) +{ + F2D *c; + int i, j, rows, cols; + + rows = a->height; + cols = a->width; + + if(rows != b->height || cols != b->width) + { + printf("fMDivide Mismatch = \nrows: %d\t%d\ncols: %d\t%d\n", rows, b->height, cols, b->width); + return NULL; + } + + c = fMallocHandle(rows, cols); + + for(i=0; i<(rows*cols); i++) + asubsref(c,i) = asubsref(a,i) / asubsref(b,i); + + return c; +} diff --git a/SD-VBS/common/c/fMinus.c b/SD-VBS/common/c/fMinus.c new file mode 100644 index 0000000..6b4954e --- /dev/null +++ b/SD-VBS/common/c/fMinus.c @@ -0,0 +1,21 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +F2D* fMinus(F2D* a, F2D* b) +{ + F2D *c; + int i, j, rows, cols; + + rows = a->height; + cols = a->width; + + c = fMallocHandle(rows, cols); + + for(i=0; i<(rows*cols); i++) + asubsref(c,i) = asubsref(a,i) - asubsref(b,i); + + return c; +} diff --git a/SD-VBS/common/c/fMtimes.c b/SD-VBS/common/c/fMtimes.c new file mode 100644 index 0000000..c765d2f --- /dev/null +++ b/SD-VBS/common/c/fMtimes.c @@ -0,0 +1,38 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +F2D* fMtimes(F2D* a, F2D* b) +{ + F2D *out; + int m, p, p1, n, i, j, k; + float temp; + + m = a->height; + p = a->width; + + p1 = b->height; + n = b->width; + + out = fMallocHandle(m,n); + + for(i=0; iheight; + cols = a->width; + + c = fMallocHandle(rows, cols); + + for(i=0; i<(rows*cols); i++) { + asubsref(c,i) = asubsref(a,i) + asubsref(b,i); + } + return c; +} diff --git a/SD-VBS/common/c/fResetArray.c b/SD-VBS/common/c/fResetArray.c new file mode 100644 index 0000000..23a853a --- /dev/null +++ b/SD-VBS/common/c/fResetArray.c @@ -0,0 +1,19 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include +#include +#include "sdvbs_common.h" + +void fResetArray(F2D *out, int rows, int cols, float val) +{ + int i, j; + + for(i=0; i +#include +#include "sdvbs_common.h" + +F2D* fResetHandle(F2D* out, int rows, int cols) +{ + int i, j; + //F2D* out; + + //out = (F2D*)malloc(sizeof(F2D) + sizeof(float)*rows*cols); + out->height = rows; + out->width = cols; + //printf("fmalloc happened\n"); + return out; +} diff --git a/SD-VBS/common/c/fReshape.c b/SD-VBS/common/c/fReshape.c new file mode 100644 index 0000000..a078826 --- /dev/null +++ b/SD-VBS/common/c/fReshape.c @@ -0,0 +1,27 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +F2D* fReshape(F2D* in, int rows, int cols) +{ + F2D *out; + int i, j, k; + int r, c; + + r = in->height; + c = in->width; + + out = fMallocHandle(rows, cols); + + k = 0; + for(i=0; iheight; + cols = input->width; + + in = fCopy(input, in); + //ind = iMallocHandle(rows,cols); + + for(i=0; iheight; + c1 = in1->width; + + buffer = (float*)malloc(sizeof(float)*r1*c1); + + sprintf(file, "%s", path); + fd = fopen(file, "r"); + + if(fd == NULL) + { + printf("Error: Expected file not opened %s\n", file); + return -1; + } + + while(!feof(fd)) + { + fscanf(fd, "%f", &buffer[count]); + count++; + } + count--; + + if(count != (r1*c1)) + { + printf("Checking error: dimensions mismatch. Expected = %d, Observed = %d \n", count, (r1*c1)); + return -1; + } + + for(i=0; itol || (buffer[i]-inVal)>tol ) + { + printf("Mismatch %d: (%f, %f)\n", i, buffer[i], inVal); + return -1; + } + } + + fclose(fd); + printf("Verification\t\t- Successful\n"); + free(buffer); + return ret; +} + + diff --git a/SD-VBS/common/c/fSetArray.c b/SD-VBS/common/c/fSetArray.c new file mode 100644 index 0000000..cd8269b --- /dev/null +++ b/SD-VBS/common/c/fSetArray.c @@ -0,0 +1,22 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include +#include +#include "sdvbs_common.h" + +F2D* fSetArray(int rows, int cols, float val) +{ + int i, j; + F2D *out; + out = fMallocHandle(rows, cols); + + for(i=0; iheight; + cols = in->width; + + sorted = fDeepCopy(in); + + for(k=0; kheight; + cols = input->width; + + in = fDeepCopy(input); + ind = iMallocHandle(rows,cols); + + for(i=0; iheight; + cols = inMat->width; + + if(cols == 1 || rows == 1) + Rcols = 1; + else + Rcols = cols; + + outMat = fSetArray(1,Rcols,0); + + if( cols == 1) + { + temp = 0; + for( j=0; jheight; + cols = inMat->width; + + if(dir == 1) + { + newRow = 1; + newCols = cols; + } + else + { + newRow = rows; + newCols = 1; + } + + outMat = fSetArray(newRow,newCols,0); + + if(dir == 1) + { + for (i=0; iheight; + cols = a->width; + + c = fMallocHandle(rows, cols); + + for(i=0; i<(rows*cols); i++) + asubsref(c,i) = asubsref(a,i) * asubsref(b,i); + + return c; +} diff --git a/SD-VBS/common/c/fTranspose.c b/SD-VBS/common/c/fTranspose.c new file mode 100644 index 0000000..9611be2 --- /dev/null +++ b/SD-VBS/common/c/fTranspose.c @@ -0,0 +1,27 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +F2D* fTranspose(F2D* a) +{ + F2D *out; + int m, p, p1, n, i, j, k; + float temp; + + m = a->height; + n = a->width; + + out = fMallocHandle(n, m); + k = 0; + for(i=0; i +#include +#include "sdvbs_common.h" + +void fWriteMatrix(F2D* input, char* inpath) +{ + FILE* fp; + char im[100]; + int rows,cols, i, j; + + sprintf(im, "%s/expected_C.txt", inpath); + fp = fopen(im, "w"); + + rows = input->height; + cols = input->width; + + for(i=0; iheight; + na = a->width; + + mb = b->height; + nb = b->width; + + ci = ma; + cj = na; + + c = fSetArray(ci, cj, 0); + + r_index = mb/2; + c_index = nb/2; + + for(i=0; i=0 && ri < ma && ci >= 0 && ci < na) + subsref(c,i,j) += subsref(a,ri,ci) * subsref(b,mm,nn); + } + } + } + } + + return c; +} diff --git a/SD-VBS/common/c/ffConv2_dY.c b/SD-VBS/common/c/ffConv2_dY.c new file mode 100644 index 0000000..e480eaa --- /dev/null +++ b/SD-VBS/common/c/ffConv2_dY.c @@ -0,0 +1,52 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +F2D* ffConv2_dY(F2D* a, F2D* b) +{ + F2D *c, *out; + int ma, na, mb, nb, ci, cj, i, j, m, n; + int r_index, c_index; + + ma = a->height; + na = a->width; + + mb = b->height; + nb = b->width; + + r_index = ceil((mb + 1.0)/2.0); + c_index = ceil((nb + 1.0)/2.0); + + ci = ma+mb-1; + cj = na+nb-1; + + c = fSetArray(ci, cj, 0); + + for(i=0; i=0 && (j-n)>=0 && (i-m)height; + cols = a->width; + + c = fMallocHandle(rows, cols); + + for(i=0; i<(rows*cols); i++) + asubsref(c,i) = asubsref(a,i) / asubsref(b,i); + + return c; +} diff --git a/SD-VBS/common/c/ffTimes.c b/SD-VBS/common/c/ffTimes.c new file mode 100644 index 0000000..8ef84b8 --- /dev/null +++ b/SD-VBS/common/c/ffTimes.c @@ -0,0 +1,21 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +F2D* ffTimes(F2D* a, float b) +{ + F2D *c; + int i, j, rows, cols; + + rows = a->height; + cols = a->width; + + c = fMallocHandle(rows, cols); + + for(i=0; i<(rows*cols); i++) + asubsref(c,i) = asubsref(a,i) * b; + + return c; +} diff --git a/SD-VBS/common/c/ffVertcat.c b/SD-VBS/common/c/ffVertcat.c new file mode 100644 index 0000000..00fa74b --- /dev/null +++ b/SD-VBS/common/c/ffVertcat.c @@ -0,0 +1,36 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +F2D* ffVertcat(F2D* matrix1, F2D* matrix2) +{ + F2D *outMatrix; + int row1, col1, row2, col2, i, j, k; + + row1 = matrix1->height; + col1 = matrix1->width; + + row2 = matrix2->height; + col2 = matrix2->width; + + outMatrix = fMallocHandle(row1+row2, col1); + + for( i=0; iheight; + na = a->width; + + mb = b->height; + nb = b->width; + + r_index = ceil((mb + 1.0)/2.0); + c_index = ceil((nb + 1.0)/2.0); + + ci = ma+mb-1; + cj = na+nb-1; + + c = fSetArray(ci, cj, 0); + + for(i=0; i=0 && (j-n)>=0 && (i-m)height; + na = a->width; + + mb = b->height; + nb = b->width; + + r_index = ceil((mb + 1.0)/2.0); + c_index = ceil((nb + 1.0)/2.0); + + ci = ma+mb-1; + cj = na+nb-1; + + c = fSetArray(ci, cj, 0); + + for(i=0; i=0 && (j-n)>=0 && (i-m)height; + cols = in->width; + + //out = fMallocHandle(rows, cols); + + for(i=0; iheight; + cols = in->width; + + out = fMallocHandle(rows, cols); + + for(i=0; iwidth; + cols += c_1; + + c_2 = b->width; + cols += c_2; + + r_3 = c->height; + c_3 = c->width; + cols += c_3; + rows = r_3; + + out = fMallocHandle(rows, cols); + + for(i=0; iwidth != in2 -> width || in1->height != in2->height) return 0; + for(int i = 0; i < in1->width;i++){ + for(int j = 0; j < in1->height;j++){ + if(subsref(in1,i,j) != subsref(in2,i,j)) return 0; + } + } + return 1; + +} + + diff --git a/SD-VBS/common/c/iDeepCopy.c b/SD-VBS/common/c/iDeepCopy.c new file mode 100644 index 0000000..8d56680 --- /dev/null +++ b/SD-VBS/common/c/iDeepCopy.c @@ -0,0 +1,23 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +I2D* iDeepCopy(I2D* in) +{ + int i, j; + I2D* out; + int rows, cols; + + rows = in->height; + cols = in->width; + + out = iMallocHandle(rows, cols); + + for(i=0; i +#include +#include "sdvbs_common.h" + +void iFreeHandle(I2D* out) +{ + if(out != NULL) + free(out); + + return; +} + diff --git a/SD-VBS/common/c/iHorzcat.c b/SD-VBS/common/c/iHorzcat.c new file mode 100644 index 0000000..ac1c4ea --- /dev/null +++ b/SD-VBS/common/c/iHorzcat.c @@ -0,0 +1,41 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +I2D* iHorzcat(I2D* a, I2D* b) +{ + I2D *out, *c; + int rows=0, cols=0, i, j, k, c_1, c_2, r_3, c_3; + int r_1; + + r_1 = a->height; + c_1 = a->width; + cols += c_1; + + c_2 = b->width; + cols += c_2; + rows = r_1; + + out = iMallocHandle(rows, cols); + + for(i=0; i +#include +#include "sdvbs_common.h" + +I2D* iMallocHandle(int rows, int cols) +{ + int i, j; + I2D* out; + + out = (I2D*)malloc(sizeof(I2D) + sizeof(int)*rows*cols); + out->height = rows; + out->width = cols; + //printf("imalloc happened\n"); + return out; +} + diff --git a/SD-VBS/common/c/iMinus.c b/SD-VBS/common/c/iMinus.c new file mode 100644 index 0000000..a0ed908 --- /dev/null +++ b/SD-VBS/common/c/iMinus.c @@ -0,0 +1,21 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +I2D* iMinus(I2D* a, I2D* b) +{ + I2D *c; + int i, j, rows, cols; + + rows = a->height; + cols = a->width; + + c = iMallocHandle(rows, cols); + + for(i=0; i<(rows*cols); i++) + asubsref(c,i) = asubsref(a,i) - asubsref(b,i); + + return c; +} diff --git a/SD-VBS/common/c/iResetArray.c b/SD-VBS/common/c/iResetArray.c new file mode 100644 index 0000000..3659d15 --- /dev/null +++ b/SD-VBS/common/c/iResetArray.c @@ -0,0 +1,22 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include +#include +#include "sdvbs_common.h" + +void iResetArray(I2D* out, int rows, int cols, int val) +{ + int i, j; + //I2D *out; + //out = iMallocHandle(rows, cols); + + for(i=0; iheight; + c = in->width; + + out = iMallocHandle(rows, cols); + + k = 0; + for(i=0; i +#include +#include "sdvbs_common.h" + +I2D* iSetArray(int rows, int cols, int val) +{ + int i, j; + I2D *out; + out = iMallocHandle(rows, cols); + + for(i=0; iheight; + cols = in->width; + + sorted = iDeepCopy(in); + + for(k=0; kheight; + cols = in->width; + + sorted = iDeepCopy(in); + ind = iMallocHandle(rows, cols); + + for(i=0; iheight; + cols = a->width; + + c = iMallocHandle(rows, cols); + + for(i=0; i<(rows*cols); i++) + asubsref(c,i) = asubsref(a,i) * asubsref(b,i); + + return c; +} + diff --git a/SD-VBS/common/c/iTranspose.c b/SD-VBS/common/c/iTranspose.c new file mode 100644 index 0000000..79b65d2 --- /dev/null +++ b/SD-VBS/common/c/iTranspose.c @@ -0,0 +1,26 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +I2D* iTranspose(I2D* a) +{ + I2D *out; + int m, p, p1, n, i, j, k; + int temp; + + m = a->height; + n = a->width; + + out = iMallocHandle(n, m); + k = 0; + for(i=0; iheight; + col1 = matrix1->width; + + row2 = matrix2->height; + col2 = matrix2->width; + + outMatrix = iMallocHandle(row1+row2, col1); + + for( i=0; i +#include "sdvbs_common.h" + +I2D* ifDeepCopy(F2D* in) +{ + int i, j; + I2D *out; + int rows, cols; + + rows = in->height; + cols = in->width; + + out = iMallocHandle(rows, cols); + + for(i=0; iheight; + p = a->width; + + p1 = b->height; + n = b->width; + + out = fMallocHandle(m,n); + + for(i=0; iheight; + na = a->width; + + mb = b->height; + nb = b->width; + + r_index = ceil((mb + 1.0)/2.0); + c_index = ceil((nb + 1.0)/2.0); + + ci = ma+mb-1; + cj = na+nb-1; + + c = iSetArray(ci, cj, 0); + + for(i=0; i=0 && (j-n)>=0 && (i-m)height; + cols = imageIn->width; + + imageOut = fSetArray(rows, cols, 0); + tempOut = fSetArray(rows, cols, 0); + kernel = iMallocHandle(1, 5); + + asubsref(kernel,0) = 1; + asubsref(kernel,1) = 4; + asubsref(kernel,2) = 6; + asubsref(kernel,3) = 4; + asubsref(kernel,4) = 1; + kernelSize = 5; + kernelSum = 16; + + startCol = 2; + endCol = cols - 2; + halfKernel = 2; + + startRow = 2; + endRow = rows - 2; + + for(i=startRow; iheight; + cols = imageIn->width; + + fResetArray(imageOut, rows, cols, 0); + fResetArray(tempOut, rows, cols, 0); + //kernel = iMallocHandle(1, 5); + + asubsref(kernel,0) = 1; + asubsref(kernel,1) = 4; + asubsref(kernel,2) = 6; + asubsref(kernel,3) = 4; + asubsref(kernel,4) = 1; + kernelSize = 5; + kernelSum = 16; + + startCol = 2; + endCol = cols - 2; + halfKernel = 2; + + startRow = 2; + endRow = rows - 2; + + for(i=startRow; iheight; + cols = imageIn->width; + + // level 1 is the base image. + + outputRows = floor((rows+1)/2); + outputCols = floor((cols+1)/2); + + temp = fSetArray(rows, outputCols, 0); + imageOut = fSetArray(outputRows, outputCols, 0); + kernel = iMallocHandle(1, 5); + + asubsref(kernel,0) = 1; + asubsref(kernel,1) = 4; + asubsref(kernel,2) = 6; + asubsref(kernel,3) = 4; + asubsref(kernel,4) = 1; + kernelSize = 5; + kernelSum = 16; + + startCol = 2; + endCol = cols - 2; + halfKernel = 2; + + startRow = 2; + endRow = rows - 2; + + for(i=startRow; i +#include +#include "sdvbs_common.h" + +I2D* isMinus(I2D* a, int b) +{ + I2D *c; + int i, j, rows, cols; + + rows = a->height; + cols = a->width; + + c = iMallocHandle(rows, cols); + + for(i=0; i<(rows*cols); i++) + asubsref(c,i) = asubsref(a,i) - b; + + return c; +} diff --git a/SD-VBS/common/c/isPlus.c b/SD-VBS/common/c/isPlus.c new file mode 100644 index 0000000..9c7438f --- /dev/null +++ b/SD-VBS/common/c/isPlus.c @@ -0,0 +1,22 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +I2D* isPlus(I2D* a, int b) +{ + I2D *c; + int i, j, rows, cols; + + rows = a->height; + cols = a->width; + + c = iMallocHandle(rows, cols); + + for(i=0; i<(rows*cols); i++) + asubsref(c,i) = asubsref(a,i) + b; + + return c; +} + diff --git a/SD-VBS/common/c/photonEndTiming.c b/SD-VBS/common/c/photonEndTiming.c new file mode 100644 index 0000000..b15c4de --- /dev/null +++ b/SD-VBS/common/c/photonEndTiming.c @@ -0,0 +1,22 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +/** C File **/ +#include +#include +#include +#include +#include +#include "timingUtils.h" +#include "sdvbs_common.h" + +unsigned int * photonEndTiming() +{ + static unsigned int *array; + array = (unsigned int*)malloc(sizeof(unsigned int)*2); + + magic_timing_begin(array[0], array[1]); + return array; +} + diff --git a/SD-VBS/common/c/photonPrintTiming.c b/SD-VBS/common/c/photonPrintTiming.c new file mode 100644 index 0000000..06df530 --- /dev/null +++ b/SD-VBS/common/c/photonPrintTiming.c @@ -0,0 +1,22 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +/** C File **/ +#include +#include +#include +#include +#include +#include "timingUtils.h" +#include "sdvbs_common.h" + +void photonPrintTiming(unsigned int * elapsed) +{ + if(elapsed[1] == 0) + printf("Cycles elapsed\t\t- %u\n\n", elapsed[0]); + else + printf("Cycles elapsed\t\t- %u%u\n\n", elapsed[1], elapsed[0]); +} + +/** End of C Code **/ diff --git a/SD-VBS/common/c/photonReportTiming.c b/SD-VBS/common/c/photonReportTiming.c new file mode 100644 index 0000000..c41d103 --- /dev/null +++ b/SD-VBS/common/c/photonReportTiming.c @@ -0,0 +1,28 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +/** C File **/ +#include +#include +#include +#include +#include +#include "timingUtils.h" +#include "sdvbs_common.h" + +unsigned int * photonReportTiming(unsigned int* startCycles,unsigned int* endCycles) +{ + + static unsigned int *elapsed; + elapsed = (unsigned int*)malloc(sizeof(unsigned int)*2); + unsigned long long start = (((unsigned long long)0x0) | startCycles[0]) << 32 | startCycles[1]; + unsigned long long end = (((unsigned long long)0x0) | endCycles[0]) << 32 | endCycles[1]; + unsigned long long diff = end - start; + elapsed[0] = (unsigned int)(diff >> 32); + elapsed[1] = (unsigned int)(diff & 0xffffffff); + return elapsed; + +} + +/** End of C Code **/ diff --git a/SD-VBS/common/c/photonStartTiming.c b/SD-VBS/common/c/photonStartTiming.c new file mode 100644 index 0000000..0d0b2b1 --- /dev/null +++ b/SD-VBS/common/c/photonStartTiming.c @@ -0,0 +1,23 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +/** C File **/ +#include +#include +#include +#include +#include +#include "timingUtils.h" +#include "sdvbs_common.h" + +unsigned int* photonStartTiming() +{ + static unsigned int *array; + + array = (unsigned int*)malloc(sizeof(unsigned int)*2); + magic_timing_begin(array[0], array[1]); + return array; +} + +/** End of C Code **/ diff --git a/SD-VBS/common/c/randWrapper.c b/SD-VBS/common/c/randWrapper.c new file mode 100644 index 0000000..cadcc32 --- /dev/null +++ b/SD-VBS/common/c/randWrapper.c @@ -0,0 +1,30 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include "sdvbs_common.h" + +F2D* randWrapper(int m, int n) +{ + F2D *out; + float seed; + int i,j; + + out = fSetArray(m, n, 0); + seed = 0.9; + + for(i=0; i +F2D* randnWrapper(int m, int n) +{ + F2D *out; + float seed; + int i,j; + + out = fSetArray(m, n, 0); + seed = 0.9; + + for(i=0; i +#include +#include "sdvbs_common.h" + +I2D* readImage(const char* pathName) +{ + // Reading BMP image + char signature[2]; + int file_size; + short int reserved1; + short int reserved2; + int loc_of_bitmap; + + int size_of_infoheader; + int width; + int height; + short int number_of_planes; + short int bits_per_pixel; + + int compression_method; + int bytes_of_bitmap; + int hori_reso; + int vert_reso; + int no_of_colors; + int no_of_imp_colors; + + int nI,nJ; + int pixSize; + + unsigned char tempb,tempg,tempr,tempjunk[12]; + int ta; + I2D* srcImage; + + FILE *input; + input = fopen(pathName,"rb"); + if(input == NULL) + { + perror("File pointer error"); + return NULL; + } + else + { + //start of header information + fread(&signature,sizeof(signature),1,input); + fread(&file_size,sizeof(file_size),1,input); + fread(&reserved1,sizeof(reserved1),1,input); + fread(&reserved2,sizeof(reserved2),1,input); + fread(&loc_of_bitmap,sizeof(loc_of_bitmap),1,input); + + fread(&size_of_infoheader,sizeof(size_of_infoheader),1,input); + fread(&width,sizeof(width),1,input); // Reads the width of the image + fread(&height,sizeof(height),1,input); // Reads the height of the image + fread(&number_of_planes,sizeof(number_of_planes),1,input); + fread(&bits_per_pixel,sizeof(bits_per_pixel),1,input); + fread(&compression_method,sizeof(compression_method),1,input); + fread(&bytes_of_bitmap,sizeof(bytes_of_bitmap),1,input); + + fread(&hori_reso,sizeof(hori_reso),1,input); + fread(&vert_reso,sizeof(vert_reso),1,input); + fread(&no_of_colors,sizeof(no_of_colors),1,input); + fread(&no_of_imp_colors,sizeof(no_of_imp_colors),1,input); + //end of header information + + srcImage = iMallocHandle(height, width); + + // Conditions to check whether the BMP is interleaved and handling few exceptions + if(srcImage->height <= 0 || srcImage->width <= 0 || signature[0] != 'B' || signature[1] != 'M' || ( bits_per_pixel !=24 && bits_per_pixel !=8 ) ) + { + printf("ERROR in BMP read: The input file is not in standard BMP format"); + return NULL; + } + fseek(input,loc_of_bitmap,SEEK_SET); + + if (bits_per_pixel == 8) + { + for(nI = (height - 1); nI >= 0 ; nI--) + { + for(nJ = 0;nJ < width; nJ++) + { + fread(&tempg,sizeof(unsigned char),1,input); + subsref(srcImage,nI,nJ) = (int)tempg; + } + } + } + else if (bits_per_pixel == 24) + { + for(nI = (height - 1); nI >= 0 ; nI--) + { + for(nJ = 0;nJ < width; nJ++) + { + fread(&tempb,sizeof(unsigned char),1,input); + fread(&tempg,sizeof(unsigned char),1,input); + fread(&tempr,sizeof(unsigned char),1,input); + ta = (3*tempr + 6*tempg + tempb)/10; + ta = tempg; + subsref(srcImage,nI,nJ) = (int)ta; + } + } + } + else + { + return NULL; + } + + fclose(input); + return srcImage; + } +} diff --git a/SD-VBS/common/c/sdvbs_common.h b/SD-VBS/common/c/sdvbs_common.h new file mode 100644 index 0000000..14e28b2 --- /dev/null +++ b/SD-VBS/common/c/sdvbs_common.h @@ -0,0 +1,139 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#ifndef _SDVBS_COMMON_ +#define _SDVBS_COMMON_ + +#include +#include +#include + +typedef struct +{ + int width; + int height; + int data[]; +}I2D; + +typedef struct +{ + int width; + int height; + unsigned int data[]; +}UI2D; + +typedef struct +{ + int width; + int height; + float data[]; +}F2D; + +#define subsref(a,i,j) a->data[(i) * a->width + (j)] +#define asubsref(a,i) a->data[i] +#define arrayref(a,i) a[i] + +/** Image read and write **/ +I2D* readImage(const char* pathName);; +F2D* readFile(unsigned char* fileName); + + +/** Memory allocation functions **/ +I2D* iMallocHandle(int rows, int cols); +F2D* fMallocHandle(int rows, int cols); +F2D* fResetHandle(F2D* out, int rows, int cols); +UI2D* uiMallocHandle(int rows, int cols); + +void iFreeHandle(I2D* out); +void fFreeHandle(F2D* out); +void uiFreeHandle(UI2D* out); + +/** Memory copy/set function **/ +I2D* iSetArray(int rows, int cols, int val); +void iResetArray(I2D* out, int rows, int cols, int val); +F2D* fSetArray(int rows, int cols, float val); +void fResetArray(F2D* out, int rows, int cols, float val); +I2D* iDeepCopy(I2D* in); +F2D* fDeepCopy(F2D* in); +F2D* fCopy(F2D* in, F2D* out); +I2D* iDeepCopyRange(I2D* in, int startRow, int numberRows, int startCol, int numberCols); +F2D* fDeepCopyRange(F2D* in, int startRow, int numberRows, int startCol, int numberCols); +F2D* fiDeepCopy(I2D* in); +void fiCopy(F2D* out, I2D* in); +I2D* ifDeepCopy(F2D* in); + + +/** Matrix operations - concatenation, reshape **/ +F2D* ffVertcat(F2D* matrix1, F2D* matrix2); +I2D* iVertcat(I2D* matrix1, I2D* matrix2); +F2D* fHorzcat(F2D* a, F2D* b); +I2D* iHorzcat(I2D* a, I2D* b); +F2D* horzcat(F2D* a, F2D* b, F2D* c); +F2D* fTranspose(F2D* a); +I2D* iTranspose(I2D* a); +F2D* fReshape(F2D* in, int rows, int cols); +I2D* iReshape(I2D* in, int rows, int cols); + + +/** Binary Operations **/ +F2D* fDivide(F2D* a, float b); +F2D* fMdivide(F2D* a, F2D* b); +F2D* ffDivide(F2D* a, F2D* b); +F2D* ffTimes(F2D* a, float b); +F2D* fTimes(F2D* a, F2D* b); +I2D* iTimes(I2D* a, I2D* b); +F2D* fMtimes(F2D* a, F2D* b); +F2D* ifMtimes(I2D* a, F2D* b); +F2D* fMinus(F2D* a, F2D* b); +I2D* iMinus(I2D* a, I2D* b); +I2D* isMinus(I2D* a, int b); +F2D* fPlus(F2D* a, F2D* b); +I2D* isPlus(I2D* a, int b); + + +/** Filtering operations **/ +F2D* calcSobel_dX(F2D* imageIn); +F2D* calcSobel_dY(F2D* imageIn); +F2D* ffConv2(F2D* a, F2D* b); +F2D* fiConv2(I2D* a, F2D* b); +F2D* ffConv2_dY(F2D* a, F2D* b); +F2D* ffiConv2(F2D* a, I2D* b); +I2D* iiConv2(I2D* a, I2D* b); + + +/** Image Transformations - resize, integration etc **/ +F2D* imageResize(F2D* imageIn); +F2D* imageBlur(I2D* imageIn); +F2D* imageReblur(I2D* imageIn, F2D* imageOut, F2D* tempOut, I2D* kernel); + + +/** Support functions **/ +F2D* fFind3(F2D* in); +F2D* fSum2(F2D* inMat, int dir); +F2D* fSum(F2D* inMat); +I2D* iSort(I2D* in, int dim); +F2D* fSort(F2D* in, int dim); +I2D* iSortIndices(I2D* in, int dim); +I2D* fSortIndices(F2D* input, int dim); +I2D* fResortIndices(F2D* input, int dim, F2D* in, I2D* ind); +F2D* randnWrapper(int m, int n); +F2D* randWrapper(int m, int n); + + +/** Checking functions **/ +int selfCheck(I2D* in1, char* path, int tol); +int fSelfCheck(F2D* in1, char* path, float tol); +void writeMatrix(I2D* input, char* inpath); +void fWriteMatrix(F2D* input, char* inpath); +int iCheck(I2D* in1, I2D* in2); + +/** Timing functions **/ +unsigned int* photonEndTiming(); +unsigned int* photonStartTiming(); +unsigned int* photonReportTiming(unsigned int* startCycles,unsigned int* endCycles); +void photonPrintTiming(unsigned int * elapsed); + + +#endif + diff --git a/SD-VBS/common/c/selfCheck.c b/SD-VBS/common/c/selfCheck.c new file mode 100644 index 0000000..e79a6a4 --- /dev/null +++ b/SD-VBS/common/c/selfCheck.c @@ -0,0 +1,65 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include +#include +#include +#include "sdvbs_common.h" + +int selfCheck(I2D* in1, char* path, int tol) +{ + int r1, c1, ret=1; + FILE* fd; + int count=0, *buffer, i, j; + char file[100]; + int* data = in1->data; + + r1 = in1->height; + c1 = in1->width; + + buffer = (int*)malloc(sizeof(int)*r1*c1); + + sprintf(file, "%s", path); + fd = fopen(file, "r"); + if(fd == NULL) + { + printf("Error: Expected file not opened \n"); + return -1; + } + + while(!feof(fd)) + { + fscanf(fd, "%d", &buffer[count]); + count++; + } + count--; + + if(count < (r1*c1)) + { + printf("Checking error: dimensions mismatch. Expected = %d, Observed = %d \n", count, (r1*c1)); + return -1; + } + + for(i=0; itol || (abs(buffer[i])-abs(data[i]))>tol) + { + printf("Checking error: Values mismtach at %d element\n", i); + printf("Expected value = %d, observed = %d\n", buffer[i], data[i] ); + return -1; + } + } + + fclose(fd); + free(buffer); + printf("Verification\t\t- Successful\n"); + return ret; +} + + + + + + + diff --git a/SD-VBS/common/c/timingUtils.h b/SD-VBS/common/c/timingUtils.h new file mode 100644 index 0000000..818728f --- /dev/null +++ b/SD-VBS/common/c/timingUtils.h @@ -0,0 +1,99 @@ +#ifdef GCC +#define magic_timing_begin(cycleLo, cycleHi) {\ + asm volatile( "rdtsc": "=a" (cycleLo), "=d" (cycleHi)); \ +}\ + +#define magic_timing_end(cycleLo, cycleHi) {\ + unsigned tempCycleLo, tempCycleHi; \ + asm volatile( "rdtsc": "=a" (tempCycleLo), "=d" (tempCycleHi)); \ + cycleLo = tempCycleLo-cycleLo;\ + cycleHi = tempCycleHi - cycleHi;\ +}\ + + + +#define magic_timing_report(cycleLo, cycleHi) {\ + printf("Timing report: %d %d\n", cycleLo, cycleHi); \ +}\ + + + + +#endif + +#ifdef METRO + +#define magic_timing_begin(cycleLo, cycleHi) {\ + asm volatile( "mfsr $8, CYCLE_LO\n\t" \ + "mfsr $9, CYCLE_HI\n\t" \ + "addu %0, $8, $0\n\t" \ + "addu %1, $9, $0\n\t" \ + :"=r" (cycleLo), "=r" (cycleHi) \ + : \ + :"$8", "$9"\ + );\ +} + +#define magic_timing_end(cycleLo, cycleHi) {\ + asm volatile( \ + "mfsr $8, CYCLE_LO\n\t" \ + "mfsr $9, CYCLE_HI\n\t" \ + "subu %0, $8, %0\n\t" \ + "subu %1, $9, %1\n\t" \ + :"=r" (cycleLo), "=r" (cycleHi) \ + : \ + :"$8", "$9"\ + ); \ +} + +#define magic_timing_report(cycleLo, cycleHi) {\ + asm volatile( "addu $8, %0, $0\n\t" \ + "mtsr PASS $8\n\t" \ + "mtsr PASS $9\n\t" \ + : \ + :"r" (cycleLo), "r" (cycleHi) \ + : "$8", "$9" \ + );\ +} + +//#define metro_magic_timing_report(cycleLo, cycleHi) {\ +// asm volatile( "nop\n\t");\ +//} + +#endif + +#ifdef BTL + +#include "/u/kvs/raw/rawlib/archlib/include/raw.h" + +#define magic_timing_begin(cycleLo, cycleHi) {\ + raw_magic_timing_report_begin();\ +} + +#define magic_timing_end(cycleLo, cycleHi) {\ + raw_magic_timing_report_end(); \ +} + +#define magic_timing_report(cycleLo, cycleHi) {\ + raw_magic_timing_report_print(); \ +} + + +// +//void metro_magic_timing_begin(int cycleLo, int cycleHi) +//{ +// raw_magic_timing_report_begin(); +//} +// +//void metro_magic_timing_end(int cycleLo, int cycleHi) +//{ +// raw_magic_timing_report_end(); +//} +// +//void metro_magic_timing_report(int cycleLo, int cycleHi) +//{ +// raw_magic_timing_report_print(); +// return; +//} + +#endif diff --git a/SD-VBS/common/c/uiFreeHandle.c b/SD-VBS/common/c/uiFreeHandle.c new file mode 100644 index 0000000..ce64ad9 --- /dev/null +++ b/SD-VBS/common/c/uiFreeHandle.c @@ -0,0 +1,15 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include +#include +#include "sdvbs_common.h" + +void uiFreeHandle(UI2D* out) +{ + free(out); + + return; +} + diff --git a/SD-VBS/common/c/uiMallocHandle.c b/SD-VBS/common/c/uiMallocHandle.c new file mode 100644 index 0000000..ee26d4c --- /dev/null +++ b/SD-VBS/common/c/uiMallocHandle.c @@ -0,0 +1,20 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include +#include +#include "sdvbs_common.h" + +UI2D* uiMallocHandle(int rows, int cols) +{ + int i, j; + UI2D* out; + + out = malloc(sizeof(UI2D) + sizeof(unsigned int)*rows*cols); + out->height = rows; + out->width = cols; + printf("uimalloc here\n"); + return out; +} + diff --git a/SD-VBS/common/c/uiResetArray.c b/SD-VBS/common/c/uiResetArray.c new file mode 100644 index 0000000..249e570 --- /dev/null +++ b/SD-VBS/common/c/uiResetArray.c @@ -0,0 +1,19 @@ +/******************************** +Author: Sravanthi Kota Venkata +********************************/ + +#include +#include +#include "sdvbs_common.h" + +void uiResetArray(UI2D* out, int rows, int cols, int val) +{ + int i, j; + + + for(i=0; i +#include +#include "sdvbs_common.h" + +UI2D* uiSetArray(int rows, int cols, int val) +{ + int i, j; + UI2D *out; + out = uiMallocHandle(rows, cols); + + for(i=0; i +#include +#include "sdvbs_common.h" + +void writeMatrix(I2D* input, char* inpath) +{ + FILE* fp; + char im[100]; + int rows,cols, i, j; + + sprintf(im, "%s/expected_C.txt", inpath); + fp = fopen(im, "w"); + + rows = input->height; + cols = input->width; + + for(i=0; i