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.

cpp_thread_safety_common.h 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. inline void pauser(){
  2. /// a portable way to pause a program
  3. std::string dummy;
  4. std::cout << "Press enter to continue...";
  5. std::getline(std::cin, dummy);
  6. }
  7. void FailIfThreadsAreZero(uint32_t numConcurrentThreads) {
  8. if(numConcurrentThreads == 0) {
  9. std::cout<<"ERROR: Invalid parameter 0 for number of concurrent calls into OpenBLAS!"<<std::endl;
  10. std::cout<<"CBLAS DGEMV thread safety test FAILED!"<<std::endl;
  11. exit(-1);
  12. }
  13. }
  14. void FillMatrices(std::vector<std::vector<double>>& matBlock, std::mt19937_64& PRNG, std::uniform_real_distribution<double>& rngdist, const blasint randomMatSize, const uint32_t numConcurrentThreads, const uint32_t numMat){
  15. for(uint32_t i=0; i<numMat; i++){
  16. for(uint32_t j = 0; j < static_cast<uint32_t>(randomMatSize*randomMatSize); j++){
  17. matBlock[i][j] = rngdist(PRNG);
  18. }
  19. }
  20. for(uint32_t i=numMat; i<(numConcurrentThreads*numMat); i+=numMat){
  21. for(uint32_t j=0; j<numMat; j++){
  22. matBlock[i+j] = matBlock[j];
  23. }
  24. }
  25. }
  26. void FillVectors(std::vector<std::vector<double>>& vecBlock, std::mt19937_64& PRNG, std::uniform_real_distribution<double>& rngdist, const blasint randomMatSize, const uint32_t numConcurrentThreads, const uint32_t numVec){
  27. for(uint32_t i=0; i<numVec; i++){
  28. for(uint32_t j = 0; j < static_cast<uint32_t>(randomMatSize); j++){
  29. vecBlock[i][j] = rngdist(PRNG);
  30. }
  31. }
  32. for(uint32_t i=numVec; i<(numConcurrentThreads*numVec); i+=numVec){
  33. for(uint32_t j=0; j<numVec; j++){
  34. vecBlock[i+j] = vecBlock[j];
  35. }
  36. }
  37. }
  38. std::mt19937_64 InitPRNG(){
  39. std::random_device rd;
  40. std::mt19937_64 PRNG(rd()); //seed PRNG using /dev/urandom or similar OS provided RNG
  41. std::uniform_real_distribution<double> rngdist{-1.0, 1.0};
  42. //make sure the internal state of the PRNG is properly mixed by generating 10M random numbers
  43. //PRNGs often have unreliable distribution uniformity and other statistical properties before their internal state is sufficiently mixed
  44. for (uint32_t i=0;i<10000000;i++) rngdist(PRNG);
  45. return PRNG;
  46. }
  47. void PrintMatrices(const std::vector<std::vector<double>>& matBlock, const blasint randomMatSize, const uint32_t numConcurrentThreads, const uint32_t numMat){
  48. for (uint32_t i=0;i<numConcurrentThreads*numMat;i++){
  49. std::cout<<i<<std::endl;
  50. for (uint32_t j = 0; j < static_cast<uint32_t>(randomMatSize); j++){
  51. for (uint32_t k = 0; k < static_cast<uint32_t>(randomMatSize); k++){
  52. std::cout<<matBlock[i][j*randomMatSize + k]<<" ";
  53. }
  54. std::cout<<std::endl;
  55. }
  56. std::cout<<std::endl;
  57. }
  58. }