File: ta_dense.cpp

package info (click to toggle)
tiledarray 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 9,568 kB
  • sloc: cpp: 53,449; javascript: 1,599; sh: 393; ansic: 226; python: 223; xml: 195; makefile: 36
file content (187 lines) | stat: -rw-r--r-- 6,399 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
/*
 * This file is a part of TiledArray.
 * Copyright (C) 2013  Virginia Tech
 *
 *  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/>.
 *
 */

#include <TiledArray/version.h>
#include <madness/world/worldmem.h>
#include <tiledarray.h>
#include <iostream>

bool to_bool(const char* str) {
  if (not strcmp(str, "0") || not strcmp(str, "no") || not strcmp(str, "false"))
    return false;
  if (not strcmp(str, "1") || not strcmp(str, "yes") || not strcmp(str, "true"))
    return true;
  throw std::runtime_error("unrecognized string specification of bool");
}

// Leave as underscore for now since without it is broken on gcc 11/03/2015 Drew
template <typename T>
void gemm_(TiledArray::World& world, const TiledArray::TiledRange& trange,
           long repeat);

int main(int argc, char** argv) {
  int rc = 0;

  try {
    // Initialize runtime
    TiledArray::World& world = TiledArray::initialize(argc, argv);

    // Get command line arguments
    if (argc < 3) {
      std::cout << "Usage: " << argv[0]
                << " matrix_size block_size [repetitions] [use_complex]\n";
      return 0;
    }
    const long matrix_size = atol(argv[1]);
    const long block_size = atol(argv[2]);
    if (matrix_size <= 0) {
      std::cerr << "Error: matrix size must be greater than zero.\n";
      return 1;
    }
    if (block_size <= 0) {
      std::cerr << "Error: block size must be greater than zero.\n";
      return 1;
    }
    if ((matrix_size % block_size) != 0ul) {
      std::cerr
          << "Error: matrix size must be evenly divisible by block size.\n";
      return 1;
    }
    const long repeat = (argc >= 4 ? atol(argv[3]) : 5);
    if (repeat <= 0) {
      std::cerr << "Error: number of repetitions must be greater than zero.\n";
      return 1;
    }
    const bool use_complex = (argc >= 5 ? to_bool(argv[4]) : false);

    const std::size_t num_blocks = matrix_size / block_size;
    const std::size_t block_count = num_blocks * num_blocks;

    if (world.rank() == 0)
      std::cout << "TiledArray: dense matrix multiply test..."
                << "\nGit HASH: " << TILEDARRAY_REVISION
                << "\nNumber of nodes     = " << world.size()
                << "\nMatrix size         = " << matrix_size << "x"
                << matrix_size << "\nBlock size          = " << block_size
                << "x" << block_size << "\nMemory per matrix   = "
                << double(matrix_size * matrix_size * sizeof(double)) / 1.0e9
                << " GB\nNumber of blocks    = " << block_count
                << "\nAverage blocks/node = "
                << double(block_count) / double(world.size())
                << "\nComplex             = "
                << (use_complex ? "true" : "false") << "\n";

    // Construct TiledRange
    std::vector<unsigned int> blocking;
    blocking.reserve(num_blocks + 1);
    for (long i = 0l; i <= matrix_size; i += block_size) blocking.push_back(i);

    std::vector<TiledArray::TiledRange1> blocking2(
        2, TiledArray::TiledRange1(blocking.begin(), blocking.end()));

    TiledArray::TiledRange trange(blocking2.begin(), blocking2.end());

    if (use_complex)
      gemm_<std::complex<double>>(world, trange, repeat);
    else
      gemm_<double>(world, trange, repeat);

    TiledArray::finalize();

  } catch (TiledArray::Exception& e) {
    std::cerr << "!! TiledArray exception: " << e.what() << "\n";
    rc = 1;
  } catch (madness::MadnessException& e) {
    std::cerr << "!! MADNESS exception: " << e.what() << "\n";
    rc = 1;
  } catch (SafeMPI::Exception& e) {
    std::cerr << "!! SafeMPI exception: " << e.what() << "\n";
    rc = 1;
  } catch (std::exception& e) {
    std::cerr << "!! std exception: " << e.what() << "\n";
    rc = 1;
  } catch (...) {
    std::cerr << "!! exception: unknown exception\n";
    rc = 1;
  }

  return rc;
}

template <typename T>
void gemm_(TiledArray::World& world, const TiledArray::TiledRange& trange,
           long repeat) {
  const bool do_memtrace = false;

  const auto n = trange.elements_range().extent()[0];
  const auto complex_T = TiledArray::detail::is_complex<T>::value;
  const double gflop =
      (complex_T ? 8 : 2)  // 1 multiply takes 6/1 flops for complex/real
                           // 1 add takes 2/1 flops for complex/real
      * double(n * n * n) / 1.0e9;

  auto memtrace = [do_memtrace, &world](const std::string& str) -> void {
    if (do_memtrace) {
      world.gop.fence();
      madness::print_meminfo(world.rank(), str);
    }
  };

  memtrace("start");
  {  // array lifetime scope
    // Construct and initialize arrays
    TiledArray::TArray<T> a(world, trange);
    TiledArray::TArray<T> b(world, trange);
    TiledArray::TArray<T> c(world, trange);
    a.fill(1.0);
    b.fill(1.0);
    memtrace("allocated a and b");

    // Start clock
    world.gop.fence();
    if (world.rank() == 0)
      std::cout << "Starting iterations: "
                << "\n";

    double total_time = 0.0;
    double total_gflop_rate = 0.0;

    // Do matrix multiplication
    for (int i = 0; i < repeat; ++i) {
      const double start = madness::wall_time();
      c("m,n") = a("m,k") * b("k,n");
      memtrace("c=a*b");
      const double time = madness::wall_time() - start;
      total_time += time;
      const double gflop_rate = gflop / time;
      total_gflop_rate += gflop_rate;
      if (world.rank() == 0)
        std::cout << "Iteration " << i + 1 << "   time=" << time
                  << "   GFLOPS=" << gflop_rate << "\n";
    }

    // Print results
    if (world.rank() == 0)
      std::cout << "Average wall time   = " << total_time / double(repeat)
                << " sec\nAverage GFLOPS      = "
                << total_gflop_rate / double(repeat) << "\n";

  }  // array lifetime scope
  memtrace("stop");
}