File: matrix_algebra.cc

package info (click to toggle)
ergo 3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 17,044 kB
  • ctags: 6,813
  • sloc: cpp: 91,488; ansic: 15,728; sh: 6,416; makefile: 1,287; yacc: 123; lex: 108
file content (268 lines) | stat: -rw-r--r-- 6,586 bytes parent folder | download
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
/* Ergo, version 3.5, a program for linear scaling electronic structure
 * calculations.
 * Copyright (C) 2016 Elias Rudberg, Emanuel H. Rubensson, Pawel Salek,
 * and Anastasia Kruchinina.
 * 
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 * 
 * Primary academic reference:
 * Kohn−Sham Density Functional Theory Electronic Structure Calculations 
 * with Linearly Scaling Computational Time and Memory Usage,
 * Elias Rudberg, Emanuel H. Rubensson, and Pawel Salek,
 * J. Chem. Theory Comput. 7, 340 (2011),
 * <http://dx.doi.org/10.1021/ct100611z>
 * 
 * For further information about Ergo, see <http://www.ergoscf.org>.
 */

#include <stdlib.h>

#include "matrix_algebra.h"
#include "memorymanag.h"
#include "output.h"

#include "../matrix/mat_gblas.h"


#define USE_BLAS_MM


#if 0
#ifdef USE_BLAS_MM
#ifdef __cplusplus
extern "C" 
#endif
void dgemm_(const char *ta,const char *tb,
	    const int *n, const int *k, const int *l,
	    const double *alpha,const double *A,const int *lda,
	    const double *B, const int *ldb,
	    const double *beta,const double *C, const int *ldc);
#endif
#endif


void multiply_matrices_general_2(int An1, int An2, int Bn1, int Bn2, const ergo_real* A, const ergo_real* B, ergo_real* AB, bool initToZero) {
  if(An2 != Bn1) {
    do_output(LOG_CAT_ERROR, LOG_AREA_LOWLEVEL, "error in multiply_matrices_general_2: (An2 != Bn1)");
    exit(0);
  }
#ifdef USE_BLAS_MM
  if(An1 == 0 || An2 == 0 || Bn1 == 0 || Bn2 == 0)
    return;
  /*  call gemm */
  ergo_real alpha = 1;
  ergo_real beta = 1;
  if(initToZero)
    beta = 0;
  mat::gemm("N", "N", &Bn2, &An1, &Bn1, &alpha, 
	    B, &Bn2, 
	    A, &An2, 
	    &beta, 
	    AB, &Bn2);
#else
  if(initToZero)
    memset(AB, 0, An1*Bn2*sizeof(ergo_real));
  for(int i = 0; i < An1; i++)
    for(int k = 0; k < An2; k++)
      for(int j = 0; j < Bn2; j++)
	AB[i*Bn2+j] += A[i*An2+k] * B[k*Bn2+j];
#endif
}



/*
  Standard matrix multiplication.
*/
void 
multiply_matrices_general(int An1, int An2, int Bn1, int Bn2, const ergo_real* A, const ergo_real* B, ergo_real* AB)
{
  int i, j;
  if(An2 != Bn1)
    {
      do_output(LOG_CAT_ERROR, LOG_AREA_LOWLEVEL, 
		"error in multiply_matrices_general: (An2 != Bn1)");
      exit(0);
    }
#ifdef USE_BLAS_MM
  if(An1 == 0 || An2 == 0 || Bn1 == 0 || Bn2 == 0)
    return;
  /*  call gemm */
  ergo_real alpha = 1;
  ergo_real beta = 0;
  ergo_real* ABtemp = (ergo_real*)ergo_malloc(An1*Bn2*sizeof(ergo_real));
  memset(ABtemp, 0, An1*Bn2*sizeof(ergo_real));
  mat::gemm("T", "T", &An1, &Bn2, &An2, &alpha, 
	    A, &An2, 
	    B, &Bn2, 
	    &beta, 
	    ABtemp, &An1);
  /*  do transpose of result */
  for(i = 0; i < An1; i++)
    for(j = 0; j < Bn2; j++)
      {
	AB[i*Bn2+j] = ABtemp[j*An1+i];
      }  
  ergo_free(ABtemp);
#else
  for(i = 0; i < An1; i++)
    for(j = 0; j < Bn2; j++)
      {
	ergo_real sum = 0;
	for(int k = 0; k < An2; k++)
	  sum += A[i*An2+k] * B[k*Bn2+j];
	AB[i*Bn2+j] = sum;
      }
#endif
}


