@@ -22,28 +22,76 @@ using std::vector;
using std : : pair ;
using std : : tuple ;
// МОЖЕТ К А К -Т О ВЫЧИСЛЯТЬ ДИРЕКТИВЫ, А ПОТОМ ДЕЛАТЬ ПРОГОНЫ?
map < size_t , size_t > createTemplateIdMapping ( const vector < ParallelRegion * > & parallelRegions )
{
size_t maxArrayId = 0 ;
for ( int z = 0 ; z < parallelRegions . size ( ) ; + + z ) {
const DataDirective & dataDirectives = parallelRegions [ z ] - > GetDataDir ( ) ;
for ( const auto & distrRule : dataDirectives . distrRules ) {
if ( distrRule . first & & ! distrRule . first - > IsTemplate ( ) ) {
maxArrayId = std : : max ( maxArrayId , ( size_t ) distrRule . first - > GetId ( ) ) ;
}
}
for ( const auto & alignRule : dataDirectives . alignRules ) {
if ( alignRule . alignArray & & ! alignRule . alignArray - > IsTemplate ( ) ) {
maxArrayId = std : : max ( maxArrayId , ( size_t ) alignRule . alignArray - > GetId ( ) ) ;
}
}
}
map < size_t , size_t > templateIdMapping ;
size_t nextTemplateId = maxArrayId + 1 ;
for ( int z = 0 ; z < parallelRegions . size ( ) ; + + z ) {
const DataDirective & dataDirectives = parallelRegions [ z ] - > GetDataDir ( ) ;
for ( const auto & distrRule : dataDirectives . distrRules ) {
if ( distrRule . first & & distrRule . first - > IsTemplate ( ) ) {
size_t originalId = distrRule . first - > GetId ( ) ;
if ( templateIdMapping . find ( originalId ) = = templateIdMapping . end ( ) ) {
templateIdMapping [ originalId ] = nextTemplateId + + ;
}
}
}
for ( const auto & alignRule : dataDirectives . alignRules ) {
if ( alignRule . alignWith & & alignRule . alignWith - > IsTemplate ( ) ) {
size_t originalId = alignRule . alignWith - > GetId ( ) ;
if ( templateIdMapping . find ( originalId ) = = templateIdMapping . end ( ) ) {
templateIdMapping [ originalId ] = nextTemplateId + + ;
}
}
}
}
return templateIdMapping ;
}
// TODO: вычислять директивы разово заранее
double runLibpredictCalc ( SgProject & project ,
const vector < size_t > & topology ,
const string & clusterConfStr ,
const vector < ParallelRegion * > & parallelRegions ,
map < string , vector < LoopGraph * > > & loopGraph ,
map < string , vector < Messages > > & SPF_messages )
map < string , vector < Messages > > & SPF_messages ,
const map < size_t , size_t > & templateIdMapping )
{
libpredict : : RetInit retInit = libpredict : : Init ( clusterConfStr , topology [ 0 ] , topology [ 1 ] , topology [ 2 ] , topology [ 3 ] ) ;
libpredict : : RetInitGrid retInitGrid = libpredict : : InitGrid ( topology [ 0 ] , topology [ 1 ] , topology [ 2 ] , topology [ 3 ] ) ;
if ( retInit ! = libpredict : : INIT_SUCCESS ) {
__spf_print ( 1 , " ERROR: Failed to initialize libpredict with cluster config: %s, return code: %d \n " , clusterConfStr . c_str ( ) , ( int ) retInit ) ;
if ( retInitGrid ! = libpredict : : INIT_GRID_ SUCCESS ) {
__spf_print ( 1 , " ERROR: Failed to initialize libpredict grid with topology: %zu %zu %zu %zu, return code: %d \n " ,
topology [ 0 ] , topology [ 1 ] , topology [ 2 ] , topology [ 3 ] , ( int ) retInitGrid ) ;
std : : wstring messageR , messageE ;
__spf_printToLongBuf ( messageE , L " Failed to initialize libpredict library with cluster config: %s , return code: %d " ,
to_wstring ( clusterConfStr ) . c_str ( ) , ( int ) retInit ) ;
__spf_printToLongBuf ( messageR , R206 ) ;
getObjectForFileFromMap ( clusterConfStr . c_str ( ) , SPF_messages ) . push_back ( Messages ( ERROR , 1 , messageR , messageE , 1063 ) ) ;
__spf_printToLongBuf ( messageE , L " Failed to initialize libpredict grid with topology: %zu %zu %zu %zu , return code: %d " ,
topology [ 0 ] , topology [ 1 ] , topology [ 2 ] , topology [ 3 ] , ( int ) retInitGrid ) ;
__spf_printToLongBuf ( messageR , R207 ) ;
getObjectForFileFromMap ( clusterConfStr . c_str ( ) , SPF_messages ) . push_back ( Messages ( ERROR , 1 , messageR , messageE , 1064 ) ) ;
return - 1 ;
}
// distribute и align из parallelRegions
// distribute and align from parallelRegions
for ( int z = 0 ; z < parallelRegions . size ( ) ; + + z ) {
const DataDirective & dataDirectives = parallelRegions [ z ] - > GetDataDir ( ) ;
const vector < int > & currentVariant = parallelRegions [ z ] - > GetCurrentVariant ( ) ;
@@ -60,7 +108,16 @@ double runLibpredictCalc(SgProject &project,
const DistrVariant * variant = distrRule . second ;
if ( array & & variant & & ! array - > IsNotDistribute ( ) ) {
size_t array Id = array - > GetId ( ) ;
size_t original Id = array - > GetId ( ) ;
size_t arrayId = originalId ;
if ( array - > IsTemplate ( ) ) {
auto it = templateIdMapping . find ( originalId ) ;
if ( it ! = templateIdMapping . end ( ) ) {
arrayId = it - > second ;
}
}
size_t elemSize = array - > GetTypeSize ( ) ;
vector < libpredict : : DistributeAxisRule > axisDistributions ;
@@ -92,9 +149,9 @@ double runLibpredictCalc(SgProject &project,
std : : wstring messageR , messageE ;
__spf_printToLongBuf ( messageE , L " Failed to distribute array '%s' with libpredict, return code: %d " ,
to_wstring ( array - > GetShortName ( ) ) . c_str ( ) , ( int ) retDistribute ) ;
__spf_printToLongBuf ( messageR , R207 ) ;
__spf_printToLongBuf ( messageR , R208 ) ;
getObjectForFileFromMap ( array - > GetDeclInfo ( ) . begin ( ) - > first . c_str ( ) , SPF_messages ) . push_back (
Messages ( ERROR , array - > GetDeclInfo ( ) . begin ( ) - > second , messageR , messageE , 1064 ) ) ;
Messages ( ERROR , array - > GetDeclInfo ( ) . begin ( ) - > second , messageR , messageE , 1065 ) ) ;
}
}
}
@@ -106,13 +163,24 @@ double runLibpredictCalc(SgProject &project,
if ( alignArray & & alignWithArray & & ! alignArray - > IsNotDistribute ( ) ) {
size_t arrayId = alignArray - > GetId ( ) ;
size_t d istributedArrayId = alignWithArray - > GetId ( ) ;
size_t originalD istributedArrayId = alignWithArray - > GetId ( ) ;
size_t distributedArrayId = originalDistributedArrayId ;
if ( alignWithArray - > IsTemplate ( ) ) {
auto it = templateIdMapping . find ( originalDistributedArrayId ) ;
if ( it ! = templateIdMapping . end ( ) ) {
distributedArrayId = it - > second ;
}
}
size_t elemSize = alignArray - > GetTypeSize ( ) ;
const auto & arraySizes = alignArray - > GetSizes ( ) ;
vector < size_t > dimensions ;
for ( int dim = 0 ; dim < alignArray - > GetDimSize ( ) ; + + dim )
dimensions . push_back ( arraySizes [ dim ] . second - arraySizes [ dim ] . first + 1 ) ;
for ( int dim = 0 ; dim < alignArray - > GetDimSize ( ) ; + + dim ) {
size_t dimSize = arraySizes [ dim ] . second - arraySizes [ dim ] . first + 1 ;
dimensions . push_back ( dimSize ) ;
}
vector < libpredict : : AlignDisplay > distributionExpressions ;
for ( int dim = 0 ; dim < alignWithArray - > GetDimSize ( ) ; + + dim ) {
@@ -155,9 +223,9 @@ double runLibpredictCalc(SgProject &project,
__spf_printToLongBuf ( messageE , L " Failed to align array '%s' with array '%s' using libpredict, return code: %d " ,
to_wstring ( alignArray - > GetShortName ( ) ) . c_str ( ) ,
to_wstring ( alignWithArray - > GetShortName ( ) ) . c_str ( ) , ( int ) retAlign ) ;
__spf_printToLongBuf ( messageR , R208 ) ;
__spf_printToLongBuf ( messageR , R209 ) ;
getObjectForFileFromMap ( alignArray - > GetDeclInfo ( ) . begin ( ) - > first . c_str ( ) , SPF_messages ) . push_back (
Messages ( ERROR , alignArray - > GetDeclInfo ( ) . begin ( ) - > second , messageR , messageE , 1065 ) ) ;
Messages ( ERROR , alignArray - > GetDeclInfo ( ) . begin ( ) - > second , messageR , messageE , 1066 ) ) ;
}
}
}
@@ -194,7 +262,6 @@ double runLibpredictCalc(SgProject &project,
}
bool corner = directive - > shadowRenewCorner [ shadowIdx ] ;
size_t number_loop_iterations = loopPtr ? static_cast < size_t > ( loopPtr - > countOfIters ) : 1 ;
libpredict : : RetShadowRenew retShadowRenew = libpredict : : ShadowRenew ( arrayId , shadow_renew , corner , number_loop_iterations ) ;
@@ -206,9 +273,9 @@ double runLibpredictCalc(SgProject &project,
std : : wstring messageR , messageE ;
__spf_printToLongBuf ( messageE , L " Failed to process shadow_renew for array '%s' with libpredict, return code: %d " ,
to_wstring ( shadowArray - > GetShortName ( ) ) . c_str ( ) , ( int ) retShadowRenew ) ;
__spf_printToLongBuf ( messageR , R209 ) ;
__spf_printToLongBuf ( messageR , R21 0 ) ;
getObjectForFileFromMap ( shadowArray - > GetDeclInfo ( ) . begin ( ) - > first . c_str ( ) , SPF_messages ) . push_back (
Messages ( ERROR , shadowArray - > GetDeclInfo ( ) . begin ( ) - > second , messageR , messageE , 1066 ) ) ;
Messages ( ERROR , shadowArray - > GetDeclInfo ( ) . begin ( ) - > second , messageR , messageE , 1067 ) ) ;
}
}
}
@@ -254,22 +321,40 @@ void runPredictScheme(SgProject &project,
clusterConfStr = firstFilePath . substr ( 0 , lastSlash + 1 ) + " cluster.conf " ;
}
// creating template ID display to avoid conflicts
map < size_t , size_t > templateIdMapping = createTemplateIdMapping ( parallelRegions ) ;
// iterating through topologies to find most optimal one
topologies = vector < vector < size_t > > ( ) ;
if ( maxSizeDist ) {
if ( maxSizeDist > 4 ) maxSizeDist = 4 ;
// TODO: look at cluster configuration
size_t n1max = 1 0;
size_t n2max = ( maxSizeDist > = 2 ) ? 10 : 1 ;
size_t n3max = ( maxSizeDist > = 3 ) ? 10 : 1 ;
size_t n4max = ( maxSizeDist > = 4 ) ? 10 : 1 ;
// Initialize cluster
int procCount = 0 ;
libpredict : : RetInitCluster retInitCluster = libpredict : : InitCluster ( clusterConfStr , procCount ) ;
if ( retInitCluster ! = libpredict : : INIT_CLUSTER_SUCCESS ) {
__spf_print ( 1 , " ERROR: Failed to initialize libpredict cluster with config: %s, return code: %d \n " , clusterConfStr . c_str ( ) , ( int ) retInitCluster ) ;
std : : wstring messageR , messageE ;
__spf_printToLongBuf ( messageE , L " Failed to initialize libpredict cluster with config: %s, return code: %d " ,
to_wstring ( clusterConfStr ) . c_str ( ) , ( int ) retInitCluster ) ;
__spf_printToLongBuf ( messageR , R206 ) ;
getObjectForFileFromMap ( clusterConfStr . c_str ( ) , SPF_messages ) . push_back ( Messages ( ERROR , 1 , messageR , messageE , 1063 ) ) ;
return ;
}
for ( size_t n1 = 2 ; n1 < = procCount ; + + n1 ) {
for ( size_t n2 = 1 ; n2 < = n1 & & n1 * n2 < = procCount ; + + n2 ) {
if ( n2 ! = 1 & & maxSizeDist < 2 | | n2 = = 1 & & maxSizeDist = = 2 ) continue ;
for ( size_t n3 = 1 ; n3 < = n2 & & n1 * n2 * n3 < = procCount ; + + n3 ) {
if ( n3 ! = 1 & & maxSizeDist < 3 | | n3 = = 1 & & maxSizeDist = = 3 ) continue ;
for ( size_t n4 = 1 ; n4 < = n3 & & n1 * n2 * n3 * n4 < = procCount ; + + n4 ) {
if ( n4 ! = 1 & & maxSizeDist < 4 | | n4 = = 1 & & maxSizeDist = = 4 ) continue ;
for ( size_t n1 = 1 ; n1 < = n1max ; + + n1 ) {
for ( size_t n2 = 1 ; n2 < = n2max ; + + n2 ) {
for ( size_t n3 = 1 ; n3 < = n3max ; + + n3 ) {
for ( size_t n4 = 1 ; n4 < = n4max ; + + n4 ) {
topologies . push_back ( vector < size_t > { n1 , n2 , n3 , n4 } ) ;
}
}
@@ -279,13 +364,12 @@ void runPredictScheme(SgProject &project,
vector < size_t > best ;
double bestTime = std : : numeric_limits < double > : : max ( ) ;
for ( auto & topology : topologies ) {
// if (DEBUG) {
// string outStr = "";
// for (const auto &elem : top)
// outStr += std::to_string(elem) + " ";
// __spf_print(1, "topology %s has time %f\n", outStr.c_str(), currTime);
// }
double currTime = runLibpredictCalc ( project , topology , clusterConfStr , parallelRegions , loopGraph , SPF_messages ) ;
double currTime = runLibpredictCalc ( project , topology , clusterConfStr , parallelRegions , loopGraph , SPF_messages , templateIdMapping ) ;
string outStr = " " ;
for ( const auto & elem : topology )
outStr + = std : : to_string ( elem ) + " " ;
__spf_print ( 1 , " topology %s has time %f \n " , outStr . c_str ( ) , currTime ) ;
if ( currTime = = - 1 )
return ;