hpc-lab-code/lab2/pthread/count_words_par_opt.c
2026-01-19 22:25:53 +08:00

144 lines
3.3 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdbool.h>
#include <string.h>
#include <sys/time.h>
#define NUM_THREADS 4
FILE *fd;
int TotalEvenWords = 0, TotalOddWords = 0, TotalWords = 0;
pthread_mutex_t mutex;
struct Result {
int words;
int even;
int odd;
};
int GetNextLine(FILE *f, char *Line)
{
if (fgets(Line, 132, f)==NULL) if (feof(f))return EOF; else return 1;
}
struct Result GetWordAndLetterCount(char *Line)
{
int Word_Count = 0, Letter_Count = 0, Even_Count = 0, Odd_Count = 0;
for (int i=0;i<132;i++)
{
if ((Line[i]!=' ')&&(Line[i]!=0)&&(Line[i]!='\n')) Letter_Count++;
else {
if (Letter_Count % 2) {
Odd_Count++;
Word_Count++;
Letter_Count = 0;
}
else {
Even_Count++;
Word_Count++;
Letter_Count = 0;
}
if (Line[i]==0) break;
}
}
struct Result r = {Word_Count, Even_Count, Odd_Count};
return r;
}
struct ThreadData {
char **lines;
int start_line;
int end_line;
};
void *count_words_thread(void *arg)
{
struct ThreadData *data = (struct ThreadData *)arg;
int local_words = 0, local_even = 0, local_odd = 0;
for (int i = data->start_line; i < data->end_line; i++) {
struct Result r = GetWordAndLetterCount(data->lines[i]);
local_words += r.words;
local_even += r.even;
local_odd += r.odd;
}
pthread_mutex_lock(&mutex);
TotalWords += local_words;
TotalEvenWords += local_even;
TotalOddWords += local_odd;
pthread_mutex_unlock(&mutex);
return NULL;
}
int main()
{
fd = fopen("./InFile1.txt", "r"); // Open file for read
if (fd == NULL) {
perror("Failed to open file");
return 1;
}
// Read all lines
char **lines = NULL;
int total_lines = 0;
char buffer[132];
while (fgets(buffer, sizeof(buffer), fd) != NULL) {
lines = realloc(lines, (total_lines + 1) * sizeof(char *));
lines[total_lines] = strdup(buffer);
total_lines++;
}
fclose(fd);
if (total_lines == 0) {
printf("No lines in file\n");
return 0;
}
struct timeval TimeStampStart, TimeStampStop;
double ExeTime;
gettimeofday(&TimeStampStart, NULL);
// Initialize mutex
pthread_mutex_init(&mutex, NULL);
// Create threads
pthread_t threads[NUM_THREADS];
struct ThreadData thread_data[NUM_THREADS];
int lines_per_thread = total_lines / NUM_THREADS;
int remainder = total_lines % NUM_THREADS;
int start = 0;
for (int i = 0; i < NUM_THREADS; i++) {
int end = start + lines_per_thread + (i < remainder ? 1 : 0);
thread_data[i].lines = lines;
thread_data[i].start_line = start;
thread_data[i].end_line = end;
pthread_create(&threads[i], NULL, count_words_thread, &thread_data[i]);
start = end;
}
// Wait for threads
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
// Destroy mutex
pthread_mutex_destroy(&mutex);
gettimeofday(&TimeStampStop, NULL);
ExeTime = (double)(TimeStampStop.tv_sec - TimeStampStart.tv_sec) +
(double)(TimeStampStop.tv_usec - TimeStampStart.tv_usec) * 1e-6;
// Free memory
for (int i = 0; i < total_lines; i++) {
free(lines[i]);
}
free(lines);
printf("Total Words = %8d\n", TotalWords);
printf("Total Even Words = %7d\nTotal Odd Words = %7d\n", TotalEvenWords, TotalOddWords);
printf("The time to count word was %f seconds\n", (ExeTime));
return 0;
}