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.

dgemm_thread_safety.cpp 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <iostream>
  2. #include <vector>
  3. #include <random>
  4. #include <future>
  5. #include "/opt/OpenBLAS_zen_serial/include/cblas.h"
  6. const blasint randomMatSize = 1024; //dimension of the random square matrices used
  7. const uint32_t numConcurrentThreads = 64; //number of concurrent calls of the functions being tested
  8. const uint32_t numTestRounds = 32; //number of testing rounds before success exit
  9. inline void pauser(){
  10. /// a portable way to pause a program
  11. std::string dummy;
  12. std::cout << "Press enter to continue...";
  13. std::getline(std::cin, dummy);
  14. }
  15. void launch_cblas_dgemm(double* A, double* B, double* C){
  16. cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans, randomMatSize, randomMatSize, randomMatSize, 1.0, A, randomMatSize, B, randomMatSize, 0.1, C, randomMatSize);
  17. }
  18. void FillMatrices(std::vector<std::vector<double>>& matBlock, std::mt19937_64& PRNG, std::uniform_real_distribution<double>& rngdist){
  19. for(uint32_t i=0; i<3; i++){
  20. for(uint32_t j=0; j<(randomMatSize*randomMatSize); j++){
  21. matBlock[i][j] = rngdist(PRNG);
  22. }
  23. }
  24. for(uint32_t i=3; i<(numConcurrentThreads*3); i+=3){
  25. matBlock[i] = matBlock[0];
  26. matBlock[i+1] = matBlock[1];
  27. matBlock[i+2] = matBlock[2];
  28. }
  29. }
  30. std::mt19937_64 InitPRNG(){
  31. std::random_device rd;
  32. std::mt19937_64 PRNG(rd()); //seed PRNG using /dev/urandom or similar OS provided RNG
  33. std::uniform_real_distribution<double> rngdist{-1.0, 1.0};
  34. //make sure the internal state of the PRNG is properly mixed by generating 10M random numbers
  35. //PRNGs often have unreliable distribution uniformity and other statistical properties before their internal state is sufficiently mixed
  36. for (uint32_t i=0;i<10000000;i++) rngdist(PRNG);
  37. return PRNG;
  38. }
  39. void PrintMatrices(const std::vector<std::vector<double>>& matBlock){
  40. for (uint32_t i=0;i<numConcurrentThreads*3;i++){
  41. std::cout<<i<<std::endl;
  42. for (uint32_t j=0;j<randomMatSize;j++){
  43. for (uint32_t k=0;k<randomMatSize;k++){
  44. std::cout<<matBlock[i][j*randomMatSize + k]<<" ";
  45. }
  46. std::cout<<std::endl;
  47. }
  48. std::cout<<std::endl;
  49. }
  50. }
  51. int main(){
  52. std::uniform_real_distribution<double> rngdist{-1.0, 1.0};
  53. std::vector<std::vector<double>> matBlock(numConcurrentThreads*3);
  54. std::vector<std::future<void>> futureBlock(numConcurrentThreads);
  55. std::cout<<"Initializing random number generator..."<<std::flush;
  56. std::mt19937_64 PRNG = InitPRNG();
  57. std::cout<<"done\n";
  58. std::cout<<"Preparing to test CBLAS DGEMM thread safety\n";
  59. std::cout<<"Allocating matrices..."<<std::flush;
  60. for(uint32_t i=0; i<(numConcurrentThreads*3); i++){
  61. matBlock[i].resize(randomMatSize*randomMatSize);
  62. }
  63. std::cout<<"done\n";
  64. std::cout<<"Filling matrices with random numbers..."<<std::flush;
  65. FillMatrices(matBlock, PRNG, rngdist);
  66. //PrintMatrices(matBlock);
  67. std::cout<<"done\n";
  68. std::cout<<"Testing CBLAS DGEMM thread safety\n";
  69. for(uint32_t R=0; R<numTestRounds; R++){
  70. std::cout<<"DGEMM round #"<<R<<std::endl;
  71. std::cout<<"Launching "<<numConcurrentThreads<<" threads..."<<std::flush;
  72. for(uint32_t i=0; i<numConcurrentThreads; i++){
  73. futureBlock[i] = std::async(std::launch::async, launch_cblas_dgemm, &matBlock[i*3][0], &matBlock[i*3+1][0], &matBlock[i*3+2][0]);
  74. //launch_cblas_dgemm( &matBlock[i][0], &matBlock[i+1][0], &matBlock[i+2][0]);
  75. }
  76. std::cout<<"done\n";
  77. std::cout<<"Waiting for threads to finish..."<<std::flush;
  78. for(uint32_t i=0; i<numConcurrentThreads; i++){
  79. futureBlock[i].get();
  80. }
  81. std::cout<<"done\n";
  82. //PrintMatrices(matBlock);
  83. std::cout<<"Comparing results from different threads..."<<std::flush;
  84. for(uint32_t i=3; i<(numConcurrentThreads*3); i+=3){
  85. for(uint32_t j=0; j<(randomMatSize*randomMatSize); j++){
  86. if (std::abs(matBlock[i+2][j] - matBlock[2][j]) > 1.0E-13){
  87. std::cout<<"ERROR: one of the threads returned a different result!"<<i+2<<std::endl;
  88. return -1;
  89. }
  90. }
  91. }
  92. std::cout<<"OK!"<<std::endl;
  93. }
  94. return 0;
  95. }