From 42d09b133f59cf743d59decf0d4e998187ae35c5 Mon Sep 17 00:00:00 2001 From: tony <292224750@qq.com> Date: Mon, 5 Jun 2023 22:07:53 +0800 Subject: [PATCH 1/2] 1. fix the uint64_t bugs 2. Note; please note the int8 is just a toy --- benchmark/scripts/scanAColCPP/drawTogether.py | 6 +- benchmark/scripts/scanARowCPP/drawTogether.py | 16 +- benchmark/scripts/scanThreads/drawTogether.py | 64 +++--- .../scripts/scanThreadsBackup/drawTogether.py | 50 ++--- .../scanThreadsRockPi5/drawTogether.py | 64 +++--- benchmark/scripts/scanThreadsRockPi5/main.c | 17 +- benchmark/src/Benchmark.cpp | 8 +- benchmark/torchscripts/TugOfWar.py | 11 +- include/CPPAlgos/BCRSCPPAlgo.h | 32 +-- include/CPPAlgos/BetaCoOFDCPPAlgo.h | 32 +-- include/CPPAlgos/CRSV2CPPAlgo.h | 32 +-- include/CPPAlgos/CoOccurringFDCPPAlgo.h | 32 +-- include/CPPAlgos/CountSketchCPPAlgo.h | 32 +-- include/CPPAlgos/EWSCPPAlgo.h | 32 +-- include/CPPAlgos/INT8CPPAlgo.h | 2 +- include/CPPAlgos/TugOfWarCPPAlgo.h | 42 ++-- .../Parallelization/BlockPartitionRunner.h | 11 +- src/CPPAlgos/BCRSCPPAlgo.cpp | 40 ++-- src/CPPAlgos/BetaCoOFDCPPAlgo.cpp | 190 +++++++++--------- src/CPPAlgos/CRSV2CPPAlgo.cpp | 64 +++--- src/CPPAlgos/CoOccurringFDCPPAlgo.cpp | 172 ++++++++-------- src/CPPAlgos/CountSketchCPPAlgo.cpp | 46 ++--- src/CPPAlgos/EWSCPPAlgo.cpp | 38 ++-- src/CPPAlgos/INT8CPPAlgo.cpp | 20 +- src/CPPAlgos/TugOfWarCPPAlgo.cpp | 71 ++++--- src/Parallelization/BlockPartitionRunner.cpp | 25 +-- test/SystemTest/CRSTest.cpp | 32 +-- test/SystemTest/EWSTest.cpp | 16 +- test/SystemTest/INT8Test.cpp | 8 +- test/SystemTest/SketchTest.cpp | 32 +-- test/torchscripts/TugOfWar.py | 11 +- 31 files changed, 625 insertions(+), 623 deletions(-) diff --git a/benchmark/scripts/scanAColCPP/drawTogether.py b/benchmark/scripts/scanAColCPP/drawTogether.py index e0400b63..0948bd1b 100755 --- a/benchmark/scripts/scanAColCPP/drawTogether.py +++ b/benchmark/scripts/scanAColCPP/drawTogether.py @@ -123,7 +123,7 @@ def compareMethod(exeSpace, commonPathBase, resultPaths, csvTemplates, periodVec def main(): exeSpace = os.path.abspath(os.path.join(os.getcwd(), "../..")) + "/" commonBase = os.path.abspath(os.path.join(os.getcwd(), "../..")) + "/results/" + scanTag + "/" - figPath = os.path.abspath(os.path.join(os.getcwd(), "../..")) + "/figures/" + scanTag+"CPP" + figPath = os.path.abspath(os.path.join(os.getcwd(), "../..")) + "/figures/" + scanTag + "CPP" methodTags = ["FD-AMM", "Co-AMM", "BCo-AMM", "Couter-sketch", "MM"] resultPaths = ["fd", "co", "co", "cs", "mm"] csvTemplates = ["config_FDAMM.csv", "config_CoAMM.csv", "config_BCoAMM.csv", "config_CounterSketch.csv", @@ -139,11 +139,11 @@ def main(): os.system("sudo rm -rf " + commonBase) os.system("sudo mkdir " + commonBase) reRun = 1 - + # sampling resultPaths = ["crs-cpp", "mm-cpp"] csvTemplates = ["config_CPPCRS.csv", "config_CPPMM.csv"] - methodTags = ["crs-cpp","mm-cpp"] + methodTags = ["crs-cpp", "mm-cpp"] elapsedTimeAll, cacheMissAll, periodAll = compareMethod(exeSpace, commonBase, resultPaths, csvTemplates, valueVec, reRun) groupLine.DrawFigure(periodAll, elapsedTimeAll, diff --git a/benchmark/scripts/scanARowCPP/drawTogether.py b/benchmark/scripts/scanARowCPP/drawTogether.py index 9be1bdba..2d77db57 100755 --- a/benchmark/scripts/scanARowCPP/drawTogether.py +++ b/benchmark/scripts/scanARowCPP/drawTogether.py @@ -123,12 +123,12 @@ def compareMethod(exeSpace, commonPathBase, resultPaths, csvTemplates, periodVec def main(): exeSpace = os.path.abspath(os.path.join(os.getcwd(), "../..")) + "/" commonBase = os.path.abspath(os.path.join(os.getcwd(), "../..")) + "/results/" + scanTag + "/" - figPath = os.path.abspath(os.path.join(os.getcwd(), "../..")) + "/figures/" + scanTag+"CPP" + figPath = os.path.abspath(os.path.join(os.getcwd(), "../..")) + "/figures/" + scanTag + "CPP" methodTags = ["FD-AMM", "Co-AMM", "BCo-AMM", "Couter-sketch", "MM"] resultPaths = ["fd", "co", "co", "cs", "mm"] csvTemplates = ["config_FDAMM.csv", "config_CoAMM.csv", "config_BCoAMM.csv", "config_CounterSketch.csv", "config_RAWMM.csv"] - valueVec = [100,200,500,1000,5000, 10000, 20000, 50000,100000] + valueVec = [100, 200, 500, 1000, 5000, 10000, 20000, 50000, 100000] valueVecDisp = np.array(valueVec) # run reRun = 0 @@ -142,12 +142,12 @@ def main(): # skech # sampling - #resultPaths = ["crs", "bcrs", "ews", "mm","mm-cpp","crs-cpp"] - resultPaths = ["mm-cpp","crs-cpp"] - csvTemplates = ["config_CPPMM.csv","config_CPPCRS.csv"] - methodTags = ["MM_CPP","CRS_CPP"] - #csvTemplates = ["config_CRS.csv", "config_BerCRS.csv", "config_EWS.csv", "config_RAWMM.csv","config_CPPMM.csv","config_CPPCRS.csv"] - #methodTags = ["CRS", "Ber-CRS", "EWS", "MM","MM_CPP","CRS_CPP"] + # resultPaths = ["crs", "bcrs", "ews", "mm","mm-cpp","crs-cpp"] + resultPaths = ["mm-cpp", "crs-cpp"] + csvTemplates = ["config_CPPMM.csv", "config_CPPCRS.csv"] + methodTags = ["MM_CPP", "CRS_CPP"] + # csvTemplates = ["config_CRS.csv", "config_BerCRS.csv", "config_EWS.csv", "config_RAWMM.csv","config_CPPMM.csv","config_CPPCRS.csv"] + # methodTags = ["CRS", "Ber-CRS", "EWS", "MM","MM_CPP","CRS_CPP"] elapsedTimeAll, cacheMissAll, periodAll = compareMethod(exeSpace, commonBase, resultPaths, csvTemplates, valueVec, reRun) groupLine.DrawFigure(periodAll, elapsedTimeAll, diff --git a/benchmark/scripts/scanThreads/drawTogether.py b/benchmark/scripts/scanThreads/drawTogether.py index e865deec..9cdaf188 100755 --- a/benchmark/scripts/scanThreads/drawTogether.py +++ b/benchmark/scripts/scanThreads/drawTogether.py @@ -80,8 +80,8 @@ def readResultSingle(singleValue, resultPath): cacheMiss = readConfig(resultFname, "cacheMiss") cacheRefs = readConfig(resultFname, "cacheRefs") froError = readConfig(resultFname, "froError") - errorBoundRatio=readConfig(resultFname, "errorBoundRatio") - return elapsedTime, cacheMiss, cacheRefs,froError,errorBoundRatio + errorBoundRatio = readConfig(resultFname, "errorBoundRatio") + return elapsedTime, cacheMiss, cacheRefs, froError, errorBoundRatio def cleanPath(path): @@ -94,15 +94,16 @@ def readResultVector(singleValueVec, resultPath): cacheMissVec = [] cacheRefVec = [] froErrorVec = [] - errorBoundRatioVec=[] + errorBoundRatioVec = [] for i in singleValueVec: - elapsedTime, cacheMiss, cacheRefs,froError,errorBoundRatio = readResultSingle(i, resultPath) + elapsedTime, cacheMiss, cacheRefs, froError, errorBoundRatio = readResultSingle(i, resultPath) elapseTimeVec.append(float(elapsedTime) / 1000.0) cacheMissVec.append(float(cacheMiss)) cacheRefVec.append(float(cacheRefs)) froErrorVec.append(float(froError)) errorBoundRatioVec.append(float(errorBoundRatio)) - return np.array(elapseTimeVec), np.array(cacheMissVec), np.array(cacheRefVec),np.array(froErrorVec),np.array(errorBoundRatioVec) + return np.array(elapseTimeVec), np.array(cacheMissVec), np.array(cacheRefVec), np.array(froErrorVec), np.array( + errorBoundRatioVec) def compareMethod(exeSpace, commonPathBase, resultPaths, csvTemplates, periodVec, reRun=1): @@ -110,24 +111,24 @@ def compareMethod(exeSpace, commonPathBase, resultPaths, csvTemplates, periodVec cacheMissAll = [] cacheRefAll = [] periodAll = [] - froAll=[] - errorBoundRatioAll=[] + froAll = [] + errorBoundRatioAll = [] for i in range(len(csvTemplates)): resultPath = commonPathBase + resultPaths[i] if (reRun == 1): os.system("sudo rm -rf " + resultPath) os.system("sudo mkdir " + resultPath) runScanVector(exeSpace, periodVec, resultPath, csvTemplates[i]) - elapsedTime, cacheMiss, cacheRef,fro,eb = readResultVector(periodVec, resultPath) + elapsedTime, cacheMiss, cacheRef, fro, eb = readResultVector(periodVec, resultPath) elapsedTimeAll.append(elapsedTime) cacheMissAll.append(cacheMiss) cacheRefAll.append(cacheRef) periodAll.append(periodVec) - cacheMissRateAll = np.array(cacheMissAll)/np.array(cacheRefAll)*100.0 + cacheMissRateAll = np.array(cacheMissAll) / np.array(cacheRefAll) * 100.0 froAll.append(fro) errorBoundRatioAll.append(eb) # periodAll.append(periodVec) - return np.array(elapsedTimeAll), cacheMissRateAll, periodAll,np.array(froAll),np.array(errorBoundRatioAll) + return np.array(elapsedTimeAll), cacheMissRateAll, periodAll, np.array(froAll), np.array(errorBoundRatioAll) def main(): @@ -136,10 +137,10 @@ def main(): figPath = os.path.abspath(os.path.join(os.getcwd(), "../..")) + "/figures/" + scanTag configTemplate = exeSpace + "config.csv" commonBase = resultPath + "/" - resultPaths = ["CRS","MM"] - csvTemplates = ["config_CPPCRS.csv","config_CPPMM.csv"] - evaTypes = ['crs-cpp','mm-cpp'] - valueVec = [1,2, 4, 6, 8, 10, 12] + resultPaths = ["CRS", "MM"] + csvTemplates = ["config_CPPCRS.csv", "config_CPPMM.csv"] + evaTypes = ['crs-cpp', 'mm-cpp'] + valueVec = [1, 2, 4, 6, 8, 10, 12] valueVecRun = valueVec print(configTemplate) reRun = 0 @@ -155,20 +156,21 @@ def main(): tRows = len(resultPaths) tCols = len(valueVec) elapseTimeAllSum = np.zeros((tRows, tCols)) - froErroAllSum = np.zeros((tRows, tCols)) - errorBoundRatioSum= np.zeros((tRows, tCols)) - cacheMissAll=np.zeros((tRows, tCols)) + froErroAllSum = np.zeros((tRows, tCols)) + errorBoundRatioSum = np.zeros((tRows, tCols)) + cacheMissAll = np.zeros((tRows, tCols)) rounds = 10 for i in range(rounds): - elapseTimeAll, ch, periodAll,fro,eb = compareMethod(exeSpace, commonBase, resultPaths, csvTemplates, valueVec, reRun) + elapseTimeAll, ch, periodAll, fro, eb = compareMethod(exeSpace, commonBase, resultPaths, csvTemplates, valueVec, + reRun) elapseTimeAllSum = elapseTimeAllSum + elapseTimeAll froErroAllSum = froErroAllSum + fro - errorBoundRatioSum = errorBoundRatioSum+eb - cacheMissAll=cacheMissAll+ch + errorBoundRatioSum = errorBoundRatioSum + eb + cacheMissAll = cacheMissAll + ch elapseTimeAllSum = elapseTimeAllSum / float(rounds) - froErroAllSum = froErroAllSum / float(rounds) - errorBoundRatioSum = errorBoundRatioSum/float(rounds) - cacheMissAll=cacheMissAll/float(rounds) + froErroAllSum = froErroAllSum / float(rounds) + errorBoundRatioSum = errorBoundRatioSum / float(rounds) + cacheMissAll = cacheMissAll / float(rounds) # evaTypes = ['FDAMM', 'MM', 'Co-FD', 'BCO-FD'] # elapseTimeVecFD, cacheMissVecFD, cacheRefVecFD = readResultVector(valueVecRun, resultPathFDAMM) @@ -177,24 +179,24 @@ def main(): # os.system("mkdir " + figPath) groupLine.DrawFigureXYnormal(periodAll, - 1/elapseTimeAllSum, + 1 / elapseTimeAllSum, evaTypes, - "#threads", "1/elapsed time (1/ms)", 0, 1, figPath + "/"+"threads" + "_elapsedTime", + "#threads", "1/elapsed time (1/ms)", 0, 1, figPath + "/" + "threads" + "_elapsedTime", True) groupLine.DrawFigureXYnormal(periodAll, - froErroAllSum*100.0, + froErroAllSum * 100.0, evaTypes, - "#threads", "normalized error %", 0, 1, figPath + "/"+"threads" + "_froError", + "#threads", "normalized error %", 0, 1, figPath + "/" + "threads" + "_froError", True) groupLine.DrawFigureXYnormal(periodAll, - errorBoundRatioSum*100.0, + errorBoundRatioSum * 100.0, evaTypes, - "#threads", "error bound ratio %", 0, 1, figPath + "/"+"threads" + "_ebRatio", + "#threads", "error bound ratio %", 0, 1, figPath + "/" + "threads" + "_ebRatio", True) groupLine.DrawFigureXYnormal(periodAll, - cacheMissAll, + cacheMissAll, evaTypes, - "#threads", "cache miss %", 0, 1, figPath + "/"+"threads" + "_cachemiss", + "#threads", "cache miss %", 0, 1, figPath + "/" + "threads" + "_cachemiss", True) # draw2yLine("watermark time (ms)",singleValueVecDisp,lat95Vec,errVec,"95% Latency (ms)","Error","ms","",figPath+"wm_lat") # draw2yLine("watermark time (ms)",singleValueVecDisp,thrVec,errVec,"Throughput (KTp/s)","Error","KTp/s","",figPath+"wm_thr") diff --git a/benchmark/scripts/scanThreadsBackup/drawTogether.py b/benchmark/scripts/scanThreadsBackup/drawTogether.py index b8173658..f67d1f23 100755 --- a/benchmark/scripts/scanThreadsBackup/drawTogether.py +++ b/benchmark/scripts/scanThreadsBackup/drawTogether.py @@ -80,8 +80,8 @@ def readResultSingle(singleValue, resultPath): cacheMiss = readConfig(resultFname, "cacheMiss") cacheRefs = readConfig(resultFname, "cacheRefs") froError = readConfig(resultFname, "froError") - errorBoundRatio=readConfig(resultFname, "errorBoundRatio") - return elapsedTime, cacheMiss, cacheRefs,froError,errorBoundRatio + errorBoundRatio = readConfig(resultFname, "errorBoundRatio") + return elapsedTime, cacheMiss, cacheRefs, froError, errorBoundRatio def cleanPath(path): @@ -94,15 +94,16 @@ def readResultVector(singleValueVec, resultPath): cacheMissVec = [] cacheRefVec = [] froErrorVec = [] - errorBoundRatioVec=[] + errorBoundRatioVec = [] for i in singleValueVec: - elapsedTime, cacheMiss, cacheRefs,froError,errorBoundRatio = readResultSingle(i, resultPath) + elapsedTime, cacheMiss, cacheRefs, froError, errorBoundRatio = readResultSingle(i, resultPath) elapseTimeVec.append(float(elapsedTime) / 1000.0) cacheMissVec.append(float(cacheMiss)) cacheRefVec.append(float(cacheRefs)) froErrorVec.append(float(froError)) errorBoundRatioVec.append(float(errorBoundRatio)) - return np.array(elapseTimeVec), np.array(cacheMissVec), np.array(cacheRefVec),np.array(froErrorVec),np.array(errorBoundRatioVec) + return np.array(elapseTimeVec), np.array(cacheMissVec), np.array(cacheRefVec), np.array(froErrorVec), np.array( + errorBoundRatioVec) def compareMethod(exeSpace, commonPathBase, resultPaths, csvTemplates, periodVec, reRun=1): @@ -110,15 +111,15 @@ def compareMethod(exeSpace, commonPathBase, resultPaths, csvTemplates, periodVec cacheMissAll = [] cacheRefAll = [] periodAll = [] - froAll=[] - errorBoundRatioAll=[] + froAll = [] + errorBoundRatioAll = [] for i in range(len(csvTemplates)): resultPath = commonPathBase + resultPaths[i] if (reRun == 1): os.system("sudo rm -rf " + resultPath) os.system("sudo mkdir " + resultPath) runScanVector(exeSpace, periodVec, resultPath, csvTemplates[i]) - elapsedTime, cacheMiss, cacheRef,fro,eb = readResultVector(periodVec, resultPath) + elapsedTime, cacheMiss, cacheRef, fro, eb = readResultVector(periodVec, resultPath) elapsedTimeAll.append(elapsedTime) cacheMissAll.append(cacheMiss) cacheRefAll.append(cacheRef) @@ -127,7 +128,7 @@ def compareMethod(exeSpace, commonPathBase, resultPaths, csvTemplates, periodVec froAll.append(fro) errorBoundRatioAll.append(eb) # periodAll.append(periodVec) - return np.array(elapsedTimeAll), cacheMissRateAll, periodAll,np.array(froAll),np.array(errorBoundRatioAll) + return np.array(elapsedTimeAll), cacheMissRateAll, periodAll, np.array(froAll), np.array(errorBoundRatioAll) def main(): @@ -136,9 +137,9 @@ def main(): figPath = os.path.abspath(os.path.join(os.getcwd(), "../..")) + "/figures/" + scanTag configTemplate = exeSpace + "config.csv" commonBase = resultPath + "/" - resultPaths = ["CRS","CRSRAW","MM","MMRAW"] - csvTemplates = ["config_CPPCRS.csv","config_CRS.csv","config_RAWMM.csv","config_CPPMM.csv"] - evaTypes = ['crs-cpp','crs-pt','mm-pt','mm-cpp'] + resultPaths = ["CRS", "CRSRAW", "MM", "MMRAW"] + csvTemplates = ["config_CPPCRS.csv", "config_CRS.csv", "config_RAWMM.csv", "config_CPPMM.csv"] + evaTypes = ['crs-cpp', 'crs-pt', 'mm-pt', 'mm-cpp'] valueVec = [2, 4, 6, 8, 10, 12] valueVecRun = valueVec print(configTemplate) @@ -155,17 +156,18 @@ def main(): tRows = len(resultPaths) tCols = len(valueVec) elapseTimeAllSum = np.zeros((tRows, tCols)) - froErroAllSum = np.zeros((tRows, tCols)) - errorBoundRatioSum= np.zeros((tRows, tCols)) + froErroAllSum = np.zeros((tRows, tCols)) + errorBoundRatioSum = np.zeros((tRows, tCols)) rounds = 1 for i in range(rounds): - elapseTimeAll, ch, periodAll,fro,eb = compareMethod(exeSpace, commonBase, resultPaths, csvTemplates, valueVec, reRun) + elapseTimeAll, ch, periodAll, fro, eb = compareMethod(exeSpace, commonBase, resultPaths, csvTemplates, valueVec, + reRun) elapseTimeAllSum = elapseTimeAllSum + elapseTimeAll froErroAllSum = froErroAllSum + fro - errorBoundRatioSum = errorBoundRatioSum+eb + errorBoundRatioSum = errorBoundRatioSum + eb elapseTimeAllSum = elapseTimeAllSum / float(rounds) - froErroAllSum = froErroAllSum / float(rounds) - errorBoundRatioSum = errorBoundRatioSum/float(rounds) + froErroAllSum = froErroAllSum / float(rounds) + errorBoundRatioSum = errorBoundRatioSum / float(rounds) # evaTypes = ['FDAMM', 'MM', 'Co-FD', 'BCO-FD'] # elapseTimeVecFD, cacheMissVecFD, cacheRefVecFD = readResultVector(valueVecRun, resultPathFDAMM) @@ -174,19 +176,19 @@ def main(): # os.system("mkdir " + figPath) groupLine.DrawFigureXYnormal(periodAll, - 1/elapseTimeAllSum, + 1 / elapseTimeAllSum, evaTypes, - "#threads", "1/elapsed time (1/ms)", 0, 1, figPath + "/"+"threads" + "_elapsedTime", + "#threads", "1/elapsed time (1/ms)", 0, 1, figPath + "/" + "threads" + "_elapsedTime", True) groupLine.DrawFigureXYnormal(periodAll, - froErroAllSum*100.0, + froErroAllSum * 100.0, evaTypes, - "#threads", "normalized error %", 0, 1, figPath + "/"+"threads" + "_froError", + "#threads", "normalized error %", 0, 1, figPath + "/" + "threads" + "_froError", True) groupLine.DrawFigureXYnormal(periodAll, - errorBoundRatioSum*100.0, + errorBoundRatioSum * 100.0, evaTypes, - "#threads", "error bound ratio %", 0, 1, figPath + "/"+"threads" + "_ebRatio", + "#threads", "error bound ratio %", 0, 1, figPath + "/" + "threads" + "_ebRatio", True) # draw2yLine("watermark time (ms)",singleValueVecDisp,lat95Vec,errVec,"95% Latency (ms)","Error","ms","",figPath+"wm_lat") # draw2yLine("watermark time (ms)",singleValueVecDisp,thrVec,errVec,"Throughput (KTp/s)","Error","KTp/s","",figPath+"wm_thr") diff --git a/benchmark/scripts/scanThreadsRockPi5/drawTogether.py b/benchmark/scripts/scanThreadsRockPi5/drawTogether.py index 026cb34e..9a87b3ae 100755 --- a/benchmark/scripts/scanThreadsRockPi5/drawTogether.py +++ b/benchmark/scripts/scanThreadsRockPi5/drawTogether.py @@ -80,8 +80,8 @@ def readResultSingle(singleValue, resultPath): cacheMiss = readConfig(resultFname, "cacheMiss") cacheRefs = readConfig(resultFname, "cacheRefs") froError = readConfig(resultFname, "froError") - errorBoundRatio=readConfig(resultFname, "errorBoundRatio") - return elapsedTime, cacheMiss, cacheRefs,froError,errorBoundRatio + errorBoundRatio = readConfig(resultFname, "errorBoundRatio") + return elapsedTime, cacheMiss, cacheRefs, froError, errorBoundRatio def cleanPath(path): @@ -94,15 +94,16 @@ def readResultVector(singleValueVec, resultPath): cacheMissVec = [] cacheRefVec = [] froErrorVec = [] - errorBoundRatioVec=[] + errorBoundRatioVec = [] for i in singleValueVec: - elapsedTime, cacheMiss, cacheRefs,froError,errorBoundRatio = readResultSingle(i, resultPath) + elapsedTime, cacheMiss, cacheRefs, froError, errorBoundRatio = readResultSingle(i, resultPath) elapseTimeVec.append(float(elapsedTime) / 1000.0) cacheMissVec.append(float(cacheMiss)) cacheRefVec.append(float(cacheRefs)) froErrorVec.append(float(froError)) errorBoundRatioVec.append(float(errorBoundRatio)) - return np.array(elapseTimeVec), np.array(cacheMissVec), np.array(cacheRefVec),np.array(froErrorVec),np.array(errorBoundRatioVec) + return np.array(elapseTimeVec), np.array(cacheMissVec), np.array(cacheRefVec), np.array(froErrorVec), np.array( + errorBoundRatioVec) def compareMethod(exeSpace, commonPathBase, resultPaths, csvTemplates, periodVec, reRun=1): @@ -110,24 +111,24 @@ def compareMethod(exeSpace, commonPathBase, resultPaths, csvTemplates, periodVec cacheMissAll = [] cacheRefAll = [] periodAll = [] - froAll=[] - errorBoundRatioAll=[] + froAll = [] + errorBoundRatioAll = [] for i in range(len(csvTemplates)): resultPath = commonPathBase + resultPaths[i] if (reRun == 1): os.system("sudo rm -rf " + resultPath) os.system("sudo mkdir " + resultPath) runScanVector(exeSpace, periodVec, resultPath, csvTemplates[i]) - elapsedTime, cacheMiss, cacheRef,fro,eb = readResultVector(periodVec, resultPath) + elapsedTime, cacheMiss, cacheRef, fro, eb = readResultVector(periodVec, resultPath) elapsedTimeAll.append(elapsedTime) cacheMissAll.append(cacheMiss) cacheRefAll.append(cacheRef) periodAll.append(periodVec) - cacheMissRateAll = np.array(cacheMissAll)/np.array(cacheRefAll)*100.0 + cacheMissRateAll = np.array(cacheMissAll) / np.array(cacheRefAll) * 100.0 froAll.append(fro) errorBoundRatioAll.append(eb) # periodAll.append(periodVec) - return np.array(elapsedTimeAll), cacheMissRateAll, periodAll,np.array(froAll),np.array(errorBoundRatioAll) + return np.array(elapsedTimeAll), cacheMissRateAll, periodAll, np.array(froAll), np.array(errorBoundRatioAll) def main(): @@ -136,10 +137,10 @@ def main(): figPath = os.path.abspath(os.path.join(os.getcwd(), "../..")) + "/figures/" + scanTag configTemplate = exeSpace + "config.csv" commonBase = resultPath + "/" - resultPaths = ["CRS","MM","counter-sketch","BCO"] - csvTemplates = ["config_CPPCRS.csv","config_CPPMM.csv","config_CPPCounterSketch.csv","config_CPPBCO.csv"] - evaTypes = ['crs-cpp','mm-cpp','counter-sketch-cpp','bco'] - valueVec = [1,2, 3,4,5,6,7,8] + resultPaths = ["CRS", "MM", "counter-sketch", "BCO"] + csvTemplates = ["config_CPPCRS.csv", "config_CPPMM.csv", "config_CPPCounterSketch.csv", "config_CPPBCO.csv"] + evaTypes = ['crs-cpp', 'mm-cpp', 'counter-sketch-cpp', 'bco'] + valueVec = [1, 2, 3, 4, 5, 6, 7, 8] valueVecRun = valueVec print(configTemplate) reRun = 0 @@ -155,20 +156,21 @@ def main(): tRows = len(resultPaths) tCols = len(valueVec) elapseTimeAllSum = np.zeros((tRows, tCols)) - froErroAllSum = np.zeros((tRows, tCols)) - errorBoundRatioSum= np.zeros((tRows, tCols)) - cacheMissAll=np.zeros((tRows, tCols)) + froErroAllSum = np.zeros((tRows, tCols)) + errorBoundRatioSum = np.zeros((tRows, tCols)) + cacheMissAll = np.zeros((tRows, tCols)) rounds = 1 for i in range(rounds): - elapseTimeAll, ch, periodAll,fro,eb = compareMethod(exeSpace, commonBase, resultPaths, csvTemplates, valueVec, reRun) + elapseTimeAll, ch, periodAll, fro, eb = compareMethod(exeSpace, commonBase, resultPaths, csvTemplates, valueVec, + reRun) elapseTimeAllSum = elapseTimeAllSum + elapseTimeAll froErroAllSum = froErroAllSum + fro - errorBoundRatioSum = errorBoundRatioSum+eb - cacheMissAll=cacheMissAll+ch + errorBoundRatioSum = errorBoundRatioSum + eb + cacheMissAll = cacheMissAll + ch elapseTimeAllSum = elapseTimeAllSum / float(rounds) - froErroAllSum = froErroAllSum / float(rounds) - errorBoundRatioSum = errorBoundRatioSum/float(rounds) - cacheMissAll=cacheMissAll/float(rounds) + froErroAllSum = froErroAllSum / float(rounds) + errorBoundRatioSum = errorBoundRatioSum / float(rounds) + cacheMissAll = cacheMissAll / float(rounds) # evaTypes = ['FDAMM', 'MM', 'Co-FD', 'BCO-FD'] # elapseTimeVecFD, cacheMissVecFD, cacheRefVecFD = readResultVector(valueVecRun, resultPathFDAMM) @@ -177,24 +179,24 @@ def main(): # os.system("mkdir " + figPath) groupLine.DrawFigureXYnormal(periodAll, - 1/elapseTimeAllSum, + 1 / elapseTimeAllSum, evaTypes, - "#threads", "1/elapsed time (1/ms)", 0, 1, figPath + "/"+"threads" + "_elapsedTime", + "#threads", "1/elapsed time (1/ms)", 0, 1, figPath + "/" + "threads" + "_elapsedTime", True) groupLine.DrawFigureXYnormal(periodAll, - froErroAllSum*100.0, + froErroAllSum * 100.0, evaTypes, - "#threads", "normalized error %", 0, 1, figPath + "/"+"threads" + "_froError", + "#threads", "normalized error %", 0, 1, figPath + "/" + "threads" + "_froError", True) groupLine.DrawFigureXYnormal(periodAll, - errorBoundRatioSum*100.0, + errorBoundRatioSum * 100.0, evaTypes, - "#threads", "error bound ratio %", 0, 1, figPath + "/"+"threads" + "_ebRatio", + "#threads", "error bound ratio %", 0, 1, figPath + "/" + "threads" + "_ebRatio", True) groupLine.DrawFigureXYnormal(periodAll, - cacheMissAll, + cacheMissAll, evaTypes, - "#threads", "cache miss %", 0, 1, figPath + "/"+"threads" + "_cachemiss", + "#threads", "cache miss %", 0, 1, figPath + "/" + "threads" + "_cachemiss", True) # draw2yLine("watermark time (ms)",singleValueVecDisp,lat95Vec,errVec,"95% Latency (ms)","Error","ms","",figPath+"wm_lat") # draw2yLine("watermark time (ms)",singleValueVecDisp,thrVec,errVec,"Throughput (KTp/s)","Error","KTp/s","",figPath+"wm_thr") diff --git a/benchmark/scripts/scanThreadsRockPi5/main.c b/benchmark/scripts/scanThreadsRockPi5/main.c index 9f1decab..35fa90f7 100644 --- a/benchmark/scripts/scanThreadsRockPi5/main.c +++ b/benchmark/scripts/scanThreadsRockPi5/main.c @@ -1,15 +1,14 @@ #include #include int isFP32Supported() { - float32x4_t a; - // Perform a NEON vector addition of FP32 values - float32_t result = vaddvq_f32(a); - printf("ok\r\n"); - // If compilation succeeds, FP32 operations are supported - return 1; + float32x4_t a; + // Perform a NEON vector addition of FP32 values + float32_t result = vaddvq_f32(a); + printf("ok\r\n"); + // If compilation succeeds, FP32 operations are supported + return 1; } -void main() -{ +void main() { -isFP32Supported(); + isFP32Supported(); } diff --git a/benchmark/src/Benchmark.cpp b/benchmark/src/Benchmark.cpp index c9597c5d..24e7da63 100644 --- a/benchmark/src/Benchmark.cpp +++ b/benchmark/src/Benchmark.cpp @@ -22,7 +22,7 @@ void runSingleThreadTest(std::string configName) { uint64_t usingMeter = cfg->tryU64("usingMeter", 0, true); std::string meterTag = cfg->tryString("meterTag", "intelMsr", true); uint64_t useCPP = cfg->tryU64("useCPP", 0, true); - uint64_t forceMP=cfg->tryU64("forceMP", 0, true); + uint64_t forceMP = cfg->tryU64("forceMP", 0, true); if (usingMeter) { eMeter = meterTable.findMeter(meterTag); if (eMeter != nullptr) { @@ -68,7 +68,7 @@ auto B = torch::rand({(long) aCol, (long) bCol});*/ ThreadPerf pef(-1); pef.setPerfList(); AMMBench::BlockPartitionRunner br; - if (threads > 1||forceMP) { + if (threads > 1 || forceMP) { INTELLI_WARNING("use multithread"); br.setConfig(cfg); br.createABC(A, B); @@ -85,7 +85,7 @@ auto B = torch::rand({(long) aCol, (long) bCol});*/ AMMBench::CPPAlgoTable cppAlgoTable; std::string cppAlgoTag = cfg->tryString("cppAlgoTag", "mm", true); AMMBench::AbstractCPPAlgoPtr cppAlgoPtr = cppAlgoTable.findCppAlgo(cppAlgoTag); - INTELLI_WARNING("single thread, algo "+cppAlgoTag); + INTELLI_WARNING("single thread, algo " + cppAlgoTag); if (eMeter != nullptr) { eMeter->startMeter(); } @@ -114,7 +114,7 @@ auto B = torch::rand({(long) aCol, (long) bCol});*/ resultCsv->edit("energyAll", (double) energyConsumption); resultCsv->edit("energyOnlyMe", (double) pureEnergy); } - if (threads > 1||forceMP) { + if (threads > 1 || forceMP) { INTELLI_WARNING("consider multithread elapsed time"); resultCsv->edit("perfElapsedTime", (uint64_t) br.getElapsedTime()); br.appendThreadInfo(resultCsv); diff --git a/benchmark/torchscripts/TugOfWar.py b/benchmark/torchscripts/TugOfWar.py index 2c1c8928..6d5a5124 100644 --- a/benchmark/torchscripts/TugOfWar.py +++ b/benchmark/torchscripts/TugOfWar.py @@ -3,10 +3,11 @@ import os import math + def tug_of_war_mat(m: int, n: int) -> torch.Tensor: - e = 1/math.sqrt(m) + e = 1 / math.sqrt(m) M = torch.randint(2, (m, n)) - return e*(2*M - 1) + return e * (2 * M - 1) @torch.jit.script @@ -17,7 +18,7 @@ def TugOfWar(A: torch.Tensor, B: torch.Tensor, l: int): delta = 0.2 i_iters = int(-math.log(delta)) - j_iters = int(2*(-math.log(delta) + math.log(-math.log(delta)))) + j_iters = int(2 * (-math.log(delta) + math.log(-math.log(delta)))) z = torch.empty((i_iters,)) AS = [] @@ -34,9 +35,9 @@ def TugOfWar(A: torch.Tensor, B: torch.Tensor, l: int): Q = tug_of_war_mat(16, p) X = A.matmul(B.matmul(Q.T)) X_hat = AS[i].matmul(SB[i].matmul(Q.T)) - y[j] = torch.norm(X - X_hat)**2 + y[j] = torch.norm(X - X_hat) ** 2 z[i] = torch.median(y) - + i_star = torch.argmin(z) return torch.matmul(AS[i_star], SB[i_star]) diff --git a/include/CPPAlgos/BCRSCPPAlgo.h b/include/CPPAlgos/BCRSCPPAlgo.h index 3f78b263..aaf1acd0 100644 --- a/include/CPPAlgos/BCRSCPPAlgo.h +++ b/include/CPPAlgos/BCRSCPPAlgo.h @@ -16,26 +16,26 @@ namespace AMMBench { * @brief The Bernoulli column row sampling (CRS) class of c++ algos * */ - class BCRSCPPAlgo : public AMMBench::AbstractCPPAlgo { - public: - BCRSCPPAlgo() { +class BCRSCPPAlgo : public AMMBench::AbstractCPPAlgo { + public: + BCRSCPPAlgo() { - } + } - ~BCRSCPPAlgo() { + ~BCRSCPPAlgo() { - } + } - /** - * @brief the virtual function provided for outside callers, rewrite in children classes - * @param A the A matrix - * @param B the B matrix - * @param sketchSize the size of sketc or sampling - * @return the output c matrix - */ - virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); + /** + * @brief the virtual function provided for outside callers, rewrite in children classes + * @param A the A matrix + * @param B the B matrix + * @param sketchSize the size of sketc or sampling + * @return the output c matrix + */ + virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); - }; +}; /** * @ingroup AMMBENCH_CppAlgos @@ -43,7 +43,7 @@ namespace AMMBench { * @brief The class to describe a shared pointer to @ref BCRSCppAlgo */ - typedef std::shared_ptr BCRSCPPAlgoPtr; +typedef std::shared_ptr BCRSCPPAlgoPtr; /** * @ingroup AMMBENCH_CppAlgos * @def newBCRSCppAlgo diff --git a/include/CPPAlgos/BetaCoOFDCPPAlgo.h b/include/CPPAlgos/BetaCoOFDCPPAlgo.h index 0b234a5f..6ff6007b 100644 --- a/include/CPPAlgos/BetaCoOFDCPPAlgo.h +++ b/include/CPPAlgos/BetaCoOFDCPPAlgo.h @@ -16,26 +16,26 @@ namespace AMMBench { * @brief The Beta Co-Occurring FD AMM class of c++ algos * */ - class BetaCoOFDCPPAlgo : public AMMBench::AbstractCPPAlgo { - public: - BetaCoOFDCPPAlgo() { +class BetaCoOFDCPPAlgo : public AMMBench::AbstractCPPAlgo { + public: + BetaCoOFDCPPAlgo() { - } + } - ~BetaCoOFDCPPAlgo() { + ~BetaCoOFDCPPAlgo() { - } + } - /** - * @brief the virtual function provided for outside callers, rewrite in children classes - * @param A the A matrix - * @param B the B matrix - * @param sketchSize the size of sketc or sampling - * @return the output c matrix - */ - virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); + /** + * @brief the virtual function provided for outside callers, rewrite in children classes + * @param A the A matrix + * @param B the B matrix + * @param sketchSize the size of sketc or sampling + * @return the output c matrix + */ + virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); - }; +}; /** * @ingroup AMMBENCH_CppAlgos @@ -43,7 +43,7 @@ namespace AMMBench { * @brief The class to describe a shared pointer to @ref BetaCoOFDCppAlgo */ - typedef std::shared_ptr BetaCoOFDCPPAlgoPtr; +typedef std::shared_ptr BetaCoOFDCPPAlgoPtr; /** * @ingroup AMMBENCH_CppAlgos * @def newBetaCoOFDCppAlgo diff --git a/include/CPPAlgos/CRSV2CPPAlgo.h b/include/CPPAlgos/CRSV2CPPAlgo.h index 36304067..142b28fd 100644 --- a/include/CPPAlgos/CRSV2CPPAlgo.h +++ b/include/CPPAlgos/CRSV2CPPAlgo.h @@ -17,26 +17,26 @@ namespace AMMBench { * @brief The column row sampling (CRS) class of c++ algos, a second implementation * */ - class CRSV2CPPAlgo : public AMMBench::AbstractCPPAlgo { - public: - CRSV2CPPAlgo() { +class CRSV2CPPAlgo : public AMMBench::AbstractCPPAlgo { + public: + CRSV2CPPAlgo() { - } + } - ~CRSV2CPPAlgo() { + ~CRSV2CPPAlgo() { - } + } - /** - * @brief the virtual function provided for outside callers, rewrite in children classes - * @param A the A matrix - * @param B the B matrix - * @param sketchSize the size of sketc or sampling - * @return the output c matrix - */ - virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); + /** + * @brief the virtual function provided for outside callers, rewrite in children classes + * @param A the A matrix + * @param B the B matrix + * @param sketchSize the size of sketc or sampling + * @return the output c matrix + */ + virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); - }; +}; /** * @ingroup AMMBENCH_CppAlgos @@ -44,7 +44,7 @@ namespace AMMBench { * @brief The class to describe a shared pointer to @ref CRSV2CppAlgo */ - typedef std::shared_ptr CRSV2CPPAlgoPtr; +typedef std::shared_ptr CRSV2CPPAlgoPtr; /** * @ingroup AMMBENCH_CppAlgos * @def newCRSV2CppAlgo diff --git a/include/CPPAlgos/CoOccurringFDCPPAlgo.h b/include/CPPAlgos/CoOccurringFDCPPAlgo.h index 60cbccf4..a91b8e11 100644 --- a/include/CPPAlgos/CoOccurringFDCPPAlgo.h +++ b/include/CPPAlgos/CoOccurringFDCPPAlgo.h @@ -16,26 +16,26 @@ namespace AMMBench { * @brief The Co-Occurring FD AMM class of c++ algos * */ - class CoOccurringFDCPPAlgo : public AMMBench::AbstractCPPAlgo { - public: - CoOccurringFDCPPAlgo() { +class CoOccurringFDCPPAlgo : public AMMBench::AbstractCPPAlgo { + public: + CoOccurringFDCPPAlgo() { - } + } - ~CoOccurringFDCPPAlgo() { + ~CoOccurringFDCPPAlgo() { - } + } - /** - * @brief the virtual function provided for outside callers, rewrite in children classes - * @param A the A matrix - * @param B the B matrix - * @param sketchSize the size of sketc or sampling - * @return the output c matrix - */ - virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); + /** + * @brief the virtual function provided for outside callers, rewrite in children classes + * @param A the A matrix + * @param B the B matrix + * @param sketchSize the size of sketc or sampling + * @return the output c matrix + */ + virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); - }; +}; /** * @ingroup AMMBENCH_CppAlgos @@ -43,7 +43,7 @@ namespace AMMBench { * @brief The class to describe a shared pointer to @ref CoOccurringFDCppAlgo */ - typedef std::shared_ptr CoOccurringFDCPPAlgoPtr; +typedef std::shared_ptr CoOccurringFDCPPAlgoPtr; /** * @ingroup AMMBENCH_CppAlgos * @def newCoOccurringFDCppAlgo diff --git a/include/CPPAlgos/CountSketchCPPAlgo.h b/include/CPPAlgos/CountSketchCPPAlgo.h index fd3ed50b..66a37b3b 100644 --- a/include/CPPAlgos/CountSketchCPPAlgo.h +++ b/include/CPPAlgos/CountSketchCPPAlgo.h @@ -17,26 +17,26 @@ namespace AMMBench { * @brief The cloumn row sampling (CRS) class of c++ algos * */ - class CountSketchCPPAlgo : public AMMBench::AbstractCPPAlgo { - public: - CountSketchCPPAlgo() { +class CountSketchCPPAlgo : public AMMBench::AbstractCPPAlgo { + public: + CountSketchCPPAlgo() { - } + } - ~CountSketchCPPAlgo() { + ~CountSketchCPPAlgo() { - } + } - /** - * @brief the virtual function provided for outside callers, rewrite in children classes - * @param A the A matrix - * @param B the B matrix - * @param sketchSize the size of sketc or sampling - * @return the output c matrix - */ - torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize) ; + /** + * @brief the virtual function provided for outside callers, rewrite in children classes + * @param A the A matrix + * @param B the B matrix + * @param sketchSize the size of sketc or sampling + * @return the output c matrix + */ + torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); - }; +}; /** * @ingroup AMMBENCH_CppAlgos @@ -44,7 +44,7 @@ namespace AMMBench { * @brief The class to describe a shared pointer to @ref CountSketchCPPAlgo */ - typedef std::shared_ptr CountSketchCPPAlgoPtr; +typedef std::shared_ptr CountSketchCPPAlgoPtr; /** * @ingroup AMMBENCH_CppAlgos * @def newCRSV2CppAlgo diff --git a/include/CPPAlgos/EWSCPPAlgo.h b/include/CPPAlgos/EWSCPPAlgo.h index 842f9309..95a4bfa7 100644 --- a/include/CPPAlgos/EWSCPPAlgo.h +++ b/include/CPPAlgos/EWSCPPAlgo.h @@ -16,26 +16,26 @@ namespace AMMBench { * @brief The Element Wise Sampling (EWS) class of c++ algos * */ - class EWSCPPAlgo : public AMMBench::AbstractCPPAlgo { - public: - EWSCPPAlgo() { +class EWSCPPAlgo : public AMMBench::AbstractCPPAlgo { + public: + EWSCPPAlgo() { - } + } - ~EWSCPPAlgo() { + ~EWSCPPAlgo() { - } + } - /** - * @brief the virtual function provided for outside callers, rewrite in children classes - * @param A the A matrix - * @param B the B matrix - * @param sketchSize the size of sketc or sampling - * @return the output c matrix - */ - virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); + /** + * @brief the virtual function provided for outside callers, rewrite in children classes + * @param A the A matrix + * @param B the B matrix + * @param sketchSize the size of sketc or sampling + * @return the output c matrix + */ + virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); - }; +}; /** * @ingroup AMMBENCH_CppAlgos @@ -43,7 +43,7 @@ namespace AMMBench { * @brief The class to describe a shared pointer to @ref EWSCppAlgo */ - typedef std::shared_ptr EWSCPPAlgoPtr; +typedef std::shared_ptr EWSCPPAlgoPtr; /** * @ingroup AMMBENCH_CppAlgos * @def newEWSCppAlgo diff --git a/include/CPPAlgos/INT8CPPAlgo.h b/include/CPPAlgos/INT8CPPAlgo.h index eb637578..ee4c15b2 100644 --- a/include/CPPAlgos/INT8CPPAlgo.h +++ b/include/CPPAlgos/INT8CPPAlgo.h @@ -21,7 +21,7 @@ namespace AMMBench { * @class INT8CPPAlgo CPPAlgos/INT8CPPAlgo.h * @brief The INT8 MM class of c++ algos */ -class INT8CPPAlgo: public AMMBench::AbstractCPPAlgo { +class INT8CPPAlgo : public AMMBench::AbstractCPPAlgo { public: INT8CPPAlgo() { diff --git a/include/CPPAlgos/TugOfWarCPPAlgo.h b/include/CPPAlgos/TugOfWarCPPAlgo.h index 3ff12d98..198c8e94 100644 --- a/include/CPPAlgos/TugOfWarCPPAlgo.h +++ b/include/CPPAlgos/TugOfWarCPPAlgo.h @@ -17,35 +17,35 @@ namespace AMMBench { * @brief The cloumn row sampling (CRS) class of c++ algos * */ - class TugOfWarCPPAlgo : public AMMBench::AbstractCPPAlgo { - double delta = 0.2; +class TugOfWarCPPAlgo : public AMMBench::AbstractCPPAlgo { + double delta = 0.2; - public: - TugOfWarCPPAlgo() { + public: + TugOfWarCPPAlgo() { - } + } - TugOfWarCPPAlgo(double delta): delta(delta) { + TugOfWarCPPAlgo(double delta) : delta(delta) { - } + } - ~TugOfWarCPPAlgo() { + ~TugOfWarCPPAlgo() { - } + } - /** - * @brief the virtual function provided for outside callers, rewrite in children classes - * @param A the A matrix - * @param B the B matrix - * @param sketchSize the size of sketc or sampling - * @return the output c matrix - */ - virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, int sketchSize); + /** + * @brief the virtual function provided for outside callers, rewrite in children classes + * @param A the A matrix + * @param B the B matrix + * @param sketchSize the size of sketc or sampling + * @return the output c matrix + */ + virtual torch::Tensor amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize); - private: - torch::Tensor generateTugOfWarMatrix(int64_t m, int64_t n); + private: + torch::Tensor generateTugOfWarMatrix(int64_t m, int64_t n); - }; +}; /** * @ingroup AMMBENCH_CppAlgos @@ -53,7 +53,7 @@ namespace AMMBench { * @brief The class to describe a shared pointer to @ref TugOfWarCppAlgo */ - typedef std::shared_ptr TugOfWarCPPAlgoPtr; +typedef std::shared_ptr TugOfWarCPPAlgoPtr; /** * @ingroup AMMBENCH_CppAlgos * @def newTugOfWarCppAlgo diff --git a/include/Parallelization/BlockPartitionRunner.h b/include/Parallelization/BlockPartitionRunner.h index 1a5a48e7..9df87b8b 100644 --- a/include/Parallelization/BlockPartitionRunner.h +++ b/include/Parallelization/BlockPartitionRunner.h @@ -32,7 +32,7 @@ class BlockPartitionWorker : public INTELLI::AbstractC20Thread { AMMBench::CPPAlgoTable cppAlgoTable; struct timeval tstart, tend; uint64_t useCPP = 0; - uint64_t osScheduling=0; + uint64_t osScheduling = 0; AMMBench::AbstractCPPAlgoPtr cppAlgoPtr = nullptr; /** * @brief Input matrix A @@ -75,9 +75,8 @@ class BlockPartitionWorker : public INTELLI::AbstractC20Thread { * @param mycore the core to be binded */ void setWorkParameters(uint64_t aStart, uint64_t aEnd, int mycore); - void setCoreBInd(int cno) - { - coreBind=cno; + void setCoreBInd(int cno) { + coreBind = cno; } ~BlockPartitionWorker() { @@ -128,7 +127,7 @@ class BlockPartitionRunner { /** * @brief special bind of first core, if need */ - uint64_t firstCoreBind=0; + uint64_t firstCoreBind = 0; public: BlockPartitionRunner() {} ~BlockPartitionRunner() {} @@ -169,7 +168,7 @@ class BlockPartitionRunner { * @brief append the running information of each thread to the result csv * @param ru The result csv to be appended */ - void appendThreadInfo(INTELLI::ConfigMapPtr ru); + void appendThreadInfo(INTELLI::ConfigMapPtr ru); }; } // AMMBench diff --git a/src/CPPAlgos/BCRSCPPAlgo.cpp b/src/CPPAlgos/BCRSCPPAlgo.cpp index 631dfd6f..2bc761b2 100644 --- a/src/CPPAlgos/BCRSCPPAlgo.cpp +++ b/src/CPPAlgos/BCRSCPPAlgo.cpp @@ -5,31 +5,31 @@ namespace AMMBench { torch::Tensor BCRSCPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t k2) { - A = A.t(); - auto A_size = A.sizes(); - int64_t n = A_size[0]; - //int64_t m = A_size[1]; - int64_t k=(int64_t)k2; - assert(n == B.size(0)); - //TORCH_CHECK(n == B.size(0)); - //TORCH_CHECK(k < n); + A = A.t(); + auto A_size = A.sizes(); + int64_t n = A_size[0]; + //int64_t m = A_size[1]; + int64_t k = (int64_t) k2; + assert(n == B.size(0)); + //TORCH_CHECK(n == B.size(0)); + //TORCH_CHECK(k < n); - //INTELLI_INFO("Running Bernoulli CRS CPP"); + //INTELLI_INFO("Running Bernoulli CRS CPP"); - // probability distribution - torch::Tensor sample = torch::rand({n}); // default: uniform - sample = sample.div(sample.sum() / k); // sum = k as per the paper + // probability distribution + torch::Tensor sample = torch::rand({n}); // default: uniform + sample = sample.div(sample.sum() / k); // sum = k as per the paper - // diagonal scaling matrix P (nxn) - torch::Tensor P = torch::diag(1.0 / torch::sqrt(sample)); + // diagonal scaling matrix P (nxn) + torch::Tensor P = torch::diag(1.0 / torch::sqrt(sample)); - // random diagonal sampling matrix K (nxn) - sample = (torch::rand({n}) < sample).to(torch::kFloat32); - torch::Tensor K = torch::diag(sample); + // random diagonal sampling matrix K (nxn) + sample = (torch::rand({n}) < sample).to(torch::kFloat32); + torch::Tensor K = torch::diag(sample); - torch::Tensor a = torch::matmul(torch::matmul(A.t(), P), K); - torch::Tensor b = torch::matmul(torch::matmul(a, K), P); + torch::Tensor a = torch::matmul(torch::matmul(A.t(), P), K); + torch::Tensor b = torch::matmul(torch::matmul(a, K), P); - return torch::matmul(b, B); + return torch::matmul(b, B); } } \ No newline at end of file diff --git a/src/CPPAlgos/BetaCoOFDCPPAlgo.cpp b/src/CPPAlgos/BetaCoOFDCPPAlgo.cpp index 05be1884..b65bcc13 100644 --- a/src/CPPAlgos/BetaCoOFDCPPAlgo.cpp +++ b/src/CPPAlgos/BetaCoOFDCPPAlgo.cpp @@ -2,113 +2,113 @@ // Created by haolan on 5/30/23. // #include -torch::Scalar get_first_element(const torch::Tensor& tensor) { - if (tensor.numel() == 1) { - return tensor.item(); - } - else { - return tensor[0].item(); - } +torch::Scalar get_first_element(const torch::Tensor &tensor) { + if (tensor.numel() == 1) { + return tensor.item(); + } else { + return tensor[0].item(); + } } -bool is_empty_tensor(const torch::Tensor& tensor) { - return tensor.numel() == 0; +bool is_empty_tensor(const torch::Tensor &tensor) { + return tensor.numel() == 0; } namespace AMMBench { -torch::Tensor attenuate(float beta, const torch::Tensor& k, int l) { - return (torch::exp(k * beta / (l - 1)) - 1) / (torch::exp(torch::tensor(beta)) - 1); +torch::Tensor attenuate(float beta, const torch::Tensor &k, int l) { + return (torch::exp(k * beta / (l - 1)) - 1) / (torch::exp(torch::tensor(beta)) - 1); } -torch::Tensor paramerizedReduceRank(const torch::Tensor& SV, float delta, int l, float beta) { - torch::Tensor indices = torch::arange(l, torch::kFloat32); - torch::Tensor attenuated_values = attenuate(beta, indices, l); - torch::Tensor parameterizedReduceRank = delta * attenuated_values; - torch::Tensor SV_shrunk = torch::clamp(SV - parameterizedReduceRank, 0); - return SV_shrunk; +torch::Tensor paramerizedReduceRank(const torch::Tensor &SV, float delta, int l, float beta) { + torch::Tensor indices = torch::arange(l, torch::kFloat32); + torch::Tensor attenuated_values = attenuate(beta, indices, l); + torch::Tensor parameterizedReduceRank = delta * attenuated_values; + torch::Tensor SV_shrunk = torch::clamp(SV - parameterizedReduceRank, 0); + return SV_shrunk; } torch::Tensor BetaCoOFDCPPAlgo::amm(const torch::Tensor A, const torch::Tensor B, uint64_t l2) { - torch::Tensor B_t = B.t(); - float beta = 1.0; - - TORCH_CHECK(A.size(1) == B_t.size(1), "Shapes of A and B are incompatible"); - int64_t mx = A.size(0); - int64_t my = B_t.size(0); - int64_t n = A.size(1); - int64_t l=(int64_t)l2; - // Initialize sketch matrices - torch::Tensor BX = torch::zeros({ mx, l }); - torch::Tensor BY = torch::zeros({ my, l }); - - // The first l iterations - for (int64_t i = 0; i < l; ++i) { - BX.slice(1, i, i + 1) = A.slice(1, i, i + 1); - BY.slice(1, i, i + 1) = B_t.slice(1, i, i + 1); + torch::Tensor B_t = B.t(); + float beta = 1.0; + + TORCH_CHECK(A.size(1) == B_t.size(1), "Shapes of A and B are incompatible"); + int64_t mx = A.size(0); + int64_t my = B_t.size(0); + int64_t n = A.size(1); + int64_t l = (int64_t) l2; + // Initialize sketch matrices + torch::Tensor BX = torch::zeros({mx, l}); + torch::Tensor BY = torch::zeros({my, l}); + + // The first l iterations + for (int64_t i = 0; i < l; ++i) { + BX.slice(1, i, i + 1) = A.slice(1, i, i + 1); + BY.slice(1, i, i + 1) = B_t.slice(1, i, i + 1); + } + + torch::Tensor zero_columns = torch::tensor({0}); + zero_columns = zero_columns.slice(0, 1); + + // Iteration l to n: insert if available, else shrink sketch matrices + for (int64_t i = l; i < n; ++i) { + // Acquire the index of a zero-valued column + if (!is_empty_tensor(zero_columns)) { + int idx = get_first_element(zero_columns).toInt(); + BX.slice(1, idx, idx + 1) = A.slice(1, i, i + 1); + BY.slice(1, idx, idx + 1) = B_t.slice(1, i, i + 1); + zero_columns = zero_columns.slice(0, 1); } - - torch::Tensor zero_columns = torch::tensor({ 0 }); - zero_columns = zero_columns.slice(0, 1); - - // Iteration l to n: insert if available, else shrink sketch matrices - for (int64_t i = l; i < n; ++i) { - // Acquire the index of a zero-valued column - if (!is_empty_tensor(zero_columns)) { - int idx = get_first_element(zero_columns).toInt(); - BX.slice(1, idx, idx + 1) = A.slice(1, i, i + 1); - BY.slice(1, idx, idx + 1) = B_t.slice(1, i, i + 1); - zero_columns = zero_columns.slice(0, 1); - } - // If no zero-valued column, shrink accordingly - else { - torch::Tensor QX, RX; - std::tie(QX, RX) = torch::linalg_qr(BX); - torch::Tensor QY, RY; - std::tie(QY, RY) = torch::linalg_qr(BY); - torch::Tensor U, SV, V; - std::tie(U, SV, V) = torch::svd(torch::matmul(RX, RY.t())); - - // Find the median of singular values - torch::Tensor S_sorted, S_indices; - std::tie(S_sorted, S_indices) = SV.sort(); - - float delta; - if (S_sorted.size(0) % 2 == 1) { - delta = S_sorted[S_sorted.size(0) / 2].item().toFloat(); - } else { - delta = torch::median(S_sorted).item().toFloat(); - } - // delta = S_sorted[0].item().toFloat(); - - // Shrink the singular values with delta - torch::Tensor SV_shrunk = paramerizedReduceRank(SV, delta, l, beta); - - // Restore SV diagonal matrix - SV = torch::diag_embed(SV_shrunk); - torch::Tensor SV_sqrt = torch::sqrt(SV); - - // Update indices of zero-valued columns - torch::Tensor zero_indices = torch::nonzero(SV_shrunk == 0).squeeze(); - zero_indices = zero_indices.view({ -1 }); // Convert to a 1D tensor - zero_columns = torch::cat({ zero_columns, zero_indices }); - // Convert tensor to a std::vector - std::vector vec(zero_columns.data_ptr(), zero_columns.data_ptr() + zero_columns.numel()); - - // Sort the vector - std::sort(vec.begin(), vec.end()); - - // Remove duplicates - vec.erase(std::unique(vec.begin(), vec.end()), vec.end()); - - // Convert std::vector back to a tensor - zero_columns = torch::from_blob(vec.data(), { static_cast(vec.size()) }, torch::kInt64).clone(); - - // Update sketch matrices - BX = torch::matmul(torch::matmul(QX, U), SV_sqrt); - BY = torch::matmul(torch::matmul(QY, V), SV_sqrt); - } + // If no zero-valued column, shrink accordingly + else { + torch::Tensor QX, RX; + std::tie(QX, RX) = torch::linalg_qr(BX); + torch::Tensor QY, RY; + std::tie(QY, RY) = torch::linalg_qr(BY); + torch::Tensor U, SV, V; + std::tie(U, SV, V) = torch::svd(torch::matmul(RX, RY.t())); + + // Find the median of singular values + torch::Tensor S_sorted, S_indices; + std::tie(S_sorted, S_indices) = SV.sort(); + + float delta; + if (S_sorted.size(0) % 2 == 1) { + delta = S_sorted[S_sorted.size(0) / 2].item().toFloat(); + } else { + delta = torch::median(S_sorted).item().toFloat(); + } + // delta = S_sorted[0].item().toFloat(); + + // Shrink the singular values with delta + torch::Tensor SV_shrunk = paramerizedReduceRank(SV, delta, l, beta); + + // Restore SV diagonal matrix + SV = torch::diag_embed(SV_shrunk); + torch::Tensor SV_sqrt = torch::sqrt(SV); + + // Update indices of zero-valued columns + torch::Tensor zero_indices = torch::nonzero(SV_shrunk == 0).squeeze(); + zero_indices = zero_indices.view({-1}); // Convert to a 1D tensor + zero_columns = torch::cat({zero_columns, zero_indices}); + // Convert tensor to a std::vector + std::vector + vec(zero_columns.data_ptr(), zero_columns.data_ptr() + zero_columns.numel()); + + // Sort the vector + std::sort(vec.begin(), vec.end()); + + // Remove duplicates + vec.erase(std::unique(vec.begin(), vec.end()), vec.end()); + + // Convert std::vector back to a tensor + zero_columns = torch::from_blob(vec.data(), {static_cast(vec.size())}, torch::kInt64).clone(); + + // Update sketch matrices + BX = torch::matmul(torch::matmul(QX, U), SV_sqrt); + BY = torch::matmul(torch::matmul(QY, V), SV_sqrt); } + } - return torch::matmul(BX, BY.t()); + return torch::matmul(BX, BY.t()); } } \ No newline at end of file diff --git a/src/CPPAlgos/CRSV2CPPAlgo.cpp b/src/CPPAlgos/CRSV2CPPAlgo.cpp index cf577020..d8222695 100644 --- a/src/CPPAlgos/CRSV2CPPAlgo.cpp +++ b/src/CPPAlgos/CRSV2CPPAlgo.cpp @@ -5,36 +5,36 @@ #include namespace AMMBench { - torch::Tensor AMMBench::CRSV2CPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t k2) { - A = A.t(); - auto A_size = A.sizes(); - int64_t n = A_size[0]; - // int64_t m = A_size[1]; - int64_t k=(int64_t)k2; - assert(n == B.size(0)); - //TORCH_CHECK(n == B.size(0)); - //TORCH_CHECK(k < n); - - //INTELLI_INFO("Running CRS V2 CPP"); - - // probability distribution - torch::Tensor sample = torch::rand({n}); // default: uniform - - // diagonal scaling matrix D (nxn) - sample = sample.div(sample.sum()); - torch::Tensor D = torch::diag(1.0 / torch::sqrt(k * sample)); - - // sampling matrix S (kxn) - torch::Tensor column_indices = torch::multinomial(sample, k, true); - torch::Tensor S = torch::zeros({k, n}); - for (int64_t row = 0; row < k; row++) { - int64_t col = column_indices[row].item(); - S[row][col] = 1; - } - - torch::Tensor a = torch::matmul(torch::matmul(A.t(), D), S.t()); - torch::Tensor b = torch::matmul(torch::matmul(a, S), D); - - return torch::matmul(b, B); - } +torch::Tensor AMMBench::CRSV2CPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t k2) { + A = A.t(); + auto A_size = A.sizes(); + int64_t n = A_size[0]; + // int64_t m = A_size[1]; + int64_t k = (int64_t) k2; + assert(n == B.size(0)); + //TORCH_CHECK(n == B.size(0)); + //TORCH_CHECK(k < n); + + //INTELLI_INFO("Running CRS V2 CPP"); + + // probability distribution + torch::Tensor sample = torch::rand({n}); // default: uniform + + // diagonal scaling matrix D (nxn) + sample = sample.div(sample.sum()); + torch::Tensor D = torch::diag(1.0 / torch::sqrt(k * sample)); + + // sampling matrix S (kxn) + torch::Tensor column_indices = torch::multinomial(sample, k, true); + torch::Tensor S = torch::zeros({k, n}); + for (int64_t row = 0; row < k; row++) { + int64_t col = column_indices[row].item(); + S[row][col] = 1; + } + + torch::Tensor a = torch::matmul(torch::matmul(A.t(), D), S.t()); + torch::Tensor b = torch::matmul(torch::matmul(a, S), D); + + return torch::matmul(b, B); +} } // AMMBench diff --git a/src/CPPAlgos/CoOccurringFDCPPAlgo.cpp b/src/CPPAlgos/CoOccurringFDCPPAlgo.cpp index 11c52526..0b531bd1 100644 --- a/src/CPPAlgos/CoOccurringFDCPPAlgo.cpp +++ b/src/CPPAlgos/CoOccurringFDCPPAlgo.cpp @@ -4,101 +4,101 @@ #include namespace AMMBench { -torch::Scalar get_first_element(const torch::Tensor& tensor) { - if (tensor.numel() == 1) { - return tensor.item(); - } - else { - return tensor[0].item(); - } +torch::Scalar get_first_element(const torch::Tensor &tensor) { + if (tensor.numel() == 1) { + return tensor.item(); + } else { + return tensor[0].item(); + } } -bool is_empty_tensor(const torch::Tensor& tensor) { - return tensor.numel() == 0; +bool is_empty_tensor(const torch::Tensor &tensor) { + return tensor.numel() == 0; } -torch::Tensor medianReduceRank(const torch::Tensor& SV, float delta) { - return torch::clamp(SV - delta, 0); +torch::Tensor medianReduceRank(const torch::Tensor &SV, float delta) { + return torch::clamp(SV - delta, 0); } torch::Tensor CoOccurringFDCPPAlgo::amm(const torch::Tensor A, const torch::Tensor B, uint64_t l2) { - torch::Tensor B_t = B.t(); - - TORCH_CHECK(A.size(1) == B_t.size(1), "Shapes of A and B are incompatible"); - int64_t mx = A.size(0); - int64_t my = B_t.size(0); - int64_t n = A.size(1); - int64_t l= (int64_t) l2; - // Initialize sketch matrices - torch::Tensor BX = torch::zeros({ mx, l }); - torch::Tensor BY = torch::zeros({ my, l }); - - // The first l iterations - for (int i = 0; i < l; ++i) { - BX.slice(1, i, i + 1) = A.slice(1, i, i + 1); - BY.slice(1, i, i + 1) = B_t.slice(1, i, i + 1); + torch::Tensor B_t = B.t(); + + TORCH_CHECK(A.size(1) == B_t.size(1), "Shapes of A and B are incompatible"); + int64_t mx = A.size(0); + int64_t my = B_t.size(0); + int64_t n = A.size(1); + int64_t l = (int64_t) l2; + // Initialize sketch matrices + torch::Tensor BX = torch::zeros({mx, l}); + torch::Tensor BY = torch::zeros({my, l}); + + // The first l iterations + for (int i = 0; i < l; ++i) { + BX.slice(1, i, i + 1) = A.slice(1, i, i + 1); + BY.slice(1, i, i + 1) = B_t.slice(1, i, i + 1); + } + + torch::Tensor zero_columns = torch::tensor({0}); + zero_columns = zero_columns.slice(0, 1); + + // Iteration l to n: insert if available, else shrink sketch matrices + for (int i = l; i < n; ++i) { + // Acquire the index of a zero-valued column + if (!is_empty_tensor(zero_columns)) { + int idx = get_first_element(zero_columns).toInt(); + BX.slice(1, idx, idx + 1) = A.slice(1, i, i + 1); + BY.slice(1, idx, idx + 1) = B_t.slice(1, i, i + 1); + zero_columns = zero_columns.slice(0, 1); } - - torch::Tensor zero_columns = torch::tensor({ 0 }); - zero_columns = zero_columns.slice(0, 1); - - // Iteration l to n: insert if available, else shrink sketch matrices - for (int i = l; i < n; ++i) { - // Acquire the index of a zero-valued column - if (!is_empty_tensor(zero_columns)) { - int idx = get_first_element(zero_columns).toInt(); - BX.slice(1, idx, idx + 1) = A.slice(1, i, i + 1); - BY.slice(1, idx, idx + 1) = B_t.slice(1, i, i + 1); - zero_columns = zero_columns.slice(0, 1); - } - // If no zero-valued column, shrink accordingly - else { - torch::Tensor QX, RX; - std::tie(QX, RX) = torch::linalg_qr(BX); - torch::Tensor QY, RY; - std::tie(QY, RY) = torch::linalg_qr(BY); - torch::Tensor U, SV, V; - std::tie(U, SV, V) = torch::svd(torch::matmul(RX, RY.t())); - - // Find the median of singular values - torch::Tensor S_sorted, S_indices; - std::tie(S_sorted, S_indices) = SV.sort(); - - float delta; - if (S_sorted.size(0) % 2 == 1) { - delta = S_sorted[S_sorted.size(0) / 2].item().toFloat(); - } else { - delta = torch::median(S_sorted).item().toFloat(); - } - // Shrink the singular values with delta - torch::Tensor SV_shrunk = medianReduceRank(SV, delta); - - // Restore SV diagonal matrix - SV = torch::diag_embed(SV_shrunk); - torch::Tensor SV_sqrt = torch::sqrt(SV); - - // Update indices of zero-valued columns - torch::Tensor zero_indices = torch::nonzero(SV_shrunk == 0).squeeze(); - zero_columns = torch::cat({zero_columns, zero_indices}); - - // Convert tensor to a std::vector - std::vector vec(zero_columns.data_ptr(), zero_columns.data_ptr() + zero_columns.numel()); - - // Sort the vector - std::sort(vec.begin(), vec.end()); - - // Remove duplicates - vec.erase(std::unique(vec.begin(), vec.end()), vec.end()); - - // Convert std::vector back to a tensor - zero_columns = torch::from_blob(vec.data(), { static_cast(vec.size()) }, torch::kInt64).clone(); - - // Update sketch matrices - BX = torch::matmul(torch::matmul(QX, U), SV_sqrt); - BY = torch::matmul(torch::matmul(QY, V), SV_sqrt); - } + // If no zero-valued column, shrink accordingly + else { + torch::Tensor QX, RX; + std::tie(QX, RX) = torch::linalg_qr(BX); + torch::Tensor QY, RY; + std::tie(QY, RY) = torch::linalg_qr(BY); + torch::Tensor U, SV, V; + std::tie(U, SV, V) = torch::svd(torch::matmul(RX, RY.t())); + + // Find the median of singular values + torch::Tensor S_sorted, S_indices; + std::tie(S_sorted, S_indices) = SV.sort(); + + float delta; + if (S_sorted.size(0) % 2 == 1) { + delta = S_sorted[S_sorted.size(0) / 2].item().toFloat(); + } else { + delta = torch::median(S_sorted).item().toFloat(); + } + // Shrink the singular values with delta + torch::Tensor SV_shrunk = medianReduceRank(SV, delta); + + // Restore SV diagonal matrix + SV = torch::diag_embed(SV_shrunk); + torch::Tensor SV_sqrt = torch::sqrt(SV); + + // Update indices of zero-valued columns + torch::Tensor zero_indices = torch::nonzero(SV_shrunk == 0).squeeze(); + zero_columns = torch::cat({zero_columns, zero_indices}); + + // Convert tensor to a std::vector + std::vector + vec(zero_columns.data_ptr(), zero_columns.data_ptr() + zero_columns.numel()); + + // Sort the vector + std::sort(vec.begin(), vec.end()); + + // Remove duplicates + vec.erase(std::unique(vec.begin(), vec.end()), vec.end()); + + // Convert std::vector back to a tensor + zero_columns = torch::from_blob(vec.data(), {static_cast(vec.size())}, torch::kInt64).clone(); + + // Update sketch matrices + BX = torch::matmul(torch::matmul(QX, U), SV_sqrt); + BY = torch::matmul(torch::matmul(QY, V), SV_sqrt); } + } - return torch::matmul(BX, BY.t()); + return torch::matmul(BX, BY.t()); } } \ No newline at end of file diff --git a/src/CPPAlgos/CountSketchCPPAlgo.cpp b/src/CPPAlgos/CountSketchCPPAlgo.cpp index 3c428e23..1d8290aa 100644 --- a/src/CPPAlgos/CountSketchCPPAlgo.cpp +++ b/src/CPPAlgos/CountSketchCPPAlgo.cpp @@ -5,30 +5,30 @@ #include namespace AMMBench { - torch::Tensor AMMBench::CountSketchCPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t k2) { - INTELLI_INFO("I am counter sketch"); - int64_t m1 = A.size(0); - int64_t n = A.size(1); - int64_t m2 = B.size(1); - int64_t k=(int64_t) k2; - // Initialize sketch matrices - torch::Tensor Ca = torch::zeros({m1, k}); - torch::Tensor Cb = torch::zeros({k, m2}); +torch::Tensor AMMBench::CountSketchCPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t k2) { + INTELLI_INFO("I am counter sketch"); + int64_t m1 = A.size(0); + int64_t n = A.size(1); + int64_t m2 = B.size(1); + int64_t k = (int64_t) k2; + // Initialize sketch matrices + torch::Tensor Ca = torch::zeros({m1, k}); + torch::Tensor Cb = torch::zeros({k, m2}); - torch::Tensor L = torch::randint(k, {n}); - torch::Tensor G = torch::randint(2, {n}); + torch::Tensor L = torch::randint(k, {n}); + torch::Tensor G = torch::randint(2, {n}); - // Modify the random column with random sign - for (int64_t i = 0; i < n; ++i) { - if (G[i].item() == 1) { - Ca.index({torch::indexing::Slice(), L[i]}).add_(A.index({torch::indexing::Slice(), i})); - Cb.index({L[i], torch::indexing::Slice()}).add_(B.index({i, torch::indexing::Slice()})); - } else { - Ca.index({torch::indexing::Slice(), L[i]}).sub_(A.index({torch::indexing::Slice(), i})); - Cb.index({L[i], torch::indexing::Slice()}).sub_(B.index({i, torch::indexing::Slice()})); - } - } - - return torch::matmul(Ca, Cb); + // Modify the random column with random sign + for (int64_t i = 0; i < n; ++i) { + if (G[i].item() == 1) { + Ca.index({torch::indexing::Slice(), L[i]}).add_(A.index({torch::indexing::Slice(), i})); + Cb.index({L[i], torch::indexing::Slice()}).add_(B.index({i, torch::indexing::Slice()})); + } else { + Ca.index({torch::indexing::Slice(), L[i]}).sub_(A.index({torch::indexing::Slice(), i})); + Cb.index({L[i], torch::indexing::Slice()}).sub_(B.index({i, torch::indexing::Slice()})); } + } + + return torch::matmul(Ca, Cb); +} } // AMMBench diff --git a/src/CPPAlgos/EWSCPPAlgo.cpp b/src/CPPAlgos/EWSCPPAlgo.cpp index 6788c5df..c2fbbff0 100644 --- a/src/CPPAlgos/EWSCPPAlgo.cpp +++ b/src/CPPAlgos/EWSCPPAlgo.cpp @@ -6,29 +6,29 @@ namespace AMMBench { torch::Tensor AMMBench::EWSCPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t k2) { - auto A_size = A.sizes(); - int64_t m = A_size[0]; - int64_t n = A_size[1]; - int64_t k=(int64_t) k2; - TORCH_CHECK(n == B.size(0)); - TORCH_CHECK(k < n); + auto A_size = A.sizes(); + int64_t m = A_size[0]; + int64_t n = A_size[1]; + int64_t k = (int64_t) k2; + TORCH_CHECK(n == B.size(0)); + TORCH_CHECK(k < n); - int64_t p = B.size(1); + int64_t p = B.size(1); - // probability distribution - torch::Tensor probs = torch::rand({m, n}); + // probability distribution + torch::Tensor probs = torch::rand({m, n}); - // S matrix that samples A with scaling - torch::Tensor mask = torch::rand_like(probs) < probs; - torch::Tensor S = torch::zeros_like(A); - S.masked_scatter_(mask, A.masked_select(mask) / probs.masked_select(mask)); + // S matrix that samples A with scaling + torch::Tensor mask = torch::rand_like(probs) < probs; + torch::Tensor S = torch::zeros_like(A); + S.masked_scatter_(mask, A.masked_select(mask) / probs.masked_select(mask)); - // R matrix that samples B with scaling - torch::Tensor probs_r = torch::rand({n, p}); // a different probabilistic distribution - torch::Tensor mask_r = torch::rand_like(probs_r) < probs_r; - torch::Tensor R = torch::zeros_like(B); - R.masked_scatter_(mask_r, B.masked_select(mask_r) / probs_r.masked_select(mask_r)); + // R matrix that samples B with scaling + torch::Tensor probs_r = torch::rand({n, p}); // a different probabilistic distribution + torch::Tensor mask_r = torch::rand_like(probs_r) < probs_r; + torch::Tensor R = torch::zeros_like(B); + R.masked_scatter_(mask_r, B.masked_select(mask_r) / probs_r.masked_select(mask_r)); - return torch::matmul(S, R); + return torch::matmul(S, R); } } \ No newline at end of file diff --git a/src/CPPAlgos/INT8CPPAlgo.cpp b/src/CPPAlgos/INT8CPPAlgo.cpp index cef801c1..595e4330 100644 --- a/src/CPPAlgos/INT8CPPAlgo.cpp +++ b/src/CPPAlgos/INT8CPPAlgo.cpp @@ -5,12 +5,12 @@ #include torch::Tensor AMMBench::INT8CPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t sketchSize) { std::cout << sketchSize; - float scalingA,scalingB; - // int16_t mulab; + float scalingA, scalingB; + // int16_t mulab; /*auto AINT8=(A*scalingA).to(torch::kInt8); auto BTINT8=(Bt*scalingB).to(torch::kInt8);*/ - // return C; + // return C; /* for(int64_t i=0;i()/127.0; - scalingB=torch::abs(B).max().item()/127.0; - auto ta=(A/scalingA).to(torch::kInt8); - auto tb= (B/scalingB).to(torch::kInt8); - // torch::matmul(ta, tb); + scalingA = torch::abs(A).max().item() / 127.0; + scalingB = torch::abs(B).max().item() / 127.0; + auto ta = (A / scalingA).to(torch::kInt8); + auto tb = (B / scalingB).to(torch::kInt8); + // torch::matmul(ta, tb); return torch::zeros({A.size(0), B.size(1)}); - return torch::matmul(ta, tb).to(torch::kFloat)*scalingA*scalingB; - //return C*scalingA*scalingB/127.0/127.0; + return torch::matmul(ta, tb).to(torch::kFloat) * scalingA * scalingB; + //return C*scalingA*scalingB/127.0/127.0; } \ No newline at end of file diff --git a/src/CPPAlgos/TugOfWarCPPAlgo.cpp b/src/CPPAlgos/TugOfWarCPPAlgo.cpp index db7a7a53..54e0d592 100644 --- a/src/CPPAlgos/TugOfWarCPPAlgo.cpp +++ b/src/CPPAlgos/TugOfWarCPPAlgo.cpp @@ -5,44 +5,43 @@ #include namespace AMMBench { - torch::Tensor TugOfWarCPPAlgo::generateTugOfWarMatrix(int64_t m, int64_t n) { - double e = 1.0 / std::sqrt(m); - torch::Tensor M = torch::randint(2, {m, n}); - return e * (2 * M - 1); +torch::Tensor TugOfWarCPPAlgo::generateTugOfWarMatrix(int64_t m, int64_t n) { + double e = 1.0 / std::sqrt(m); + torch::Tensor M = torch::randint(2, {m, n}); + return e * (2 * M - 1); +} + +torch::Tensor TugOfWarCPPAlgo::amm(torch::Tensor A, torch::Tensor B, uint64_t l2) { + int64_t n = A.size(1); + int64_t p = B.size(1); + int64_t l = (int64_t) l2; + int64_t i_iters = static_cast(-std::log(delta)); + int64_t j_iters = static_cast(2 * (-std::log(delta) + std::log(-std::log(delta)))); + + torch::Tensor z = torch::empty({i_iters}); + std::vector AS; + std::vector SB; + + for (int64_t i = 0; i < i_iters; ++i) { + torch::Tensor S = generateTugOfWarMatrix(l, n); + SB.push_back(S.matmul(B)); + AS.push_back(A.matmul(S.t())); + + torch::Tensor y = torch::empty({j_iters}); + + for (int64_t j = 0; j < j_iters; ++j) { + torch::Tensor Q = generateTugOfWarMatrix(16, p); + torch::Tensor X = A.matmul(B.matmul(Q.t())); + torch::Tensor X_hat = AS[i].matmul(SB[i].matmul(Q.t())); + y[j] = torch::norm(X - X_hat).pow(2); } - torch::Tensor TugOfWarCPPAlgo::amm(torch::Tensor A, torch::Tensor B, int l) { - int n = A.size(1); - int p = B.size(1); + z[i] = at::median(y); + } - int i_iters = static_cast(-std::log(delta)); - int j_iters = static_cast(2 * (-std::log(delta) + std::log(-std::log(delta)))); + torch::Tensor z_argmin = z.argmin(); + int64_t i_star = z_argmin.item(); - torch::Tensor z = torch::empty({i_iters}); - std::vector AS; - std::vector SB; - - for (int i = 0; i < i_iters; ++i) { - torch::Tensor S = generateTugOfWarMatrix(l, n); - SB.push_back(S.matmul(B)); - AS.push_back(A.matmul(S.t())); - - torch::Tensor y = torch::empty({j_iters}); - - for (int j = 0; j < j_iters; ++j) { - torch::Tensor Q = generateTugOfWarMatrix(16, p); - torch::Tensor X = A.matmul(B.matmul(Q.t())); - torch::Tensor X_hat = AS[i].matmul(SB[i].matmul(Q.t())); - y[j] = torch::norm(X - X_hat).pow(2); - } - - - z[i] = at::median(y); - } - - torch::Tensor z_argmin = z.argmin(); - int i_star = z_argmin.item(); - - return AS[i_star].matmul(SB[i_star]); - } + return AS[i_star].matmul(SB[i_star]); +} } // AMMBench diff --git a/src/Parallelization/BlockPartitionRunner.cpp b/src/Parallelization/BlockPartitionRunner.cpp index 8737ad39..cdc9234a 100644 --- a/src/Parallelization/BlockPartitionRunner.cpp +++ b/src/Parallelization/BlockPartitionRunner.cpp @@ -7,7 +7,7 @@ void AMMBench::BlockPartitionWorker::setConfig(INTELLI::ConfigMapPtr _cfg) { cfg = _cfg; sketchDimension = cfg->tryU64("sketchDimension", 50, true); - osScheduling =cfg->tryU64("osScheduling",0,false); + osScheduling = cfg->tryU64("osScheduling", 0, false); std::string ptFile = cfg->tryString("ptFile", "torchscripts/FDAMM.pt", true); useCPP = cfg->tryU64("useCPP", 0, true); if (useCPP) { @@ -42,8 +42,7 @@ void AMMBench::BlockPartitionWorker::inlineMain() { /** * @brief 1. bind core and torch setting */ - if(!osScheduling) - { + if (!osScheduling) { INTELLI::UtilityFunctions::bind2Core((int) coreBind); } torch::set_num_threads(1); @@ -74,7 +73,7 @@ void AMMBench::BlockPartitionRunner::setConfig(INTELLI::ConfigMapPtr _cfg) { cfg = _cfg; threads = cfg->tryU64("threads", 2, true); workers = std::vector(threads); - firstCoreBind=cfg->tryU64("firstCoreBind",0, false); + firstCoreBind = cfg->tryU64("firstCoreBind", 0, false); for (uint64_t i = 0; i < threads; i++) { workers[i] = newBlockPartitionWorker(); workers[i]->setConfig(cfg); @@ -94,10 +93,9 @@ void AMMBench::BlockPartitionRunner::createABC(torch::Tensor A, torch::Tensor B) workers[i]->setABC(matA, matB, matC); workers[i]->setWorkParameters(start_row, end_row, i); } - if(firstCoreBind!=0) - { - workers[0]->setCoreBInd((int)firstCoreBind); - INTELLI_INFO("first thread is bound to core" + to_string(firstCoreBind) ); + if (firstCoreBind != 0) { + workers[0]->setCoreBInd((int) firstCoreBind); + INTELLI_INFO("first thread is bound to core" + to_string(firstCoreBind)); } } torch::Tensor AMMBench::BlockPartitionRunner::parallelForward() { @@ -120,19 +118,18 @@ torch::Tensor AMMBench::BlockPartitionRunner::runAMM(torch::Tensor A, torch::Ten } uint64_t AMMBench::BlockPartitionRunner::getElapsedTime() { uint64_t ti = 0; - uint64_t tMax=0; + uint64_t tMax = 0; for (uint64_t i = 0; i < threads; i++) { ti = workers[i]->getElapsedTime(); - if(ti>tMax) - { - tMax=ti; + if (ti > tMax) { + tMax = ti; } } return tMax; } void AMMBench::BlockPartitionRunner::appendThreadInfo(INTELLI::ConfigMapPtr ru) { for (uint64_t i = 0; i < threads; i++) { - std::string keyElapesedTime="thread"+ to_string(i)+"RunTime"; - ru->edit(keyElapesedTime,(uint64_t) workers[i]->getElapsedTime()); + std::string keyElapesedTime = "thread" + to_string(i) + "RunTime"; + ru->edit(keyElapesedTime, (uint64_t) workers[i]->getElapsedTime()); } } \ No newline at end of file diff --git a/test/SystemTest/CRSTest.cpp b/test/SystemTest/CRSTest.cpp index 1208968f..f7150d54 100644 --- a/test/SystemTest/CRSTest.cpp +++ b/test/SystemTest/CRSTest.cpp @@ -74,23 +74,23 @@ TEST_CASE("Test CRS in cpp", "[short]") } TEST_CASE("Test CRS v2 in cpp", "[short]") { - torch::manual_seed(114514); - AMMBench::CRSV2CPPAlgo crs; - auto A = torch::rand({400, 400}); - auto B = torch::rand({400, 400}); - auto realC = torch::matmul(A, B); - auto ammC = crs.amm(A, B, 20); - double froError = INTELLI::UtilityFunctions::relativeFrobeniusNorm(realC, ammC); - REQUIRE(froError < 0.5); + torch::manual_seed(114514); + AMMBench::CRSV2CPPAlgo crs; + auto A = torch::rand({400, 400}); + auto B = torch::rand({400, 400}); + auto realC = torch::matmul(A, B); + auto ammC = crs.amm(A, B, 20); + double froError = INTELLI::UtilityFunctions::relativeFrobeniusNorm(realC, ammC); + REQUIRE(froError < 0.5); } TEST_CASE("Test Bernoulli CRS in cpp", "[short]") { - torch::manual_seed(114514); - AMMBench::BCRSCPPAlgo bcrs; - auto A = torch::rand({400, 400}); - auto B = torch::rand({400, 400}); - auto realC = torch::matmul(A, B); - auto ammC = bcrs.amm(A, B, 20); - double froError = INTELLI::UtilityFunctions::relativeFrobeniusNorm(realC, ammC); - REQUIRE(froError < 0.5); + torch::manual_seed(114514); + AMMBench::BCRSCPPAlgo bcrs; + auto A = torch::rand({400, 400}); + auto B = torch::rand({400, 400}); + auto realC = torch::matmul(A, B); + auto ammC = bcrs.amm(A, B, 20); + double froError = INTELLI::UtilityFunctions::relativeFrobeniusNorm(realC, ammC); + REQUIRE(froError < 0.5); } \ No newline at end of file diff --git a/test/SystemTest/EWSTest.cpp b/test/SystemTest/EWSTest.cpp index 34c76cf7..135e132d 100644 --- a/test/SystemTest/EWSTest.cpp +++ b/test/SystemTest/EWSTest.cpp @@ -56,12 +56,12 @@ TEST_CASE("Test the COLUMN ROW SAMPLINGS", "[short]") } TEST_CASE("Test EWS in cpp", "[short]") { - torch::manual_seed(114514); - AMMBench::EWSCPPAlgo ews; - auto A = torch::rand({400, 400}); - auto B = torch::rand({400, 400}); - auto realC = torch::matmul(A, B); - auto ammC = ews.amm(A, B, 20); - double froError = INTELLI::UtilityFunctions::relativeFrobeniusNorm(realC, ammC); - REQUIRE(froError < 0.5); + torch::manual_seed(114514); + AMMBench::EWSCPPAlgo ews; + auto A = torch::rand({400, 400}); + auto B = torch::rand({400, 400}); + auto realC = torch::matmul(A, B); + auto ammC = ews.amm(A, B, 20); + double froError = INTELLI::UtilityFunctions::relativeFrobeniusNorm(realC, ammC); + REQUIRE(froError < 0.5); } \ No newline at end of file diff --git a/test/SystemTest/INT8Test.cpp b/test/SystemTest/INT8Test.cpp index 7778862f..c24f02d4 100644 --- a/test/SystemTest/INT8Test.cpp +++ b/test/SystemTest/INT8Test.cpp @@ -15,10 +15,10 @@ TEST_CASE("Test int8", "[short]") auto B = torch::rand({4, 4}); auto realC = torch::matmul(A, B); auto ammC = int8mm.amm(A, B, 20); - std::cout<<"int8:"< torch.Tensor: - e = 1/math.sqrt(m) + e = 1 / math.sqrt(m) M = torch.randint(2, (m, n)) - return e*(2*M - 1) + return e * (2 * M - 1) @torch.jit.script @@ -17,7 +18,7 @@ def TugOfWar(A: torch.Tensor, B: torch.Tensor, l: int): delta = 0.2 i_iters = int(-math.log(delta)) - j_iters = int(2*(-math.log(delta) + math.log(-math.log(delta)))) + j_iters = int(2 * (-math.log(delta) + math.log(-math.log(delta)))) z = torch.empty((i_iters,)) AS = [] @@ -34,9 +35,9 @@ def TugOfWar(A: torch.Tensor, B: torch.Tensor, l: int): Q = tug_of_war_mat(16, p) X = A.matmul(B.matmul(Q.T)) X_hat = AS[i].matmul(SB[i].matmul(Q.T)) - y[j] = torch.norm(X - X_hat)**2 + y[j] = torch.norm(X - X_hat) ** 2 z[i] = torch.median(y) - + i_star = torch.argmin(z) return torch.matmul(AS[i_star], SB[i_star]) From f9abc513c5c5eee647c3a9808b70fe01c6d8e20f Mon Sep 17 00:00:00 2001 From: tony <292224750@qq.com> Date: Mon, 5 Jun 2023 22:08:58 +0800 Subject: [PATCH 2/2] 1. fix the uint64_t bugs 2. Note; please note the int8 is just a toy --- commit.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commit.sh b/commit.sh index 60a35a78..98040b2a 100755 --- a/commit.sh +++ b/commit.sh @@ -1,4 +1,4 @@ -BRANCH=uint64_t-bug +BRANCH=sbug git init git checkout -b $BRANCH git add .