7 #include "cpp_thread_safety_common.h"
9 void launch_cblas_dgemv(double* A, double* x, double* y, const blasint randomMatSize){
10 const blasint inc = 1;
11 cblas_dgemv(CblasColMajor, CblasNoTrans, randomMatSize, randomMatSize, 1.0, A, randomMatSize, x, inc, 0.1, y, inc);
14 int main(int argc, char* argv[]){
15 blasint randomMatSize = 1024; //dimension of the random square matrices and vectors being used
16 uint32_t numConcurrentThreads = 52; //number of concurrent calls of the functions being tested
17 uint32_t numTestRounds = 16; //number of testing rounds before success exit
18 uint32_t maxHwThreads = omp_get_max_threads();
20 if (maxHwThreads < 52)
21 numConcurrentThreads = maxHwThreads;
24 std::cout<<"ERROR: too many arguments for thread safety tester"<<std::endl;
28 std::vector<std::string> cliArgs;
29 for (int i = 1; i < argc; i++){
30 cliArgs.push_back(argv[i]);
31 std::cout<<argv[i]<<std::endl;
33 randomMatSize = std::stoul(cliArgs.at(0));
34 numConcurrentThreads = std::stoul(cliArgs.at(1));
35 numTestRounds = std::stoul(cliArgs.at(2));
38 std::uniform_real_distribution<double> rngdist{-1.0, 1.0};
39 std::vector<std::vector<double>> matBlock(numConcurrentThreads);
40 std::vector<std::vector<double>> vecBlock(numConcurrentThreads*2);
41 std::vector<std::future<void>> futureBlock(numConcurrentThreads);
43 std::cout<<"*----------------------------*\n";
44 std::cout<<"| DGEMV thread safety tester |\n";
45 std::cout<<"*----------------------------*\n";
46 std::cout<<"Size of random matrices and vectors(N=M): "<<randomMatSize<<'\n';
47 std::cout<<"Number of concurrent calls into OpenBLAS : "<<numConcurrentThreads<<'\n';
48 std::cout<<"Number of testing rounds : "<<numTestRounds<<'\n';
49 std::cout<<"This test will need "<<((static_cast<uint64_t>(randomMatSize*randomMatSize)*numConcurrentThreads*8)+(static_cast<uint64_t>(randomMatSize)*numConcurrentThreads*8*2))/static_cast<double>(1024*1024)<<" MiB of RAM\n"<<std::endl;
51 FailIfThreadsAreZero(numConcurrentThreads);
53 std::cout<<"Initializing random number generator..."<<std::flush;
54 std::mt19937_64 PRNG = InitPRNG();
57 std::cout<<"Preparing to test CBLAS DGEMV thread safety\n";
58 std::cout<<"Allocating matrices..."<<std::flush;
59 for(uint32_t i=0; i<numConcurrentThreads; i++){
60 matBlock.at(i).resize(randomMatSize*randomMatSize);
63 std::cout<<"Allocating vectors..."<<std::flush;
64 for(uint32_t i=0; i<(numConcurrentThreads*2); i++){
65 vecBlock.at(i).resize(randomMatSize);
70 std::cout<<"Filling matrices with random numbers..."<<std::flush;
71 FillMatrices(matBlock, PRNG, rngdist, randomMatSize, numConcurrentThreads, 1);
72 //PrintMatrices(matBlock, randomMatSize, numConcurrentThreads);
74 std::cout<<"Filling vectors with random numbers..."<<std::flush;
75 FillVectors(vecBlock, PRNG, rngdist, randomMatSize, numConcurrentThreads, 2);
78 std::cout<<"Testing CBLAS DGEMV thread safety"<<std::endl;
79 omp_set_num_threads(numConcurrentThreads);
80 for(uint32_t R=0; R<numTestRounds; R++){
81 std::cout<<"DGEMV round #"<<R<<std::endl;
82 std::cout<<"Launching "<<numConcurrentThreads<<" threads simultaneously using OpenMP..."<<std::flush;
83 #pragma omp parallel for default(none) shared(futureBlock, matBlock, vecBlock, randomMatSize, numConcurrentThreads)
84 for(uint32_t i=0; i<numConcurrentThreads; i++){
85 futureBlock[i] = std::async(std::launch::async, launch_cblas_dgemv, &matBlock[i][0], &vecBlock[i*2][0], &vecBlock[i*2+1][0], randomMatSize);
88 std::cout<<"Waiting for threads to finish..."<<std::flush;
89 for(uint32_t i=0; i<numConcurrentThreads; i++){
93 std::cout<<"Comparing results from different threads..."<<std::flush;
94 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++){
96 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 std::cout<<"ERROR: one of the threads returned a different result! Index : "<<i+1<<std::endl;
98 std::cout<<"CBLAS DGEMV thread safety test FAILED!"<<std::endl;
103 std::cout<<"OK!\n"<<std::endl;
105 std::cout<<"CBLAS DGEMV thread safety test PASSED!\n"<<std::endl;