diff --git a/1706-4/brazhnikov_ea/TBB/Fox.cpp b/1706-4/brazhnikov_ea/TBB/Fox.cpp new file mode 100644 index 0000000..fa1f7c9 --- /dev/null +++ b/1706-4/brazhnikov_ea/TBB/Fox.cpp @@ -0,0 +1,106 @@ +#include "Fox.h" +#include +#include + +using namespace tbb; +using namespace std; + +void PrintMatrix(double* matrix, int N) { + + int M = N; + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) + { + cout << matrix[i * M + j] << " "; + for (int k = 0; k < (5 - log10((double)(matrix[i * M + j]))); k++) + cout << " "; + } + cout << endl; + } + cout << endl; +} + +void MatrixCreate(double* A, double* B, double* C, double* CSeq, int size) { + for (int i = 0; i < size * size; ++i) { + A[i] = rand() % 10 + 1; + B[i] = rand() % 10 + 1; + C[i] = 0; + CSeq[i] = 0; + } +} + +void Multiplication(double* Ablock, double* Bblock, double* Cblock, int _blockSize) +{ + double temp; + for (int i = 0; i < _blockSize; i++) + for (int j = 0; j < _blockSize; j++) { + temp = 0; + for (int k = 0; k < _blockSize; k++) + temp += Ablock[i * _blockSize + k] * Bblock[k * _blockSize + j]; + Cblock[i * _blockSize + j] += static_cast(temp * 100) / 100; + } +} + + +void SequentialAlgorithm(double* A, double* B, double* C, int size) { + for (int i = 0; i < size * size; ++i) + C[i] = 0; + Multiplication(A, B, C, size); +} + +class ClassFoxAlg { +private: + static int threadCounter; + static mutex myMutex; + const double* A; + const double* B; + double* C; + int size; + int procNum; +public: + static void SetToZerothreadCounter() { + threadCounter = 0; + } + + ClassFoxAlg(const double* A_, const double* B_, double* C_, int size_, int procNum_) : A(A_), B(B_), C(C_), size(size_), procNum(procNum_) {} + + void operator()(const tbb::blocked_range& iter) const { + int Grid = int(sqrt(procNum)); + int BlockSize = size / Grid; + { + + tbb::mutex::scoped_lock lock; + lock.acquire(myMutex); + int RowIndex = threadCounter / Grid; + int ColIndex = threadCounter % Grid; + threadCounter++; + lock.release(); + + for (int iter = 0; iter < Grid; iter++) + { + for (int i = RowIndex * BlockSize; i < (RowIndex + 1) * BlockSize; i++) + { + for (int j = ColIndex * BlockSize; j < (ColIndex + 1) * BlockSize; j++) + { + for (int k = iter * BlockSize; k < (iter + 1) * BlockSize; k++) + { + C[i * size + j] += A[i * size + k] * B[k * size + j]; + } + } + } + } + } + } +}; + + +tbb::mutex ClassFoxAlg::myMutex; +int ClassFoxAlg::threadCounter = 0; +void Fox(const double* A, const double* B, double* C, int size, int procNum) { + + ClassFoxAlg::SetToZerothreadCounter(); + + ClassFoxAlg processing(A, B, C, size, procNum); + + parallel_for(blocked_range(0, procNum, 1), processing); +} diff --git a/1706-4/brazhnikov_ea/TBB/Fox.h b/1706-4/brazhnikov_ea/TBB/Fox.h new file mode 100644 index 0000000..598f22e --- /dev/null +++ b/1706-4/brazhnikov_ea/TBB/Fox.h @@ -0,0 +1,7 @@ +#pragma once + +void PrintMatrix(double* matrix, int N); +void MatrixCreate(double* A, double* B, double* C, double* CSeq, int size); +void Multiplication(double* Ablock, double* Bblock, double* Cblock, int _blockSize); +void SequentialAlgorithm(double* A, double* B, double* C, int size); +void Fox(const double* A, const double* B, double* C, int size, int procNum); \ No newline at end of file diff --git a/1706-4/brazhnikov_ea/TBB/Source.cpp b/1706-4/brazhnikov_ea/TBB/Source.cpp new file mode 100644 index 0000000..3a276fc --- /dev/null +++ b/1706-4/brazhnikov_ea/TBB/Source.cpp @@ -0,0 +1,81 @@ +#include +#include +#include "Fox.h" + +using namespace tbb; +using namespace std; + +int main(int argc, char** argv) +{ + double* A; + double* B; + double* CSeq; + double* CFox; + + double StartFoxAlg = 0, FinFoxAlg = 0, StartSeqAlg = 0, FinSeqAlg = 0; //time + + int size = 16; //matrix size + int procNum = 4; //num of proc + int BlockSize = int(sqrt(procNum)); //size of block + + // + task_scheduler_init init(procNum); + // + if (BlockSize * BlockSize == procNum && size % BlockSize == 0) { + + + //initialization + A = new double[size * size]; + B = new double[size * size]; + CSeq = new double[size * size]; + CFox = new double[size * size]; + MatrixCreate(A, B, CFox, CSeq, size); + // + + //print on screen + if (size < 30) { + cout << "Matrix A" << endl; + PrintMatrix(A, size); + cout << "Matrix B" << endl; + PrintMatrix(B, size); + } + // + + //FoxAlgPar + tick_count StartFoxAlg = tick_count::now(); + if (procNum != 1) { + + Fox(A, B, CFox, size, procNum); + + } + tick_count FinFoxAlg = tick_count::now(); + // + + //FoxAlgSeq + ///task_scheduler_init init(1); + tick_count StartSeqAlg = tick_count::now(); + SequentialAlgorithm(A, B, CSeq, size); + tick_count FinSeqAlg = tick_count::now(); + // + + + //print on screen + if (size < 30) { + cout << "Parallel Alg " << endl; + PrintMatrix(CFox, size); + } + if (size < 30) { + cout << "Seq Alg " << endl; + PrintMatrix(CSeq, size); + } + cout << "===========================================" << endl; + cout << "Time of parallel alg is " << (FinFoxAlg - StartFoxAlg).seconds() << endl; + cout << "Time of seq alg is " << (FinSeqAlg - StartSeqAlg).seconds() << endl; + cout << "Acceleration = " << (FinSeqAlg - StartSeqAlg).seconds() / (FinFoxAlg - StartFoxAlg).seconds() << endl; + // + } + else + { + cout << "Error" << endl; + } +} \ No newline at end of file diff --git a/1706-4/brazhnikov_ea/TBB/TBB.sln b/1706-4/brazhnikov_ea/TBB/TBB.sln new file mode 100644 index 0000000..6b85a4b --- /dev/null +++ b/1706-4/brazhnikov_ea/TBB/TBB.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29920.165 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TBB", "TBB.vcxproj", "{6A60F036-CF08-4356-9E8B-06690C390060}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6A60F036-CF08-4356-9E8B-06690C390060}.Debug|x64.ActiveCfg = Debug|x64 + {6A60F036-CF08-4356-9E8B-06690C390060}.Debug|x64.Build.0 = Debug|x64 + {6A60F036-CF08-4356-9E8B-06690C390060}.Debug|x86.ActiveCfg = Debug|Win32 + {6A60F036-CF08-4356-9E8B-06690C390060}.Debug|x86.Build.0 = Debug|Win32 + {6A60F036-CF08-4356-9E8B-06690C390060}.Release|x64.ActiveCfg = Release|x64 + {6A60F036-CF08-4356-9E8B-06690C390060}.Release|x64.Build.0 = Release|x64 + {6A60F036-CF08-4356-9E8B-06690C390060}.Release|x86.ActiveCfg = Release|Win32 + {6A60F036-CF08-4356-9E8B-06690C390060}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {41A46690-ABDA-41FE-A7CD-1B11BB81A1B7} + EndGlobalSection +EndGlobal diff --git a/1706-4/brazhnikov_ea/TBB/TBB.vcxproj b/1706-4/brazhnikov_ea/TBB/TBB.vcxproj new file mode 100644 index 0000000..09e3fa7 --- /dev/null +++ b/1706-4/brazhnikov_ea/TBB/TBB.vcxproj @@ -0,0 +1,158 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + {6A60F036-CF08-4356-9E8B-06690C390060} + TBB + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + D:\App\CProjects\TBB\tbb-2020.2-win\tbb\include;D:\App\CProjects\TBB\tbb-2020.2-win\tbb\include;%(AdditionalIncludeDirectories) + + + Console + true + D:\App\CProjects\TBB\tbb-2020.2-win\tbb\lib\intel64\vc14_uwp;D:\App\CProjects\TBB\tbb-2020.2-win\tbb\lib\intel64\vc14;%(AdditionalLibraryDirectories) + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + D:\App\CProjects\TBB\tbb-2020.2-win\tbb\include; + + + Console + true + D:\App\CProjects\TBB\tbb-2020.2-win\tbb\lib\intel64\vc14 + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + D:\App\CProjects\TBB\tbb-2020.2-win\tbb\include; + + + Console + true + true + true + D:\App\CProjects\TBB\tbb-2020.2-win\tbb\lib\intel64\vc14_uwp; + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + D:\App\CProjects\TBB\tbb-2020.2-win\tbb\include; + + + Console + true + true + true + D:\App\CProjects\TBB\tbb-2020.2-win\tbb\lib\intel64\vc14 + + + + + + + + + + + + + \ No newline at end of file diff --git a/1706-4/brazhnikov_ea/TBB/TBB.vcxproj.filters b/1706-4/brazhnikov_ea/TBB/TBB.vcxproj.filters new file mode 100644 index 0000000..6699dbe --- /dev/null +++ b/1706-4/brazhnikov_ea/TBB/TBB.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Файлы ресурсов + + + Исходные файлы + + + + + Файлы заголовков + + + \ No newline at end of file diff --git a/1706-4/brazhnikov_ea/TBB/TBB.vcxproj.user b/1706-4/brazhnikov_ea/TBB/TBB.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/1706-4/brazhnikov_ea/TBB/TBB.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file