/usr/include/madness/tensor/mxm.h is in libmadness-dev 0.10-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | /*
This file is part of MADNESS.
Copyright (C) 2007,2010 Oak Ridge National Laboratory
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
For more information please contact:
Robert J. Harrison
Oak Ridge National Laboratory
One Bethel Valley Road
P.O. Box 2008, MS-6367
email: harrisonrj@ornl.gov
tel: 865-241-3937
fax: 865-572-0680
$Id$
*/
#ifndef MADNESS_TENSOR_MXM_H__INCLUDED
#define MADNESS_TENSOR_MXM_H__INCLUDED
/// \file tensor/mxm.h
/// \brief Internal use only
// This file is ONLY included into tensor.cc ... separated here just
// to shrink file size. Don't try to include anywhere else
// Due to both flakey compilers and performance concerns,
// we use a simple reference implementation of the mxm
// routines for all except T=double.
/// Matrix * matrix reference implementation (slow but correct)
template <typename T, typename Q, typename S>
static inline void mxm(long dimi, long dimj, long dimk,
T* restrict c, const Q* restrict a,
const S* restrict b) {
/*
c(i,j) = c(i,j) + sum(k) a(i,k)*b(k,j)
where it is assumed that the last index in each array is has unit
stride and the dimensions are as provided.
*/
for (long i=0; i<dimi; ++i) {
for (long k=0; k<dimk; ++k) {
for (long j=0; j<dimj; ++j) {
c[i*dimj+j] += a[i*dimk+k]*b[k*dimj+j];
}
}
}
}
/// Matrix transpose * matrix ... reference implementation (slow but correct)
template <typename T, typename Q, typename S>
static inline
void mTxm(long dimi, long dimj, long dimk,
T* restrict c, const Q* restrict a,
const S* restrict b) {
/*
c(i,j) = c(i,j) + sum(k) a(k,i)*b(k,j)
where it is assumed that the last index in each array is has unit
stride and the dimensions are as provided.
i loop might be long in anticipated application
*/
for (long k=0; k<dimk; ++k) {
for (long j=0; j<dimj; ++j) {
for (long i=0; i<dimi; ++i) {
c[i*dimj+j] += a[k*dimi+i]*b[k*dimj+j];
}
}
}
}
/// Matrix * matrix transpose ... reference implementation (slow but correct)
template <typename T, typename Q, typename S>
static inline void mxmT(long dimi, long dimj, long dimk,
T* restrict c, const Q* restrict a,
const S* restrict b) {
/*
c(i,j) = c(i,j) + sum(k) a(i,k)*b(j,k)
where it is assumed that the last index in each array is has unit
stride and the dimensions are as provided.
i loop might be long in anticipated application
*/
for (long i=0; i<dimi; ++i) {
for (long j=0; j<dimj; ++j) {
T sum = 0;
for (long k=0; k<dimk; ++k) {
sum += a[i*dimk+k]*b[j*dimk+k];
}
c[i*dimj+j] += sum;
}
}
}
/// Matrix transpose * matrix transpose reference implementation (slow but correct)
template <typename T, typename Q, typename S>
static inline void mTxmT(long dimi, long dimj, long dimk,
T* restrict c, const Q* restrict a,
const S* restrict b) {
/*
c(i,j) = c(i,j) + sum(k) a(k,i)*b(j,k)
where it is assumed that the last index in each array is has unit
stride and the dimensions are as provided.
*/
for (long i=0; i<dimi; ++i) {
for (long j=0; j<dimj; ++j) {
for (long k=0; k<dimk; ++k) {
c[i*dimj+j] += a[k*dimi+i]*b[j*dimk+k];
}
}
}
}
// The following are restricted to double only
/// Matrix transpose * matrix (hand unrolled version)
template <>
inline void mTxm(long dimi, long dimj, long dimk,
double* restrict c, const double* restrict a,
const double* restrict b) {
/*
c(i,j) = c(i,j) + sum(k) a(k,i)*b(k,j) <--- NOTE ACCUMULATION INTO C
where it is assumed that the last index in each array is has unit
stride and the dimensions are as provided.
i loop might be long in anticipated application
4-way unrolled k loop ... empirically fastest on PIII
compared to 2/3 way unrolling (though not by much).
*/
long dimk4 = (dimk/4)*4;
for (long i=0; i<dimi; ++i,c+=dimj) {
const double* ai = a+i;
const double* p = b;
for (long k=0; k<dimk4; k+=4,ai+=4*dimi,p+=4*dimj) {
double ak0i = ai[0 ];
double ak1i = ai[dimi];
double ak2i = ai[dimi+dimi];
double ak3i = ai[dimi+dimi+dimi];
const double* bk0 = p;
const double* bk1 = p+dimj;
const double* bk2 = p+dimj+dimj;
const double* bk3 = p+dimj+dimj+dimj;
for (long j=0; j<dimj; ++j) {
c[j] += ak0i*bk0[j] + ak1i*bk1[j] + ak2i*bk2[j] + ak3i*bk3[j];
}
}
for (long k=dimk4; k<dimk; ++k) {
double aki = a[k*dimi+i];
const double* bk = b+k*dimj;
for (long j=0; j<dimj; ++j) {
c[j] += aki*bk[j];
}
}
}
}
/// Matrix * matrix transpose (hand unrolled version)
template <>
inline void mxmT(long dimi, long dimj, long dimk,
double* restrict c,
const double* restrict a, const double* restrict b) {
/*
c(i,j) = c(i,j) + sum(k) a(i,k)*b(j,k)
where it is assumed that the last index in each array is has unit
stride and the dimensions are as provided.
j loop might be long in anticipated application
Unrolled i loop. Empirically fastest on PIII compared
to unrolling j, or both i&j.
*/
long dimi2 = (dimi/2)*2;
for (long i=0; i<dimi2; i+=2) {
const double* ai0 = a+i*dimk;
const double* ai1 = a+i*dimk+dimk;
double* restrict ci0 = c+i*dimj;
double* restrict ci1 = c+i*dimj+dimj;
for (long j=0; j<dimj; ++j) {
double sum0 = 0;
double sum1 = 0;
const double* bj = b + j*dimk;
for (long k=0; k<dimk; ++k) {
sum0 += ai0[k]*bj[k];
sum1 += ai1[k]*bj[k];
}
ci0[j] += sum0;
ci1[j] += sum1;
}
}
for (long i=dimi2; i<dimi; ++i) {
const double* ai = a+i*dimk;
double* restrict ci = c+i*dimj;
for (long j=0; j<dimj; ++j) {
double sum = 0;
const double* bj = b+j*dimk;
for (long k=0; k<dimk; ++k) {
sum += ai[k]*bj[k];
}
ci[j] += sum;
}
}
}
/// Matrix * matrix (hand unrolled version)
template <>
inline void mxm(long dimi, long dimj, long dimk,
double* restrict c, const double* restrict a, const double* restrict b) {
/*
c(i,j) = c(i,j) + sum(k) a(i,k)*b(k,j)
where it is assumed that the last index in each array is has unit
stride and the dimensions are as provided.
4-way unrolled k loop ... empirically fastest on PIII
compared to 2/3 way unrolling (though not by much).
*/
long dimk4 = (dimk/4)*4;
for (long i=0; i<dimi; ++i, c+=dimj,a+=dimk) {
const double* p = b;
for (long k=0; k<dimk4; k+=4,p+=4*dimj) {
double aik0 = a[k ];
double aik1 = a[k+1];
double aik2 = a[k+2];
double aik3 = a[k+3];
const double* bk0 = p;
const double* bk1 = bk0+dimj;
const double* bk2 = bk1+dimj;
const double* bk3 = bk2+dimj;
for (long j=0; j<dimj; ++j) {
c[j] += aik0*bk0[j] + aik1*bk1[j] + aik2*bk2[j] + aik3*bk3[j];
}
}
for (long k=dimk4; k<dimk; ++k) {
double aik = a[k];
for (long j=0; j<dimj; ++j) {
c[j] += aik*b[k*dimj+j];
}
}
}
}
/// Matrix transpose * matrix transpose (hand tiled and unrolled)
template <>
inline void mTxmT(long dimi, long dimj, long dimk,
double* restrict csave, const double* restrict asave, const double* restrict b) {
/*
c(i,j) = c(i,j) + sum(k) a(k,i)*b(j,k)
where it is assumed that the last index in each array is has unit
stride and the dimensions are as provided.
Tiled k, copy row of a into temporary, and unroll j once.
*/
const int ktile=32;
double ai[ktile];
long dimj2 = (dimj/2)*2;
for (long klo=0; klo<dimk; klo+=ktile, asave+=ktile*dimi, b+=ktile) {
long khi = klo+ktile;
if (khi > dimk) khi = dimk;
long nk = khi-klo;
const double *restrict a = asave;
double *restrict c = csave;
for (long i=0; i<dimi; ++i,c+=dimj,++a) {
const double* q = a;
for (long k=0; k<nk; ++k,q+=dimi) ai[k] = *q;
const double* bj0 = b;
for (long j=0; j<dimj2; j+=2,bj0+=2*dimk) {
const double* bj1 = bj0+dimk;
double sum0 = 0;
double sum1 = 0;
for (long k=0; k<nk; ++k) {
sum0 += ai[k]*bj0[k];
sum1 += ai[k]*bj1[k];
}
c[j ] += sum0;
c[j+1] += sum1;
}
for (long j=dimj2; j<dimj; ++j,bj0+=dimk) {
double sum = 0;
for (long k=0; k<nk; ++k) {
sum += ai[k]*bj0[k];
}
c[j] += sum;
}
}
}
}
#endif // MADNESS_TENSOR_MXM_H__INCLUDED
|