File: blastime.cc

package info (click to toggle)
ergo 3.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 17,396 kB
  • sloc: cpp: 94,740; ansic: 17,015; sh: 7,559; makefile: 1,402; yacc: 127; lex: 110; awk: 23
file content (243 lines) | stat: -rw-r--r-- 7,826 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
/* Ergo, version 3.8, a program for linear scaling electronic structure
 * calculations.
 * Copyright (C) 2019 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:
 * Ergo: An open-source program for linear-scaling electronic structure
 * calculations,
 * Elias Rudberg, Emanuel H. Rubensson, Pawel Salek, and Anastasia
 * Kruchinina,
 * SoftwareX 7, 107 (2018),
 * <http://dx.doi.org/10.1016/j.softx.2018.03.005>
 * 
 * For further information about Ergo, see <http://www.ergoscf.org>.
 */

/** @file blastime.cc

    \brief Benchmark code for matrix-matrix multiplication (gemm)
    operation, measuring timings for different matrix sizes.
*/

#include <iostream>
#include <fstream>
#include <iomanip> /* For setprecision in fstream */
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <string.h>
#include <sys/time.h>
#include <vector>

#include "mat_gblas.h"

static const int MIN_TIME_PER_STEP = 5;
static const int SIZE_INCREMENT = 2;

static double get_wall_seconds() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  double seconds = tv.tv_sec + (double)tv.tv_usec / 1000000;
  return seconds;
}

template<class T>
static void tomatlabfile(char* name,T* values,int s,std::ofstream& output);

template<typename real>
int mainFun(int maxDim, double* timev, double* gflops, bool writeTomFile) {
  try {
    //    bool CPUtime = false;
    const real ONE=1.0;
    const real ZERO=0.0;
    double start, end;
    int i;
    int steps = 500000;//50000000;

    /* Find reasonable number of steps */
    double secondsTaken = 0;
    int testSize = SIZE_INCREMENT;
    while (secondsTaken < MIN_TIME_PER_STEP) {
      steps = steps*3;
      real* A=new real [testSize*testSize];
      real* B=new real [testSize*testSize];
      real* C=new real [testSize*testSize];
      for(i = testSize*testSize-1; i>=0; i--) A[i] = 1.0;
      for(i = testSize*testSize-1; i>=0; i--) B[i] = 1.0;
      for(i = testSize*testSize-1; i>=0; i--) C[i] = 0.0;
      int m = testSize;
      int n = testSize;
      int k = testSize;
      start = get_wall_seconds();
      for(int j=0; j<steps; j++) {
	       mat::gemm("N","N",&m,&n,&k,&ONE,A,&m,B,&k,&ZERO,C,&m);
      }
      end = get_wall_seconds();
      secondsTaken = end-start;
    }
    printf("%d tests took %6.2f seconds.\n", steps, secondsTaken);
    
    /* Run actual benchmark */
    int maxStep = maxDim/SIZE_INCREMENT;
    for (int step = 1; step <= maxStep; step++) {
      int size = step*SIZE_INCREMENT;
      real* A=new real [size*size];
      real* B=new real [size*size];
      real* C=new real [size*size];
      for(i = size*size-1; i>=0; i--) A[i] = 1.0;
      for(i = size*size-1; i>=0; i--) B[i] = 1.0;
      for(i = size*size-1; i>=0; i--) C[i] = 0.0;
      int m = size;
      int n = size;
      int k = size;
      
      start = get_wall_seconds();
      for(int j=0; j<steps; j++) {
	       mat::gemm("N","N",&m,&n,&k,&ONE,A,&m,B,&k,&ZERO,C,&m);
      }
      end = get_wall_seconds();
      secondsTaken = end-start;
      timev[step-1] = secondsTaken;
      gflops[step-1]=(2*pow(double(size),3)+4*pow(double(size),2))/(timev[step-1]*1e9)*steps;

      // gflops[step-1]=((2*pow(size,3)+4*pow(size,2))/timev[step-1])*(steps/1000000000);
      delete[] A;
      delete[] B;
      delete[] C;
      std::cout<<"size="<<std::setw(4)<<size
	       <<"    steps="<<std::setw(8)<<steps
	       <<"    time="<<std::setw(6)<<timev[step-1]
	       <<"    Gflops="<<std::setw(10)
	       <<gflops[step-1]<<std::endl;
      if (timev[step-1]>MIN_TIME_PER_STEP*2) {
	/* This prediction does not really work for large relative
	   matrix size increments. But it does its job sufficiently well. */
	int newSteps = 
	  ceil(double(steps)*MIN_TIME_PER_STEP*1.5/double(timev[step-1]));
	std::cout << "Recomputing new steps "<< newSteps <<" = "
		  << steps << " * " << MIN_TIME_PER_STEP << " / "
		  << timev[step-1] << std::endl;
	steps = newSteps;
      }
    }
  }
  catch (std::exception & e) {
    std::cout << "Exception caught: "<<e.what() << std::endl;
    std::exit(1);
  }
  return 0;
}