/*
  Matrix multiplication when the first matrix is transposed.
*/
void 
multiply_matrices_general_T_1(int An1, int An2, int Bn1, int Bn2, const ergo_real* A, const ergo_real* B, ergo_real* AB)
{
  int i, j;
  if(An1 != Bn1)
    {
      do_output(LOG_CAT_ERROR, LOG_AREA_LOWLEVEL, "error in multiply_matrices_general_T_1: (An1 != Bn1)");
      exit(0);
    }
  ergo_real* At = (ergo_real*)ergo_malloc(An1*An2*sizeof(ergo_real));
  for(i = 0; i < An1; i++)
    for(j = 0; j < An2; j++)
      At[j*An1+i] = A[i*An2+j];
  multiply_matrices_general(An2, An1, Bn1, Bn2, At, B, AB);
  ergo_free(At);
}

/*
  Matrix multiplication when the second matrix is transposed.
*/
void 
multiply_matrices_general_T_2(int An1, int An2, int Bn1, int Bn2, const ergo_real* A, const ergo_real* B, ergo_real* AB)
{
  int i, j;
  if(An2 != Bn2)
    {
      do_output(LOG_CAT_ERROR, LOG_AREA_LOWLEVEL, "error in multiply_matrices_general_T_2: (An2 != Bn2)");
      exit(0);
    }
  ergo_real* Bt = (ergo_real*)ergo_malloc(Bn1*Bn2*sizeof(ergo_real));
  for(i = 0; i < Bn1; i++)
    for(j = 0; j < Bn2; j++)
      Bt[j*Bn1+i] = B[i*Bn2+j];
  multiply_matrices_general(An1, An2, Bn2, Bn1, A, Bt, AB);
  ergo_free(Bt);
}


void 
multiply2matrices(int n, ergo_real* A, ergo_real* B, ergo_real* AB)
{
#ifdef USE_BLAS_MM
  if(n == 0)
    return;
  /*  call dgemm */
  int i, j;
  int nn = n;
  ergo_real alpha = 1;
  ergo_real beta = 0;
  mat::gemm("T", "T", &nn, &nn, &nn, &alpha, 
	    A, &nn, 
	    B, &nn, 
	    &beta, 
	    AB, &nn);
  /*  do transpose of result */
  for(i = 0; i < n; i++)
    for(j = i; j < n; j++)
      {
	ergo_real temp = AB[i*n+j];
	AB[i*n+j] = AB[j*n+i];
	AB[j*n+i] = temp;
      }
#else
  int i, j, k;
  for(i = 0; i < n; i++)
    for(j = 0; j < n; j++)
      {
	ergo_real sum = 0;
	for(k = 0; k < n; k++)
	  sum += A[i*n+k] * B[k*n+j];
	AB[i*n+j] = sum;
      }
#endif
}


void 
multiply2matricesSymm(int n, ergo_real* A, ergo_real* B, ergo_real* AB)
{
  int i, j, k;
  for(i = 0; i < n; i++)
    for(j = 0; j < n; j++)
      {
	ergo_real sum = 0;
	for(k = 0; k < n; k++)
	  sum += A[i*n+k] * B[j*n+k];
	AB[i*n+j] = sum;
      }
}

void 
multiply2matricesSymmResult(int n, ergo_real* A, ergo_real* B, ergo_real* AB)
{
  int i, j, k;
  for(i = 0; i < n; i++)
    for(j = 0; j <= i; j++)
      {
	ergo_real sum = 0;
	for(k = 0; k < n; k++)
	  sum += A[i*n+k] * B[k*n+j];
	AB[i*n+j] = sum;
      }
  for(i = 0; i < n; i++)
    for(j = i+1; j < n; j++)
      AB[i*n+j] = AB[j*n+i];
}


void 
computeSquareOfSymmetricMatrix(int n, const ergo_real* Aa, const ergo_real* Ab, ergo_real* A2)
{
  int i, j;
  for(i = 0; i < n; i++)
    for(j = 0; j < n; j++)
      {
	ergo_real sum = 0;
	int k;
	for(k = 0; k < n; k++)
	  sum += Aa[i*n+k] * Ab[j*n+k];
	A2[i*n+j] = sum;
      }
  return;
}


void multiply3matrices(int n, ergo_real* A, ergo_real* B, ergo_real* C, ergo_real* ABC)
{
  ergo_real* AB = (ergo_real*)ergo_malloc(n*n*sizeof(ergo_real));
  multiply2matrices(n, A, B, AB);
  multiply2matrices(n, AB, C, ABC);
  ergo_free(AB);
}