Skip to content

added parallel code#10

Merged
MeryAmr merged 3 commits into
mainfrom
parallel-version
Oct 31, 2025
Merged

added parallel code#10
MeryAmr merged 3 commits into
mainfrom
parallel-version

Conversation

@unminnn
Copy link
Copy Markdown
Collaborator

@unminnn unminnn commented Oct 31, 2025

Closes #3

adds parallel code.

compile with: gcc -fopenmp parallel.c -O2 -o parallel

check: ./parallel -h for usage

@unminnn unminnn self-assigned this Oct 31, 2025
@unminnn unminnn added the enhancement New feature or request label Oct 31, 2025
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a parallel word counting implementation using OpenMP. The program reads a text file and counts words using multiple threads, then benchmarks the performance across multiple runs.

Key Changes

  • Implements parallel word counting using OpenMP thread-based parallelization
  • Adds command-line argument handling for file path and number of benchmark runs
  • Includes timing statistics (min, max, avg) across multiple runs

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/parallel.c
double max = 0.0;
double sum = 0.0;
double avg = 0.0;
double *times = malloc(sizeof(double) * RUNS);
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing null check after malloc. If memory allocation fails, the program will crash when accessing times[count] at line 126.

Suggested change
double *times = malloc(sizeof(double) * RUNS);
double *times = malloc(sizeof(double) * RUNS);
if (!times)
{
perror("malloc");
return 1;
}

Copilot uses AI. Check for mistakes.
Comment thread src/parallel.c
Comment on lines +110 to +117
if (i == 0)
{
local_count++;
}
else if (isspace((unsigned char)buf[i - 1]))
{
local_count++;
}
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Word counting logic is incorrect for multi-threaded execution. Each thread checks if i == 0 to detect word start, but only thread 0 will have i == 0 as the actual start of the buffer. Other threads starting at start > 0 will incorrectly count a word at their chunk boundary if the character is non-space, even if it's in the middle of a word. This will lead to incorrect word counts.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot open a new pull request to apply changes based on this feedback

Comment thread src/parallel.c
{
local_count++;
}
else if (isspace((unsigned char)buf[i - 1]))
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out-of-bounds memory access when i == start and start > 0. Thread checks buf[i - 1] without verifying that i > start, which could access memory outside the thread's intended chunk range, potentially causing race conditions or incorrect results.

Suggested change
else if (isspace((unsigned char)buf[i - 1]))
else if (i > start && isspace((unsigned char)buf[i - 1]))

Copilot uses AI. Check for mistakes.
Comment thread src/parallel.c
printf("avg time: %.6f seconds\n", avg);
printf("num of runs: %d\n", RUNS);
free(buf);
free(times);
Copy link

Copilot AI Oct 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Memory leak: times array is freed even when malloc at line 49 might have failed (no null check). Additionally, if malloc fails and returns NULL, calling free(NULL) is safe but the program should have exited earlier.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

Copilot AI commented Oct 31, 2025

@unminnn I've opened a new pull request, #11, to work on those changes. Once the pull request is ready, I'll request review from you.

@MeryAmr MeryAmr merged commit ab61515 into main Oct 31, 2025
unminnn and others added 2 commits October 31, 2025 17:16
[WIP] Add fixes based on feedback for parallel code implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

openMP parallelization

4 participants