Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug-Fix/wrong sum bug fix #22

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Hello-Foss-PyThread.cpp
Submodule Hello-Foss-PyThread.cpp added at 12f174
46 changes: 24 additions & 22 deletions src/wrong_sum.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "parallel_sum.h"
#include "paralle_sum.h"
#include <omp.h>
#include <iostream>

Expand All @@ -7,31 +7,33 @@ int parallel_sum(const int arr[], int size, int step_size) {

#pragma omp parallel
{

for (int i = 0; i < size; i += step_size) {
int start = i, end = i + step_size - 1;

// Ensure end index is within bounds
if (end >= size) end = size - 1;

std::cout << "Thread computing Sum(" << start << "," << end << ") from "
<< omp_get_thread_num() << " of " << omp_get_num_threads() << std::endl;

#pragma omp task
{
int psum = 0;
std::cout << "Task computing Sum(" << start << "," << end << ") from "
<< omp_get_thread_num() << " of " << omp_get_num_threads() << std::endl;

for (int j = start; j <= end; j++) {
psum += arr[j];
}
#pragma omp single // Ensure only one thread creates tasks
{
for (int i = 0; i < size; i += step_size) {
int start = i, end = i + step_size - 1;

// Ensure end index is within bounds
if (end >= size) end = size - 1;

#pragma omp critical
#pragma omp task // Create a task for summation
{
sum += psum;
int psum = 0;
std::cout << "Task computing Sum(" << start << "," << end << ") from "
<< omp_get_thread_num() << " of " << omp_get_num_threads() << std::endl;

for (int j = start; j <= end; j++) {
psum += arr[j];
}

#pragma omp critical // Critical section to update global sum
{
sum += psum;
}
}
}

// Ensure all tasks are completed before returning the final sum
#pragma omp taskwait
}
}

Expand Down