File: perf-test.c

package info (click to toggle)
rtfilter 1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 500 kB
  • sloc: ansic: 2,671; makefile: 248; sh: 83; python: 57
file content (236 lines) | stat: -rw-r--r-- 5,663 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 *  Copyright (C) 2010-2011 Nicolas Bourdaud <nicolas.bourdaud@gmail.com>
 *
 *  The program is free software: you can redistribute it and/or
 *  modify it under the terms of the version 3 of the GNU General
 *  Public License as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <complex.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>

#include "rtfilter.h"

#define NCHANN 64
#define NSAMPLE 4
#define NITER 10000
#define FILTORDER 4

static
void* align_alloc(size_t alignment, size_t size)
{
#if HAVE_POSIX_MEMALIGN
	void* memptr = NULL;
	if (posix_memalign(&memptr, alignment, size))
		return NULL;

	return memptr;
#else
	void* origptr = malloc(sizeof(void*) + alignment + size);
	char* ptr = ((char*) origptr) + sizeof(void*);
	ptr += alignment - ((uintptr_t) ptr) % alignment;
	*(void**)(ptr - sizeof(origptr)) = origptr;
	return ptr;
#endif /* if HAVE_POSIX_MEMALIGN */
}

static
void  align_free(void* memptr)
{
#if HAVE_POSIX_MEMALIGN
	free(memptr);
#else
	free(*(((void**) memptr) - 1));
#endif
}


static
size_t sizeof_data(int type)
{
	size_t dsize = 0;

	if (type == RTF_FLOAT)
		dsize = sizeof(float);
	else if (type == RTF_DOUBLE)
		dsize = sizeof(double);
	else if (type == RTF_CFLOAT)
		dsize = sizeof(complex float);
	else if (type == RTF_CDOUBLE)
		dsize = sizeof(complex double);

	return dsize;
}

static
void set_coeffs(unsigned int len, int type, void* coef,
                void** num, void** den)
{
	memset(coef, 0, sizeof_data(type)*2*len);
	*num = coef;

	if (type == RTF_FLOAT) {
		((float*) coef)[len] = 1.0;
		*den = ((float*) coef) + len;
	} else if (type == RTF_DOUBLE) {
		((double*) coef)[len] = 1.0;
		*den = ((double*) coef) + len;
	} else if (type == RTF_CFLOAT) {
		((complex float*) coef)[len] = 1.0;
		*den = ((complex float*) coef) + len;
	} else if (type == RTF_CDOUBLE) {
		((complex double*) coef)[len] = 1.0;
		*den = ((complex double*) coef) + len;
	}
}

static
void print_version(void)
{
	unsigned int line = 0;
	char version[128];

	while (rtf_get_version(version, sizeof(version), line))
		printf((line++ ? "       %s\n" : "version: %s\n"), version);
}


int main(int argc, char *argv[])
{
	int nch, ns, niter, filtorder;
	int k, opt, datintype, datouttype, ptype, vect;
	struct timespec start, stop;
	long long delay = 0, delayv = 0;
	long long tc, dt, timing, mintime, mintimev;
	hfilter filt = NULL;
	void *buff1, *buff2, *origbuff, *coef;
	void *num = NULL, *den = NULL;

	/* Process command-line options */
	nch = NCHANN;
	ns = NSAMPLE;
	niter = NITER;
	filtorder = FILTORDER;
	vect = 1;
	ptype = datintype = RTF_FLOAT;

	while ((opt = getopt(argc, argv, "c:s:i:o:d:p:v:h")) != -1) {
		switch (opt) {
		case 'c':
			nch = atoi(optarg);
			break;
		case 's':
			ns = atoi(optarg);
			break;
		case 'i':
			niter = atoi(optarg);
			break;
		case 'o':
			filtorder = atoi(optarg);
			break;
		case 'd':
			datintype = atoi(optarg);
			break;
		case 'p':
			ptype = atoi(optarg);
			break;
		case 'v':
			vect = atoi(optarg);
			break;
		case 'h':
		default:        /* '?' */
			fprintf(stderr,
			        "Usage: %s [-c numchannel] [-s numsample] [-i numiteration] [-o filterorder]\n",
			        argv[0]);
			exit(EXIT_FAILURE);
		}
	}

	print_version();
	printf(
		"filter order: %i \tnumber of channels: %i \t\tlength of batch: %i\n",
		filtorder,
		nch,
		ns);

	datouttype = datintype;
	if (ptype & RTF_COMPLEX_MASK)
		datouttype |= RTF_COMPLEX_MASK;

	/* Allocate buffers */
	origbuff = align_alloc(16, sizeof_data(datintype) * nch * ns + 8);
	buff2 = align_alloc(16, sizeof_data(datouttype) * nch * ns);
	coef = malloc(2 * sizeof_data(ptype) * filtorder);
	if (!origbuff || !buff2 || !coef) {
		fprintf(stderr, "buffer allocation failed\n");
		goto out;
	}

	buff1 = (vect == 0) ? (char*) origbuff + 8 : origbuff;
	/* set signals (zeros) */
	memset(buff1, 0, nch * ns * sizeof_data(datintype));
	set_coeffs(filtorder, ptype, coef, &num, &den);


	/* Estimate timecall of clockgettime */
	tc = LONG_MAX;
	for (k = 0; k < 1000; k++) {
		clock_gettime(CLOCK_MONOTONIC, &start);
		clock_gettime(CLOCK_MONOTONIC, &stop);
		delay = ((stop.tv_sec - start.tv_sec) * 1000000000
		         + (stop.tv_nsec - start.tv_nsec));
		tc = delay >= tc ? tc : delay;
	}

	/* create filters */
	filt = rtf_create_filter(nch, datintype,
	                         filtorder, num,
	                         filtorder, den, ptype);
	if (!filt) {
		fprintf(stderr, "Creation of filter failed\n");
		goto out;
	}

	delay = delayv = 0;
	mintime = mintimev = LONG_MAX;
	for (k = 0; k < niter; k++) {
		/* Test normal version */
		clock_gettime(CLOCK_REALTIME, &start);
		rtf_filter(filt, buff1, buff2, ns);
		clock_gettime(CLOCK_REALTIME, &stop);
		timing = ((stop.tv_sec - start.tv_sec) * 1000000000
		          + (stop.tv_nsec - start.tv_nsec)) - tc;
		delay += timing;
		mintime = mintime > timing ? timing : mintime;
	}

	dt = mintime;
	printf("min time per call: %i nsec\n", (int) dt);
	printf("min time per sample: %lg nsec\n",
	       (double) dt / (double) (ns * nch));

out:
	rtf_destroy_filter(filt);
	align_free(origbuff);
	align_free(buff2);
	free(coef);

	return 0;
}