7 #include "cpp_thread_safety_common.h"
9 void launch_cblas_dgemm(double* A, double* B, double* C, const blasint randomMatSize){
10 cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, randomMatSize, randomMatSize, randomMatSize, 1.0, A, randomMatSize, B, randomMatSize, 0.1, C, randomMatSize);
13 int main(int argc, char* argv[]){
14 blasint randomMatSize = 1024; //dimension of the random square matrices used
15 uint32_t numConcurrentThreads = 52; //number of concurrent calls of the functions being tested
16 uint32_t numTestRounds = 16; //number of testing rounds before success exit
19 std::cout<<"ERROR: too many arguments for thread safety tester"<<std::endl;
24 std::vector<std::string> cliArgs;
25 for (int i = 1; i < argc; i++){
26 cliArgs.push_back(argv[i]);
27 std::cout<<argv[i]<<std::endl;
29 randomMatSize = std::stoul(cliArgs[0]);
30 numConcurrentThreads = std::stoul(cliArgs[1]);
31 numTestRounds = std::stoul(cliArgs[2]);
34 std::uniform_real_distribution<double> rngdist{-1.0, 1.0};
35 std::vector<std::vector<double>> matBlock(numConcurrentThreads*3);
36 std::vector<std::future<void>> futureBlock(numConcurrentThreads);
38 std::cout<<"*----------------------------*\n";
39 std::cout<<"| DGEMM thread safety tester |\n";
40 std::cout<<"*----------------------------*\n";
41 std::cout<<"Size of random matrices(N=M=K): "<<randomMatSize<<'\n';
42 std::cout<<"Number of concurrent calls into OpenBLAS : "<<numConcurrentThreads<<'\n';
43 std::cout<<"Number of testing rounds : "<<numTestRounds<<'\n';
44 std::cout<<"This test will need "<<(static_cast<uint64_t>(randomMatSize*randomMatSize)*numConcurrentThreads*3*8)/static_cast<double>(1024*1024)<<" MiB of RAM\n"<<std::endl;
46 std::cout<<"Initializing random number generator..."<<std::flush;
47 std::mt19937_64 PRNG = InitPRNG();
50 std::cout<<"Preparing to test CBLAS DGEMM thread safety\n";
51 std::cout<<"Allocating matrices..."<<std::flush;
52 for(uint32_t i=0; i<(numConcurrentThreads*3); i++){
53 matBlock[i].resize(randomMatSize*randomMatSize);
57 std::cout<<"Filling matrices with random numbers..."<<std::flush;
58 FillMatrices(matBlock, PRNG, rngdist, randomMatSize, numConcurrentThreads, 3);
59 //PrintMatrices(matBlock, randomMatSize, numConcurrentThreads, 3);
61 std::cout<<"Testing CBLAS DGEMM thread safety\n";
62 omp_set_num_threads(numConcurrentThreads);
63 for(uint32_t R=0; R<numTestRounds; R++){
64 std::cout<<"DGEMM round #"<<R<<std::endl;
65 std::cout<<"Launching "<<numConcurrentThreads<<" threads simultaneously using OpenMP..."<<std::flush;
66 #pragma omp parallel for default(none) shared(futureBlock, matBlock, randomMatSize, numConcurrentThreads)
67 for(uint32_t i=0; i<numConcurrentThreads; i++){
68 futureBlock[i] = std::async(std::launch::async, launch_cblas_dgemm, &matBlock[i*3][0], &matBlock[i*3+1][0], &matBlock[i*3+2][0], randomMatSize);
69 //launch_cblas_dgemm( &matBlock[i][0], &matBlock[i+1][0], &matBlock[i+2][0]);
72 std::cout<<"Waiting for threads to finish..."<<std::flush;
73 for(uint32_t i=0; i<numConcurrentThreads; i++){
77 //PrintMatrices(matBlock, randomMatSize, numConcurrentThreads, 3);
78 std::cout<<"Comparing results from different threads..."<<std::flush;
79 for(uint32_t i=3; i<(numConcurrentThreads*3); i+=3){ //i is the index of matrix A, for a given thread
80 for(uint32_t j = 0; j < static_cast<uint32_t>(randomMatSize*randomMatSize); j++){
81 if (std::abs(matBlock[i+2][j] - matBlock[2][j]) > 1.0E-13){ //i+2 is the index of matrix C, for a given thread
82 std::cout<<"ERROR: one of the threads returned a different result! Index : "<<i+2<<std::endl;
83 std::cout<<"CBLAS DGEMM thread safety test FAILED!"<<std::endl;
88 std::cout<<"OK!\n"<<std::endl;
90 std::cout<<"CBLAS DGEMM thread safety test PASSED!\n"<<std::endl;