Mercurial > hg > sv-dependency-builds
comparison src/fftw-3.3.3/libbench2/tensor.c @ 10:37bf6b4a2645
Add FFTW3
author | Chris Cannam |
---|---|
date | Wed, 20 Mar 2013 15:35:50 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
9:c0fb53affa76 | 10:37bf6b4a2645 |
---|---|
1 /* | |
2 * Copyright (c) 2001 Matteo Frigo | |
3 * Copyright (c) 2001 Massachusetts Institute of Technology | |
4 * | |
5 * This program is free software; you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License as published by | |
7 * the Free Software Foundation; either version 2 of the License, or | |
8 * (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 * GNU General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU General Public License | |
16 * along with this program; if not, write to the Free Software | |
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
18 * | |
19 */ | |
20 | |
21 #include "bench.h" | |
22 #include <stdlib.h> | |
23 | |
24 bench_tensor *mktensor(int rnk) | |
25 { | |
26 bench_tensor *x; | |
27 | |
28 BENCH_ASSERT(rnk >= 0); | |
29 | |
30 x = (bench_tensor *)bench_malloc(sizeof(bench_tensor)); | |
31 if (FINITE_RNK(rnk) && rnk > 0) | |
32 x->dims = (bench_iodim *)bench_malloc(sizeof(bench_iodim) * rnk); | |
33 else | |
34 x->dims = 0; | |
35 | |
36 x->rnk = rnk; | |
37 return x; | |
38 } | |
39 | |
40 void tensor_destroy(bench_tensor *sz) | |
41 { | |
42 bench_free0(sz->dims); | |
43 bench_free(sz); | |
44 } | |
45 | |
46 int tensor_sz(const bench_tensor *sz) | |
47 { | |
48 int i, n = 1; | |
49 | |
50 if (!FINITE_RNK(sz->rnk)) | |
51 return 0; | |
52 | |
53 for (i = 0; i < sz->rnk; ++i) | |
54 n *= sz->dims[i].n; | |
55 return n; | |
56 } | |
57 | |
58 | |
59 /* total order among bench_iodim's */ | |
60 static int dimcmp(const bench_iodim *a, const bench_iodim *b) | |
61 { | |
62 if (b->is != a->is) | |
63 return (b->is - a->is); /* shorter strides go later */ | |
64 if (b->os != a->os) | |
65 return (b->os - a->os); /* shorter strides go later */ | |
66 return (int)(a->n - b->n); /* larger n's go later */ | |
67 } | |
68 | |
69 bench_tensor *tensor_compress(const bench_tensor *sz) | |
70 { | |
71 int i, rnk; | |
72 bench_tensor *x; | |
73 | |
74 BENCH_ASSERT(FINITE_RNK(sz->rnk)); | |
75 for (i = rnk = 0; i < sz->rnk; ++i) { | |
76 BENCH_ASSERT(sz->dims[i].n > 0); | |
77 if (sz->dims[i].n != 1) | |
78 ++rnk; | |
79 } | |
80 | |
81 x = mktensor(rnk); | |
82 for (i = rnk = 0; i < sz->rnk; ++i) { | |
83 if (sz->dims[i].n != 1) | |
84 x->dims[rnk++] = sz->dims[i]; | |
85 } | |
86 | |
87 if (rnk) { | |
88 /* God knows how qsort() behaves if n==0 */ | |
89 qsort(x->dims, (size_t)x->rnk, sizeof(bench_iodim), | |
90 (int (*)(const void *, const void *))dimcmp); | |
91 } | |
92 | |
93 return x; | |
94 } | |
95 | |
96 int tensor_unitstridep(bench_tensor *t) | |
97 { | |
98 BENCH_ASSERT(FINITE_RNK(t->rnk)); | |
99 return (t->rnk == 0 || | |
100 (t->dims[t->rnk - 1].is == 1 && t->dims[t->rnk - 1].os == 1)); | |
101 } | |
102 | |
103 /* detect screwy real padded rowmajor... ugh */ | |
104 int tensor_real_rowmajorp(bench_tensor *t, int sign, int in_place) | |
105 { | |
106 int i; | |
107 | |
108 BENCH_ASSERT(FINITE_RNK(t->rnk)); | |
109 | |
110 i = t->rnk - 1; | |
111 | |
112 if (--i >= 0) { | |
113 bench_iodim *d = t->dims + i; | |
114 if (sign < 0) { | |
115 if (d[0].is != d[1].is * (in_place ? 2*(d[1].n/2 + 1) : d[1].n)) | |
116 return 0; | |
117 if (d[0].os != d[1].os * (d[1].n/2 + 1)) | |
118 return 0; | |
119 } | |
120 else { | |
121 if (d[0].is != d[1].is * (d[1].n/2 + 1)) | |
122 return 0; | |
123 if (d[0].os != d[1].os * (in_place ? 2*(d[1].n/2 + 1) : d[1].n)) | |
124 return 0; | |
125 } | |
126 } | |
127 | |
128 while (--i >= 0) { | |
129 bench_iodim *d = t->dims + i; | |
130 if (d[0].is != d[1].is * d[1].n) | |
131 return 0; | |
132 if (d[0].os != d[1].os * d[1].n) | |
133 return 0; | |
134 } | |
135 return 1; | |
136 } | |
137 | |
138 int tensor_rowmajorp(bench_tensor *t) | |
139 { | |
140 int i; | |
141 | |
142 BENCH_ASSERT(FINITE_RNK(t->rnk)); | |
143 | |
144 i = t->rnk - 1; | |
145 while (--i >= 0) { | |
146 bench_iodim *d = t->dims + i; | |
147 if (d[0].is != d[1].is * d[1].n) | |
148 return 0; | |
149 if (d[0].os != d[1].os * d[1].n) | |
150 return 0; | |
151 } | |
152 return 1; | |
153 } | |
154 | |
155 static void dimcpy(bench_iodim *dst, const bench_iodim *src, int rnk) | |
156 { | |
157 int i; | |
158 if (FINITE_RNK(rnk)) | |
159 for (i = 0; i < rnk; ++i) | |
160 dst[i] = src[i]; | |
161 } | |
162 | |
163 bench_tensor *tensor_append(const bench_tensor *a, const bench_tensor *b) | |
164 { | |
165 if (!FINITE_RNK(a->rnk) || !FINITE_RNK(b->rnk)) { | |
166 return mktensor(RNK_MINFTY); | |
167 } else { | |
168 bench_tensor *x = mktensor(a->rnk + b->rnk); | |
169 dimcpy(x->dims, a->dims, a->rnk); | |
170 dimcpy(x->dims + a->rnk, b->dims, b->rnk); | |
171 return x; | |
172 } | |
173 } | |
174 | |
175 static int imax(int a, int b) | |
176 { | |
177 return (a > b) ? a : b; | |
178 } | |
179 | |
180 static int imin(int a, int b) | |
181 { | |
182 return (a < b) ? a : b; | |
183 } | |
184 | |
185 #define DEFBOUNDS(name, xs) \ | |
186 void name(bench_tensor *t, int *lbp, int *ubp) \ | |
187 { \ | |
188 int lb = 0; \ | |
189 int ub = 1; \ | |
190 int i; \ | |
191 \ | |
192 BENCH_ASSERT(FINITE_RNK(t->rnk)); \ | |
193 \ | |
194 for (i = 0; i < t->rnk; ++i) { \ | |
195 bench_iodim *d = t->dims + i; \ | |
196 int n = d->n; \ | |
197 int s = d->xs; \ | |
198 lb = imin(lb, lb + s * (n - 1)); \ | |
199 ub = imax(ub, ub + s * (n - 1)); \ | |
200 } \ | |
201 \ | |
202 *lbp = lb; \ | |
203 *ubp = ub; \ | |
204 } | |
205 | |
206 DEFBOUNDS(tensor_ibounds, is) | |
207 DEFBOUNDS(tensor_obounds, os) | |
208 | |
209 bench_tensor *tensor_copy(const bench_tensor *sz) | |
210 { | |
211 bench_tensor *x = mktensor(sz->rnk); | |
212 dimcpy(x->dims, sz->dims, sz->rnk); | |
213 return x; | |
214 } | |
215 | |
216 /* Like tensor_copy, but copy only rnk dimensions starting with start_dim. */ | |
217 bench_tensor *tensor_copy_sub(const bench_tensor *sz, int start_dim, int rnk) | |
218 { | |
219 bench_tensor *x; | |
220 | |
221 BENCH_ASSERT(FINITE_RNK(sz->rnk) && start_dim + rnk <= sz->rnk); | |
222 x = mktensor(rnk); | |
223 dimcpy(x->dims, sz->dims + start_dim, rnk); | |
224 return x; | |
225 } | |
226 | |
227 bench_tensor *tensor_copy_swapio(const bench_tensor *sz) | |
228 { | |
229 bench_tensor *x = tensor_copy(sz); | |
230 int i; | |
231 if (FINITE_RNK(x->rnk)) | |
232 for (i = 0; i < x->rnk; ++i) { | |
233 int s; | |
234 s = x->dims[i].is; | |
235 x->dims[i].is = x->dims[i].os; | |
236 x->dims[i].os = s; | |
237 } | |
238 return x; | |
239 } |