6
6
#include " ../cblas.h"
7
7
#include " cpp_thread_safety_common.h"
8
8
9
- void launch_cblas_dgemv (double * A, double * x, double * y, const blasint randomMatSize){
9
+ void launch_cblas_dgemv (double * A, double * x, double * y, const blasint randomMatSize)
10
+ {
10
11
const blasint inc = 1 ;
11
12
cblas_dgemv (CblasColMajor, CblasNoTrans, randomMatSize, randomMatSize, 1.0 , A, randomMatSize, x, inc, 0.1 , y, inc);
12
- }
13
+ }
13
14
14
- int main (int argc, char * argv[]){
15
+ int main (int argc, char * argv[])
16
+ {
15
17
blasint randomMatSize = 1024 ; // dimension of the random square matrices and vectors being used
16
18
uint32_t numConcurrentThreads = 52 ; // number of concurrent calls of the functions being tested
17
19
uint32_t numTestRounds = 16 ; // number of testing rounds before success exit
@@ -20,20 +22,23 @@ int main(int argc, char* argv[]){
20
22
if (maxHwThreads < 52 )
21
23
numConcurrentThreads = maxHwThreads;
22
24
23
- if (argc > 4 ){
25
+ if (argc > 4 )
26
+ {
24
27
std::cout<<" ERROR: too many arguments for thread safety tester" <<std::endl;
25
28
abort ();
26
- }
27
- if (argc == 4 ){
29
+ }
30
+ if (argc == 4 )
31
+ {
28
32
std::vector<std::string> cliArgs;
29
- for (int i = 1 ; i < argc; i++){
33
+ for (int i = 1 ; i < argc; i++)
34
+ {
30
35
cliArgs.push_back (argv[i]);
31
36
std::cout<<argv[i]<<std::endl;
32
- }
37
+ }
33
38
randomMatSize = std::stoul (cliArgs.at (0 ));
34
39
numConcurrentThreads = std::stoul (cliArgs.at (1 ));
35
40
numTestRounds = std::stoul (cliArgs.at (2 ));
36
- }
41
+ }
37
42
38
43
std::uniform_real_distribution<double > rngdist{-1.0 , 1.0 };
39
44
std::vector<std::vector<double >> matBlock (numConcurrentThreads);
@@ -56,15 +61,18 @@ int main(int argc, char* argv[]){
56
61
57
62
std::cout<<" Preparing to test CBLAS DGEMV thread safety\n " ;
58
63
std::cout<<" Allocating matrices..." <<std::flush;
59
- for (uint32_t i=0 ; i<numConcurrentThreads; i++){
64
+ for (uint32_t i=0 ; i<numConcurrentThreads; i++)
65
+ {
60
66
matBlock.at (i).resize (randomMatSize*randomMatSize);
61
- }
67
+ }
62
68
std::cout<<" done\n " ;
63
69
std::cout<<" Allocating vectors..." <<std::flush;
64
- for (uint32_t i=0 ; i<(numConcurrentThreads*2 ); i++){
70
+ for (uint32_t i=0 ; i<(numConcurrentThreads*2 ); i++)
71
+ {
65
72
vecBlock.at (i).resize (randomMatSize);
66
- }
73
+ }
67
74
std::cout<<" done\n " ;
75
+
68
76
// pauser();
69
77
70
78
std::cout<<" Filling matrices with random numbers..." <<std::flush;
@@ -77,31 +85,35 @@ int main(int argc, char* argv[]){
77
85
78
86
std::cout<<" Testing CBLAS DGEMV thread safety" <<std::endl;
79
87
omp_set_num_threads (numConcurrentThreads);
80
- for (uint32_t R=0 ; R<numTestRounds; R++){
88
+ for (uint32_t R=0 ; R<numTestRounds; R++)
89
+ {
81
90
std::cout<<" DGEMV round #" <<R<<std::endl;
82
91
std::cout<<" Launching " <<numConcurrentThreads<<" threads simultaneously using OpenMP..." <<std::flush;
83
92
#pragma omp parallel for default(none) shared(futureBlock, matBlock, vecBlock, randomMatSize, numConcurrentThreads)
84
- for (uint32_t i=0 ; i<numConcurrentThreads; i++){
93
+ for (uint32_t i=0 ; i<numConcurrentThreads; i++)
94
+ {
85
95
futureBlock[i] = std::async (std::launch::async, launch_cblas_dgemv, &matBlock[i][0 ], &vecBlock[i*2 ][0 ], &vecBlock[i*2 +1 ][0 ], randomMatSize);
86
- }
96
+ }
87
97
std::cout<<" done\n " ;
88
98
std::cout<<" Waiting for threads to finish..." <<std::flush;
89
- for (uint32_t i=0 ; i<numConcurrentThreads; i++){
99
+ for (uint32_t i=0 ; i<numConcurrentThreads; i++)
100
+ {
90
101
futureBlock[i].get ();
91
- }
102
+ }
92
103
std::cout<<" done\n " ;
93
104
std::cout<<" Comparing results from different threads..." <<std::flush;
94
105
for (uint32_t i=2 ; i<(numConcurrentThreads*2 ); i+=2 ){ // i is the index of vector x, for a given thread
95
- for (uint32_t j = 0 ; j < static_cast <uint32_t >(randomMatSize); j++){
106
+ for (uint32_t j = 0 ; j < static_cast <uint32_t >(randomMatSize); j++)
107
+ {
96
108
if (std::abs (vecBlock[i+1 ][j] - vecBlock[1 ][j]) > 1.0E-13 ){ // i+1 is the index of vector y, for a given thread
97
109
std::cout<<" ERROR: one of the threads returned a different result! Index : " <<i+1 <<std::endl;
98
110
std::cout<<" CBLAS DGEMV thread safety test FAILED!" <<std::endl;
99
111
return -1 ;
112
+ }
100
113
}
101
114
}
115
+ std::cout<<" OK!\n " <<std::endl;
102
116
}
103
- std::cout<<" OK!\n " <<std::endl;
104
- }
105
117
std::cout<<" CBLAS DGEMV thread safety test PASSED!\n " <<std::endl;
106
118
return 0 ;
107
- }
119
+ }
0 commit comments