编写OpenMP程序一些线程是生产者一些线程是消费者。在文件集合中每个生产者针对一个文件从文件中读取文本将读出的文本行插入到一个共享的队列中。消费者从队列中取出文本行并对文本行进行分词strtok函数。符号是被空白符分开的单词当消费者发现一个单词后将单词输出。体会线程安全性3利用 parallel for 指令将循环所需工作量按一定的方式分配到 no 2 个线程上执行进行两两合并最后得到一个完整
以下是一个使用OpenMP编写的生产者-消费者程序的示例代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#define MAX_LINE_LENGTH 256
// 定义一个共享的队列结构
typedef struct {
char** lines;
int size;
int capacity;
} Queue;
// 初始化队列
Queue* initQueue(int capacity) {
Queue* queue = (Queue*)malloc(sizeof(Queue));
queue->lines = (char**)malloc(sizeof(char*) * capacity);
queue->size = 0;
queue->capacity = capacity;
return queue;
}
// 入队
void enqueue(Queue* queue, char* line) {
if (queue->size < queue->capacity) {
queue->lines[queue->size] = line;
queue->size++;
}
}
// 出队
char* dequeue(Queue* queue) {
if (queue->size > 0) {
char* line = queue->lines[0];
for (int i = 0; i < queue->size - 1; i++) {
queue->lines[i] = queue->lines[i + 1];
}
queue->size--;
return line;
} else {
return NULL;
}
}
// 释放队列内存
void freeQueue(Queue* queue) {
free(queue->lines);
free(queue);
}
// 生产者函数
void producer(Queue* queue, FILE* file) {
char line[MAX_LINE_LENGTH];
while (fgets(line, MAX_LINE_LENGTH, file) != NULL) {
char* newLine = (char*)malloc(sizeof(char) * MAX_LINE_LENGTH);
strcpy(newLine, line);
#pragma omp critical
{
enqueue(queue, newLine);
}
}
}
// 消费者函数
void consumer(Queue* queue) {
char* line;
while (1) {
#pragma omp critical
{
line = dequeue(queue);
}
if (line == NULL) {
break;
}
char* word = strtok(line, " ");
while (word != NULL) {
printf("%s\n", word);
word = strtok(NULL, " ");
}
free(line);
}
}
int main() {
FILE* file1 = fopen("file1.txt", "r");
FILE* file2 = fopen("file2.txt", "r");
Queue* queue = initQueue(100);
// 创建并行区域,设置线程数为2
#pragma omp parallel num_threads(2)
{
int threadId = omp_get_thread_num();
if (threadId == 0) {
producer(queue, file1);
} else if (threadId == 1) {
producer(queue, file2);
}
}
// 创建并行区域,设置线程数为2
#pragma omp parallel num_threads(2)
{
int threadId = omp_get_thread_num();
if (threadId == 0) {
consumer(queue);
} else if (threadId == 1) {
consumer(queue);
}
}
fclose(file1);
fclose(file2);
freeQueue(queue);
return 0;
}
这个程序中,我们使用了OpenMP的#pragma omp parallel指令来创建并行区域,并通过num_threads参数来指定线程数。在第一个并行区域中,我们将生产者函数producer分配给两个线程,每个线程负责读取一个文件,并将读取到的文本行插入到共享队列中。在第二个并行区域中,我们将消费者函数consumer分配给两个线程,每个线程负责从队列中取出文本行,并进行分词操作。
为了实现线程安全性,我们使用了OpenMP的#pragma omp critical指令来保护对共享队列的访问,确保同一时间只有一个线程能够对队列进行操作。这样可以避免多个线程同时读取或写入队列而导致的竞争条件和数据不一致性问题。
对于第二个问题,按照题目要求,我们可以使用OpenMP的parallel for指令将循环的工作量按一定的方式分配给no / 2个线程来执行。具体实现如下:
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define SIZE 8
void merge(int* arr, int low, int mid, int high) {
int* temp = (int*)malloc(sizeof(int) * (high - low + 1));
int i = low;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= high) {
if (arr[i] < arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= high) {
temp[k++] = arr[j++];
}
for (int i = low; i <= high; i++) {
arr[i] = temp[i - low];
}
free(temp);
}
void mergeSort(int* arr, int low, int high) {
if (low < high) {
int mid = (low + high) / 2;
#pragma omp parallel sections
{
#pragma omp section
{
mergeSort(arr, low, mid);
}
#pragma omp section
{
mergeSort(arr, mid + 1, high);
}
}
merge(arr, low, mid, high);
}
}
int main() {
int arr[SIZE] = {5, 3, 8, 6, 2, 7, 1, 4};
#pragma omp parallel for num_threads(SIZE / 2)
for (int i = 0; i < SIZE; i++) {
printf("Thread %d: %d\n", omp_get_thread_num(), arr[i]);
}
mergeSort(arr, 0, SIZE - 1);
printf("Sorted array: ");
for (int i = 0; i < SIZE; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
在这个程序中,我们使用了OpenMP的#pragma omp parallel for指令,并通过num_threads参数将循环的工作量分配给no / 2个线程来执行。这样每个线程负责处理一部分数组元素,最后进行两两合并得到一个完整的有序数据序列。
需要注意的是,为了确保有序数据序列的正确性,我们在递归的mergeSort函数中使用了#pragma omp parallel sections指令来创建并行部分。这样可以确保每个线程在递归调用时都能够按照正确的顺序执行,避免出现数据不一致的情况
原文地址: https://www.cveoy.top/t/topic/icle 著作权归作者所有。请勿转载和采集!