hpc-lab-code/submit/lab2/pthread/count_words_par.c
2026-01-21 18:02:30 +08:00

142 lines
3.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <stdbool.h>
#include <string.h>
#include <sys/time.h>
int 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;
for (int i = data->start_line; i < data->end_line; i++) {
struct Result r = GetWordAndLetterCount(data->lines[i]);
pthread_mutex_lock(&mutex);
TotalWords += r.words;
TotalEvenWords += r.even;
TotalOddWords += r.odd;
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main(int argc, char** argv)
{
fd = fopen("./InFile1.txt", "r"); // 打开文件读取
if (fd == NULL) {
perror("Failed to open file");
return 1;
}
if (argc > 1){
NUM_THREADS = atoi(argv[1]);
}
// 读取所有行
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);
// 初始化互斥锁
pthread_mutex_init(&mutex, NULL);
// 创建线程
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;
}
// 等待线程结束
for (int i = 0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
// 销毁互斥锁
pthread_mutex_destroy(&mutex);
gettimeofday(&TimeStampStop, NULL);
ExeTime = (double)(TimeStampStop.tv_sec - TimeStampStart.tv_sec) +
(double)(TimeStampStop.tv_usec - TimeStampStart.tv_usec) * 1e-6;
// 释放内存
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;
}