Update dgemv_thread_safety.cpp

This commit is contained in:
Mayank Raj 2022-07-24 11:51:25 +05:30 committed by GitHub
parent c43ec53bdd
commit a9939111d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 22 deletions

View File

@ -6,12 +6,14 @@
#include "../cblas.h" #include "../cblas.h"
#include "cpp_thread_safety_common.h" #include "cpp_thread_safety_common.h"
void launch_cblas_dgemv(double* A, double* x, double* y, const blasint randomMatSize){ void launch_cblas_dgemv(double* A, double* x, double* y, const blasint randomMatSize)
{
const blasint inc = 1; const blasint inc = 1;
cblas_dgemv(CblasColMajor, CblasNoTrans, randomMatSize, randomMatSize, 1.0, A, randomMatSize, x, inc, 0.1, y, inc); cblas_dgemv(CblasColMajor, CblasNoTrans, randomMatSize, randomMatSize, 1.0, A, randomMatSize, x, inc, 0.1, y, inc);
} }
int main(int argc, char* argv[]){ int main(int argc, char* argv[])
{
blasint randomMatSize = 1024; //dimension of the random square matrices and vectors being used blasint randomMatSize = 1024; //dimension of the random square matrices and vectors being used
uint32_t numConcurrentThreads = 52; //number of concurrent calls of the functions being tested uint32_t numConcurrentThreads = 52; //number of concurrent calls of the functions being tested
uint32_t numTestRounds = 16; //number of testing rounds before success exit uint32_t numTestRounds = 16; //number of testing rounds before success exit
@ -20,13 +22,16 @@ int main(int argc, char* argv[]){
if (maxHwThreads < 52) if (maxHwThreads < 52)
numConcurrentThreads = maxHwThreads; numConcurrentThreads = maxHwThreads;
if (argc > 4){ if (argc > 4)
{
std::cout<<"ERROR: too many arguments for thread safety tester"<<std::endl; std::cout<<"ERROR: too many arguments for thread safety tester"<<std::endl;
abort(); abort();
} }
if(argc == 4){ if(argc == 4)
{
std::vector<std::string> cliArgs; std::vector<std::string> cliArgs;
for (int i = 1; i < argc; i++){ for (int i = 1; i < argc; i++)
{
cliArgs.push_back(argv[i]); cliArgs.push_back(argv[i]);
std::cout<<argv[i]<<std::endl; std::cout<<argv[i]<<std::endl;
} }
@ -56,15 +61,18 @@ int main(int argc, char* argv[]){
std::cout<<"Preparing to test CBLAS DGEMV thread safety\n"; std::cout<<"Preparing to test CBLAS DGEMV thread safety\n";
std::cout<<"Allocating matrices..."<<std::flush; std::cout<<"Allocating matrices..."<<std::flush;
for(uint32_t i=0; i<numConcurrentThreads; i++){ for(uint32_t i=0; i<numConcurrentThreads; i++)
{
matBlock.at(i).resize(randomMatSize*randomMatSize); matBlock.at(i).resize(randomMatSize*randomMatSize);
} }
std::cout<<"done\n"; std::cout<<"done\n";
std::cout<<"Allocating vectors..."<<std::flush; std::cout<<"Allocating vectors..."<<std::flush;
for(uint32_t i=0; i<(numConcurrentThreads*2); i++){ for(uint32_t i=0; i<(numConcurrentThreads*2); i++)
{
vecBlock.at(i).resize(randomMatSize); vecBlock.at(i).resize(randomMatSize);
} }
std::cout<<"done\n"; std::cout<<"done\n";
//pauser(); //pauser();
std::cout<<"Filling matrices with random numbers..."<<std::flush; std::cout<<"Filling matrices with random numbers..."<<std::flush;
@ -77,22 +85,26 @@ int main(int argc, char* argv[]){
std::cout<<"Testing CBLAS DGEMV thread safety"<<std::endl; std::cout<<"Testing CBLAS DGEMV thread safety"<<std::endl;
omp_set_num_threads(numConcurrentThreads); omp_set_num_threads(numConcurrentThreads);
for(uint32_t R=0; R<numTestRounds; R++){ for(uint32_t R=0; R<numTestRounds; R++)
{
std::cout<<"DGEMV round #"<<R<<std::endl; std::cout<<"DGEMV round #"<<R<<std::endl;
std::cout<<"Launching "<<numConcurrentThreads<<" threads simultaneously using OpenMP..."<<std::flush; std::cout<<"Launching "<<numConcurrentThreads<<" threads simultaneously using OpenMP..."<<std::flush;
#pragma omp parallel for default(none) shared(futureBlock, matBlock, vecBlock, randomMatSize, numConcurrentThreads) #pragma omp parallel for default(none) shared(futureBlock, matBlock, vecBlock, randomMatSize, numConcurrentThreads)
for(uint32_t i=0; i<numConcurrentThreads; i++){ for(uint32_t i=0; i<numConcurrentThreads; i++)
{
futureBlock[i] = std::async(std::launch::async, launch_cblas_dgemv, &matBlock[i][0], &vecBlock[i*2][0], &vecBlock[i*2+1][0], randomMatSize); futureBlock[i] = std::async(std::launch::async, launch_cblas_dgemv, &matBlock[i][0], &vecBlock[i*2][0], &vecBlock[i*2+1][0], randomMatSize);
} }
std::cout<<"done\n"; std::cout<<"done\n";
std::cout<<"Waiting for threads to finish..."<<std::flush; std::cout<<"Waiting for threads to finish..."<<std::flush;
for(uint32_t i=0; i<numConcurrentThreads; i++){ for(uint32_t i=0; i<numConcurrentThreads; i++)
{
futureBlock[i].get(); futureBlock[i].get();
} }
std::cout<<"done\n"; std::cout<<"done\n";
std::cout<<"Comparing results from different threads..."<<std::flush; std::cout<<"Comparing results from different threads..."<<std::flush;
for(uint32_t i=2; i<(numConcurrentThreads*2); i+=2){ //i is the index of vector x, for a given thread for(uint32_t i=2; i<(numConcurrentThreads*2); i+=2){ //i is the index of vector x, for a given thread
for(uint32_t j = 0; j < static_cast<uint32_t>(randomMatSize); j++){ for(uint32_t j = 0; j < static_cast<uint32_t>(randomMatSize); j++)
{
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 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
std::cout<<"ERROR: one of the threads returned a different result! Index : "<<i+1<<std::endl; std::cout<<"ERROR: one of the threads returned a different result! Index : "<<i+1<<std::endl;
std::cout<<"CBLAS DGEMV thread safety test FAILED!"<<std::endl; std::cout<<"CBLAS DGEMV thread safety test FAILED!"<<std::endl;
@ -104,4 +116,4 @@ int main(int argc, char* argv[]){
} }
std::cout<<"CBLAS DGEMV thread safety test PASSED!\n"<<std::endl; std::cout<<"CBLAS DGEMV thread safety test PASSED!\n"<<std::endl;
return 0; return 0;
} }