cannam@167: /* cannam@167: * Copyright (c) 2001 Matteo Frigo cannam@167: * Copyright (c) 2001 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: #include "libbench2/bench.h" cannam@167: #include cannam@167: cannam@167: bench_tensor *mktensor(int rnk) cannam@167: { cannam@167: bench_tensor *x; cannam@167: cannam@167: BENCH_ASSERT(rnk >= 0); cannam@167: cannam@167: x = (bench_tensor *)bench_malloc(sizeof(bench_tensor)); cannam@167: if (BENCH_FINITE_RNK(rnk) && rnk > 0) cannam@167: x->dims = (bench_iodim *)bench_malloc(sizeof(bench_iodim) * rnk); cannam@167: else cannam@167: x->dims = 0; cannam@167: cannam@167: x->rnk = rnk; cannam@167: return x; cannam@167: } cannam@167: cannam@167: void tensor_destroy(bench_tensor *sz) cannam@167: { cannam@167: bench_free0(sz->dims); cannam@167: bench_free(sz); cannam@167: } cannam@167: cannam@167: size_t tensor_sz(const bench_tensor *sz) cannam@167: { cannam@167: int i; cannam@167: size_t n = 1; cannam@167: cannam@167: if (!BENCH_FINITE_RNK(sz->rnk)) cannam@167: return 0; cannam@167: cannam@167: for (i = 0; i < sz->rnk; ++i) cannam@167: n *= sz->dims[i].n; cannam@167: return n; cannam@167: } cannam@167: cannam@167: cannam@167: /* total order among bench_iodim's */ cannam@167: static int dimcmp(const bench_iodim *a, const bench_iodim *b) cannam@167: { cannam@167: if (b->is != a->is) cannam@167: return (b->is - a->is); /* shorter strides go later */ cannam@167: if (b->os != a->os) cannam@167: return (b->os - a->os); /* shorter strides go later */ cannam@167: return (int)(a->n - b->n); /* larger n's go later */ cannam@167: } cannam@167: cannam@167: bench_tensor *tensor_compress(const bench_tensor *sz) cannam@167: { cannam@167: int i, rnk; cannam@167: bench_tensor *x; cannam@167: cannam@167: BENCH_ASSERT(BENCH_FINITE_RNK(sz->rnk)); cannam@167: for (i = rnk = 0; i < sz->rnk; ++i) { cannam@167: BENCH_ASSERT(sz->dims[i].n > 0); cannam@167: if (sz->dims[i].n != 1) cannam@167: ++rnk; cannam@167: } cannam@167: cannam@167: 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: cannam@167: if (rnk) { cannam@167: /* God knows how qsort() behaves if n==0 */ cannam@167: qsort(x->dims, (size_t)x->rnk, sizeof(bench_iodim), cannam@167: (int (*)(const void *, const void *))dimcmp); cannam@167: } cannam@167: cannam@167: return x; cannam@167: } cannam@167: cannam@167: int tensor_unitstridep(bench_tensor *t) cannam@167: { cannam@167: BENCH_ASSERT(BENCH_FINITE_RNK(t->rnk)); cannam@167: return (t->rnk == 0 || cannam@167: (t->dims[t->rnk - 1].is == 1 && t->dims[t->rnk - 1].os == 1)); cannam@167: } cannam@167: cannam@167: /* detect screwy real padded rowmajor... ugh */ cannam@167: int tensor_real_rowmajorp(bench_tensor *t, int sign, int in_place) cannam@167: { cannam@167: int i; cannam@167: cannam@167: BENCH_ASSERT(BENCH_FINITE_RNK(t->rnk)); cannam@167: cannam@167: i = t->rnk - 1; cannam@167: cannam@167: if (--i >= 0) { cannam@167: bench_iodim *d = t->dims + i; cannam@167: if (sign < 0) { cannam@167: if (d[0].is != d[1].is * (in_place ? 2*(d[1].n/2 + 1) : d[1].n)) cannam@167: return 0; cannam@167: if (d[0].os != d[1].os * (d[1].n/2 + 1)) cannam@167: return 0; cannam@167: } cannam@167: else { cannam@167: if (d[0].is != d[1].is * (d[1].n/2 + 1)) cannam@167: return 0; cannam@167: if (d[0].os != d[1].os * (in_place ? 2*(d[1].n/2 + 1) : d[1].n)) cannam@167: return 0; cannam@167: } cannam@167: } cannam@167: cannam@167: while (--i >= 0) { cannam@167: bench_iodim *d = t->dims + i; cannam@167: if (d[0].is != d[1].is * d[1].n) cannam@167: return 0; cannam@167: if (d[0].os != d[1].os * d[1].n) cannam@167: return 0; cannam@167: } cannam@167: return 1; cannam@167: } cannam@167: cannam@167: int tensor_rowmajorp(bench_tensor *t) cannam@167: { cannam@167: int i; cannam@167: cannam@167: BENCH_ASSERT(BENCH_FINITE_RNK(t->rnk)); cannam@167: cannam@167: i = t->rnk - 1; cannam@167: while (--i >= 0) { cannam@167: bench_iodim *d = t->dims + i; cannam@167: if (d[0].is != d[1].is * d[1].n) cannam@167: return 0; cannam@167: if (d[0].os != d[1].os * d[1].n) cannam@167: return 0; cannam@167: } cannam@167: return 1; cannam@167: } cannam@167: cannam@167: static void dimcpy(bench_iodim *dst, const bench_iodim *src, int rnk) cannam@167: { cannam@167: int i; cannam@167: if (BENCH_FINITE_RNK(rnk)) cannam@167: for (i = 0; i < rnk; ++i) cannam@167: dst[i] = src[i]; cannam@167: } cannam@167: cannam@167: bench_tensor *tensor_append(const bench_tensor *a, const bench_tensor *b) cannam@167: { cannam@167: if (!BENCH_FINITE_RNK(a->rnk) || !BENCH_FINITE_RNK(b->rnk)) { cannam@167: return mktensor(BENCH_RNK_MINFTY); cannam@167: } else { cannam@167: bench_tensor *x = mktensor(a->rnk + b->rnk); cannam@167: dimcpy(x->dims, a->dims, a->rnk); cannam@167: dimcpy(x->dims + a->rnk, b->dims, b->rnk); cannam@167: return x; cannam@167: } cannam@167: } cannam@167: cannam@167: static int imax(int a, int b) cannam@167: { cannam@167: return (a > b) ? a : b; cannam@167: } cannam@167: cannam@167: static int imin(int a, int b) cannam@167: { cannam@167: return (a < b) ? a : b; cannam@167: } cannam@167: cannam@167: #define DEFBOUNDS(name, xs) \ cannam@167: void name(bench_tensor *t, int *lbp, int *ubp) \ cannam@167: { \ cannam@167: int lb = 0; \ cannam@167: int ub = 1; \ cannam@167: int i; \ cannam@167: \ cannam@167: BENCH_ASSERT(BENCH_FINITE_RNK(t->rnk)); \ cannam@167: \ cannam@167: for (i = 0; i < t->rnk; ++i) { \ cannam@167: bench_iodim *d = t->dims + i; \ cannam@167: int n = d->n; \ cannam@167: int s = d->xs; \ cannam@167: lb = imin(lb, lb + s * (n - 1)); \ cannam@167: ub = imax(ub, ub + s * (n - 1)); \ cannam@167: } \ cannam@167: \ cannam@167: *lbp = lb; \ cannam@167: *ubp = ub; \ cannam@167: } cannam@167: cannam@167: DEFBOUNDS(tensor_ibounds, is) cannam@167: DEFBOUNDS(tensor_obounds, os) cannam@167: cannam@167: bench_tensor *tensor_copy(const bench_tensor *sz) cannam@167: { cannam@167: bench_tensor *x = mktensor(sz->rnk); cannam@167: dimcpy(x->dims, sz->dims, sz->rnk); cannam@167: return x; cannam@167: } cannam@167: cannam@167: /* Like tensor_copy, but copy only rnk dimensions starting with start_dim. */ cannam@167: bench_tensor *tensor_copy_sub(const bench_tensor *sz, int start_dim, int rnk) cannam@167: { cannam@167: bench_tensor *x; cannam@167: cannam@167: BENCH_ASSERT(BENCH_FINITE_RNK(sz->rnk) && start_dim + rnk <= sz->rnk); cannam@167: x = mktensor(rnk); cannam@167: dimcpy(x->dims, sz->dims + start_dim, rnk); cannam@167: return x; cannam@167: } cannam@167: cannam@167: bench_tensor *tensor_copy_swapio(const bench_tensor *sz) cannam@167: { cannam@167: bench_tensor *x = tensor_copy(sz); cannam@167: int i; cannam@167: if (BENCH_FINITE_RNK(x->rnk)) cannam@167: for (i = 0; i < x->rnk; ++i) { cannam@167: int s; cannam@167: s = x->dims[i].is; cannam@167: x->dims[i].is = x->dims[i].os; cannam@167: x->dims[i].os = s; cannam@167: } cannam@167: return x; cannam@167: }