-
Notifications
You must be signed in to change notification settings - Fork 0
added parallel code #10
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,173 @@ | ||||||
| #include <stdio.h> | ||||||
| #include <stdlib.h> | ||||||
| #include <ctype.h> | ||||||
| #include <string.h> | ||||||
| #include <omp.h> | ||||||
|
|
||||||
| double getMax(const double *arr, int n); | ||||||
| double getMin(const double *arr, int n); | ||||||
| double getSum(const double *arr, int n); | ||||||
|
|
||||||
| void printUsage(const char *progName) | ||||||
| { | ||||||
| printf("Usage: %s <filename> <runs>\n", progName); | ||||||
| printf(" <filename> : Path to the input file (default: ../data/large.txt)\n"); | ||||||
| printf(" <runs> : Number of runs to perform (default: 50)\n"); | ||||||
| } | ||||||
|
|
||||||
| int main(int argc, char *argv[]) | ||||||
| { | ||||||
|
|
||||||
| char *filename = "../data/large.txt"; // Adjust path if needed | ||||||
| int RUNS = 50; | ||||||
| if (argc > 1) | ||||||
| { | ||||||
| if (argc == 2 && strcmp(argv[1], "-h") == 0) | ||||||
| { | ||||||
| printUsage(argv[0]); | ||||||
| return 0; | ||||||
| } | ||||||
| if (argc >= 2) | ||||||
| { | ||||||
| filename = argv[1]; | ||||||
| } | ||||||
| if (argc >= 3) | ||||||
| { | ||||||
| RUNS = atoi(argv[2]); | ||||||
| if (RUNS <= 0) | ||||||
| { | ||||||
| fprintf(stderr, "Invalid number of RUNS specified. Using default 50.\n"); | ||||||
| RUNS = 50; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| double min = 0.0; | ||||||
| double max = 0.0; | ||||||
| double sum = 0.0; | ||||||
| double avg = 0.0; | ||||||
| double *times = malloc(sizeof(double) * RUNS); | ||||||
|
|
||||||
| FILE *file = fopen(filename, "rb"); | ||||||
| if (!file) | ||||||
| { | ||||||
| perror("Error opening file"); | ||||||
| return 1; | ||||||
| } | ||||||
|
|
||||||
| if (fseek(file, 0, SEEK_END) != 0) | ||||||
| { | ||||||
| perror("fseek"); | ||||||
| fclose(file); | ||||||
| return 1; | ||||||
| } | ||||||
| long file_size = ftell(file); | ||||||
| if (file_size < 0) | ||||||
| { | ||||||
| perror("ftell"); | ||||||
| fclose(file); | ||||||
| return 1; | ||||||
| } | ||||||
| rewind(file); | ||||||
|
|
||||||
| char *buf = malloc((size_t)file_size + 1); | ||||||
| if (!buf) | ||||||
| { | ||||||
| perror("malloc"); | ||||||
| fclose(file); | ||||||
| return 1; | ||||||
| } | ||||||
|
|
||||||
| size_t read = fread(buf, 1, (size_t)file_size, file); | ||||||
| buf[read] = '\0'; | ||||||
| fclose(file); | ||||||
|
|
||||||
| long long total_words = 0; | ||||||
| int nthreads = 1; | ||||||
| for (int count = 0; count < RUNS; count++) | ||||||
| { | ||||||
| total_words = 0; | ||||||
| double t0 = omp_get_wtime(); | ||||||
| #pragma omp parallel | ||||||
| { | ||||||
| int tid = omp_get_thread_num(); | ||||||
| int nth = omp_get_num_threads(); | ||||||
| #pragma omp single | ||||||
| nthreads = nth; | ||||||
|
|
||||||
| size_t chunk = (read + nth - 1) / nth; // ceil division | ||||||
| size_t start = tid * chunk; | ||||||
| size_t end = start + chunk; | ||||||
| if (end > read) | ||||||
| end = read; | ||||||
|
|
||||||
| long long local_count = 0; | ||||||
| for (size_t i = start; i < end; ++i) | ||||||
| { | ||||||
| unsigned char c = (unsigned char)buf[i]; | ||||||
| if (!isspace(c)) | ||||||
| { | ||||||
| if (i == 0) | ||||||
| { | ||||||
| local_count++; | ||||||
| } | ||||||
| else if (isspace((unsigned char)buf[i - 1])) | ||||||
|
||||||
| else if (isspace((unsigned char)buf[i - 1])) | |
| else if (i > start && isspace((unsigned char)buf[i - 1])) |
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Copilot
AI
Oct 31, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.