Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <cmath>
- #include <vector>
- #include <chrono>
- #include <numeric> // For sum calculations
- #include <mutex>
- // Matrix dimensions and number of threads
- const int N = 1000; // Matrix size (NxN)
- const int NTHRD = 4; // Number of threads
- float matrix[N][N]; // Global matrix
- float sequentialSum = 0; // Sum from sequential calculation
- float multithreadedSum = 0; // Sum from multithreaded calculation
- std::mutex columnMutex; // Mutex for column synchronization
- // Thread data structure
- struct ThreadData {
- int startRow;
- int endRow;
- int column;
- float partialSum; // To store the partial sum calculated by each thread
- };
- // Function to calculate arithmetic mean of the previous column
- float calculateASPS(int column) {
- if (column == 0) return 0.0f;
- float sum = 0.0f;
- for (int i = 0; i < N; ++i) {
- sum += matrix[i][column - 1];
- }
- return sum / N;
- }
- // Function to fill part of the matrix and calculate partial sum
- DWORD WINAPI fillMatrixPart(LPVOID param) {
- ThreadData* data = (ThreadData*)param;
- data->partialSum = 0;
- for (int i = data->startRow; i < data->endRow; ++i) {
- float asps = calculateASPS(data->column); // Calculate arithmetic mean of the previous column
- float sum = 0;
- for (int k = 0; k <= i; ++k) {
- sum += k * sin(data->column) - data->column * cos(k);
- }
- matrix[i][data->column] = asps + sum;
- data->partialSum += matrix[i][data->column];
- }
- return 0;
- }
- // Sequential implementation
- void fillMatrixSequential() {
- sequentialSum = 0;
- for (int j = 0; j < N; ++j) {
- float asps = calculateASPS(j);
- for (int i = 0; i < N; ++i) {
- float sum = 0;
- for (int k = 0; k <= i; ++k) {
- sum += k * sin(j) - j * cos(k);
- }
- matrix[i][j] = asps + sum;
- sequentialSum += matrix[i][j];
- }
- }
- }
- int main() {
- // Measure time for sequential execution
- auto start = std::chrono::high_resolution_clock::now();
- fillMatrixSequential();
- auto end = std::chrono::high_resolution_clock::now();
- auto durationSequential = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
- std::cout << "Sequential execution time: " << durationSequential << " ms" << std::endl;
- std::cout << "Sequential sum: " << sequentialSum << std::endl;
- // Measure time for multithreaded execution
- HANDLE threads[NTHRD];
- ThreadData threadData[NTHRD];
- multithreadedSum = 0;
- start = std::chrono::high_resolution_clock::now();
- for (int j = 0; j < N; ++j) {
- float asps = calculateASPS(j); // Calculate arithmetic mean for the column
- int rowsPerThread = N / NTHRD;
- for (int i = 0; i < NTHRD; ++i) {
- threadData[i].startRow = i * rowsPerThread;
- threadData[i].endRow = (i == NTHRD - 1) ? N : (i + 1) * rowsPerThread;
- threadData[i].column = j;
- threadData[i].partialSum = 0;
- threads[i] = CreateThread(NULL, 0, fillMatrixPart, &threadData[i], 0, NULL);
- }
- // Wait for all threads to finish
- WaitForMultipleObjects(NTHRD, threads, TRUE, INFINITE);
- // Aggregate partial sums
- for (int i = 0; i < NTHRD; ++i) {
- multithreadedSum += threadData[i].partialSum;
- CloseHandle(threads[i]);
- }
- }
- end = std::chrono::high_resolution_clock::now();
- auto durationMultithreaded = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
- std::cout << "Multithreaded execution time: " << durationMultithreaded << " ms" << std::endl;
- std::cout << "Multithreaded sum: " << multithreadedSum << std::endl;
- // Calculate speedup
- std::cout << "Speedup: " << (float)durationSequential / durationMultithreaded << "x" << std::endl;
- // Calculate sum accuracy and percentage accuracy
- float sumDifference = fabs(sequentialSum - multithreadedSum);
- float accuracyPercentage = ((1.0f - (sumDifference / fabs(sequentialSum))) * 100.0f);
- std::cout << "Sum Difference: " << sumDifference << std::endl;
- std::cout << "Accuracy [%]: " << accuracyPercentage << "%" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement