#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include…
- User
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <omp.h>
#define MAX_WORD_LENGTH 20 #define NUM_BUCKETS 100
typedef struct { char *word; unsigned int count; } WordInfo;
unsigned int hash(const char *str) { unsigned int hash = 0; while (*str) { hash = (hash * 31) + (str++); } return hash; }
WordInfo find_word(const char *word, WordInfo *table) { unsigned int index = hash(word) % NUM_BUCKETS; WordInfo *p = &table[index];
while (p->word != NULL && strcmp(p->word, word) != 0) { p++; } return p;}
void count_words(const char *filename, WordInfo *table, int num_threads) { FILE *file = fopen(filename, “r”); if (file == NULL) { printf(“Error: Unable to open file %s\n”, filename); exit(EXIT_FAILURE); }
int i; char word[MAX_WORD_LENGTH]; #pragma omp parallel num_threads(num_threads) private(word) { while (fscanf(file, “%s”, word) != EOF) { for (i = 0; i < strlen(word); i++) { word[i] = tolower(word[i]); } WordInfo entry = find_word(word, table); if (entry->word == NULL) { entry->word = (char)malloc(sizeof(char) * (strlen(word) + 1)); strcpy(entry->word, word); } entry->count++; } } fclose(file);}
void print_table(WordInfo *table) { int i; for (i = 0; i < NUM_BUCKETS; i++) { if (table[i].word != NULL) { printf(“%s: %d\n”, table[i].word, table[i].count); } } }
void free_table(WordInfo *table) { int i; for (i = 0; i < NUM_BUCKETS; i++) { if (table[i].word != NULL) { free(table[i].word); } } }
int main() { WordInfo table[NUM_BUCKETS] = {{NULL, 0}}; int num_threads = 4;
count_words(“example.txt”, table, num_threads); print_table(table); free_table(table); return 0;}
在这个示例程序中,我们使用OpenMP来并行处理文件中单词的出现次数。我们使用一个哈希表来存储每个单词和它出现的次数。我们使用fscanf函数从文件中读取单词,从而忽略了文本中的空格和其他分隔符。在处理单词之前,我们将其转换为小写字母,并在哈希表中查找它。如果单词不在哈希表中,我们为它分配内存,并将计数器初始化为1。如果单词已经在哈希表中,则只需将计数器递增。
在主函数中,我们启动多个线程处理输入文件。我们使用指令#pragma omp parallel启动多个线程,并将读取文件和计数任务分配给这些线程。通过使用多个线程,我们可以更快地处理大型文本文件。
你可以修改参数num_threads来控制使用的线程数以及处理速度。 不用哈希表可以吗 是的,通过使用OpenMP的reduction指令,可以实现不使用哈希表来计算文本单词出现频率。以下是一个示例程序:
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <omp.h>
#define MAX_WORD_LENGTH 20
void count_words(const char *filename, int num_threads) { FILE *file = fopen(filename, “r”); if (file == NULL) { printf(“Error: Unable to open file %s\n”, filename); exit(EXIT_FAILURE); }
int i, count = 0; char word[MAX_WORD_LENGTH]; #pragma omp parallel num_threads(num_threads) private(word) reduction(+:count) { while (fscanf(file, “%s”, word) != EOF) { for (i = 0; i < strlen(word); i++) { word[i] = tolower(word[i]); } count++; } } fclose(file); printf(“Total words: %d\n”, count); - Assistant
- User
为什么一直使用指针?不使用可以吗/
- Assistant
在示例程序中使用指针是为了更好的内存管理和处理效率。因为我们需要动态地分配内存来存储每个单词和它的出现次数,并且我们需要在哈希表中快速查找单词并更新它的计数器。使用指针可以更轻松地实现这些功能,并且能够更有效地利用内存。
我们也可以使用其他方法来实现单词计数的功能,比如不使用指针来存储单词及其计数器,而是使用数组或结构体来存储它们。然而,这种方法可能会增加处理复杂性,并且可能需要更多的内存来存储数据。因此,在处理大型文本文件时,使用指针来进行内存管理和数据访问可能是更好的选择。
- User
好吧,输出上一个代码的串行版本
- Assistant
下面是一个串行版本的代码示例,用于计算输入文件中每个单词的出现频率,并按照频率从高到低的顺序输出单词列表。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define MAX_WORD_LENGTH 20 #define NUM_BUCKETS 100 typedef struct { char *word; unsigned int count; } WordInfo; unsigned int hash(const char *str) { unsigned int hash = 0; while (*str) { hash = (hash * 31) + (*str++); } return hash; } WordInfo find_word(const char *word, WordInfo *table) { unsigned int index = hash(word) % NUM_BUCKETS; WordInfo *p = &table[index]; while (p->word != NULL && strcmp(p->word, word) != 0) { p++; } return *p; } void count_words(const char *filename, WordInfo *table) { FILE *file = fopen(filename, "r"); if (file == NULL) { printf("Error: Unable to open file %s\n", filename); exit(EXIT_FAILURE); } int i; char word[MAX_WORD_LENGTH]; while (fscanf(file, "%s", word) != EOF) { for (i = 0; i < strlen(word); i++) { word[i] = tolower(word[i]); } WordInfo entry = find_word(word, table); if (entry.word == NULL) { entry.word = (char *)malloc(sizeof(char) * (strlen(word) + 1)); strcpy(entry.word, word); entry.count = 1; } else { entry.count++; } } fclose(file); } int cmpfunc(const void *a, const void *b) { WordInfo w1 = *(WordInfo *)a; WordInfo w2 = *(WordInfo *)b; if (w1.count < w2.count) return 1; else if (w1.count > w2.count) return -1; else return 0; } void print_table(WordInfo *table) { int i; WordInfo sorted_table[NUM_BUCKETS]; memcpy(sorted_table, table, sizeof(WordInfo) * NUM_BUCKETS); qsort(sorted_table, NUM_BUCKETS, sizeof(WordInfo), cmpfunc); for (i = 0; i < NUM_BUCKETS; i++) { if (sorted_table[i].word != NULL) { printf("%s: %d\n", sorted_table[i].word, sorted_table[i].count); } } } void free_table(WordInfo *table) { int i; for (i = 0; i < NUM_BUCKETS; i++) { if (table[i].word != NULL) { free(table[i].word); } } } int main()