1 |
#define CUSP_USE_TEXTURE_MEMORY |
2 |
|
3 |
#include <cusp/csr_matrix.h> |
4 |
#include <cusp/io/matrix_market.h> |
5 |
#include <cusp/gallery/poisson.h> |
6 |
|
7 |
#include <iostream> |
8 |
#include <string> |
9 |
#include <map> |
10 |
#include <cmath> |
11 |
#include <limits> |
12 |
|
13 |
#include <cusp/multiply.h> |
14 |
#include <cusp/detail/device/spmv/csr_scalar.h> |
15 |
|
16 |
#include "bytes_per_spmv.h" |
17 |
#include "utility.h" |
18 |
#include "benchmark.h" |
19 |
|
20 |
typedef std::map<std::string, std::string> ArgumentMap; |
21 |
ArgumentMap args; |
22 |
|
23 |
std::string process_args(int argc, char ** argv) |
24 |
{ |
25 |
std::string filename; |
26 |
|
27 |
for(int i = 1; i < argc; i++) |
28 |
{ |
29 |
std::string arg(argv[i]); |
30 |
|
31 |
if (arg.substr(0,2) == "--") |
32 |
{ |
33 |
std::string::size_type n = arg.find('=',2); |
34 |
|
35 |
if (n == std::string::npos) |
36 |
args[arg.substr(2)] = std::string(); // (key) |
37 |
else |
38 |
args[arg.substr(2, n - 2)] = arg.substr(n + 1); // (key,value) |
39 |
} |
40 |
else |
41 |
{ |
42 |
filename = arg; |
43 |
} |
44 |
} |
45 |
|
46 |
return filename; |
47 |
} |
48 |
|
49 |
void usage(int argc, char** argv) |
50 |
{ |
51 |
std::cout << "Usage:\n"; |
52 |
std::cout << "\t" << argv[0] << "\n"; |
53 |
std::cout << "\t" << argv[0] << " my_matrix.mtx\n"; |
54 |
std::cout << "\t" << argv[0] << " my_matrix.mtx --device=1\n"; |
55 |
std::cout << "\t" << argv[0] << " my_matrix.mtx --value_type=double\n\n"; |
56 |
std::cout << "Note: my_matrix.mtx must be real-valued sparse matrix in the MatrixMarket file format.\n"; |
57 |
std::cout << " If no matrix file is provided then a simple example is created.\n"; |
58 |
} |
59 |
|
60 |
|
61 |
template <typename IndexType, typename ValueType> |
62 |
void test_all_formats(std::string& filename) |
63 |
{ |
64 |
int device_id = args.count("device") ? atoi(args["device"].c_str()) : 0; |
65 |
set_device(device_id); |
66 |
list_devices(); |
67 |
|
68 |
std::cout << "Running on Device " << device_id << "\n\n"; |
69 |
|
70 |
// load a matrix stored in MatrixMarket format |
71 |
cusp::csr_matrix<IndexType, ValueType, cusp::host_memory> host_matrix; |
72 |
|
73 |
if (filename == "") |
74 |
{ |
75 |
std::cout << "Generated matrix (poisson5pt) "; |
76 |
cusp::gallery::poisson5pt(host_matrix, 512, 512); |
77 |
} |
78 |
else |
79 |
{ |
80 |
cusp::io::read_matrix_market_file(host_matrix, filename); |
81 |
std::cout << "Read matrix (" << filename << ") "; |
82 |
} |
83 |
|
84 |
std::cout << "with shape (" << host_matrix.num_rows << "," << host_matrix.num_cols << ") and " |
85 |
<< host_matrix.num_entries << " entries" << "\n\n"; |
86 |
|
87 |
FILE * fid = fopen(BENCHMARK_OUTPUT_FILE_NAME, "a"); |
88 |
fprintf(fid, "file=%s rows=%d cols=%d nonzeros=%d\n", filename.c_str(), (int) host_matrix.num_rows, (int) host_matrix.num_cols, (int) host_matrix.num_entries); |
89 |
fclose(fid); |
90 |
|
91 |
test_coo(host_matrix); |
92 |
test_csr(host_matrix); |
93 |
test_dia(host_matrix); |
94 |
test_ell(host_matrix); |
95 |
test_hyb(host_matrix); |
96 |
} |
97 |
|
98 |
int main(int argc, char** argv) |
99 |
{ |
100 |
std::string filename = process_args(argc, argv); |
101 |
|
102 |
if (args.count("help")) |
103 |
{ |
104 |
usage(argc, argv); |
105 |
return 0; |
106 |
} |
107 |
|
108 |
// select ValueType |
109 |
std::string value_type = args.count("value_type") ? args["value_type"] : "float"; |
110 |
std::cout << "\nComputing SpMV with \'" << value_type << "\' values.\n\n"; |
111 |
|
112 |
if (value_type == "float") |
113 |
{ |
114 |
test_all_formats<int,float>(filename); |
115 |
} |
116 |
else if (value_type == "double") |
117 |
{ |
118 |
int current_device = -1; |
119 |
cudaDeviceProp properties; |
120 |
cudaGetDevice(¤t_device); |
121 |
cudaGetDeviceProperties(&properties, current_device); |
122 |
if (properties.major == 1 && properties.minor < 3) |
123 |
std::cerr << "ERROR: Support for \'double\' requires Compute Capability 1.3 or greater\n\n"; |
124 |
else |
125 |
test_all_formats<int,double>(filename); |
126 |
} |
127 |
else |
128 |
{ |
129 |
std::cerr << "ERROR: Unsupported type \'" << value_type << "\'\n\n"; |
130 |
} |
131 |
|
132 |
return 0; |
133 |
} |
134 |
|