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
- // 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
- // Thread data structure
- struct ThreadData {
- int startRow;
- int endRow;
- float partialSum; // To store the partial sum calculated by each thread
- };
- // 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) {
- for (int j = 0; j < N; ++j) {
- float sum = 0;
- for (int k = 0; k <= i; ++k) {
- sum += k * sin(j) - j * cos(k);
- }
- matrix[i][j] = sum;
- data->partialSum += sum;
- }
- }
- return 0;
- }
- // Sequential implementation
- void fillMatrixSequential() {
- sequentialSum = 0;
- for (int i = 0; i < N; ++i) {
- for (int j = 0; j < N; ++j) {
- float sum = 0;
- for (int k = 0; k <= i; ++k) {
- sum += k * sin(j) - j * cos(k);
- }
- matrix[i][j] = sum;
- sequentialSum += sum;
- }
- }
- }
- 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];
- int rowsPerThread = N / NTHRD;
- start = std::chrono::high_resolution_clock::now();
- for (int i = 0; i < NTHRD; ++i) {
- threadData[i].startRow = i * rowsPerThread;
- threadData[i].endRow = (i == NTHRD - 1) ? N : (i + 1) * rowsPerThread;
- 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);
- // Calculate the total sum from all threads
- multithreadedSum = 0;
- for (int i = 0; i < NTHRD; ++i) {
- multithreadedSum += threadData[i].partialSum;
- }
- 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;
- // Verify correctness using percentage difference and sum difference
- float sumDifference = fabs(sequentialSum - multithreadedSum);
- float accuracyPercentage = ((sequentialSum - sumDifference) / sequentialSum) * 100.0f;
- std::cout << "Accuracy (Sequential to Multithreaded): " << accuracyPercentage << "%" << std::endl;
- std::cout << "Sum Difference: " << sumDifference << std::endl;
- // Clean up thread handles
- for (int i = 0; i < NTHRD; ++i) {
- CloseHandle(threads[i]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement