-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
171 lines (146 loc) · 5.76 KB
/
main.cpp
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
/*
* (c) 2017 Virginia Polytechnic Institute & State University (Virginia Tech)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License Version 2.1.
*
* 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
* LICENSE in the root of the repository for details.
*
*/
/* For testing of different SpTrans solutions */
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <sys/time.h> // timing
#include "matio.h"
#include "sptrans.h"
#ifdef MKL
#include <mkl_spblas.h>
#include <mkl.h>
#include <typeinfo>
#endif
#define valT double
double dtime() // milliseconds
{
double tseconds = 0.0;
struct timeval mytime;
gettimeofday(&mytime, (struct timezone*)0);
tseconds = (double)(mytime.tv_sec + mytime.tv_usec*1.0e-6);
return (tseconds*1.0e3);
}
int main(int argc, char **argv)
{
char *filename = NULL;
if(argc > 1)
{
filename = argv[1];
}
if(filename == NULL)
{
std::cout << "Error: No file provided!\n";
return EXIT_FAILURE;
}
std::cout << "matrix: " << filename << std::endl;
// input
int m, n, nnzA;
int *csrRowPtrA;
int *csrColIdxA;
valT *csrValA;
int retCode = read_mtx_mat(m, n, nnzA, csrRowPtrA, csrColIdxA, csrValA, filename);
if(retCode != 0)
{
std::cout << "Failed to read the matrix from " << filename << "!\n";
return EXIT_FAILURE;
}
double tstart, tstop, ttime;
#ifdef MKL
int nthread;
#pragma omp parallel
nthread = omp_get_num_threads();
mkl_set_num_threads(nthread);
sparse_index_base_t indexing = SPARSE_INDEX_BASE_ZERO;
sparse_status_t status = SPARSE_STATUS_SUCCESS;
valT *cscval;
sparse_matrix_t A, AT;
MKL_INT cscn, cscm, *pntrb, *pntre, *cscidx;
if(typeid(valT) == typeid(float))
{
status = mkl_sparse_s_create_csr(&A, SPARSE_INDEX_BASE_ZERO,
(MKL_INT)m, (MKL_INT)n, (MKL_INT *)csrRowPtrA,
(MKL_INT *)(csrRowPtrA + 1), (MKL_INT *)csrColIdxA, (float*)csrValA);
tstart = dtime();
status = mkl_sparse_convert_csr(A, SPARSE_OPERATION_TRANSPOSE, &AT);
tstop = dtime();
ttime = tstop - tstart;
std::cout << "MKL(time): " << ttime << " ms\n";
status = mkl_sparse_s_export_csr(AT, &indexing,
&cscn, &cscm, &pntrb, &pntre, &cscidx, (float**)&cscval);
} else if(typeid(valT) == typeid(double))
{
status = mkl_sparse_d_create_csr(&A, SPARSE_INDEX_BASE_ZERO,
(MKL_INT)m, (MKL_INT)n, (MKL_INT *)csrRowPtrA,
(MKL_INT *)(csrRowPtrA + 1), (MKL_INT *)csrColIdxA, (double*)csrValA);
tstart = dtime();
status = mkl_sparse_convert_csr(A, SPARSE_OPERATION_TRANSPOSE, &AT);
tstop = dtime();
ttime = tstop - tstart;
std::cout << "MKL(time): " << ttime << " ms\n";
status = mkl_sparse_d_export_csr(AT, &indexing,
&cscn, &cscm, &pntrb, &pntre, &cscidx, (double**)&cscval);
}
if (SPARSE_STATUS_SUCCESS != status)
{
std::cout << "Failed to do MKL convert!\n" << std::endl;
mkl_sparse_destroy(A);
mkl_sparse_destroy(AT);
free(csrColIdxA);
free(csrValA);
free(csrRowPtrA);
return EXIT_FAILURE;
}
#endif
int *cscRowIdxA = (int *)malloc(nnzA * sizeof(int));
int *cscColPtrA = (int *)malloc((n + 1) * sizeof(int));
valT *cscValA = (valT *)malloc(nnzA * sizeof(valT));
// clear the buffers
std::fill_n(cscRowIdxA, nnzA, 0);
std::fill_n(cscValA, nnzA, 0);
std::fill_n(cscColPtrA, n+1, 0);
tstart = dtime();
sptrans_scanTrans<int, valT>(m, n, nnzA, csrRowPtrA, csrColIdxA, csrValA, cscRowIdxA, cscColPtrA, cscValA);
tstop = dtime();
ttime = tstop - tstart;
std::cout << "scanTrans(time): " << ttime << " ms\n";
#ifdef MKL
std::cout << "check vals: " << std::boolalpha << std::equal(cscval, cscval+nnzA, cscValA) << std::endl;
std::cout << "check rowIdx: " << std::boolalpha << std::equal((int*)cscidx, (int*)(cscidx+nnzA), cscRowIdxA) << std::endl;
std::cout << "check colPtr: " << std::boolalpha << std::equal((int*)pntrb, (int*)(pntrb+n), cscColPtrA) << std::endl;
#endif
// clear the buffers
std::fill_n(cscRowIdxA, nnzA, 0);
std::fill_n(cscValA, nnzA, 0);
std::fill_n(cscColPtrA, n+1, 0);
tstart = dtime();
sptrans_mergeTrans<int, valT>(m, n, nnzA, csrRowPtrA, csrColIdxA, csrValA, cscRowIdxA, cscColPtrA, cscValA);
tstop = dtime();
ttime = tstop - tstart;
std::cout << "mergeTrans(time): " << ttime << " ms\n";
#ifdef MKL
std::cout << "check vals: " << std::boolalpha << std::equal(cscval, cscval+nnzA, cscValA) << std::endl;
std::cout << "check rowIdx: " << std::boolalpha << std::equal((int*)cscidx, (int*)(cscidx+nnzA), cscRowIdxA) << std::endl;
std::cout << "check colPtr: " << std::boolalpha << std::equal((int*)pntrb, (int*)(pntrb+n), cscColPtrA) << std::endl;
#endif
free(csrRowPtrA);
free(csrColIdxA);
free(csrValA);
free(cscRowIdxA);
free(cscColPtrA);
free(cscValA);
#ifdef MKL
mkl_sparse_destroy(A);
mkl_sparse_destroy(AT);
#endif
}