You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dgemv_thread_safety.cpp 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <future>
  5. #include <omp.h>
  6. #include "../cblas.h"
  7. #include "cpp_thread_safety_common.h"
  8. void launch_cblas_dgemv(double* A, double* x, double* y, const blasint randomMatSize){
  9. const blasint inc = 1;
  10. cblas_dgemv(CblasColMajor, CblasNoTrans, randomMatSize, randomMatSize, 1.0, A, randomMatSize, x, inc, 0.1, y, inc);
  11. }
  12. int main(int argc, char* argv[]){
  13. blasint randomMatSize = 1024; //dimension of the random square matrices and vectors being used
  14. uint32_t numConcurrentThreads = 52; //number of concurrent calls of the functions being tested
  15. uint32_t numTestRounds = 16; //number of testing rounds before success exit
  16. if (argc > 4){
  17. std::cout<<"ERROR: too many arguments for thread safety tester"<<std::endl;
  18. abort();
  19. }
  20. if(argc == 4){
  21. std::vector<std::string> cliArgs;
  22. for (int i = 1; i < argc; i++){
  23. cliArgs.push_back(argv[i]);
  24. std::cout<<argv[i]<<std::endl;
  25. }
  26. randomMatSize = std::stoul(cliArgs.at(0));
  27. numConcurrentThreads = std::stoul(cliArgs.at(1));
  28. numTestRounds = std::stoul(cliArgs.at(2));
  29. }
  30. std::uniform_real_distribution<double> rngdist{-1.0, 1.0};
  31. std::vector<std::vector<double>> matBlock(numConcurrentThreads);
  32. std::vector<std::vector<double>> vecBlock(numConcurrentThreads*2);
  33. std::vector<std::future<void>> futureBlock(numConcurrentThreads);
  34. std::cout<<"*----------------------------*\n";
  35. std::cout<<"| DGEMV thread safety tester |\n";
  36. std::cout<<"*----------------------------*\n";
  37. std::cout<<"Size of random matrices and vectors(N=M): "<<randomMatSize<<'\n';
  38. std::cout<<"Number of concurrent calls into OpenBLAS : "<<numConcurrentThreads<<'\n';
  39. std::cout<<"Number of testing rounds : "<<numTestRounds<<'\n';
  40. 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;
  41. std::cout<<"Initializing random number generator..."<<std::flush;
  42. std::mt19937_64 PRNG = InitPRNG();
  43. std::cout<<"done\n";
  44. std::cout<<"Preparing to test CBLAS DGEMV thread safety\n";
  45. std::cout<<"Allocating matrices..."<<std::flush;
  46. for(uint32_t i=0; i<numConcurrentThreads; i++){
  47. matBlock.at(i).resize(randomMatSize*randomMatSize);
  48. }
  49. std::cout<<"done\n";
  50. std::cout<<"Allocating vectors..."<<std::flush;
  51. for(uint32_t i=0; i<(numConcurrentThreads*2); i++){
  52. vecBlock.at(i).resize(randomMatSize);
  53. }
  54. std::cout<<"done\n";
  55. //pauser();
  56. std::cout<<"Filling matrices with random numbers..."<<std::flush;
  57. FillMatrices(matBlock, PRNG, rngdist, randomMatSize, numConcurrentThreads, 1);
  58. //PrintMatrices(matBlock, randomMatSize, numConcurrentThreads);
  59. std::cout<<"done\n";
  60. std::cout<<"Filling vectors with random numbers..."<<std::flush;
  61. FillVectors(vecBlock, PRNG, rngdist, randomMatSize, numConcurrentThreads, 2);
  62. std::cout<<"done\n";
  63. std::cout<<"Testing CBLAS DGEMV thread safety"<<std::endl;
  64. omp_set_num_threads(numConcurrentThreads);
  65. for(uint32_t R=0; R<numTestRounds; R++){
  66. std::cout<<"DGEMV round #"<<R<<std::endl;
  67. std::cout<<"Launching "<<numConcurrentThreads<<" threads simultaneously using OpenMP..."<<std::flush;
  68. #pragma omp parallel for default(none) shared(futureBlock, matBlock, vecBlock, randomMatSize, numConcurrentThreads)
  69. for(uint32_t i=0; i<numConcurrentThreads; i++){
  70. futureBlock[i] = std::async(std::launch::async, launch_cblas_dgemv, &matBlock[i][0], &vecBlock[i*2][0], &vecBlock[i*2+1][0], randomMatSize);
  71. }
  72. std::cout<<"done\n";
  73. std::cout<<"Waiting for threads to finish..."<<std::flush;
  74. for(uint32_t i=0; i<numConcurrentThreads; i++){
  75. futureBlock[i].get();
  76. }
  77. std::cout<<"done\n";
  78. std::cout<<"Comparing results from different threads..."<<std::flush;
  79. for(uint32_t i=2; i<(numConcurrentThreads*2); i+=2){ //i is the index of vector x, for a given thread
  80. for(uint32_t j = 0; j < static_cast<uint32_t>(randomMatSize); j++){
  81. 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
  82. std::cout<<"ERROR: one of the threads returned a different result! Index : "<<i+1<<std::endl;
  83. std::cout<<"CBLAS DGEMV thread safety test FAILED!"<<std::endl;
  84. return -1;
  85. }
  86. }
  87. }
  88. std::cout<<"OK!\n"<<std::endl;
  89. }
  90. std::cout<<"CBLAS DGEMV thread safety test PASSED!\n"<<std::endl;
  91. return 0;
  92. }