cannam@127: /* cannam@127: * Copyright (c) 2003, 2007-14 Matteo Frigo cannam@127: * Copyright (c) 2003, 2007-14 Massachusetts Institute of Technology cannam@127: * cannam@127: * This program is free software; you can redistribute it and/or modify cannam@127: * it under the terms of the GNU General Public License as published by cannam@127: * the Free Software Foundation; either version 2 of the License, or cannam@127: * (at your option) any later version. cannam@127: * cannam@127: * This program is distributed in the hope that it will be useful, cannam@127: * but WITHOUT ANY WARRANTY; without even the implied warranty of cannam@127: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the cannam@127: * GNU General Public License for more details. cannam@127: * cannam@127: * You should have received a copy of the GNU General Public License cannam@127: * along with this program; if not, write to the Free Software cannam@127: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA cannam@127: * cannam@127: */ cannam@127: cannam@127: cannam@127: #include "ifftw.h" cannam@127: cannam@127: static int signof(INT x) cannam@127: { cannam@127: if (x < 0) return -1; cannam@127: if (x == 0) return 0; cannam@127: /* if (x > 0) */ return 1; cannam@127: } cannam@127: cannam@127: /* total order among iodim's */ cannam@127: int X(dimcmp)(const iodim *a, const iodim *b) cannam@127: { cannam@127: INT sai = X(iabs)(a->is), sbi = X(iabs)(b->is); cannam@127: INT sao = X(iabs)(a->os), sbo = X(iabs)(b->os); cannam@127: INT sam = X(imin)(sai, sao), sbm = X(imin)(sbi, sbo); cannam@127: cannam@127: /* in descending order of min{istride, ostride} */ cannam@127: if (sam != sbm) cannam@127: return signof(sbm - sam); cannam@127: cannam@127: /* in case of a tie, in descending order of istride */ cannam@127: if (sbi != sai) cannam@127: return signof(sbi - sai); cannam@127: cannam@127: /* in case of a tie, in descending order of ostride */ cannam@127: if (sbo != sao) cannam@127: return signof(sbo - sao); cannam@127: cannam@127: /* in case of a tie, in ascending order of n */ cannam@127: return signof(a->n - b->n); cannam@127: } cannam@127: cannam@127: static void canonicalize(tensor *x) cannam@127: { cannam@127: if (x->rnk > 1) { cannam@127: qsort(x->dims, (unsigned)x->rnk, sizeof(iodim), cannam@127: (int (*)(const void *, const void *))X(dimcmp)); cannam@127: } cannam@127: } cannam@127: cannam@127: static int compare_by_istride(const iodim *a, const iodim *b) cannam@127: { cannam@127: INT sai = X(iabs)(a->is), sbi = X(iabs)(b->is); cannam@127: cannam@127: /* in descending order of istride */ cannam@127: return signof(sbi - sai); cannam@127: } cannam@127: cannam@127: static tensor *really_compress(const tensor *sz) cannam@127: { cannam@127: int i, rnk; cannam@127: tensor *x; cannam@127: cannam@127: A(FINITE_RNK(sz->rnk)); cannam@127: for (i = rnk = 0; i < sz->rnk; ++i) { cannam@127: A(sz->dims[i].n > 0); cannam@127: if (sz->dims[i].n != 1) cannam@127: ++rnk; cannam@127: } cannam@127: cannam@127: x = X(mktensor)(rnk); cannam@127: for (i = rnk = 0; i < sz->rnk; ++i) { cannam@127: if (sz->dims[i].n != 1) cannam@127: x->dims[rnk++] = sz->dims[i]; cannam@127: } cannam@127: return x; cannam@127: } cannam@127: cannam@127: /* Like tensor_copy, but eliminate n == 1 dimensions, which cannam@127: never affect any transform or transform vector. cannam@127: cannam@127: Also, we sort the tensor into a canonical order of decreasing cannam@127: strides (see X(dimcmp) for an exact definition). In general, cannam@127: processing a loop/array in order of decreasing stride will improve cannam@127: locality. Both forward and backwards traversal of the tensor are cannam@127: considered e.g. by vrank-geq1, so sorting in increasing cannam@127: vs. decreasing order is not really important. */ cannam@127: tensor *X(tensor_compress)(const tensor *sz) cannam@127: { cannam@127: tensor *x = really_compress(sz); cannam@127: canonicalize(x); cannam@127: return x; cannam@127: } cannam@127: cannam@127: /* Return whether the strides of a and b are such that they form an cannam@127: effective contiguous 1d array. Assumes that a.is >= b.is. */ cannam@127: static int strides_contig(iodim *a, iodim *b) cannam@127: { cannam@127: return (a->is == b->is * b->n && a->os == b->os * b->n); cannam@127: } cannam@127: cannam@127: /* Like tensor_compress, but also compress into one dimension any cannam@127: group of dimensions that form a contiguous block of indices with cannam@127: some stride. (This can safely be done for transform vector sizes.) */ cannam@127: tensor *X(tensor_compress_contiguous)(const tensor *sz) cannam@127: { cannam@127: int i, rnk; cannam@127: tensor *sz2, *x; cannam@127: cannam@127: if (X(tensor_sz)(sz) == 0) cannam@127: return X(mktensor)(RNK_MINFTY); cannam@127: cannam@127: sz2 = really_compress(sz); cannam@127: A(FINITE_RNK(sz2->rnk)); cannam@127: cannam@127: if (sz2->rnk <= 1) { /* nothing to compress. */ cannam@127: if (0) { cannam@127: /* this call is redundant, because "sz->rnk <= 1" implies cannam@127: that the tensor is already canonical, but I am writing cannam@127: it explicitly because "logically" we need to canonicalize cannam@127: the tensor before returning. */ cannam@127: canonicalize(sz2); cannam@127: } cannam@127: return sz2; cannam@127: } cannam@127: cannam@127: /* sort in descending order of |istride|, so that compressible cannam@127: dimensions appear contigously */ cannam@127: qsort(sz2->dims, (unsigned)sz2->rnk, sizeof(iodim), cannam@127: (int (*)(const void *, const void *))compare_by_istride); cannam@127: cannam@127: /* compute what the rank will be after compression */ cannam@127: for (i = rnk = 1; i < sz2->rnk; ++i) cannam@127: if (!strides_contig(sz2->dims + i - 1, sz2->dims + i)) cannam@127: ++rnk; cannam@127: cannam@127: /* merge adjacent dimensions whenever possible */ cannam@127: x = X(mktensor)(rnk); cannam@127: x->dims[0] = sz2->dims[0]; cannam@127: for (i = rnk = 1; i < sz2->rnk; ++i) { cannam@127: if (strides_contig(sz2->dims + i - 1, sz2->dims + i)) { cannam@127: x->dims[rnk - 1].n *= sz2->dims[i].n; cannam@127: x->dims[rnk - 1].is = sz2->dims[i].is; cannam@127: x->dims[rnk - 1].os = sz2->dims[i].os; cannam@127: } else { cannam@127: A(rnk < x->rnk); cannam@127: x->dims[rnk++] = sz2->dims[i]; cannam@127: } cannam@127: } cannam@127: cannam@127: X(tensor_destroy)(sz2); cannam@127: cannam@127: /* reduce to canonical form */ cannam@127: canonicalize(x); cannam@127: return x; cannam@127: } cannam@127: cannam@127: /* The inverse of X(tensor_append): splits the sz tensor into cannam@127: tensor a followed by tensor b, where a's rank is arnk. */ cannam@127: void X(tensor_split)(const tensor *sz, tensor **a, int arnk, tensor **b) cannam@127: { cannam@127: A(FINITE_RNK(sz->rnk) && FINITE_RNK(arnk)); cannam@127: cannam@127: *a = X(tensor_copy_sub)(sz, 0, arnk); cannam@127: *b = X(tensor_copy_sub)(sz, arnk, sz->rnk - arnk); cannam@127: } cannam@127: cannam@127: /* TRUE if the two tensors are equal */ cannam@127: int X(tensor_equal)(const tensor *a, const tensor *b) cannam@127: { cannam@127: if (a->rnk != b->rnk) cannam@127: return 0; cannam@127: cannam@127: if (FINITE_RNK(a->rnk)) { cannam@127: int i; cannam@127: for (i = 0; i < a->rnk; ++i) cannam@127: if (0 cannam@127: || a->dims[i].n != b->dims[i].n cannam@127: || a->dims[i].is != b->dims[i].is cannam@127: || a->dims[i].os != b->dims[i].os cannam@127: ) cannam@127: return 0; cannam@127: } cannam@127: cannam@127: return 1; cannam@127: } cannam@127: cannam@127: /* TRUE if the sets of input and output locations described by cannam@127: (append sz vecsz) are the same */ cannam@127: int X(tensor_inplace_locations)(const tensor *sz, const tensor *vecsz) cannam@127: { cannam@127: tensor *t = X(tensor_append)(sz, vecsz); cannam@127: tensor *ti = X(tensor_copy_inplace)(t, INPLACE_IS); cannam@127: tensor *to = X(tensor_copy_inplace)(t, INPLACE_OS); cannam@127: tensor *tic = X(tensor_compress_contiguous)(ti); cannam@127: tensor *toc = X(tensor_compress_contiguous)(to); cannam@127: cannam@127: int retval = X(tensor_equal)(tic, toc); cannam@127: cannam@127: X(tensor_destroy)(t); cannam@127: X(tensor_destroy4)(ti, to, tic, toc); cannam@127: cannam@127: return retval; cannam@127: }