运用Pthread实现并行计算矩阵向量乘法


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define MAX 10000

int thread_count;  // number of threads
int m;  // row
int n;  // column

double A[MAX][MAX];  // matrix input
double x[MAX];  // vector input
double y[MAX];  // result

void* Pth_mat_vect(void* rank);

int main(int argc, char* argv[]) {
    int i, j;
    // char* path;
    char tmp;
    FILE* fpread;
    FILE* fpwrite;
    long thread;

    // printf("Please input the path/name of input file: ");
    // gets(path);
    fpread = fopen(argv[1], "r");
    if(fpread == NULL) {  
	printf("Read error!");        
	return 0;  
    }
    // fscanf(fpread, "%c", &tmp);
    tmp = getc(fpread);
    // if (tmp != 'm') {
        // printf("Read error!");        
	// return 0;
    // }
    fscanf(fpread, "%d\n", &m);
    // fscanf(fpread, "%c", &tmp);
    tmp = getc(fpread);
    // if (tmp != 'n') {
        // printf("Read error!");        
	// return 0;
    // }
    fscanf(fpread, "%d\n", &n);
    // fscanf(fpread, "%c", &tmp);
    tmp = getc(fpread);
    // if (tmp != 't') {
        // printf("Read error!");        
	// return 0;
    // }
    fscanf(fpread, "%d\n", &thread_count);
    // fscanf(fpread, "%c", &tmp);
    tmp = getc(fpread);
    // if (tmp != 'A') {
        // printf("Read error!");        
	// return 0;
    // }
    for (i = 0; i < m; i++) {
	for (j = 0; j < n; j++) {
	    if (j != n - 1)
		fscanf(fpread, "%lf ", &A[i][j]);
	    else
		fscanf(fpread, "%lf \n", &A[i][j]);
        }
    }
    // fscanf(fpread, "%c", &tmp);
    tmp = getc(fpread);
    // if (tmp != 'X') {
        // printf("Read error!");        
	// return 0;
    // }
    for (i = 0; i < n; i++) {
	fscanf(fpread, "%lf ", &x[i]);
    }
    fclose(fpread);
	    

    pthread_t* thread_handles;

    // thread_count = strtol(argv[1], NULL, 10);

    thread_handles = malloc (thread_count*sizeof(pthread_t));

    for (thread = 0; thread < thread_count; thread++) {
        pthread_create(&thread_handles[thread], NULL, Pth_mat_vect, (void*) thread);
    }

    // printf("Hello from the main thread\n");

    for (thread = 0; thread < thread_count; thread++) {
        pthread_join(thread_handles[thread], NULL);
    }

    free(thread_handles);

    fpwrite = fopen("Output.txt", "w");
    if(fpwrite == NULL) {  
	printf("Write error!");        
	return 0;  
    }
    for (i = 0; i < m; i++) {
	fprintf(fpwrite, "%f ", y[i]);
    }
    fclose(fpwrite);
    printf("%d %d %d", m, n, thread_count);
    for (i = 0; i < m; i++) {
	for (j = 0; j < n; j++) {
	    printf("%f ", A[i][j]);
        }
    }
    printf("\n");
    for (i = 0; i < n; i++) {
	printf("%f ", x[i]);
    }
    return 0;
}

void* Pth_mat_vect(void* rank) {
    long my_rank = (long) rank;
    int i, j;
    // consider that m cannnot be divided by thread_count exactly
    int local_m = (m % thread_count != 0) ? ((int)(m / thread_count) + 1) : (m / thread_count);
    int my_first_row = my_rank * local_m;
    // the last thread
    int my_last_row = ((my_rank + 1) * local_m - 1) > m - 1 ? (m - 1) : ((my_rank + 1) * local_m - 1);

    for (i = my_first_row; i <= my_last_row; i++) {
	y[i] = 0.0;
	for (j = 0; j < n; j++) {
	    y[i] += A[i][j] * x[j];
	}
    }

    return NULL;
}

在Linux环境下,使用以下命令编译生成可执行文件

gcc 文件名.c -o 文件名

测试命令如下

./文件名 sample_input.txt

  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值