summaryrefslogtreecommitdiffstats
path: root/SD-VBS/common/c
diff options
context:
space:
mode:
authorLeo Chan <leochanj@live.unc.edu>2020-10-22 01:53:21 -0400
committerJoshua Bakita <jbakita@cs.unc.edu>2020-10-22 01:56:35 -0400
commitd17b33131c14864bd1eae275f49a3f148e21cf29 (patch)
tree0d8f77922e8d193cb0f6edab83018f057aad64a0 /SD-VBS/common/c
parent601ed25a4c5b66cb75315832c15613a727db2c26 (diff)
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.
Diffstat (limited to 'SD-VBS/common/c')
-rw-r--r--SD-VBS/common/c/calcSobel_dX.c77
-rw-r--r--SD-VBS/common/c/calcSobel_dY.c79
-rw-r--r--SD-VBS/common/c/extra.h479
-rw-r--r--SD-VBS/common/c/fCopy.c24
-rw-r--r--SD-VBS/common/c/fDeepCopy.c24
-rw-r--r--SD-VBS/common/c/fDeepCopyRange.c24
-rw-r--r--SD-VBS/common/c/fDivide.c23
-rw-r--r--SD-VBS/common/c/fFind3.c46
-rw-r--r--SD-VBS/common/c/fFreeHandle.c16
-rw-r--r--SD-VBS/common/c/fHorzcat.c40
-rw-r--r--SD-VBS/common/c/fMallocHandle.c18
-rw-r--r--SD-VBS/common/c/fMdivide.c27
-rw-r--r--SD-VBS/common/c/fMinus.c21
-rw-r--r--SD-VBS/common/c/fMtimes.c38
-rw-r--r--SD-VBS/common/c/fPlus.c21
-rw-r--r--SD-VBS/common/c/fResetArray.c19
-rw-r--r--SD-VBS/common/c/fResetHandle.c19
-rw-r--r--SD-VBS/common/c/fReshape.c27
-rw-r--r--SD-VBS/common/c/fResortIndices.c77
-rw-r--r--SD-VBS/common/c/fSelfCheck.c59
-rw-r--r--SD-VBS/common/c/fSetArray.c22
-rw-r--r--SD-VBS/common/c/fSort.c43
-rw-r--r--SD-VBS/common/c/fSortIndices.c77
-rw-r--r--SD-VBS/common/c/fSum.c55
-rw-r--r--SD-VBS/common/c/fSum2.c56
-rw-r--r--SD-VBS/common/c/fTimes.c21
-rw-r--r--SD-VBS/common/c/fTranspose.c27
-rw-r--r--SD-VBS/common/c/fWriteMatrix.c34
-rw-r--r--SD-VBS/common/c/ffConv2.c47
-rw-r--r--SD-VBS/common/c/ffConv2_dY.c52
-rw-r--r--SD-VBS/common/c/ffDivide.c21
-rw-r--r--SD-VBS/common/c/ffTimes.c21
-rw-r--r--SD-VBS/common/c/ffVertcat.c36
-rw-r--r--SD-VBS/common/c/ffiConv2.c54
-rw-r--r--SD-VBS/common/c/fiConv2.c54
-rw-r--r--SD-VBS/common/c/fiCopy.c23
-rw-r--r--SD-VBS/common/c/fiDeepCopy.c23
-rw-r--r--SD-VBS/common/c/horzcat.c48
-rw-r--r--SD-VBS/common/c/iCheck.c18
-rw-r--r--SD-VBS/common/c/iDeepCopy.c23
-rw-r--r--SD-VBS/common/c/iDeepCopyRange.c23
-rw-r--r--SD-VBS/common/c/iFreeHandle.c16
-rw-r--r--SD-VBS/common/c/iHorzcat.c41
-rw-r--r--SD-VBS/common/c/iMallocHandle.c20
-rw-r--r--SD-VBS/common/c/iMinus.c21
-rw-r--r--SD-VBS/common/c/iResetArray.c22
-rw-r--r--SD-VBS/common/c/iReshape.c25
-rw-r--r--SD-VBS/common/c/iSetArray.c22
-rw-r--r--SD-VBS/common/c/iSort.c43
-rw-r--r--SD-VBS/common/c/iSortIndices.c47
-rw-r--r--SD-VBS/common/c/iTimes.c22
-rw-r--r--SD-VBS/common/c/iTranspose.c26
-rw-r--r--SD-VBS/common/c/iVertcat.c34
-rw-r--r--SD-VBS/common/c/ifDeepCopy.c25
-rw-r--r--SD-VBS/common/c/ifMtimes.c38
-rw-r--r--SD-VBS/common/c/iiConv2.c55
-rw-r--r--SD-VBS/common/c/imageBlur.c67
-rw-r--r--SD-VBS/common/c/imageReblur.c67
-rw-r--r--SD-VBS/common/c/imageResize.c78
-rw-r--r--SD-VBS/common/c/isMinus.c23
-rw-r--r--SD-VBS/common/c/isPlus.c22
-rw-r--r--SD-VBS/common/c/photonEndTiming.c22
-rw-r--r--SD-VBS/common/c/photonPrintTiming.c22
-rw-r--r--SD-VBS/common/c/photonReportTiming.c28
-rw-r--r--SD-VBS/common/c/photonStartTiming.c23
-rw-r--r--SD-VBS/common/c/randWrapper.c30
-rw-r--r--SD-VBS/common/c/randnWrapper.c40
-rw-r--r--SD-VBS/common/c/readFile.c42
-rw-r--r--SD-VBS/common/c/readImage.c112
-rw-r--r--SD-VBS/common/c/sdvbs_common.h139
-rw-r--r--SD-VBS/common/c/selfCheck.c65
-rw-r--r--SD-VBS/common/c/timingUtils.h99
-rw-r--r--SD-VBS/common/c/uiFreeHandle.c15
-rw-r--r--SD-VBS/common/c/uiMallocHandle.c20
-rw-r--r--SD-VBS/common/c/uiResetArray.c19
-rw-r--r--SD-VBS/common/c/uiSetArray.c21
-rw-r--r--SD-VBS/common/c/writeMatrix.c34
77 files changed, 3381 insertions, 0 deletions
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9F2D* calcSobel_dX(F2D* imageIn)
10{
11 int rows, cols;
12 F2D *kernel_1, *kernel_2;
13 float temp;
14 int kernelSize, startCol, endCol, halfKernel, startRow, endRow, i, j, kernelSum;
15 int k, kernelSum_1, kernelSum_2;
16 F2D *imageOut, *tempOut;
17
18 rows = imageIn->height;
19 cols = imageIn->width;
20
21 imageOut = fSetArray(rows, cols, 0);
22 tempOut = fSetArray(rows, cols, 0);
23 kernel_1 = fMallocHandle(1, 3);
24 kernel_2 = fMallocHandle(1, 3);
25
26 asubsref(kernel_1,0) = 1;
27 asubsref(kernel_1,1) = 2;
28 asubsref(kernel_1,2) = 1;
29
30 kernelSize = 3;
31 kernelSum_1 = 4;
32
33 asubsref(kernel_2,0) = 1;
34 asubsref(kernel_2,1) = 0;
35 asubsref(kernel_2,2) = -1;
36
37 kernelSum_2 = 2;
38
39 startCol = 1;
40 endCol = cols - 1;
41 halfKernel = 1;
42
43 startRow = 1;
44 endRow = rows - 1;
45
46 for(i=startRow; i<endRow; i++)
47 {
48 for(j=startCol; j<endCol; j++)
49 {
50 temp = 0;
51 for(k=-halfKernel; k<=halfKernel; k++)
52 {
53 temp += subsref(imageIn,i,j+k) * asubsref(kernel_2,k+halfKernel);
54 }
55 subsref(tempOut,i,j) = temp/kernelSum_2;
56 }
57 }
58
59 for(i=startRow; i<endRow; i++)
60 {
61 for(j=startCol; j<endCol; j++)
62 {
63 temp = 0;
64 for(k=-halfKernel; k<=halfKernel; k++)
65 {
66 temp += subsref(tempOut,(i+k),j) * asubsref(kernel_1,k+halfKernel);
67 }
68 subsref(imageOut,i,j) = temp/(float)kernelSum_1;
69 }
70 }
71
72 fFreeHandle(tempOut);
73 fFreeHandle(kernel_1);
74 fFreeHandle(kernel_2);
75 return imageOut;
76
77}
diff --git a/SD-VBS/common/c/calcSobel_dY.c b/SD-VBS/common/c/calcSobel_dY.c
new file mode 100644
index 0000000..d2ed379
--- /dev/null
+++ b/SD-VBS/common/c/calcSobel_dY.c
@@ -0,0 +1,79 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* calcSobel_dY(F2D* imageIn)
8{
9 int rows, cols;
10 I2D *kernel_1, *kernel_2;
11 float temp;
12 int kernelSize, startCol, endCol, halfKernel, startRow, endRow, i, j, kernelSum;
13 int k, kernelSum_2, outputRows, outputCols;
14 F2D *imageOut, *tempOut;
15 float kernelSum_1;
16
17 rows = imageIn->height;
18 cols = imageIn->width;
19
20 // level 1 is the base image.
21
22 outputRows = rows;
23 outputCols = cols;
24
25 imageOut = fSetArray(outputRows, outputCols, 0);
26 tempOut = fSetArray(outputRows, outputCols, 0);
27 kernel_1 = iMallocHandle(1, 3);
28 kernel_2 = iMallocHandle(1, 3);
29
30 asubsref(kernel_1,0) = 1;
31 asubsref(kernel_1,1) = 0;
32 asubsref(kernel_1,2) = -1;
33 kernelSize = 3;
34 kernelSum_1 = 2.0;
35
36 asubsref(kernel_2,0) = 1;
37 asubsref(kernel_2,1) = 2;
38 asubsref(kernel_2,2) = 1;
39 kernelSum_2 = 4;
40
41 startCol = 1;
42 endCol = cols - 1;
43 halfKernel = 1;
44
45 startRow = 1;
46 endRow = rows - 1;
47
48 for(i=startRow; i<endRow; i++)
49 {
50 for(j=startCol; j<endCol; j++)
51 {
52 temp = 0;
53 for(k=-halfKernel; k<=halfKernel; k++)
54 {
55 temp += subsref(imageIn,(i+k),j) * asubsref(kernel_1,k+halfKernel);
56 }
57 subsref(tempOut,i,j) = temp/kernelSum_1;
58 }
59 }
60
61 for(i=startRow; i<endRow; i++)
62 {
63 for(j=startCol; j<endCol; j++)
64 {
65 temp = 0;
66 for(k=-halfKernel; k<=halfKernel; k++)
67 {
68 temp += subsref(tempOut,i,j+k) * asubsref(kernel_2,k+halfKernel);
69 }
70 subsref(imageOut,i,j) = temp/(float)kernelSum_2;
71 }
72 }
73
74 fFreeHandle(tempOut);
75 iFreeHandle(kernel_1);
76 iFreeHandle(kernel_2);
77 return imageOut;
78
79}
diff --git a/SD-VBS/common/c/extra.h b/SD-VBS/common/c/extra.h
new file mode 100644
index 0000000..8c67b33
--- /dev/null
+++ b/SD-VBS/common/c/extra.h
@@ -0,0 +1,479 @@
1/**
2 * Copyright 2019 Sims Hill Osborne and 2020 Joshua Bakita
3 *
4 * This header provides facilities by which to separably run and time TACLeBench
5 * To use this for paired task timing, define PAIRED (pass CFLAGS=-DPAIRED to make)
6 **/
7#define _GNU_SOURCE
8#include <fcntl.h> // For O_CREAT and O_RDWR
9#include <sched.h> // For sched_yield()
10#include <semaphore.h> // For sem_{open, post, wait}()
11#include <stdio.h>
12#include <stdlib.h> // For exit()
13#include <string.h> // For strlen()
14#include <sys/mman.h> // For mlockall()
15#include <unistd.h> // For ftruncate()
16#include <time.h>
17
18// This is only visible if _GNU_SOURCE is defined, and that define does not
19// come along to places where this file is included. Address this by manually
20// forcing it into the global namespace.
21extern int sched_getcpu();
22
23// These constants correspond to the imx6q-sabredb platform
24#define LINE_SIZE 32
25#define L2_SIZE 16*2048*32
26
27#if __arm__
28#include <unistd.h>
29#include <sys/syscall.h>
30#endif
31
32#define LITMUS 1
33#define MC2 0
34#define MMDC_PROF 0
35
36#if LITMUS
37#include <litmus.h>
38#endif
39
40#if MMDC_PROF
41#include "/media/speedy/litmus/tools/mmdc/mmdc.h"
42#endif
43
44#if LITMUS
45#define SET_UP LOAD_PARAMS SETUP_LITMUS
46#else
47#define SET_UP LOAD_PARAMS
48#endif
49
50#if MMDC_PROF
51#define LOAD_PARAMS LOAD_PARAMS_ITRL SETUP_MMDC
52#else
53#define LOAD_PARAMS LOAD_PARAMS_ITRL
54#endif
55
56// Store state globally so that the job can be outside main()
57// Arrays use float as a comprimise between overflow and size
58// Paired arrays use long longs as precision is more important for those times
59#ifdef PAIRED
60long long *_rt_start_time;
61long long *_rt_end_time;
62#else
63float *_rt_exec_time;
64#endif
65#if MMDC_PERF
66float *_rt_mmdc_read;
67float *_rt_mmdc_write;
68#endif
69long _rt_jobs_complete;
70long _rt_max_jobs;
71int _rt_core;
72int _rt_will_output;
73struct timespec _rt_start, _rt_end;
74
75char *_rt_run_id;
76char *_rt_our_prog_name;
77char *_rt_other_prog_name;
78char *_rt_other_core;
79#define _RT_FILENAME_LEN 64
80#define _BILLION (1000*1000*1000)
81#ifdef PAIRED
82char *_rt_barrier;
83sem_t *_rt_first_sem, *_rt_second_sem;
84int _rt_lock_id;
85#endif
86
87static void _rt_load_params_itrl(int argc, char **argv) {
88#ifdef PAIRED
89 if (argc != 8) {
90 fprintf(stderr, "Usage: %s <name> <loops> <my core> <other core> <other name> <runID> <lockID>", argv[0]);
91 fprintf(stderr, " <name> string for logging. Name of this task.\n");
92 fprintf(stderr, " <loops> integer number of iterations. -1 for infinite.\n");
93 fprintf(stderr, " <my core> UNUSED. Core is now auto-detected.\n");
94 fprintf(stderr, " <other core> integer for logging. Core of paired task.\n");
95 fprintf(stderr, " <other name> string for logging. Name of paired task.\n");
96 fprintf(stderr, " <runID> string to append with .txt to yield output file name.\n");
97 fprintf(stderr, " <lockID> 1 to indicate this is pair member 1, otherwise pair member 2.\n");
98 exit(1);
99 }
100#else
101 if (argc != 6) {
102 fprintf(stderr, "Usage: %s <name> <loops> <my core> <runID> <save results?>\n", argv[0]);
103 fprintf(stderr, " <name> string for logging. Name of this task.\n");
104 fprintf(stderr, " <loops> integer number of iterations. -1 for infinite.\n");
105 fprintf(stderr, " <my core> UNUSED. Core is now auto-detected.\n");
106 fprintf(stderr, " <runID> string to append with .txt to yield output file name.\n");
107 fprintf(stderr, " <save results?> 1 to save results, 0 to discard.\n");
108 exit(1);
109 }
110#endif
111 _rt_our_prog_name = argv[1];
112 _rt_max_jobs = atol(argv[2]);
113 _rt_core = sched_getcpu();
114#ifdef PAIRED
115 _rt_other_core = argv[4];
116 _rt_other_prog_name = argv[5];
117 _rt_run_id = argv[6];
118 _rt_lock_id = atoi(argv[7]);
119 // The paired version doesn't support disabling output (legacy compatibility)
120 _rt_will_output = 1;
121#else
122 _rt_other_core = "none";
123 _rt_other_prog_name = "none";
124 _rt_run_id = argv[4];
125 _rt_will_output = atoi(argv[5]);
126#endif /* PAIRED */
127 if (_rt_max_jobs < 0 && _rt_will_output != 0) {
128 fprintf(stderr, "Infinite loops only supported when _rt_will_output is disabled!\n");
129 exit(1);
130 }
131 if (strlen(_rt_run_id) + 5 > _RT_FILENAME_LEN) {
132 fprintf(stderr, "Run ID is too large! Keep it to less than %d characters.\n", _RT_FILENAME_LEN);
133 exit(1);
134 }
135#ifdef PAIRED
136 _rt_start_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(long long));
137 _rt_end_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(long long));
138 if (!_rt_end_time || !_rt_start_time) {
139 perror("Unable to allocate buffers for execution times");
140 exit(1);
141 }
142 _rt_first_sem = sem_open("/_libextra_first_sem", O_CREAT, 644, 0);
143 _rt_second_sem = sem_open("/_libextra_second_sem", O_CREAT, 644, 0);
144 if (_rt_first_sem == SEM_FAILED || _rt_second_sem == SEM_FAILED) {
145 perror("Error while creating semaphores");
146 exit(1);
147 }
148 int barrier_file = shm_open("/_libextra_barrier", O_CREAT | O_RDWR, 644);
149 if (barrier_file == -1) {
150 perror("Error while creating shared memory for barrier synchronization");
151 exit(1);
152 }
153 if (ftruncate(barrier_file, 1) == -1) {
154 perror("Error while setting size of shared memory for barrier synchronization");
155 exit(1);
156 }
157 _rt_barrier = mmap(NULL, 1, PROT_WRITE, MAP_SHARED, barrier_file, 0);
158 if (_rt_barrier == MAP_FAILED) {
159 perror("Error while mapping shared memory for barrier synchronization");
160 exit(1);
161 }
162 *_rt_barrier = 0;
163#else
164 _rt_exec_time = calloc(_rt_max_jobs * _rt_will_output, sizeof(float));
165 if (!_rt_exec_time) {
166 perror("Unable to allocate buffer for execution times");
167 exit(1);
168 }
169#endif /* PAIRED */
170 _rt_jobs_complete = 0;
171 mlockall(MCL_CURRENT || MCL_FUTURE);
172}
173#define LOAD_PARAMS_ITRL _rt_load_params_itrl(argc, argv);
174
175#define SETUP_MMDC \
176 _rt_mmdc_read = calloc(_rt_max_jobs * _rt_will_output, sizeof(float));\
177 _rt_mmdc_write = calloc(_rt_max_jobs * _rt_will_output, sizeof(float));\
178 if (!_rt_mmdc_read || !_rt_mmdc_write) {\
179 perror("Unable to allocate buffer for MMDC data");\
180 exit(1);\
181 }\
182 MMDC_PROFILE_RES_t mmdc_res;\
183 memset(&mmdc_res, 0, sizeof(MMDC_PROFILE_RES_t));\
184 int fd = open("/dev/mem", O_RDWR, 0);\
185 if (fd < 0) {\
186 perror("Unable to open /dev/mem");\
187 exit(1);\
188 }\
189 pMMDC_t mmdc = mmap(NULL, 0x4000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, MMDC_P0_IPS_BASE_ADDR);\
190 if (mmdc == MAP_FAILED) {\
191 perror("Unable to map MMDC address space");\
192 exit(1);\
193 }\
194 mmdc->madpcr1 = axi_arm1;\
195 msync(&(mmdc->madpcr1),4,MS_SYNC);
196
197#define SETUP_LITMUS \
198 unsigned int wait = 0; \
199 if (be_migrate_to_domain(_rt_core) < 0) { \
200 perror("Unable to migrate to specified CPU"); \
201 exit(1); \
202 } \
203 struct rt_task rt_param; \
204 init_rt_task_param(&rt_param); \
205 /* Supposedly the next two parameters are irrelevant when reservations are enabled, but I'm leaving them anyway... */ \
206 rt_param.exec_cost = ms2ns(999); \
207 rt_param.period = ms2ns(1000); \
208 rt_param.priority = LITMUS_HIGHEST_PRIORITY; \
209 rt_param.cls = RT_CLASS_HARD; \
210 rt_param.release_policy = TASK_PERIODIC; \
211 rt_param.budget_policy = NO_ENFORCEMENT; \
212 rt_param.cpu = _rt_core; \
213 if (set_rt_task_param(gettid(), &rt_param) < 0) { \
214 perror("Unable to set real-time parameters"); \
215 exit(1); \
216 } \
217 if (init_litmus() != 0) { \
218 perror("init_litmus failed"); \
219 exit(1); \
220 } \
221 MC2_SETUP \
222 if (task_mode(LITMUS_RT_TASK) != 0) { \
223 perror("Unable to become real-time task"); \
224 exit(1); \
225 } \
226 if (wait && wait_for_ts_release() != 0) { \
227 perror("Unable to wait for taskset release"); \
228 exit(1); \
229 }
230
231#if MC2
232#define MC2_SETUP \
233
234 set_page_color(rt_param.cpu);
235#else
236#define MC2_SETUP
237#endif
238
239#define CLEANUP_LITMUS \
240 if (task_mode(BACKGROUND_TASK) != 0) { \
241 perror("Unable to become a real-time task"); \
242 exit(1); \
243 } \
244
245#if __arm__
246// On ARM, manually flush the cache
247#define FLUSH_CACHES \
248 volatile uint8_t buffer[L2_SIZE * 4]; \
249 for (uint32_t j = 0; j < 4; j++) \
250 for (uint32_t i = 0; i < L2_SIZE * 4; i += LINE_SIZE) \
251 buffer[i]++;
252#else
253// On x86 call the wbinvld instruction (it's in a kernel module due to it being ring-0)
254#define FLUSH_CACHES \
255 FILE *fp = fopen("/proc/wbinvd", "r");\
256 if (fp == NULL) {\
257 perror("Cache flush module interface cannot be opened");\
258 exit(1);\
259 }\
260 char dummy;\
261 if (fread(&dummy, 1, 1, fp) == 0) {\
262 perror("Unable to access cache flush module interface");\
263 exit(1);\
264 }\
265 fclose(fp);
266#endif
267
268// This semaphore-based synchronization is from Sims
269#define FIRST_UNLOCK \
270 if (_rt_lock_id == 1) {\
271 if (sem_post(_rt_second_sem) != 0) {\
272 perror("Unable to unlock second semaphore");\
273 exit(1);\
274 }\
275 } \
276 else {\
277 if (sem_post(_rt_first_sem) != 0) {\
278 perror("Unable to unlock first semaphore");\
279 exit(1);\
280 }\
281 } \
282
283#define FIRST_LOCK \
284 if (_rt_lock_id == 1) {\
285 if (sem_wait(_rt_first_sem) != 0) {\
286 perror("Unable to wait on first semaphore");\
287 exit(1);\
288 }\
289 }\
290 else {\
291 if (sem_wait(_rt_second_sem) != 0) {\
292 perror("Unable to wait on second semaphore");\
293 exit(1);\
294 }\
295 }
296
297// This ensures a very low difference between pair member start times
298#define BARRIER_SYNC \
299 if (__sync_bool_compare_and_swap(_rt_barrier, 0, 1)) {\
300 while (!__sync_bool_compare_and_swap(_rt_barrier, 0, 0)) {};\
301 }\
302 else {\
303 __sync_bool_compare_and_swap(_rt_barrier, 1, 0);\
304 }
305
306// Buffer timing result from a single job
307static void _rt_save_job_result() {
308 if (_rt_jobs_complete >= _rt_max_jobs) {
309 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);
310 exit(1);
311 }
312 if (_rt_jobs_complete > -1 && _rt_will_output) {
313#ifdef PAIRED
314 _rt_start_time[_rt_jobs_complete] = _rt_start.tv_sec;
315 _rt_start_time[_rt_jobs_complete] *= _BILLION;
316 _rt_start_time[_rt_jobs_complete] += _rt_start.tv_nsec;
317 _rt_end_time[_rt_jobs_complete] = _rt_end.tv_sec;
318 _rt_end_time[_rt_jobs_complete] *= _BILLION;
319 _rt_end_time[_rt_jobs_complete] += _rt_end.tv_nsec;
320#else
321 _rt_exec_time[_rt_jobs_complete] = _rt_end.tv_sec - _rt_start.tv_sec;
322 _rt_exec_time[_rt_jobs_complete] *= _BILLION;
323 _rt_exec_time[_rt_jobs_complete] += _rt_end.tv_nsec - _rt_start.tv_nsec;
324#endif /* PAIRED */
325#if MMDC_PROF
326 _rt_mmdc_read[_rt_jobs_complete] = mmdc_res.read_bytes;
327 _rt_mmdc_write[_rt_jobs_complete] = mmdc_res.write_bytes;
328#endif
329 }
330}
331
332// Save all buffered timing results to disk
333static void _rt_write_to_file() {
334 char fileName[_RT_FILENAME_LEN];
335 FILE *fp;
336 munlockall();
337 if (!_rt_will_output)
338 goto out;
339 strcpy(fileName, _rt_run_id);
340 strcat(fileName, ".txt");
341 fp = fopen(fileName, "a");
342 if (fp == NULL) {
343 perror("Unable to open output file");
344 exit(1);
345 }
346 // Baseline output uses a similar format with "none" for unused fields
347 for (int i = 0; i < _rt_jobs_complete; i++){
348 fprintf(fp, "%s %s %u %s %ld", _rt_our_prog_name, _rt_other_prog_name,
349 _rt_core, _rt_other_core, _rt_max_jobs);
350#ifdef PAIRED
351 // For unclear legacy reasons, paired tasks emit sec and ns separately
352 fprintf(fp, " %lld %lld %lld %lld",
353 _rt_start_time[i] / _BILLION, _rt_start_time[i] % _BILLION,
354 _rt_end_time[i] / _BILLION, _rt_end_time[i] % _BILLION);
355#else
356 fprintf(fp, " %.f", _rt_exec_time[i]);
357#endif /* PAIRED */
358 fprintf(fp, " %s %d %.f %.f\n", _rt_run_id, i,
359#if MMDC_PROF
360 _rt_mmdc_read[i], _rt_mmdc_write[i]);
361#else
362 0.0, 0.0);
363#endif /* MMDC_PROF */
364 }
365 fclose(fp);
366out:
367#if LITMUS
368 CLEANUP_LITMUS
369#endif /* LITMUS */
370#ifdef PAIRED
371 munmap(_rt_barrier, 1);
372 shm_unlink("/_libextra_barrier");
373 sem_unlink("/_libextra_first_sem");
374 sem_unlink("/_libextra_second_sem");
375 free(_rt_start_time);
376 free(_rt_end_time);
377#else
378 free(_rt_exec_time);
379#endif /* PAIRED */
380#if MMDC_PROF
381 free(_rt_mmdc_read);
382 free(_rt_mmdc_write);
383#endif /* MMDC_PROF */
384}
385
386// Start a job
387static void _rt_start_loop() {
388#if LITMUS
389 if (sleep_next_period() != 0) {
390 perror("Unable to sleep for next period");
391 }
392#else
393 sched_yield();
394#endif /* LITMUS */
395#ifdef PAIRED
396 FIRST_UNLOCK
397 FIRST_LOCK
398#endif /* PAIRED */
399 FLUSH_CACHES
400#ifdef PAIRED
401 BARRIER_SYNC
402#endif /* PAIRED */
403#if MMDC_PROF
404 /* This disables profiling, resets the counters, clears the overflow bit, and enables profiling */
405 start_mmdc_profiling(mmdc);
406#endif /* MMDC_PROF */
407 clock_gettime(CLOCK_MONOTONIC, &_rt_start);
408}
409
410// Complete a job
411static void _rt_stop_loop() {
412 clock_gettime(CLOCK_MONOTONIC, &_rt_end);
413#if MMDC_PROF
414 /* This freezes the profiling and makes results available */
415 pause_mmdc_profiling(mmdc);
416 get_mmdc_profiling_results(mmdc, &mmdc_res);
417#endif /* MMDC_PROF */
418 _rt_save_job_result();
419 _rt_jobs_complete++;
420}
421
422/****** New API ******
423 * Intended structure:
424 *
425 * |int main(int argc, char **argv) {
426 * | SET_UP
427 * | ...
428 * | for_each_job {
429 * | tacleInit();
430 * | tacleMain();
431 * | }
432 * | WRITE_TO_FILE
433 * |}
434 *
435 * The main() function must call its parameters argc and argv for SET_UP to be
436 * able to read them.
437 * Only SET_UP necessarily has to be in main().
438 *
439 * We use some niche C features, here's a quick explaination:
440 * 1. The && operator doesn't evaluate the right-hand side of the expression
441 * unless the left side evaluated to true. We use this to only execute
442 * _rt_start_loop() when the loop will actually run.
443 * 2. The comma operator executes the first expression and then throws away the
444 * result. We use this to call our void function from inside a comparison.
445 */
446#define for_each_job \
447 for (; _rt_jobs_complete < _rt_max_jobs && (_rt_start_loop(),1); \
448 _rt_stop_loop())
449
450/****** Legacy API ******
451 * Intended structure:
452 *
453 * |int main(int argc, char **argv) {
454 * | SET_UP
455 * | for (jobsComplete=0; jobsComplete<maxJobs; jobsComplete++){
456 * | START_LOOP
457 * | tacleInit();
458 * | tacleMain();
459 * | STOP_LOOP
460 * | }
461 * | WRITE_TO_FILE
462 * | tacleReturn
463 * |}
464 *
465 * The main() function must call its parameters argc and argv for SET_UP to be
466 * able to read them.
467 */
468static int jobsComplete = 0;
469#define START_LOOP _rt_start_loop();
470#define STOP_LOOP _rt_stop_loop();
471#define WRITE_TO_FILE _rt_write_to_file();
472#define maxJobs _rt_max_jobs
473// Has been part of STOP_LOOP for quite some time
474#define SAVE_RESULTS \
475 #warning "The SAVE_RESULTS macro is deprecated and will soon be removed!";
476// Unclear if SLEEP is used anywhere.
477#define SLEEP \
478 #warning "The SLEEP macro is deprecated and may be removed!" \
479 nanosleep((const struct timespec[]){{0, 1000000}}, NULL);
diff --git a/SD-VBS/common/c/fCopy.c b/SD-VBS/common/c/fCopy.c
new file mode 100644
index 0000000..f9b844d
--- /dev/null
+++ b/SD-VBS/common/c/fCopy.c
@@ -0,0 +1,24 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fCopy(F2D* in, F2D* out)
8{
9 int i, j;
10 //F2D* out;
11 int rows, cols;
12
13 rows = in->height;
14 cols = in->width;
15
16 //out = fMallocHandle(rows, cols);
17
18 for(i=0; i<rows; i++) {
19 for(j=0; j<cols; j++) {
20 subsref(out,i,j) = subsref(in,i,j);
21 }
22 }
23 return out;
24}
diff --git a/SD-VBS/common/c/fDeepCopy.c b/SD-VBS/common/c/fDeepCopy.c
new file mode 100644
index 0000000..332926e
--- /dev/null
+++ b/SD-VBS/common/c/fDeepCopy.c
@@ -0,0 +1,24 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fDeepCopy(F2D* in)
8{
9 int i, j;
10 F2D* out;
11 int rows, cols;
12
13 rows = in->height;
14 cols = in->width;
15
16 out = fMallocHandle(rows, cols);
17
18 for(i=0; i<rows; i++) {
19 for(j=0; j<cols; j++) {
20 subsref(out,i,j) = subsref(in,i,j);
21 }
22 }
23 return out;
24}
diff --git a/SD-VBS/common/c/fDeepCopyRange.c b/SD-VBS/common/c/fDeepCopyRange.c
new file mode 100644
index 0000000..e9a2b29
--- /dev/null
+++ b/SD-VBS/common/c/fDeepCopyRange.c
@@ -0,0 +1,24 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fDeepCopyRange(F2D* in, int startRow, int numberRows, int startCol, int numberCols)
8{
9 int i, j, k;
10 F2D *out;
11 int rows, cols;
12
13 rows = numberRows + startRow;
14 cols = numberCols + startCol;
15 out = fMallocHandle(numberRows, numberCols);
16
17 k = 0;
18 for(i=startRow; i<rows; i++)
19 for(j=startCol; j<cols; j++)
20 asubsref(out,k++) = subsref(in,i,j);
21
22 return out;
23
24}
diff --git a/SD-VBS/common/c/fDivide.c b/SD-VBS/common/c/fDivide.c
new file mode 100644
index 0000000..7d7f90a
--- /dev/null
+++ b/SD-VBS/common/c/fDivide.c
@@ -0,0 +1,23 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fDivide(F2D* a, float b)
8{
9 F2D *c;
10 int i, j, rows, cols;
11
12 rows = a->height;
13 cols = a->width;
14
15 c = fMallocHandle(rows, cols);
16
17 for(i=0; i<(rows*cols); i++)
18 {
19 asubsref(c,i) = asubsref(a,i) / b;
20 }
21
22 return c;
23}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fFind3(F2D* in)
8{
9 int r, k, y, x, i, j;
10 F2D *points;
11
12 y = in->height;
13 x = in->width;
14
15 r = 0;
16 for(i=0; i<y; i++)
17 {
18 for(j=0; j<x; j++)
19 {
20 if(subsref(in,i,j) != 0)
21 r++;
22 }
23 }
24
25 points = fSetArray(r, 3, 0);
26
27 k = 0;
28 for(j=0; j<x; j++)
29 {
30 for(i=0; i<y; i++)
31 {
32 if( subsref(in,i,j) != 0)
33 {
34 subsref(points,k,0) = j*1.0;
35 subsref(points,k,1) = i*1.0;
36 subsref(points,k,2) = subsref(in,i,j);
37 k++;
38 }
39 }
40 }
41
42 return points;
43}
44
45
46
diff --git a/SD-VBS/common/c/fFreeHandle.c b/SD-VBS/common/c/fFreeHandle.c
new file mode 100644
index 0000000..7616583
--- /dev/null
+++ b/SD-VBS/common/c/fFreeHandle.c
@@ -0,0 +1,16 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9void fFreeHandle(F2D* out)
10{
11 if(out != NULL)
12 free(out);
13
14 return;
15}
16
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fHorzcat(F2D* a, F2D* b)
8{
9 F2D* out_, *out, *c;
10 int rows=0, cols=0, i, j, k, c_1, c_2, r_3, c_3;
11 int r_1;
12
13 r_1 = a->height;
14 c_1 = a->width;
15 cols += c_1;
16 c_2 = b->width;
17 cols += c_2;
18 rows = r_1;
19
20 out = fMallocHandle(rows, cols);
21
22 for(i=0; i<rows; i++)
23 {
24 k = 0;
25 for(j=0; j<c_1; j++)
26 {
27 subsref(out,i,k) = subsref(a,i,j);
28 k++;
29 }
30 for(j=0; j<c_2; j++)
31 {
32 subsref(out,i,k) = subsref(b,i,j);
33 k++;
34 }
35 }
36
37 return out;
38}
39
40
diff --git a/SD-VBS/common/c/fMallocHandle.c b/SD-VBS/common/c/fMallocHandle.c
new file mode 100644
index 0000000..6ce917c
--- /dev/null
+++ b/SD-VBS/common/c/fMallocHandle.c
@@ -0,0 +1,18 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9F2D* fMallocHandle(int rows, int cols)
10{
11 int i, j;
12 F2D* out;
13
14 out = (F2D*)malloc(sizeof(F2D) + sizeof(float)*rows*cols);
15 out->height = rows;
16 out->width = cols;
17 return out;
18}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fMdivide(F2D* a, F2D* b)
8{
9 F2D *c;
10 int i, j, rows, cols;
11
12 rows = a->height;
13 cols = a->width;
14
15 if(rows != b->height || cols != b->width)
16 {
17 printf("fMDivide Mismatch = \nrows: %d\t%d\ncols: %d\t%d\n", rows, b->height, cols, b->width);
18 return NULL;
19 }
20
21 c = fMallocHandle(rows, cols);
22
23 for(i=0; i<(rows*cols); i++)
24 asubsref(c,i) = asubsref(a,i) / asubsref(b,i);
25
26 return c;
27}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fMinus(F2D* a, F2D* b)
8{
9 F2D *c;
10 int i, j, rows, cols;
11
12 rows = a->height;
13 cols = a->width;
14
15 c = fMallocHandle(rows, cols);
16
17 for(i=0; i<(rows*cols); i++)
18 asubsref(c,i) = asubsref(a,i) - asubsref(b,i);
19
20 return c;
21}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fMtimes(F2D* a, F2D* b)
8{
9 F2D *out;
10 int m, p, p1, n, i, j, k;
11 float temp;
12
13 m = a->height;
14 p = a->width;
15
16 p1 = b->height;
17 n = b->width;
18
19 out = fMallocHandle(m,n);
20
21 for(i=0; i<m; i++)
22 {
23 for(j=0; j<n; j++)
24 {
25 temp = 0;
26 for(k=0; k<p; k++)
27 {
28 temp += subsref(b,k,j) * subsref(a,i,k);
29 }
30 subsref(out,i,j) = temp;
31 }
32 }
33
34 return out;
35}
36
37
38
diff --git a/SD-VBS/common/c/fPlus.c b/SD-VBS/common/c/fPlus.c
new file mode 100644
index 0000000..98900eb
--- /dev/null
+++ b/SD-VBS/common/c/fPlus.c
@@ -0,0 +1,21 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fPlus(F2D* a, F2D* b)
8{
9 F2D *c;
10 int i, j, rows, cols;
11
12 rows = a->height;
13 cols = a->width;
14
15 c = fMallocHandle(rows, cols);
16
17 for(i=0; i<(rows*cols); i++) {
18 asubsref(c,i) = asubsref(a,i) + asubsref(b,i);
19 }
20 return c;
21}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9void fResetArray(F2D *out, int rows, int cols, float val)
10{
11 int i, j;
12
13 for(i=0; i<rows; i++) {
14 for(j=0; j<cols; j++) {
15 subsref(out,i,j) = val;
16 }
17 }
18
19}
diff --git a/SD-VBS/common/c/fResetHandle.c b/SD-VBS/common/c/fResetHandle.c
new file mode 100644
index 0000000..da1a8f2
--- /dev/null
+++ b/SD-VBS/common/c/fResetHandle.c
@@ -0,0 +1,19 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9F2D* fResetHandle(F2D* out, int rows, int cols)
10{
11 int i, j;
12 //F2D* out;
13
14 //out = (F2D*)malloc(sizeof(F2D) + sizeof(float)*rows*cols);
15 out->height = rows;
16 out->width = cols;
17 //printf("fmalloc happened\n");
18 return out;
19}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fReshape(F2D* in, int rows, int cols)
8{
9 F2D *out;
10 int i, j, k;
11 int r, c;
12
13 r = in->height;
14 c = in->width;
15
16 out = fMallocHandle(rows, cols);
17
18 k = 0;
19 for(i=0; i<c; i++) {
20 for(j=0; j<r; j++) {
21 asubsref(out,k++) = subsref(in,j,i);
22 }
23 }
24 return out;
25}
26
27
diff --git a/SD-VBS/common/c/fResortIndices.c b/SD-VBS/common/c/fResortIndices.c
new file mode 100644
index 0000000..d4d1cda
--- /dev/null
+++ b/SD-VBS/common/c/fResortIndices.c
@@ -0,0 +1,77 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* fResortIndices(F2D* input, int dim, F2D* in, I2D* ind)
8{
9 int rows, cols;
10 //F2D *in;
11 int i, j, k;
12 //I2D *ind;
13
14 rows = input->height;
15 cols = input->width;
16
17 in = fCopy(input, in);
18 //ind = iMallocHandle(rows,cols);
19
20 for(i=0; i<cols; i++) {
21 for(j=0; j<rows; j++) {
22 subsref(ind,j,i) = 0;
23 }
24 }
25
26if(dim == 1)
27{
28 for(k=0; k<rows; k++)
29 {
30 for(i=0; i<cols; i++)
31 {
32 float localMax = subsref(in,k,i);
33 int localIndex = i;
34 subsref(ind,k,i) = i;
35 for(j=0; j<cols; j++)
36 {
37 if(localMax < subsref(in,k,j))
38 {
39 subsref(ind,k,i) = j;
40 localMax = subsref(in,k,j);
41 localIndex = j;
42 }
43 }
44 subsref(in,k,localIndex) = 0;
45 }
46 }
47
48 //fFreeHandle(in);
49 return ind;
50}
51
52 for(k=0; k<cols; k++)
53 {
54 for(i=0; i<rows; i++)
55 {
56 float localMax = subsref(in,i,k);
57 int localIndex = i;
58 subsref(ind,i,k) = i;
59 for(j=0; j<rows; j++)
60 {
61 if(localMax < subsref(in,j,k))
62 {
63 subsref(ind,i,k) = j;
64 localMax = subsref(in,j,k);
65 localIndex = j;
66 }
67 }
68 subsref(in,localIndex,k) = 0;
69 }
70 }
71
72 //fFreeHandle(in);
73 return ind;
74}
75
76
77
diff --git a/SD-VBS/common/c/fSelfCheck.c b/SD-VBS/common/c/fSelfCheck.c
new file mode 100644
index 0000000..fe146b5
--- /dev/null
+++ b/SD-VBS/common/c/fSelfCheck.c
@@ -0,0 +1,59 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7int fSelfCheck(F2D* in1, char* path, float tol)
8{
9 int r1, c1, ret=1;
10 float *buffer;
11 FILE* fd;
12 int count=0, i, j;
13 char file[256];
14
15 r1 = in1->height;
16 c1 = in1->width;
17
18 buffer = (float*)malloc(sizeof(float)*r1*c1);
19
20 sprintf(file, "%s", path);
21 fd = fopen(file, "r");
22
23 if(fd == NULL)
24 {
25 printf("Error: Expected file not opened %s\n", file);
26 return -1;
27 }
28
29 while(!feof(fd))
30 {
31 fscanf(fd, "%f", &buffer[count]);
32 count++;
33 }
34 count--;
35
36 if(count != (r1*c1))
37 {
38 printf("Checking error: dimensions mismatch. Expected = %d, Observed = %d \n", count, (r1*c1));
39 return -1;
40 }
41
42 for(i=0; i<r1*c1; i++)
43 {
44 float inVal = asubsref(in1,i);
45
46 if( (inVal-buffer[i])>tol || (buffer[i]-inVal)>tol )
47 {
48 printf("Mismatch %d: (%f, %f)\n", i, buffer[i], inVal);
49 return -1;
50 }
51 }
52
53 fclose(fd);
54 printf("Verification\t\t- Successful\n");
55 free(buffer);
56 return ret;
57}
58
59
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9F2D* fSetArray(int rows, int cols, float val)
10{
11 int i, j;
12 F2D *out;
13 out = fMallocHandle(rows, cols);
14
15 for(i=0; i<rows; i++) {
16 for(j=0; j<cols; j++) {
17 subsref(out,i,j) = val;
18 }
19 }
20 return out;
21
22}
diff --git a/SD-VBS/common/c/fSort.c b/SD-VBS/common/c/fSort.c
new file mode 100644
index 0000000..8aef21f
--- /dev/null
+++ b/SD-VBS/common/c/fSort.c
@@ -0,0 +1,43 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fSort(F2D* in, int dim)
8{
9 F2D *sorted;
10 int rows, cols, i, j, k, temp;
11
12 rows = in->height;
13 cols = in->width;
14
15 sorted = fDeepCopy(in);
16
17 for(k=0; k<cols; k++)
18 {
19 for(i=0; i<rows; i++)
20 {
21 for(j=i+1; j<rows; j++)
22 {
23 float sik, sjk;
24 sik = subsref(sorted,i,k);
25 sjk = subsref(sorted,j,k);
26
27 if(sik < sjk)
28 {
29 temp = sjk;
30 sjk = sik;
31 sik = temp;
32 }
33 }
34 }
35 }
36
37 return sorted;
38
39}
40
41
42
43
diff --git a/SD-VBS/common/c/fSortIndices.c b/SD-VBS/common/c/fSortIndices.c
new file mode 100644
index 0000000..753eda5
--- /dev/null
+++ b/SD-VBS/common/c/fSortIndices.c
@@ -0,0 +1,77 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* fSortIndices(F2D* input, int dim)
8{
9 int rows, cols;
10 F2D *in;
11 int i, j, k;
12 I2D *ind;
13
14 rows = input->height;
15 cols = input->width;
16
17 in = fDeepCopy(input);
18 ind = iMallocHandle(rows,cols);
19
20 for(i=0; i<cols; i++) {
21 for(j=0; j<rows; j++) {
22 subsref(ind,j,i) = 0;
23 }
24 }
25
26if(dim == 1)
27{
28 for(k=0; k<rows; k++)
29 {
30 for(i=0; i<cols; i++)
31 {
32 float localMax = subsref(in,k,i);
33 int localIndex = i;
34 subsref(ind,k,i) = i;
35 for(j=0; j<cols; j++)
36 {
37 if(localMax < subsref(in,k,j))
38 {
39 subsref(ind,k,i) = j;
40 localMax = subsref(in,k,j);
41 localIndex = j;
42 }
43 }
44 subsref(in,k,localIndex) = 0;
45 }
46 }
47
48 fFreeHandle(in);
49 return ind;
50}
51
52 for(k=0; k<cols; k++)
53 {
54 for(i=0; i<rows; i++)
55 {
56 float localMax = subsref(in,i,k);
57 int localIndex = i;
58 subsref(ind,i,k) = i;
59 for(j=0; j<rows; j++)
60 {
61 if(localMax < subsref(in,j,k))
62 {
63 subsref(ind,i,k) = j;
64 localMax = subsref(in,j,k);
65 localIndex = j;
66 }
67 }
68 subsref(in,localIndex,k) = 0;
69 }
70 }
71
72 fFreeHandle(in);
73 return ind;
74}
75
76
77
diff --git a/SD-VBS/common/c/fSum.c b/SD-VBS/common/c/fSum.c
new file mode 100644
index 0000000..cd9483a
--- /dev/null
+++ b/SD-VBS/common/c/fSum.c
@@ -0,0 +1,55 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fSum(F2D* inMat)
8{
9 F2D *outMat;
10 int rows, cols, i, j, k;
11 float temp;
12 int newRow, newCols;
13 int Rcols;
14
15 rows = inMat->height;
16 cols = inMat->width;
17
18 if(cols == 1 || rows == 1)
19 Rcols = 1;
20 else
21 Rcols = cols;
22
23 outMat = fSetArray(1,Rcols,0);
24
25 if( cols == 1)
26 {
27 temp = 0;
28 for( j=0; j<rows; j++)
29 temp = temp + subsref(inMat,j,0);
30 asubsref(outMat,0) = temp;
31 }
32 else if( rows == 1)
33 {
34 temp = 0;
35 for( j=0; j<cols; j++)
36 temp = temp + asubsref(inMat,j);
37 asubsref(outMat,0) = temp;
38 }
39 else
40 {
41 for( i=0; i<cols; i++)
42 {
43 temp = 0;
44 for( j=0; j<rows; j++)
45 temp = temp + subsref(inMat,j,i);
46 asubsref(outMat,i) = temp;
47 }
48 }
49
50 return outMat;
51}
52
53
54
55
diff --git a/SD-VBS/common/c/fSum2.c b/SD-VBS/common/c/fSum2.c
new file mode 100644
index 0000000..8078249
--- /dev/null
+++ b/SD-VBS/common/c/fSum2.c
@@ -0,0 +1,56 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fSum2(F2D* inMat, int dir)
8{
9 F2D *outMat;
10 int rows, cols, i, j, k;
11 float temp;
12 int newRow, newCols;
13
14 rows = inMat->height;
15 cols = inMat->width;
16
17 if(dir == 1)
18 {
19 newRow = 1;
20 newCols = cols;
21 }
22 else
23 {
24 newRow = rows;
25 newCols = 1;
26 }
27
28 outMat = fSetArray(newRow,newCols,0);
29
30 if(dir == 1)
31 {
32 for (i=0; i<cols; i++)
33 {
34 temp = 0;
35 for( j=0; j<rows; j++)
36 temp = temp + subsref(inMat,j,i);
37 asubsref(outMat,i) = temp;
38 }
39 }
40 else
41 {
42 for( i=0; i<rows; i++)
43 {
44 temp = 0;
45 for( j=0; j<cols; j++)
46 temp = temp + subsref(inMat,i,j);
47 subsref(outMat,i,0) = temp;
48 }
49 }
50
51 return outMat;
52}
53
54
55
56
diff --git a/SD-VBS/common/c/fTimes.c b/SD-VBS/common/c/fTimes.c
new file mode 100644
index 0000000..bfab489
--- /dev/null
+++ b/SD-VBS/common/c/fTimes.c
@@ -0,0 +1,21 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fTimes(F2D* a, F2D* b)
8{
9 F2D *c;
10 int i, j, rows, cols;
11
12 rows = a->height;
13 cols = a->width;
14
15 c = fMallocHandle(rows, cols);
16
17 for(i=0; i<(rows*cols); i++)
18 asubsref(c,i) = asubsref(a,i) * asubsref(b,i);
19
20 return c;
21}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fTranspose(F2D* a)
8{
9 F2D *out;
10 int m, p, p1, n, i, j, k;
11 float temp;
12
13 m = a->height;
14 n = a->width;
15
16 out = fMallocHandle(n, m);
17 k = 0;
18 for(i=0; i<n; i++)
19 {
20 for(j=0; j<m; j++)
21 asubsref(out,k++) = subsref(a,j,i);
22 }
23
24 return out;
25}
26
27
diff --git a/SD-VBS/common/c/fWriteMatrix.c b/SD-VBS/common/c/fWriteMatrix.c
new file mode 100644
index 0000000..822af92
--- /dev/null
+++ b/SD-VBS/common/c/fWriteMatrix.c
@@ -0,0 +1,34 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9void fWriteMatrix(F2D* input, char* inpath)
10{
11 FILE* fp;
12 char im[100];
13 int rows,cols, i, j;
14
15 sprintf(im, "%s/expected_C.txt", inpath);
16 fp = fopen(im, "w");
17
18 rows = input->height;
19 cols = input->width;
20
21 for(i=0; i<rows; i++)
22 {
23 for(j=0; j<cols; j++)
24 {
25 fprintf(fp, "%f\t", subsref(input, i, j));
26 }
27 fprintf(fp, "\n");
28 }
29
30 fclose(fp);
31}
32
33
34
diff --git a/SD-VBS/common/c/ffConv2.c b/SD-VBS/common/c/ffConv2.c
new file mode 100644
index 0000000..450c5d2
--- /dev/null
+++ b/SD-VBS/common/c/ffConv2.c
@@ -0,0 +1,47 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* ffConv2(F2D* a, F2D* b)
8{
9 F2D *c, *out;
10 int ma, na, mb, nb, ci, cj, i, j, m, n, ri, mm, nn;
11 int r_index, c_index;
12
13 ma = a->height;
14 na = a->width;
15
16 mb = b->height;
17 nb = b->width;
18
19 ci = ma;
20 cj = na;
21
22 c = fSetArray(ci, cj, 0);
23
24 r_index = mb/2;
25 c_index = nb/2;
26
27 for(i=0; i<ma; i++)
28 {
29 for(j=0; j<na; j++)
30 {
31 for(m=0; m<mb; m++)
32 {
33 mm = mb-1-m;
34 for(n=0; n<nb; n++)
35 {
36 nn = nb-1-n;
37 ri = i+m-r_index;
38 ci = j+n-c_index;
39 if(ri >=0 && ri < ma && ci >= 0 && ci < na)
40 subsref(c,i,j) += subsref(a,ri,ci) * subsref(b,mm,nn);
41 }
42 }
43 }
44 }
45
46 return c;
47}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* ffConv2_dY(F2D* a, F2D* b)
8{
9 F2D *c, *out;
10 int ma, na, mb, nb, ci, cj, i, j, m, n;
11 int r_index, c_index;
12
13 ma = a->height;
14 na = a->width;
15
16 mb = b->height;
17 nb = b->width;
18
19 r_index = ceil((mb + 1.0)/2.0);
20 c_index = ceil((nb + 1.0)/2.0);
21
22 ci = ma+mb-1;
23 cj = na+nb-1;
24
25 c = fSetArray(ci, cj, 0);
26
27 for(i=0; i<cj; i++)
28 {
29 for(j=0; j<ci; j++)
30 {
31 for(m=0; m<na; m++)
32 {
33 for(n=0; n<ma; n++)
34 {
35 if( (i-m)>=0 && (j-n)>=0 && (i-m)<nb && (j-n)<mb )
36 subsref(c,j,i) += subsref(a,n,m) * subsref(b,j-n,i-m);
37 }
38 }
39 }
40 }
41
42 out = fMallocHandle(ma, na);
43 for(i=0; i<ma; i++)
44 {
45 for(j=0; j<na; j++)
46 {
47 subsref(out,i,j) = subsref(c,(i+r_index-1),(j+c_index-1));
48 }
49 }
50
51 return out;
52}
diff --git a/SD-VBS/common/c/ffDivide.c b/SD-VBS/common/c/ffDivide.c
new file mode 100644
index 0000000..0607d0b
--- /dev/null
+++ b/SD-VBS/common/c/ffDivide.c
@@ -0,0 +1,21 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* ffDivide(F2D* a, F2D* b)
8{
9 F2D *c;
10 int i, j, rows, cols;
11
12 rows = a->height;
13 cols = a->width;
14
15 c = fMallocHandle(rows, cols);
16
17 for(i=0; i<(rows*cols); i++)
18 asubsref(c,i) = asubsref(a,i) / asubsref(b,i);
19
20 return c;
21}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* ffTimes(F2D* a, float b)
8{
9 F2D *c;
10 int i, j, rows, cols;
11
12 rows = a->height;
13 cols = a->width;
14
15 c = fMallocHandle(rows, cols);
16
17 for(i=0; i<(rows*cols); i++)
18 asubsref(c,i) = asubsref(a,i) * b;
19
20 return c;
21}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* ffVertcat(F2D* matrix1, F2D* matrix2)
8{
9 F2D *outMatrix;
10 int row1, col1, row2, col2, i, j, k;
11
12 row1 = matrix1->height;
13 col1 = matrix1->width;
14
15 row2 = matrix2->height;
16 col2 = matrix2->width;
17
18 outMatrix = fMallocHandle(row1+row2, col1);
19
20 for( i=0; i<col1; i++)
21 {
22 for (j=0; j<row1; j++)
23 {
24 subsref(outMatrix,j,i) = subsref(matrix1,j,i);
25 }
26 for( k=0; k<row2; k++)
27 {
28 subsref(outMatrix,(k+row1),i) = subsref(matrix2,k,i);
29 }
30 }
31
32 return outMatrix;
33
34}
35
36
diff --git a/SD-VBS/common/c/ffiConv2.c b/SD-VBS/common/c/ffiConv2.c
new file mode 100644
index 0000000..83c7466
--- /dev/null
+++ b/SD-VBS/common/c/ffiConv2.c
@@ -0,0 +1,54 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* ffiConv2(F2D* a, I2D* b)
8{
9 F2D *c;
10 F2D *out;
11 int ma, na, mb, nb, ci, cj, i, j, m, n;
12 int r_index, c_index;
13
14 ma = a->height;
15 na = a->width;
16
17 mb = b->height;
18 nb = b->width;
19
20 r_index = ceil((mb + 1.0)/2.0);
21 c_index = ceil((nb + 1.0)/2.0);
22
23 ci = ma+mb-1;
24 cj = na+nb-1;
25
26 c = fSetArray(ci, cj, 0);
27
28 for(i=0; i<ci; i++)
29 {
30 for(j=0; j<cj; j++)
31 {
32 for(m=0; m<ma; m++)
33 {
34 for(n=0; n<na; n++)
35 {
36 if( (i-m)>=0 && (j-n)>=0 && (i-m)<mb && (j-n)<nb )
37 subsref(c,i,j) += subsref(a,m,n) * subsref(b,(i-m),(j-n));
38 }
39 }
40
41 }
42 }
43
44 out = fMallocHandle(ma, na);
45 for(i=0; i<ma; i++)
46 {
47 for(j=0; j<na; j++)
48 {
49 subsref(out,i,j) = subsref(c,(i+r_index-1),(j+c_index-1));
50 }
51 }
52
53 return out;
54}
diff --git a/SD-VBS/common/c/fiConv2.c b/SD-VBS/common/c/fiConv2.c
new file mode 100644
index 0000000..87fc4ec
--- /dev/null
+++ b/SD-VBS/common/c/fiConv2.c
@@ -0,0 +1,54 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fiConv2(I2D* a, F2D* b)
8{
9 F2D *c;
10 F2D *out;
11 int ma, na, mb, nb, ci, cj, i, j, m, n;
12 int r_index, c_index;
13
14 ma = a->height;
15 na = a->width;
16
17 mb = b->height;
18 nb = b->width;
19
20 r_index = ceil((mb + 1.0)/2.0);
21 c_index = ceil((nb + 1.0)/2.0);
22
23 ci = ma+mb-1;
24 cj = na+nb-1;
25
26 c = fSetArray(ci, cj, 0);
27
28 for(i=0; i<ci; i++)
29 {
30 for(j=0; j<cj; j++)
31 {
32 for(m=0; m<ma; m++)
33 {
34 for(n=0; n<na; n++)
35 {
36 if( (i-m)>=0 && (j-n)>=0 && (i-m)<mb && (j-n)<nb )
37 subsref(c,i,j) += subsref(a,m,n) * subsref(b,(i-m),(j-n));
38 }
39 }
40
41 }
42 }
43
44 out = fMallocHandle(ma, na);
45 for(i=0; i<ma; i++)
46 {
47 for(j=0; j<na; j++)
48 {
49 subsref(out,i,j) = subsref(c,(i+r_index-1),(j+c_index-1));
50 }
51 }
52
53 return out;
54}
diff --git a/SD-VBS/common/c/fiCopy.c b/SD-VBS/common/c/fiCopy.c
new file mode 100644
index 0000000..27c164c
--- /dev/null
+++ b/SD-VBS/common/c/fiCopy.c
@@ -0,0 +1,23 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7void fiCopy(F2D* out, I2D* in)
8{
9 int i, j;
10 //F2D *out;
11 int rows, cols;
12
13 rows = in->height;
14 cols = in->width;
15
16 //out = fMallocHandle(rows, cols);
17
18 for(i=0; i<rows; i++)
19 for(j=0; j<cols; j++)
20 subsref(out,i,j) = subsref(in,i,j) + 0.0;
21
22 //return out;
23}
diff --git a/SD-VBS/common/c/fiDeepCopy.c b/SD-VBS/common/c/fiDeepCopy.c
new file mode 100644
index 0000000..7058945
--- /dev/null
+++ b/SD-VBS/common/c/fiDeepCopy.c
@@ -0,0 +1,23 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* fiDeepCopy(I2D* in)
8{
9 int i, j;
10 F2D *out;
11 int rows, cols;
12
13 rows = in->height;
14 cols = in->width;
15
16 out = fMallocHandle(rows, cols);
17
18 for(i=0; i<rows; i++)
19 for(j=0; j<cols; j++)
20 subsref(out,i,j) = subsref(in,i,j) + 0.0;
21
22 return out;
23}
diff --git a/SD-VBS/common/c/horzcat.c b/SD-VBS/common/c/horzcat.c
new file mode 100644
index 0000000..2041396
--- /dev/null
+++ b/SD-VBS/common/c/horzcat.c
@@ -0,0 +1,48 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* horzcat(F2D* a, F2D* b, F2D* c)
8{
9 F2D *out;
10 int rows=0, cols=0, i, j, k, c_1, c_2, r_3, c_3;
11
12 c_1 = a->width;
13 cols += c_1;
14
15 c_2 = b->width;
16 cols += c_2;
17
18 r_3 = c->height;
19 c_3 = c->width;
20 cols += c_3;
21 rows = r_3;
22
23 out = fMallocHandle(rows, cols);
24
25 for(i=0; i<rows; i++)
26 {
27 k = 0;
28 for(j=0; j<c_1; j++)
29 {
30 subsref(out,i,k) = subsref(a,i,j);
31 k++;
32 }
33 for(j=0; j<c_2; j++)
34 {
35 subsref(out,i,k) = subsref(b,i,j);
36 k++;
37 }
38 for(j=0; j<c_3; j++)
39 {
40 subsref(out,i,k) = subsref(c,i,j);
41 k++;
42 }
43 }
44
45 return out;
46}
47
48
diff --git a/SD-VBS/common/c/iCheck.c b/SD-VBS/common/c/iCheck.c
new file mode 100644
index 0000000..707d04a
--- /dev/null
+++ b/SD-VBS/common/c/iCheck.c
@@ -0,0 +1,18 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7int iCheck(I2D* in1, I2D* in2){
8 if(in1->width != in2 -> width || in1->height != in2->height) return 0;
9 for(int i = 0; i < in1->width;i++){
10 for(int j = 0; j < in1->height;j++){
11 if(subsref(in1,i,j) != subsref(in2,i,j)) return 0;
12 }
13 }
14 return 1;
15
16}
17
18
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* iDeepCopy(I2D* in)
8{
9 int i, j;
10 I2D* out;
11 int rows, cols;
12
13 rows = in->height;
14 cols = in->width;
15
16 out = iMallocHandle(rows, cols);
17
18 for(i=0; i<rows; i++)
19 for(j=0; j<cols; j++)
20 subsref(out,i,j) = subsref(in,i,j);
21
22 return out;
23}
diff --git a/SD-VBS/common/c/iDeepCopyRange.c b/SD-VBS/common/c/iDeepCopyRange.c
new file mode 100644
index 0000000..f3fa6e3
--- /dev/null
+++ b/SD-VBS/common/c/iDeepCopyRange.c
@@ -0,0 +1,23 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* iDeepCopyRange(I2D* in, int startRow, int numberRows, int startCol, int numberCols)
8{
9 int i, j, k;
10 I2D *out;
11 int rows, cols;
12
13 rows = numberRows + startRow;
14 cols = numberCols + startCol;
15 out = iMallocHandle(numberRows, numberCols);
16
17 k = 0;
18 for(i=startRow; i<rows; i++)
19 for(j=startCol; j<cols; j++)
20 asubsref(out,k++) = subsref(in,i,j);
21
22 return out;
23}
diff --git a/SD-VBS/common/c/iFreeHandle.c b/SD-VBS/common/c/iFreeHandle.c
new file mode 100644
index 0000000..c45db21
--- /dev/null
+++ b/SD-VBS/common/c/iFreeHandle.c
@@ -0,0 +1,16 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9void iFreeHandle(I2D* out)
10{
11 if(out != NULL)
12 free(out);
13
14 return;
15}
16
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* iHorzcat(I2D* a, I2D* b)
8{
9 I2D *out, *c;
10 int rows=0, cols=0, i, j, k, c_1, c_2, r_3, c_3;
11 int r_1;
12
13 r_1 = a->height;
14 c_1 = a->width;
15 cols += c_1;
16
17 c_2 = b->width;
18 cols += c_2;
19 rows = r_1;
20
21 out = iMallocHandle(rows, cols);
22
23 for(i=0; i<rows; i++)
24 {
25 k = 0;
26 for(j=0; j<c_1; j++)
27 {
28 subsref(out,i,k) = subsref(a,i,j);
29 k++;
30 }
31 for(j=0; j<c_2; j++)
32 {
33 subsref(out,i,k) = subsref(b,i,j);
34 k++;
35 }
36 }
37
38 return out;
39}
40
41
diff --git a/SD-VBS/common/c/iMallocHandle.c b/SD-VBS/common/c/iMallocHandle.c
new file mode 100644
index 0000000..afebf86
--- /dev/null
+++ b/SD-VBS/common/c/iMallocHandle.c
@@ -0,0 +1,20 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9I2D* iMallocHandle(int rows, int cols)
10{
11 int i, j;
12 I2D* out;
13
14 out = (I2D*)malloc(sizeof(I2D) + sizeof(int)*rows*cols);
15 out->height = rows;
16 out->width = cols;
17 //printf("imalloc happened\n");
18 return out;
19}
20
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* iMinus(I2D* a, I2D* b)
8{
9 I2D *c;
10 int i, j, rows, cols;
11
12 rows = a->height;
13 cols = a->width;
14
15 c = iMallocHandle(rows, cols);
16
17 for(i=0; i<(rows*cols); i++)
18 asubsref(c,i) = asubsref(a,i) - asubsref(b,i);
19
20 return c;
21}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9void iResetArray(I2D* out, int rows, int cols, int val)
10{
11 int i, j;
12 //I2D *out;
13 //out = iMallocHandle(rows, cols);
14
15 for(i=0; i<rows; i++) {
16 for(j=0; j<cols; j++) {
17 subsref(out,i,j) = val;
18 }
19 }
20 //return out;
21
22}
diff --git a/SD-VBS/common/c/iReshape.c b/SD-VBS/common/c/iReshape.c
new file mode 100644
index 0000000..511a8ed
--- /dev/null
+++ b/SD-VBS/common/c/iReshape.c
@@ -0,0 +1,25 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* iReshape(I2D* in, int rows, int cols)
8{
9 I2D *out;
10 int i, j, k;
11 int r, c;
12
13 r = in->height;
14 c = in->width;
15
16 out = iMallocHandle(rows, cols);
17
18 k = 0;
19 for(i=0; i<c; i++)
20 for(j=0; j<r; j++)
21 asubsref(out,k++) = subsref(in,j,i);
22
23 return out;
24}
25
diff --git a/SD-VBS/common/c/iSetArray.c b/SD-VBS/common/c/iSetArray.c
new file mode 100644
index 0000000..205bd13
--- /dev/null
+++ b/SD-VBS/common/c/iSetArray.c
@@ -0,0 +1,22 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9I2D* iSetArray(int rows, int cols, int val)
10{
11 int i, j;
12 I2D *out;
13 out = iMallocHandle(rows, cols);
14
15 for(i=0; i<rows; i++) {
16 for(j=0; j<cols; j++) {
17 subsref(out,i,j) = val;
18 }
19 }
20 return out;
21
22}
diff --git a/SD-VBS/common/c/iSort.c b/SD-VBS/common/c/iSort.c
new file mode 100644
index 0000000..5dbdfb9
--- /dev/null
+++ b/SD-VBS/common/c/iSort.c
@@ -0,0 +1,43 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* iSort(I2D* in, int dim)
8{
9 I2D *sorted;
10 int rows, cols, i, j, k, temp;
11
12 rows = in->height;
13 cols = in->width;
14
15 sorted = iDeepCopy(in);
16
17 for(k=0; k<cols; k++)
18 {
19 for(i=0; i<rows; i++)
20 {
21 for(j=i+1; j<rows; j++)
22 {
23 int sik, sjk;
24 sik = subsref(sorted,i,k);
25 sjk = subsref(sorted,j,k);
26
27 if(sik < sjk)
28 {
29 temp = sjk;
30 sjk = sik;
31 sik = temp;
32 }
33 }
34 }
35 }
36
37 return sorted;
38
39}
40
41
42
43
diff --git a/SD-VBS/common/c/iSortIndices.c b/SD-VBS/common/c/iSortIndices.c
new file mode 100644
index 0000000..5939c32
--- /dev/null
+++ b/SD-VBS/common/c/iSortIndices.c
@@ -0,0 +1,47 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* iSortIndices(I2D* in, int dim)
8{
9 I2D *sorted;
10 int rows, cols, i, j, k, temp;
11 I2D *ind;
12
13 rows = in->height;
14 cols = in->width;
15
16 sorted = iDeepCopy(in);
17 ind = iMallocHandle(rows, cols);
18
19 for(i=0; i<cols; i++)
20 for(j=0; j<rows; j++)
21 subsref(ind,j,i) = 0;
22
23 for(k=0; k<cols; k++)
24 {
25 for(i=0; i<rows; i++)
26 {
27 int localMax = subsref(in,i,k);
28 int localIndex = i;
29 subsref(ind,i,k) = i;
30 for(j=0; j<rows; j++)
31 {
32 if(localMax < subsref(in,j,k))
33 {
34 subsref(ind,i,k) = j;
35 localMax = subsref(in,j,k);
36 localIndex = j;
37 }
38 }
39 subsref(in,localIndex,k) = 0;
40 }
41 }
42
43 return ind;
44}
45
46
47
diff --git a/SD-VBS/common/c/iTimes.c b/SD-VBS/common/c/iTimes.c
new file mode 100644
index 0000000..479c39d
--- /dev/null
+++ b/SD-VBS/common/c/iTimes.c
@@ -0,0 +1,22 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* iTimes(I2D* a, I2D* b)
8{
9 I2D *c;
10 int i, j, rows, cols;
11
12 rows = a->height;
13 cols = a->width;
14
15 c = iMallocHandle(rows, cols);
16
17 for(i=0; i<(rows*cols); i++)
18 asubsref(c,i) = asubsref(a,i) * asubsref(b,i);
19
20 return c;
21}
22
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* iTranspose(I2D* a)
8{
9 I2D *out;
10 int m, p, p1, n, i, j, k;
11 int temp;
12
13 m = a->height;
14 n = a->width;
15
16 out = iMallocHandle(n, m);
17 k = 0;
18 for(i=0; i<n; i++)
19 {
20 for(j=0; j<m; j++)
21 asubsref(out,k++) = subsref(a,j,i);
22 }
23
24 return out;
25}
26
diff --git a/SD-VBS/common/c/iVertcat.c b/SD-VBS/common/c/iVertcat.c
new file mode 100644
index 0000000..2c2d857
--- /dev/null
+++ b/SD-VBS/common/c/iVertcat.c
@@ -0,0 +1,34 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* iVertcat(I2D* matrix1, I2D* matrix2)
8{
9 I2D *outMatrix;
10 int row1, col1, row2, col2, i, j, k;
11
12 row1 = matrix1->height;
13 col1 = matrix1->width;
14
15 row2 = matrix2->height;
16 col2 = matrix2->width;
17
18 outMatrix = iMallocHandle(row1+row2, col1);
19
20 for( i=0; i<col1; i++)
21 {
22 for (j=0; j<row1; j++)
23 {
24 subsref(outMatrix,j,i) = subsref(matrix1,j,i);
25 }
26 for( k=0; k<row2; k++)
27 {
28 subsref(outMatrix,(k+row1),i) = subsref(matrix2,k,i);
29 }
30 }
31 return outMatrix;
32}
33
34
diff --git a/SD-VBS/common/c/ifDeepCopy.c b/SD-VBS/common/c/ifDeepCopy.c
new file mode 100644
index 0000000..a899340
--- /dev/null
+++ b/SD-VBS/common/c/ifDeepCopy.c
@@ -0,0 +1,25 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <math.h>
6#include "sdvbs_common.h"
7
8I2D* ifDeepCopy(F2D* in)
9{
10 int i, j;
11 I2D *out;
12 int rows, cols;
13
14 rows = in->height;
15 cols = in->width;
16
17 out = iMallocHandle(rows, cols);
18
19 for(i=0; i<rows; i++)
20 for(j=0; j<cols; j++)
21 subsref(out,i,j) = (int)(subsref(in,i,j));
22
23 return out;
24
25}
diff --git a/SD-VBS/common/c/ifMtimes.c b/SD-VBS/common/c/ifMtimes.c
new file mode 100644
index 0000000..15b3631
--- /dev/null
+++ b/SD-VBS/common/c/ifMtimes.c
@@ -0,0 +1,38 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* ifMtimes(I2D* a, F2D* b)
8{
9 F2D *out;
10 int m, p, p1, n, i, j, k;
11 float temp;
12
13 m = a->height;
14 p = a->width;
15
16 p1 = b->height;
17 n = b->width;
18
19 out = fMallocHandle(m,n);
20
21 for(i=0; i<m; i++)
22 {
23 for(j=0; j<n; j++)
24 {
25 temp = 0;
26 for(k=0; k<p; k++)
27 {
28 temp += subsref(b,k,j) * subsref(a,i,k);
29 }
30 subsref(out,i,j) = temp;
31 }
32 }
33
34 return out;
35}
36
37
38
diff --git a/SD-VBS/common/c/iiConv2.c b/SD-VBS/common/c/iiConv2.c
new file mode 100644
index 0000000..436b206
--- /dev/null
+++ b/SD-VBS/common/c/iiConv2.c
@@ -0,0 +1,55 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* iiConv2(I2D* a, I2D* b)
8{
9 I2D *c;
10 I2D *out;
11 int ma, na, mb, nb, ci, cj, i, j, m, n;
12 int r_index, c_index;
13
14 ma = a->height;
15 na = a->width;
16
17 mb = b->height;
18 nb = b->width;
19
20 r_index = ceil((mb + 1.0)/2.0);
21 c_index = ceil((nb + 1.0)/2.0);
22
23 ci = ma+mb-1;
24 cj = na+nb-1;
25
26 c = iSetArray(ci, cj, 0);
27
28 for(i=0; i<ci; i++)
29 {
30 for(j=0; j<cj; j++)
31 {
32 for(m=0; m<ma; m++)
33 {
34 for(n=0; n<na; n++)
35 {
36 if( (i-m)>=0 && (j-n)>=0 && (i-m)<mb && (j-n)<nb )
37 subsref(c,i,j) += subsref(a,m,n) * subsref(b,i-m,j-n);
38 }
39 }
40
41 }
42 }
43
44 out = iMallocHandle(ma, na);
45 for(i=0; i<ma; i++)
46 {
47 for(j=0; j<na; j++)
48 {
49 subsref(out,i,j) = subsref(c,(i+r_index-1),(j+c_index-1));
50 }
51 }
52
53
54 return out;
55}
diff --git a/SD-VBS/common/c/imageBlur.c b/SD-VBS/common/c/imageBlur.c
new file mode 100644
index 0000000..8e3ad92
--- /dev/null
+++ b/SD-VBS/common/c/imageBlur.c
@@ -0,0 +1,67 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* imageBlur(I2D* imageIn)
8{
9 int rows, cols;
10 F2D *imageOut, *tempOut;
11 float temp;
12 I2D *kernel;
13 int k, kernelSize, startCol, endCol, halfKernel, startRow, endRow, i, j, kernelSum;
14
15 rows = imageIn->height;
16 cols = imageIn->width;
17
18 imageOut = fSetArray(rows, cols, 0);
19 tempOut = fSetArray(rows, cols, 0);
20 kernel = iMallocHandle(1, 5);
21
22 asubsref(kernel,0) = 1;
23 asubsref(kernel,1) = 4;
24 asubsref(kernel,2) = 6;
25 asubsref(kernel,3) = 4;
26 asubsref(kernel,4) = 1;
27 kernelSize = 5;
28 kernelSum = 16;
29
30 startCol = 2;
31 endCol = cols - 2;
32 halfKernel = 2;
33
34 startRow = 2;
35 endRow = rows - 2;
36
37 for(i=startRow; i<endRow; i++){
38 for(j=startCol; j<endCol; j++)
39 {
40 temp = 0;
41 for(k=-halfKernel; k<=halfKernel; k++)
42 {
43 temp += subsref(imageIn,i,j+k) * asubsref(kernel,k+halfKernel);
44 }
45 subsref(tempOut,i,j) = temp/kernelSum;
46 }
47 }
48
49 for(i=startRow; i<endRow; i++)
50 {
51 for(j=startCol; j<endCol; j++)
52 {
53 temp = 0;
54 for(k=-halfKernel; k<=halfKernel; k++)
55 {
56 temp += subsref(tempOut,(i+k),j) * asubsref(kernel,k+halfKernel);
57 }
58 subsref(imageOut,i,j) = temp/kernelSum;
59 }
60 }
61
62 fFreeHandle(tempOut);
63 iFreeHandle(kernel);
64 return imageOut;
65}
66
67
diff --git a/SD-VBS/common/c/imageReblur.c b/SD-VBS/common/c/imageReblur.c
new file mode 100644
index 0000000..1755f67
--- /dev/null
+++ b/SD-VBS/common/c/imageReblur.c
@@ -0,0 +1,67 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* imageReblur(I2D* imageIn, F2D* imageOut, F2D* tempOut, I2D* kernel)
8{
9 int rows, cols;
10 //F2D *imageOut, *tempOut;
11 float temp;
12 //I2D *kernel;
13 int k, kernelSize, startCol, endCol, halfKernel, startRow, endRow, i, j, kernelSum;
14
15 rows = imageIn->height;
16 cols = imageIn->width;
17
18 fResetArray(imageOut, rows, cols, 0);
19 fResetArray(tempOut, rows, cols, 0);
20 //kernel = iMallocHandle(1, 5);
21
22 asubsref(kernel,0) = 1;
23 asubsref(kernel,1) = 4;
24 asubsref(kernel,2) = 6;
25 asubsref(kernel,3) = 4;
26 asubsref(kernel,4) = 1;
27 kernelSize = 5;
28 kernelSum = 16;
29
30 startCol = 2;
31 endCol = cols - 2;
32 halfKernel = 2;
33
34 startRow = 2;
35 endRow = rows - 2;
36
37 for(i=startRow; i<endRow; i++){
38 for(j=startCol; j<endCol; j++)
39 {
40 temp = 0;
41 for(k=-halfKernel; k<=halfKernel; k++)
42 {
43 temp += subsref(imageIn,i,j+k) * asubsref(kernel,k+halfKernel);
44 }
45 subsref(tempOut,i,j) = temp/kernelSum;
46 }
47 }
48
49 for(i=startRow; i<endRow; i++)
50 {
51 for(j=startCol; j<endCol; j++)
52 {
53 temp = 0;
54 for(k=-halfKernel; k<=halfKernel; k++)
55 {
56 temp += subsref(tempOut,(i+k),j) * asubsref(kernel,k+halfKernel);
57 }
58 subsref(imageOut,i,j) = temp/kernelSum;
59 }
60 }
61
62 //fFreeHandle(tempOut);
63 //iFreeHandle(kernel);
64 return imageOut;
65}
66
67
diff --git a/SD-VBS/common/c/imageResize.c b/SD-VBS/common/c/imageResize.c
new file mode 100644
index 0000000..72c0881
--- /dev/null
+++ b/SD-VBS/common/c/imageResize.c
@@ -0,0 +1,78 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* imageResize(F2D* imageIn)
8{
9 int m, k, rows, cols;
10 F2D *imageOut;
11 I2D *kernel;
12 float tempVal;
13 int kernelSize, startCol, endCol, halfKernel, startRow, endRow, i, j, kernelSum;
14 int outputRows, outputCols;
15 F2D *temp;
16
17 rows = imageIn->height;
18 cols = imageIn->width;
19
20 // level 1 is the base image.
21
22 outputRows = floor((rows+1)/2);
23 outputCols = floor((cols+1)/2);
24
25 temp = fSetArray(rows, outputCols, 0);
26 imageOut = fSetArray(outputRows, outputCols, 0);
27 kernel = iMallocHandle(1, 5);
28
29 asubsref(kernel,0) = 1;
30 asubsref(kernel,1) = 4;
31 asubsref(kernel,2) = 6;
32 asubsref(kernel,3) = 4;
33 asubsref(kernel,4) = 1;
34 kernelSize = 5;
35 kernelSum = 16;
36
37 startCol = 2;
38 endCol = cols - 2;
39 halfKernel = 2;
40
41 startRow = 2;
42 endRow = rows - 2;
43
44 for(i=startRow; i<endRow; i++)
45 {
46 m = 0;
47 for(j=startCol; j<endCol; j+=2)
48 {
49 tempVal = 0;
50 for(k=-halfKernel; k<=halfKernel; k++)
51 {
52 tempVal += subsref(imageIn,i,j+k) * asubsref(kernel,k+halfKernel);
53 }
54 subsref(temp,i,m) = tempVal/kernelSum;
55 m = m+1;
56 }
57 }
58
59 m = 0;
60 for(i=startRow; i<endRow; i+=2)
61 {
62 for(j=0; j<outputCols; j++)
63 {
64 tempVal = 0;
65 for(k=-halfKernel; k<=halfKernel; k++)
66 {
67 tempVal += subsref(temp,(i+k),j) * asubsref(kernel,k+halfKernel);
68 }
69 subsref(imageOut,m,j) = (tempVal/kernelSum);
70 }
71 m = m+1;
72 }
73
74 fFreeHandle(temp);
75 iFreeHandle(kernel);
76 return imageOut;
77
78}
diff --git a/SD-VBS/common/c/isMinus.c b/SD-VBS/common/c/isMinus.c
new file mode 100644
index 0000000..da0eb89
--- /dev/null
+++ b/SD-VBS/common/c/isMinus.c
@@ -0,0 +1,23 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9I2D* isMinus(I2D* a, int b)
10{
11 I2D *c;
12 int i, j, rows, cols;
13
14 rows = a->height;
15 cols = a->width;
16
17 c = iMallocHandle(rows, cols);
18
19 for(i=0; i<(rows*cols); i++)
20 asubsref(c,i) = asubsref(a,i) - b;
21
22 return c;
23}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7I2D* isPlus(I2D* a, int b)
8{
9 I2D *c;
10 int i, j, rows, cols;
11
12 rows = a->height;
13 cols = a->width;
14
15 c = iMallocHandle(rows, cols);
16
17 for(i=0; i<(rows*cols); i++)
18 asubsref(c,i) = asubsref(a,i) + b;
19
20 return c;
21}
22
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5/** C File **/
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <assert.h>
10#include <math.h>
11#include "timingUtils.h"
12#include "sdvbs_common.h"
13
14unsigned int * photonEndTiming()
15{
16 static unsigned int *array;
17 array = (unsigned int*)malloc(sizeof(unsigned int)*2);
18
19 magic_timing_begin(array[0], array[1]);
20 return array;
21}
22
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5/** C File **/
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <assert.h>
10#include <math.h>
11#include "timingUtils.h"
12#include "sdvbs_common.h"
13
14void photonPrintTiming(unsigned int * elapsed)
15{
16 if(elapsed[1] == 0)
17 printf("Cycles elapsed\t\t- %u\n\n", elapsed[0]);
18 else
19 printf("Cycles elapsed\t\t- %u%u\n\n", elapsed[1], elapsed[0]);
20}
21
22/** 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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5/** C File **/
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <assert.h>
10#include <math.h>
11#include "timingUtils.h"
12#include "sdvbs_common.h"
13
14unsigned int * photonReportTiming(unsigned int* startCycles,unsigned int* endCycles)
15{
16
17 static unsigned int *elapsed;
18 elapsed = (unsigned int*)malloc(sizeof(unsigned int)*2);
19 unsigned long long start = (((unsigned long long)0x0) | startCycles[0]) << 32 | startCycles[1];
20 unsigned long long end = (((unsigned long long)0x0) | endCycles[0]) << 32 | endCycles[1];
21 unsigned long long diff = end - start;
22 elapsed[0] = (unsigned int)(diff >> 32);
23 elapsed[1] = (unsigned int)(diff & 0xffffffff);
24 return elapsed;
25
26}
27
28/** 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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5/** C File **/
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <assert.h>
10#include <math.h>
11#include "timingUtils.h"
12#include "sdvbs_common.h"
13
14unsigned int* photonStartTiming()
15{
16 static unsigned int *array;
17
18 array = (unsigned int*)malloc(sizeof(unsigned int)*2);
19 magic_timing_begin(array[0], array[1]);
20 return array;
21}
22
23/** 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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* randWrapper(int m, int n)
8{
9 F2D *out;
10 float seed;
11 int i,j;
12
13 out = fSetArray(m, n, 0);
14 seed = 0.9;
15
16 for(i=0; i<m; i++)
17 {
18 for(j=0; j<n; j++)
19 {
20 if(i<j)
21 subsref(out,i,j) = seed * ((i+1.0)/(j+1.0));
22 else
23 subsref(out,i,j) = seed * ((j+1.0)/(i+1.0));
24 }
25 }
26
27 return out;
28}
29
30
diff --git a/SD-VBS/common/c/randnWrapper.c b/SD-VBS/common/c/randnWrapper.c
new file mode 100644
index 0000000..4701b0e
--- /dev/null
+++ b/SD-VBS/common/c/randnWrapper.c
@@ -0,0 +1,40 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6#include <math.h>
7F2D* randnWrapper(int m, int n)
8{
9 F2D *out;
10 float seed;
11 int i,j;
12
13 out = fSetArray(m, n, 0);
14 seed = 0.9;
15
16 for(i=0; i<m; i++)
17 {
18 for(j=0; j<n; j++)
19 {
20 if(i<j)
21 subsref(out,i,j) = seed * ((i+1.0)/(j+1.0));
22 else
23 subsref(out,i,j) = seed * ((j+1.0)/(i+1.0));
24 }
25 }
26
27 for(i=0; i<m ;i++)
28 {
29 for(j=0; j<n; j++)
30 {
31 float w;
32 w = subsref(out,i,j);
33 w = ((-2.0 * log(w))/w);
34 subsref(out,i,j) = w;
35 }
36 }
37
38 return out;
39}
40
diff --git a/SD-VBS/common/c/readFile.c b/SD-VBS/common/c/readFile.c
new file mode 100644
index 0000000..2c16abd
--- /dev/null
+++ b/SD-VBS/common/c/readFile.c
@@ -0,0 +1,42 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include "sdvbs_common.h"
6
7F2D* readFile(unsigned char* fileName)
8{
9 FILE* fp;
10 F2D *fill;
11 float temp;
12 int rows, cols;
13 int i, j;
14
15 fp = fopen(fileName, "r");
16 if(fp == NULL)
17 {
18 printf("Error in file %s\n", fileName);
19 return NULL;
20 }
21
22 fscanf(fp, "%d", &cols);
23 fscanf(fp, "%d", &rows);
24
25 fill = fSetArray(rows, cols, 0);
26
27 for(i=0; i<rows; i++)
28 {
29 for(j=0; j<cols; j++)
30 {
31 fscanf(fp, "%f", &(subsref(fill,i,j)) );
32 }
33 }
34
35 fclose(fp);
36 return fill;
37}
38
39
40
41
42
diff --git a/SD-VBS/common/c/readImage.c b/SD-VBS/common/c/readImage.c
new file mode 100644
index 0000000..a4dd990
--- /dev/null
+++ b/SD-VBS/common/c/readImage.c
@@ -0,0 +1,112 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9I2D* readImage(const char* pathName)
10{
11 // Reading BMP image
12 char signature[2];
13 int file_size;
14 short int reserved1;
15 short int reserved2;
16 int loc_of_bitmap;
17
18 int size_of_infoheader;
19 int width;
20 int height;
21 short int number_of_planes;
22 short int bits_per_pixel;
23
24 int compression_method;
25 int bytes_of_bitmap;
26 int hori_reso;
27 int vert_reso;
28 int no_of_colors;
29 int no_of_imp_colors;
30
31 int nI,nJ;
32 int pixSize;
33
34 unsigned char tempb,tempg,tempr,tempjunk[12];
35 int ta;
36 I2D* srcImage;
37
38 FILE *input;
39 input = fopen(pathName,"rb");
40 if(input == NULL)
41 {
42 perror("File pointer error");
43 return NULL;
44 }
45 else
46 {
47 //start of header information
48 fread(&signature,sizeof(signature),1,input);
49 fread(&file_size,sizeof(file_size),1,input);
50 fread(&reserved1,sizeof(reserved1),1,input);
51 fread(&reserved2,sizeof(reserved2),1,input);
52 fread(&loc_of_bitmap,sizeof(loc_of_bitmap),1,input);
53
54 fread(&size_of_infoheader,sizeof(size_of_infoheader),1,input);
55 fread(&width,sizeof(width),1,input); // Reads the width of the image
56 fread(&height,sizeof(height),1,input); // Reads the height of the image
57 fread(&number_of_planes,sizeof(number_of_planes),1,input);
58 fread(&bits_per_pixel,sizeof(bits_per_pixel),1,input);
59 fread(&compression_method,sizeof(compression_method),1,input);
60 fread(&bytes_of_bitmap,sizeof(bytes_of_bitmap),1,input);
61
62 fread(&hori_reso,sizeof(hori_reso),1,input);
63 fread(&vert_reso,sizeof(vert_reso),1,input);
64 fread(&no_of_colors,sizeof(no_of_colors),1,input);
65 fread(&no_of_imp_colors,sizeof(no_of_imp_colors),1,input);
66 //end of header information
67
68 srcImage = iMallocHandle(height, width);
69
70 // Conditions to check whether the BMP is interleaved and handling few exceptions
71 if(srcImage->height <= 0 || srcImage->width <= 0 || signature[0] != 'B' || signature[1] != 'M' || ( bits_per_pixel !=24 && bits_per_pixel !=8 ) )
72 {
73 printf("ERROR in BMP read: The input file is not in standard BMP format");
74 return NULL;
75 }
76 fseek(input,loc_of_bitmap,SEEK_SET);
77
78 if (bits_per_pixel == 8)
79 {
80 for(nI = (height - 1); nI >= 0 ; nI--)
81 {
82 for(nJ = 0;nJ < width; nJ++)
83 {
84 fread(&tempg,sizeof(unsigned char),1,input);
85 subsref(srcImage,nI,nJ) = (int)tempg;
86 }
87 }
88 }
89 else if (bits_per_pixel == 24)
90 {
91 for(nI = (height - 1); nI >= 0 ; nI--)
92 {
93 for(nJ = 0;nJ < width; nJ++)
94 {
95 fread(&tempb,sizeof(unsigned char),1,input);
96 fread(&tempg,sizeof(unsigned char),1,input);
97 fread(&tempr,sizeof(unsigned char),1,input);
98 ta = (3*tempr + 6*tempg + tempb)/10;
99 ta = tempg;
100 subsref(srcImage,nI,nJ) = (int)ta;
101 }
102 }
103 }
104 else
105 {
106 return NULL;
107 }
108
109 fclose(input);
110 return srcImage;
111 }
112}
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#ifndef _SDVBS_COMMON_
6#define _SDVBS_COMMON_
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <math.h>
11
12typedef struct
13{
14 int width;
15 int height;
16 int data[];
17}I2D;
18
19typedef struct
20{
21 int width;
22 int height;
23 unsigned int data[];
24}UI2D;
25
26typedef struct
27{
28 int width;
29 int height;
30 float data[];
31}F2D;
32
33#define subsref(a,i,j) a->data[(i) * a->width + (j)]
34#define asubsref(a,i) a->data[i]
35#define arrayref(a,i) a[i]
36
37/** Image read and write **/
38I2D* readImage(const char* pathName);;
39F2D* readFile(unsigned char* fileName);
40
41
42/** Memory allocation functions **/
43I2D* iMallocHandle(int rows, int cols);
44F2D* fMallocHandle(int rows, int cols);
45F2D* fResetHandle(F2D* out, int rows, int cols);
46UI2D* uiMallocHandle(int rows, int cols);
47
48void iFreeHandle(I2D* out);
49void fFreeHandle(F2D* out);
50void uiFreeHandle(UI2D* out);
51
52/** Memory copy/set function **/
53I2D* iSetArray(int rows, int cols, int val);
54void iResetArray(I2D* out, int rows, int cols, int val);
55F2D* fSetArray(int rows, int cols, float val);
56void fResetArray(F2D* out, int rows, int cols, float val);
57I2D* iDeepCopy(I2D* in);
58F2D* fDeepCopy(F2D* in);
59F2D* fCopy(F2D* in, F2D* out);
60I2D* iDeepCopyRange(I2D* in, int startRow, int numberRows, int startCol, int numberCols);
61F2D* fDeepCopyRange(F2D* in, int startRow, int numberRows, int startCol, int numberCols);
62F2D* fiDeepCopy(I2D* in);
63void fiCopy(F2D* out, I2D* in);
64I2D* ifDeepCopy(F2D* in);
65
66
67/** Matrix operations - concatenation, reshape **/
68F2D* ffVertcat(F2D* matrix1, F2D* matrix2);
69I2D* iVertcat(I2D* matrix1, I2D* matrix2);
70F2D* fHorzcat(F2D* a, F2D* b);
71I2D* iHorzcat(I2D* a, I2D* b);
72F2D* horzcat(F2D* a, F2D* b, F2D* c);
73F2D* fTranspose(F2D* a);
74I2D* iTranspose(I2D* a);
75F2D* fReshape(F2D* in, int rows, int cols);
76I2D* iReshape(I2D* in, int rows, int cols);
77
78
79/** Binary Operations **/
80F2D* fDivide(F2D* a, float b);
81F2D* fMdivide(F2D* a, F2D* b);
82F2D* ffDivide(F2D* a, F2D* b);
83F2D* ffTimes(F2D* a, float b);
84F2D* fTimes(F2D* a, F2D* b);
85I2D* iTimes(I2D* a, I2D* b);
86F2D* fMtimes(F2D* a, F2D* b);
87F2D* ifMtimes(I2D* a, F2D* b);
88F2D* fMinus(F2D* a, F2D* b);
89I2D* iMinus(I2D* a, I2D* b);
90I2D* isMinus(I2D* a, int b);
91F2D* fPlus(F2D* a, F2D* b);
92I2D* isPlus(I2D* a, int b);
93
94
95/** Filtering operations **/
96F2D* calcSobel_dX(F2D* imageIn);
97F2D* calcSobel_dY(F2D* imageIn);
98F2D* ffConv2(F2D* a, F2D* b);
99F2D* fiConv2(I2D* a, F2D* b);
100F2D* ffConv2_dY(F2D* a, F2D* b);
101F2D* ffiConv2(F2D* a, I2D* b);
102I2D* iiConv2(I2D* a, I2D* b);
103
104
105/** Image Transformations - resize, integration etc **/
106F2D* imageResize(F2D* imageIn);
107F2D* imageBlur(I2D* imageIn);
108F2D* imageReblur(I2D* imageIn, F2D* imageOut, F2D* tempOut, I2D* kernel);
109
110
111/** Support functions **/
112F2D* fFind3(F2D* in);
113F2D* fSum2(F2D* inMat, int dir);
114F2D* fSum(F2D* inMat);
115I2D* iSort(I2D* in, int dim);
116F2D* fSort(F2D* in, int dim);
117I2D* iSortIndices(I2D* in, int dim);
118I2D* fSortIndices(F2D* input, int dim);
119I2D* fResortIndices(F2D* input, int dim, F2D* in, I2D* ind);
120F2D* randnWrapper(int m, int n);
121F2D* randWrapper(int m, int n);
122
123
124/** Checking functions **/
125int selfCheck(I2D* in1, char* path, int tol);
126int fSelfCheck(F2D* in1, char* path, float tol);
127void writeMatrix(I2D* input, char* inpath);
128void fWriteMatrix(F2D* input, char* inpath);
129int iCheck(I2D* in1, I2D* in2);
130
131/** Timing functions **/
132unsigned int* photonEndTiming();
133unsigned int* photonStartTiming();
134unsigned int* photonReportTiming(unsigned int* startCycles,unsigned int* endCycles);
135void photonPrintTiming(unsigned int * elapsed);
136
137
138#endif
139
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <math.h>
8#include "sdvbs_common.h"
9
10int selfCheck(I2D* in1, char* path, int tol)
11{
12 int r1, c1, ret=1;
13 FILE* fd;
14 int count=0, *buffer, i, j;
15 char file[100];
16 int* data = in1->data;
17
18 r1 = in1->height;
19 c1 = in1->width;
20
21 buffer = (int*)malloc(sizeof(int)*r1*c1);
22
23 sprintf(file, "%s", path);
24 fd = fopen(file, "r");
25 if(fd == NULL)
26 {
27 printf("Error: Expected file not opened \n");
28 return -1;
29 }
30
31 while(!feof(fd))
32 {
33 fscanf(fd, "%d", &buffer[count]);
34 count++;
35 }
36 count--;
37
38 if(count < (r1*c1))
39 {
40 printf("Checking error: dimensions mismatch. Expected = %d, Observed = %d \n", count, (r1*c1));
41 return -1;
42 }
43
44 for(i=0; i<r1*c1; i++)
45 {
46 if((abs(data[i])-abs(buffer[i]))>tol || (abs(buffer[i])-abs(data[i]))>tol)
47 {
48 printf("Checking error: Values mismtach at %d element\n", i);
49 printf("Expected value = %d, observed = %d\n", buffer[i], data[i] );
50 return -1;
51 }
52 }
53
54 fclose(fd);
55 free(buffer);
56 printf("Verification\t\t- Successful\n");
57 return ret;
58}
59
60
61
62
63
64
65
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 @@
1#ifdef GCC
2#define magic_timing_begin(cycleLo, cycleHi) {\
3 asm volatile( "rdtsc": "=a" (cycleLo), "=d" (cycleHi)); \
4}\
5
6#define magic_timing_end(cycleLo, cycleHi) {\
7 unsigned tempCycleLo, tempCycleHi; \
8 asm volatile( "rdtsc": "=a" (tempCycleLo), "=d" (tempCycleHi)); \
9 cycleLo = tempCycleLo-cycleLo;\
10 cycleHi = tempCycleHi - cycleHi;\
11}\
12
13
14
15#define magic_timing_report(cycleLo, cycleHi) {\
16 printf("Timing report: %d %d\n", cycleLo, cycleHi); \
17}\
18
19
20
21
22#endif
23
24#ifdef METRO
25
26#define magic_timing_begin(cycleLo, cycleHi) {\
27 asm volatile( "mfsr $8, CYCLE_LO\n\t" \
28 "mfsr $9, CYCLE_HI\n\t" \
29 "addu %0, $8, $0\n\t" \
30 "addu %1, $9, $0\n\t" \
31 :"=r" (cycleLo), "=r" (cycleHi) \
32 : \
33 :"$8", "$9"\
34 );\
35}
36
37#define magic_timing_end(cycleLo, cycleHi) {\
38 asm volatile( \
39 "mfsr $8, CYCLE_LO\n\t" \
40 "mfsr $9, CYCLE_HI\n\t" \
41 "subu %0, $8, %0\n\t" \
42 "subu %1, $9, %1\n\t" \
43 :"=r" (cycleLo), "=r" (cycleHi) \
44 : \
45 :"$8", "$9"\
46 ); \
47}
48
49#define magic_timing_report(cycleLo, cycleHi) {\
50 asm volatile( "addu $8, %0, $0\n\t" \
51 "mtsr PASS $8\n\t" \
52 "mtsr PASS $9\n\t" \
53 : \
54 :"r" (cycleLo), "r" (cycleHi) \
55 : "$8", "$9" \
56 );\
57}
58
59//#define metro_magic_timing_report(cycleLo, cycleHi) {\
60// asm volatile( "nop\n\t");\
61//}
62
63#endif
64
65#ifdef BTL
66
67#include "/u/kvs/raw/rawlib/archlib/include/raw.h"
68
69#define magic_timing_begin(cycleLo, cycleHi) {\
70 raw_magic_timing_report_begin();\
71}
72
73#define magic_timing_end(cycleLo, cycleHi) {\
74 raw_magic_timing_report_end(); \
75}
76
77#define magic_timing_report(cycleLo, cycleHi) {\
78 raw_magic_timing_report_print(); \
79}
80
81
82//
83//void metro_magic_timing_begin(int cycleLo, int cycleHi)
84//{
85// raw_magic_timing_report_begin();
86//}
87//
88//void metro_magic_timing_end(int cycleLo, int cycleHi)
89//{
90// raw_magic_timing_report_end();
91//}
92//
93//void metro_magic_timing_report(int cycleLo, int cycleHi)
94//{
95// raw_magic_timing_report_print();
96// return;
97//}
98
99#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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9void uiFreeHandle(UI2D* out)
10{
11 free(out);
12
13 return;
14}
15
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9UI2D* uiMallocHandle(int rows, int cols)
10{
11 int i, j;
12 UI2D* out;
13
14 out = malloc(sizeof(UI2D) + sizeof(unsigned int)*rows*cols);
15 out->height = rows;
16 out->width = cols;
17 printf("uimalloc here\n");
18 return out;
19}
20
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 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9void uiResetArray(UI2D* out, int rows, int cols, int val)
10{
11 int i, j;
12
13
14 for(i=0; i<rows; i++)
15 for(j=0; j<cols; j++)
16 subsref(out,i,j) = val;
17
18
19}
diff --git a/SD-VBS/common/c/uiSetArray.c b/SD-VBS/common/c/uiSetArray.c
new file mode 100644
index 0000000..871a84a
--- /dev/null
+++ b/SD-VBS/common/c/uiSetArray.c
@@ -0,0 +1,21 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9UI2D* uiSetArray(int rows, int cols, int val)
10{
11 int i, j;
12 UI2D *out;
13 out = uiMallocHandle(rows, cols);
14
15 for(i=0; i<rows; i++)
16 for(j=0; j<cols; j++)
17 subsref(out,i,j) = val;
18
19 return out;
20
21}
diff --git a/SD-VBS/common/c/writeMatrix.c b/SD-VBS/common/c/writeMatrix.c
new file mode 100644
index 0000000..a457734
--- /dev/null
+++ b/SD-VBS/common/c/writeMatrix.c
@@ -0,0 +1,34 @@
1/********************************
2Author: Sravanthi Kota Venkata
3********************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7#include "sdvbs_common.h"
8
9void writeMatrix(I2D* input, char* inpath)
10{
11 FILE* fp;
12 char im[100];
13 int rows,cols, i, j;
14
15 sprintf(im, "%s/expected_C.txt", inpath);
16 fp = fopen(im, "w");
17
18 rows = input->height;
19 cols = input->width;
20
21 for(i=0; i<rows; i++)
22 {
23 for(j=0; j<cols; j++)
24 {
25 fprintf(fp, "%d\t", subsref(input, i, j));
26 }
27 fprintf(fp, "\n");
28 }
29
30 fclose(fp);
31}
32
33
34