annotate src/fftw-3.3.3/kernel/tensor7.c @ 70:9e21af8f0420

Opus for Windows (MSVC)
author Chris Cannam
date Fri, 25 Jan 2019 12:15:58 +0000
parents 37bf6b4a2645
children
rev   line source
Chris@10 1 /*
Chris@10 2 * Copyright (c) 2003, 2007-11 Matteo Frigo
Chris@10 3 * Copyright (c) 2003, 2007-11 Massachusetts Institute of Technology
Chris@10 4 *
Chris@10 5 * This program is free software; you can redistribute it and/or modify
Chris@10 6 * it under the terms of the GNU General Public License as published by
Chris@10 7 * the Free Software Foundation; either version 2 of the License, or
Chris@10 8 * (at your option) any later version.
Chris@10 9 *
Chris@10 10 * This program is distributed in the hope that it will be useful,
Chris@10 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@10 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@10 13 * GNU General Public License for more details.
Chris@10 14 *
Chris@10 15 * You should have received a copy of the GNU General Public License
Chris@10 16 * along with this program; if not, write to the Free Software
Chris@10 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Chris@10 18 *
Chris@10 19 */
Chris@10 20
Chris@10 21
Chris@10 22 #include "ifftw.h"
Chris@10 23
Chris@10 24 static int signof(INT x)
Chris@10 25 {
Chris@10 26 if (x < 0) return -1;
Chris@10 27 if (x == 0) return 0;
Chris@10 28 /* if (x > 0) */ return 1;
Chris@10 29 }
Chris@10 30
Chris@10 31 /* total order among iodim's */
Chris@10 32 int X(dimcmp)(const iodim *a, const iodim *b)
Chris@10 33 {
Chris@10 34 INT sai = X(iabs)(a->is), sbi = X(iabs)(b->is);
Chris@10 35 INT sao = X(iabs)(a->os), sbo = X(iabs)(b->os);
Chris@10 36 INT sam = X(imin)(sai, sao), sbm = X(imin)(sbi, sbo);
Chris@10 37
Chris@10 38 /* in descending order of min{istride, ostride} */
Chris@10 39 if (sam != sbm)
Chris@10 40 return signof(sbm - sam);
Chris@10 41
Chris@10 42 /* in case of a tie, in descending order of istride */
Chris@10 43 if (sbi != sai)
Chris@10 44 return signof(sbi - sai);
Chris@10 45
Chris@10 46 /* in case of a tie, in descending order of ostride */
Chris@10 47 if (sbo != sao)
Chris@10 48 return signof(sbo - sao);
Chris@10 49
Chris@10 50 /* in case of a tie, in ascending order of n */
Chris@10 51 return signof(a->n - b->n);
Chris@10 52 }
Chris@10 53
Chris@10 54 static void canonicalize(tensor *x)
Chris@10 55 {
Chris@10 56 if (x->rnk > 1) {
Chris@10 57 qsort(x->dims, (size_t)x->rnk, sizeof(iodim),
Chris@10 58 (int (*)(const void *, const void *))X(dimcmp));
Chris@10 59 }
Chris@10 60 }
Chris@10 61
Chris@10 62 static int compare_by_istride(const iodim *a, const iodim *b)
Chris@10 63 {
Chris@10 64 INT sai = X(iabs)(a->is), sbi = X(iabs)(b->is);
Chris@10 65
Chris@10 66 /* in descending order of istride */
Chris@10 67 return signof(sbi - sai);
Chris@10 68 }
Chris@10 69
Chris@10 70 static tensor *really_compress(const tensor *sz)
Chris@10 71 {
Chris@10 72 int i, rnk;
Chris@10 73 tensor *x;
Chris@10 74
Chris@10 75 A(FINITE_RNK(sz->rnk));
Chris@10 76 for (i = rnk = 0; i < sz->rnk; ++i) {
Chris@10 77 A(sz->dims[i].n > 0);
Chris@10 78 if (sz->dims[i].n != 1)
Chris@10 79 ++rnk;
Chris@10 80 }
Chris@10 81
Chris@10 82 x = X(mktensor)(rnk);
Chris@10 83 for (i = rnk = 0; i < sz->rnk; ++i) {
Chris@10 84 if (sz->dims[i].n != 1)
Chris@10 85 x->dims[rnk++] = sz->dims[i];
Chris@10 86 }
Chris@10 87 return x;
Chris@10 88 }
Chris@10 89
Chris@10 90 /* Like tensor_copy, but eliminate n == 1 dimensions, which
Chris@10 91 never affect any transform or transform vector.
Chris@10 92
Chris@10 93 Also, we sort the tensor into a canonical order of decreasing
Chris@10 94 strides (see X(dimcmp) for an exact definition). In general,
Chris@10 95 processing a loop/array in order of decreasing stride will improve
Chris@10 96 locality. Both forward and backwards traversal of the tensor are
Chris@10 97 considered e.g. by vrank-geq1, so sorting in increasing
Chris@10 98 vs. decreasing order is not really important. */
Chris@10 99 tensor *X(tensor_compress)(const tensor *sz)
Chris@10 100 {
Chris@10 101 tensor *x = really_compress(sz);
Chris@10 102 canonicalize(x);
Chris@10 103 return x;
Chris@10 104 }
Chris@10 105
Chris@10 106 /* Return whether the strides of a and b are such that they form an
Chris@10 107 effective contiguous 1d array. Assumes that a.is >= b.is. */
Chris@10 108 static int strides_contig(iodim *a, iodim *b)
Chris@10 109 {
Chris@10 110 return (a->is == b->is * b->n && a->os == b->os * b->n);
Chris@10 111 }
Chris@10 112
Chris@10 113 /* Like tensor_compress, but also compress into one dimension any
Chris@10 114 group of dimensions that form a contiguous block of indices with
Chris@10 115 some stride. (This can safely be done for transform vector sizes.) */
Chris@10 116 tensor *X(tensor_compress_contiguous)(const tensor *sz)
Chris@10 117 {
Chris@10 118 int i, rnk;
Chris@10 119 tensor *sz2, *x;
Chris@10 120
Chris@10 121 if (X(tensor_sz)(sz) == 0)
Chris@10 122 return X(mktensor)(RNK_MINFTY);
Chris@10 123
Chris@10 124 sz2 = really_compress(sz);
Chris@10 125 A(FINITE_RNK(sz2->rnk));
Chris@10 126
Chris@10 127 if (sz2->rnk <= 1) { /* nothing to compress. */
Chris@10 128 if (0) {
Chris@10 129 /* this call is redundant, because "sz->rnk <= 1" implies
Chris@10 130 that the tensor is already canonical, but I am writing
Chris@10 131 it explicitly because "logically" we need to canonicalize
Chris@10 132 the tensor before returning. */
Chris@10 133 canonicalize(sz2);
Chris@10 134 }
Chris@10 135 return sz2;
Chris@10 136 }
Chris@10 137
Chris@10 138 /* sort in descending order of |istride|, so that compressible
Chris@10 139 dimensions appear contigously */
Chris@10 140 qsort(sz2->dims, (size_t)sz2->rnk, sizeof(iodim),
Chris@10 141 (int (*)(const void *, const void *))compare_by_istride);
Chris@10 142
Chris@10 143 /* compute what the rank will be after compression */
Chris@10 144 for (i = rnk = 1; i < sz2->rnk; ++i)
Chris@10 145 if (!strides_contig(sz2->dims + i - 1, sz2->dims + i))
Chris@10 146 ++rnk;
Chris@10 147
Chris@10 148 /* merge adjacent dimensions whenever possible */
Chris@10 149 x = X(mktensor)(rnk);
Chris@10 150 x->dims[0] = sz2->dims[0];
Chris@10 151 for (i = rnk = 1; i < sz2->rnk; ++i) {
Chris@10 152 if (strides_contig(sz2->dims + i - 1, sz2->dims + i)) {
Chris@10 153 x->dims[rnk - 1].n *= sz2->dims[i].n;
Chris@10 154 x->dims[rnk - 1].is = sz2->dims[i].is;
Chris@10 155 x->dims[rnk - 1].os = sz2->dims[i].os;
Chris@10 156 } else {
Chris@10 157 A(rnk < x->rnk);
Chris@10 158 x->dims[rnk++] = sz2->dims[i];
Chris@10 159 }
Chris@10 160 }
Chris@10 161
Chris@10 162 X(tensor_destroy)(sz2);
Chris@10 163
Chris@10 164 /* reduce to canonical form */
Chris@10 165 canonicalize(x);
Chris@10 166 return x;
Chris@10 167 }
Chris@10 168
Chris@10 169 /* The inverse of X(tensor_append): splits the sz tensor into
Chris@10 170 tensor a followed by tensor b, where a's rank is arnk. */
Chris@10 171 void X(tensor_split)(const tensor *sz, tensor **a, int arnk, tensor **b)
Chris@10 172 {
Chris@10 173 A(FINITE_RNK(sz->rnk) && FINITE_RNK(arnk));
Chris@10 174
Chris@10 175 *a = X(tensor_copy_sub)(sz, 0, arnk);
Chris@10 176 *b = X(tensor_copy_sub)(sz, arnk, sz->rnk - arnk);
Chris@10 177 }
Chris@10 178
Chris@10 179 /* TRUE if the two tensors are equal */
Chris@10 180 int X(tensor_equal)(const tensor *a, const tensor *b)
Chris@10 181 {
Chris@10 182 if (a->rnk != b->rnk)
Chris@10 183 return 0;
Chris@10 184
Chris@10 185 if (FINITE_RNK(a->rnk)) {
Chris@10 186 int i;
Chris@10 187 for (i = 0; i < a->rnk; ++i)
Chris@10 188 if (0
Chris@10 189 || a->dims[i].n != b->dims[i].n
Chris@10 190 || a->dims[i].is != b->dims[i].is
Chris@10 191 || a->dims[i].os != b->dims[i].os
Chris@10 192 )
Chris@10 193 return 0;
Chris@10 194 }
Chris@10 195
Chris@10 196 return 1;
Chris@10 197 }
Chris@10 198
Chris@10 199 /* TRUE if the sets of input and output locations described by
Chris@10 200 (append sz vecsz) are the same */
Chris@10 201 int X(tensor_inplace_locations)(const tensor *sz, const tensor *vecsz)
Chris@10 202 {
Chris@10 203 tensor *t = X(tensor_append)(sz, vecsz);
Chris@10 204 tensor *ti = X(tensor_copy_inplace)(t, INPLACE_IS);
Chris@10 205 tensor *to = X(tensor_copy_inplace)(t, INPLACE_OS);
Chris@10 206 tensor *tic = X(tensor_compress_contiguous)(ti);
Chris@10 207 tensor *toc = X(tensor_compress_contiguous)(to);
Chris@10 208
Chris@10 209 int retval = X(tensor_equal)(tic, toc);
Chris@10 210
Chris@10 211 X(tensor_destroy)(t);
Chris@10 212 X(tensor_destroy4)(ti, to, tic, toc);
Chris@10 213
Chris@10 214 return retval;
Chris@10 215 }