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