template<typename T>
static void tomatlabfile(const char* name, T* values, int s,
			 std::ofstream& output) {
  output<<name<<"=[";
  for (int i=0;i<s;i++)
    {
      output<<std::setprecision(10)<<values[i]<<'\n';
    }
  output<<"];"<<std::endl;
}




int main(int argc,char* argv[]) {
  int maxDim;
  char path[200];
  bool writeTomFile = true;
  switch (argc) {
  case 2:
    maxDim = atoi(argv[1]); /* Max matrix dimension. */
    writeTomFile = false;
    break;
  case 3:
    maxDim = atoi(argv[1]); /* Max matrix dimension. */
    strcpy(path, argv[2]);  /* Matlab filename.      */
    break;
  default:
    std::cerr<<"Wrong number of input arguments. Give at least one argument (max matrix dimension) and optionally as a second argument the name of the m-file to create." << std::endl;
    std::exit(1);
  }
  int maxSize = maxDim/SIZE_INCREMENT;
  std::vector<double> timevDouble(maxSize);
  std::vector<double> gflopsDouble(maxSize);
  std::vector<double> timevSingle(maxSize);
  std::vector<double> gflopsSingle(maxSize);
  maxDim = maxSize*SIZE_INCREMENT;

  time_t startTime;
  time_t endTime;
  std::cout<<"Starting gemm benchmark, double precision"<<std::endl;
  time(&startTime);
  if (!mainFun<double>(maxDim, &timevDouble[0], &gflopsDouble[0], writeTomFile)) {
    time(&endTime);
    std::cout<<"Ended gemm benchmark, double precision, wall time: "
	     <<endTime-startTime
	     <<std::endl;    
  }
  std::cout<<"Starting gemm benchmark, single precision"<<std::endl;
  time(&startTime);
  if (!mainFun<float>(maxDim, &timevSingle[0], &gflopsSingle[0], writeTomFile)) {
    time(&endTime);
    std::cout<<"Ended gemm benchmark, single precision, wall time: "
	     <<endTime-startTime
	     <<std::endl;
  }
  
  if (writeTomFile) {
    std::ofstream output(path);
    if (!output) {
      std::cout<<"Cannot open outputfile"<<std::endl;
      std::exit(1);
    }
    output<<"nv=1:" << SIZE_INCREMENT <<":"<<maxDim<<";"<<std::endl;
    tomatlabfile<double>("timeDouble"  , &timevDouble [0], maxSize, output);
    tomatlabfile<double>("GflopsDouble", &gflopsDouble[0], maxSize, output);
    tomatlabfile<double>("timeSingle"  , &timevSingle [0], maxSize, output);
    tomatlabfile<double>("GflopsSingle", &gflopsSingle[0], maxSize, output);
    output<<"minX = 0;\n"
	  <<"maxX = max(nv);\n"
	  <<"minY = 0;\n"
	  <<"maxY = max([GflopsDouble GflopsSingle]);\n"
	  <<"lwidth = 1;\n"
	  <<"fsize  = 12;\n"
	  <<"figure;\n"
	  <<std::endl
	  <<"subplot(221)\n"
	  <<"plot(nv,GflopsDouble,'LineWidth',lwidth);nflops=100000;\n"
	  <<"axis([minX maxX minY maxY(1)])\n"
	  <<"set(gca,'FontSize',fsize)\n"
	  <<"title('Double Precision')\n"
	  <<"xlabel('Matrix Size')\n"
	  <<"ylabel('Gflops')\n"
	  <<"grid on\n"
	  <<std::endl
	  <<"subplot(222)\n"
	  <<"plot(nv,GflopsSingle,'LineWidth',lwidth);nflops=100000;\n"
	  <<"axis([minX maxX minY maxY(2)])\n"
	  <<"set(gca,'FontSize',fsize)\n"
	  <<"title('Single Precision')\n"
	  <<"xlabel('Matrix Size')\n"
	  <<"ylabel('Gflops')\n"
	  <<"grid on\n";
  }
  
  
  std::exit(0);
}