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