并行算法与GPU编程备忘录

本文详述了使用Pthread进行多线程编程的实践,包括图像翻转、生产者消费者模型(涉及信号量和条件变量)以及计算圆周率的多种方法。接着,深入探讨CUDA编程,通过实例演示了矢量求和、矩阵转置和规约计算的核函数实现,同时对比了不同方法的优劣。
摘要由CSDN通过智能技术生成

Pthread多线程

图像翻转

因为代码太多了,因此只放线程函数核心代码

int n;  // 水平翻转时 n为ip.Vpixels 垂直翻转时为ip.Hpixels  
int rank = 1;  // 当前为第1个线程
int cur_n = n / thread_count;  //当前线程所需要处理的行/列数
int start = rank * cur_n;  // 从哪一行/列开始处理
int end = start + cur_n;  // 到哪一行/列处理完毕
// 垂直翻转(上下)
for(int col = start;col<end;col+=3){
   
    for(int row = 0;row<ip.Vpixels/2;row++){
   
        swap( img[row][col], img[ip.Vpixels - row -1][col] );
        swap( img[row][col+1], img[ip.Vpixels - row -1][col+1] );
        swap( img[row][col+2], img[ip.Vpixels - row -1][col+2] );
    }
}

// 水平翻转(左右)
for(row = start;row<end;row++){
   
    for(col = 0;col<ip.Hpixels*3/2;col+=3){
   
        swap( img[row][col], img[row][ip.Hpixel*3 - col -3] );
        swap( img[row][col+1], img[row][ip.Hpixel*3 - col -2] );
        swap( img[row][col+2], img[row][ip.Hpixel*3 - col -1] );

    }
}

生产者消费者

信号量
int t = 0;  // 消费者减t,生产者加t
sem_t empty,full;  // 信号量
pthread_mutex_t mutex;  // 互斥量

int main(){
   
    pthread_t producer;
    pthread_t consumer;
    // 初始化互斥量
    pthread_mutex_init(&mutex,NULL);  
    // 初始化信号量
    // 第2个参数代表几个进程共享该互斥量,0代表多线程共享
    sem_init(empty,0,10);  // empty信号量为10
    sem_init(full,0,0);  // full信号量为0
    // 生产者消费者函数内循环之间的时间间隔
    int producer_time = 2;
    int consumer_time = 3;
    // 创建线程
    pthread_create(
        &producer,  // 指明哪个线程
        NULL,
        producer_function,  // 线程执行什么函数
        &producer_time  // 传给函数的参数
    );
    pthread_create(
        &consumer,
        NULL,
        consumer_function,
        &consumer_time
    );
    // 等待线程结束
    pthread_join(producer,NULL);
    pthread_join(consumer,NULL);
  	// 销毁互斥量
    pthread_mutex_destroy(&mutex);
    return 0;
}

void* producer_function(void *time){
   
    int time = *(int*) time;  // 时间间隔
    while(true){
   
        sem_wait(&empty);  // empty信号量减1
        pthread_mutex_lock(&mutex);  // 加锁
        t++;  // 生产
        printf("生产");  // 随便写个printf意思意思
        pthread_mutex_unlock(&mutex);  // 解锁
        sem_post(&full);  //full信号量加1
        sleep(time);
    }
}

void* consumer_function(void *time){
   
    int time = *(int*) time;
    while(true){
   
        sem_wait(&full);
        pthread_mutex_lock(&mutex);
        t--;
        printf("消费");
        pthread_mutex_unlock(&mutex);
        sem_post(&empty);
        sleep(time);
    }
}
条件变量
int t = 0;  // 消费者减t,生产者加t
pthread_mutex_t mutex;  // 互斥量
pthread_cond_t cond_full;  // 非满条件变量
pthread_cond_t cond_empty;  // 非空条件变量

int main(){
   
    pthread_t producer;
    pthread_t consumer;
    // 初始化互斥量
    pthread_mutex_init(&mutex,NULL);  
    // 初始化条件变量
    pthread_cond_init(&cond_full,NULL);
    pthread_cond_init(&cond_empty,NULL);
    // 创建线程
    pthread_create(
        &producer,  // 指明哪个线程
        NULL,
        producer_function,  // 线程执行什么函数
        NULL  // 传给函数的参数
    );
    pthread_create(
        &consumer,
        NULL,
        consumer_function,
        NULL
    );
    // 等待线程结束
    pthread_join(producer,NULL);
    pthread_join(consumer,NULL);
  	// 销毁
    pthread_mutex_destroy(&mutex)
  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值