Skip to content

Commit c00289b

Browse files
authored
upload thread safety test folder
1 parent 83b5c6b commit c00289b

File tree

4 files changed

+262
-0
lines changed

4 files changed

+262
-0
lines changed

cpp_thread_test/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
include ../Makefile.rule
2+
3+
all :: dgemv_tester dgemm_tester
4+
5+
dgemv_tester :
6+
$(CXX) $(COMMON_OPT) -Wall -Wextra -Wshadow -fopenmp -std=c++11 dgemv_thread_safety.cpp ../libopenblas.a -lpthread -o dgemv_tester
7+
./dgemv_tester
8+
9+
dgemm_tester : dgemv_tester
10+
$(CXX) $(COMMON_OPT) -Wall -Wextra -Wshadow -fopenmp -std=c++11 dgemm_thread_safety.cpp ../libopenblas.a -lpthread -o dgemm_tester
11+
./dgemm_tester
12+
13+
clean ::
14+
rm -f dgemv_tester dgemm_tester
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
8+
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){
9+
for(uint32_t i=0; i<numMat; i++){
10+
for(uint32_t j = 0; j < static_cast<uint32_t>(randomMatSize*randomMatSize); j++){
11+
matBlock[i][j] = rngdist(PRNG);
12+
}
13+
}
14+
for(uint32_t i=numMat; i<(numConcurrentThreads*numMat); i+=numMat){
15+
for(uint32_t j=0; j<numMat; j++){
16+
matBlock[i+j] = matBlock[j];
17+
}
18+
}
19+
}
20+
21+
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){
22+
for(uint32_t i=0; i<numVec; i++){
23+
for(uint32_t j = 0; j < static_cast<uint32_t>(randomMatSize); j++){
24+
vecBlock[i][j] = rngdist(PRNG);
25+
}
26+
}
27+
for(uint32_t i=numVec; i<(numConcurrentThreads*numVec); i+=numVec){
28+
for(uint32_t j=0; j<numVec; j++){
29+
vecBlock[i+j] = vecBlock[j];
30+
}
31+
}
32+
}
33+
34+
std::mt19937_64 InitPRNG(){
35+
std::random_device rd;
36+
std::mt19937_64 PRNG(rd()); //seed PRNG using /dev/urandom or similar OS provided RNG
37+
std::uniform_real_distribution<double> rngdist{-1.0, 1.0};
38+
//make sure the internal state of the PRNG is properly mixed by generating 10M random numbers
39+
//PRNGs often have unreliable distribution uniformity and other statistical properties before their internal state is sufficiently mixed
40+
for (uint32_t i=0;i<10000000;i++) rngdist(PRNG);
41+
return PRNG;
42+
}
43+
44+
void PrintMatrices(const std::vector<std::vector<double>>& matBlock, const blasint randomMatSize, const uint32_t numConcurrentThreads, const uint32_t numMat){
45+
for (uint32_t i=0;i<numConcurrentThreads*numMat;i++){
46+
std::cout<<i<<std::endl;
47+
for (uint32_t j = 0; j < static_cast<uint32_t>(randomMatSize); j++){
48+
for (uint32_t k = 0; k < static_cast<uint32_t>(randomMatSize); k++){
49+
std::cout<<matBlock[i][j*randomMatSize + k]<<" ";
50+
}
51+
std::cout<<std::endl;
52+
}
53+
std::cout<<std::endl;
54+
}
55+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
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);
11+
}
12+
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
17+
18+
if (argc > 4){
19+
std::cout<<"ERROR: too many arguments for thread safety tester"<<std::endl;
20+
abort();
21+
}
22+
23+
if(argc == 4){
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;
28+
}
29+
randomMatSize = std::stoul(cliArgs[0]);
30+
numConcurrentThreads = std::stoul(cliArgs[1]);
31+
numTestRounds = std::stoul(cliArgs[2]);
32+
}
33+
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);
37+
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;
45+
46+
std::cout<<"Initializing random number generator..."<<std::flush;
47+
std::mt19937_64 PRNG = InitPRNG();
48+
std::cout<<"done\n";
49+
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);
54+
}
55+
std::cout<<"done\n";
56+
//pauser();
57+
std::cout<<"Filling matrices with random numbers..."<<std::flush;
58+
FillMatrices(matBlock, PRNG, rngdist, randomMatSize, numConcurrentThreads, 3);
59+
//PrintMatrices(matBlock, randomMatSize, numConcurrentThreads, 3);
60+
std::cout<<"done\n";
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]);
70+
}
71+
std::cout<<"done\n";
72+
std::cout<<"Waiting for threads to finish..."<<std::flush;
73+
for(uint32_t i=0; i<numConcurrentThreads; i++){
74+
futureBlock[i].get();
75+
}
76+
std::cout<<"done\n";
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;
84+
return -1;
85+
}
86+
}
87+
}
88+
std::cout<<"OK!\n"<<std::endl;
89+
}
90+
std::cout<<"CBLAS DGEMM thread safety test PASSED!\n"<<std::endl;
91+
return 0;
92+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
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);
12+
}
13+
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+
19+
if (argc > 4){
20+
std::cout<<"ERROR: too many arguments for thread safety tester"<<std::endl;
21+
abort();
22+
}
23+
if(argc == 4){
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;
28+
}
29+
randomMatSize = std::stoul(cliArgs.at(0));
30+
numConcurrentThreads = std::stoul(cliArgs.at(1));
31+
numTestRounds = std::stoul(cliArgs.at(2));
32+
}
33+
34+
std::uniform_real_distribution<double> rngdist{-1.0, 1.0};
35+
std::vector<std::vector<double>> matBlock(numConcurrentThreads);
36+
std::vector<std::vector<double>> vecBlock(numConcurrentThreads*2);
37+
std::vector<std::future<void>> futureBlock(numConcurrentThreads);
38+
39+
std::cout<<"*----------------------------*\n";
40+
std::cout<<"| DGEMV thread safety tester |\n";
41+
std::cout<<"*----------------------------*\n";
42+
std::cout<<"Size of random matrices and vectors(N=M): "<<randomMatSize<<'\n';
43+
std::cout<<"Number of concurrent calls into OpenBLAS : "<<numConcurrentThreads<<'\n';
44+
std::cout<<"Number of testing rounds : "<<numTestRounds<<'\n';
45+
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;
46+
47+
std::cout<<"Initializing random number generator..."<<std::flush;
48+
std::mt19937_64 PRNG = InitPRNG();
49+
std::cout<<"done\n";
50+
51+
std::cout<<"Preparing to test CBLAS DGEMV thread safety\n";
52+
std::cout<<"Allocating matrices..."<<std::flush;
53+
for(uint32_t i=0; i<numConcurrentThreads; i++){
54+
matBlock.at(i).resize(randomMatSize*randomMatSize);
55+
}
56+
std::cout<<"done\n";
57+
std::cout<<"Allocating vectors..."<<std::flush;
58+
for(uint32_t i=0; i<(numConcurrentThreads*2); i++){
59+
vecBlock.at(i).resize(randomMatSize);
60+
}
61+
std::cout<<"done\n";
62+
//pauser();
63+
64+
std::cout<<"Filling matrices with random numbers..."<<std::flush;
65+
FillMatrices(matBlock, PRNG, rngdist, randomMatSize, numConcurrentThreads, 1);
66+
//PrintMatrices(matBlock, randomMatSize, numConcurrentThreads);
67+
std::cout<<"done\n";
68+
std::cout<<"Filling vectors with random numbers..."<<std::flush;
69+
FillVectors(vecBlock, PRNG, rngdist, randomMatSize, numConcurrentThreads, 2);
70+
std::cout<<"done\n";
71+
72+
std::cout<<"Testing CBLAS DGEMV thread safety"<<std::endl;
73+
omp_set_num_threads(numConcurrentThreads);
74+
for(uint32_t R=0; R<numTestRounds; R++){
75+
std::cout<<"DGEMV round #"<<R<<std::endl;
76+
std::cout<<"Launching "<<numConcurrentThreads<<" threads simultaneously using OpenMP..."<<std::flush;
77+
#pragma omp parallel for default(none) shared(futureBlock, matBlock, vecBlock, randomMatSize, numConcurrentThreads)
78+
for(uint32_t i=0; i<numConcurrentThreads; i++){
79+
futureBlock[i] = std::async(std::launch::async, launch_cblas_dgemv, &matBlock[i][0], &vecBlock[i*2][0], &vecBlock[i*2+1][0], randomMatSize);
80+
}
81+
std::cout<<"done\n";
82+
std::cout<<"Waiting for threads to finish..."<<std::flush;
83+
for(uint32_t i=0; i<numConcurrentThreads; i++){
84+
futureBlock[i].get();
85+
}
86+
std::cout<<"done\n";
87+
std::cout<<"Comparing results from different threads..."<<std::flush;
88+
for(uint32_t i=2; i<(numConcurrentThreads*2); i+=2){ //i is the index of vector x, for a given thread
89+
for(uint32_t j = 0; j < static_cast<uint32_t>(randomMatSize); j++){
90+
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
91+
std::cout<<"ERROR: one of the threads returned a different result! Index : "<<i+1<<std::endl;
92+
std::cout<<"CBLAS DGEMV thread safety test FAILED!"<<std::endl;
93+
return -1;
94+
}
95+
}
96+
}
97+
std::cout<<"OK!\n"<<std::endl;
98+
}
99+
std::cout<<"CBLAS DGEMV thread safety test PASSED!\n"<<std::endl;
100+
return 0;
101+
}

0 commit comments

Comments
 (0)