并行计算上机代码
多线程求PI:
#include <stdio.h> #include <stdlib.h> #include <pthread.h> double res; typedef struct Args { long n; long begin; long step; } Args; void *cal(void *arg); int main(int argc, char *argv[]) { long i; long n = atol(argv[1]); int np = atoi(argv[2]); Args *arg; double* val; pthread_t *pid; pid = (pthread_t *)malloc(np * sizeof(pthread_t)); res = 0.0; for (i = 0; i < np; i++) { arg = (Args *)malloc(sizeof(Args)); arg->n = n; arg->begin = 2*i+1; arg->step = np; pthread_create(&pid[i], NULL, cal, (void *)arg); } for (i = 0; i < np; i++) { pthread_join(pid[i], (void *)val); } res = res * 4.0; printf("%lf\n", res); free(pid); return 0; } void *cal(void *_arg) { long i; double val; Args *arg = (Args *)_arg; val = 0.0; for (i = arg->begin; i <= arg->n; i += 2*arg->step) { if(i % 4 == 3) val -= 1.0 / i; else val += 1.0 / i; } res += val; return NULL; }
多线程矩阵乘法:
矩阵乘法用多线程实现
幂次用快速幂实现
#include <stdio.h> #include <stdlib.h> #include <pthread.h> #define maxn 3 #define Matrix double typedef struct Args { long n; long begin; long step; } Args; Matrix res[maxn][maxn] = {{1.0, 0.0, 0.0},{0.0, 1.0, 0.0},{0.0, 0.0, 1.0}}; /*结果矩阵*/ Matrix A[maxn][maxn] = {{0.1, 0.2, 0.3}, {0.4, 0.5, 0.4}, {0.3, 0.2, 0.1}}; /*原矩阵*/ Matrix tmp[maxn][maxn]; /*中间量*/ long i, j, k; void *cal(void *arg); void *cal1(void *_arg); void output(Matrix a); int main(int argc, char *argv[]) { long n = atol(argv[1]); int np = atoi(argv[2]); Args *arg; for(i = 0; i < maxn; i++){ for(j = 0; j < maxn; j++){ printf("%.20lf ", A[i][j]); } puts(""); } puts(""); pthread_t *pid; pid = (pthread_t *)malloc(np * sizeof(pthread_t)); while(n > 0){ if(n & 1){ /* res = res * A*/ pthread_t *pid; pid = (pthread_t *)malloc(np * sizeof(pthread_t)); for(i = 0; i < maxn; i++){ for(j = 0; j < maxn; j++){ tmp[i][j] = 0.0; } } for (i = 0; i < np; i++) { arg = (Args *)malloc(sizeof(Args)); arg->n = n; arg->begin = i+1; arg->step = np; pthread_create(&pid[i], NULL, cal, (void *)arg); } for (i = 0; i < np; i++) { pthread_join(pid[i], NULL); } free(pid); // printf("tmp ********************\n"); // for(i = 0; i < maxn; i++){ // for(j = 0; j < maxn; j++){ // printf("%.20lf ", tmp[i][j]); // }puts(""); // } for(i = 0; i < maxn; i++){ for(j = 0; j < maxn; j++){ res[i][j] = tmp[i][j]; } } } /* A = A * A*/ pthread_t *pid; pid = (pthread_t *)malloc(np * sizeof(pthread_t)); for(i = 0; i < maxn; i++){ for(j = 0; j < maxn; j++){ tmp[i][j] = 0.0; } } for (i = 0; i < np; i++) { arg = (Args *)malloc(sizeof(Args)); arg->n = n; arg->begin = i+1; arg->step = np; pthread_create(&pid[i], NULL, cal1, (void *)arg); } for (i = 0; i < np; i++) { pthread_join(pid[i], NULL); } free(pid); for(i = 0; i < maxn; i++){ for(j = 0; j < maxn; j++){ A[i][j] = tmp[i][j]; } } n >>= 1; } /* for (i = 0; i < np; i++) { arg = (Args *)malloc(sizeof(Args)); arg->n = n; arg->begin = i+1; arg->step = np; pthread_create(&pid[i], NULL, cal, (void *)arg); } for (i = 0; i < np; i++) { pthread_join(pid[i], NULL); } */ for(i = 0; i < maxn; i++){ for(j = 0; j < maxn; j++){ printf("%.20lf ", res[i][j]); } puts(""); } puts(""); return 0; } void *cal(void *_arg) { long i; Args *arg = (Args *)_arg; for(i = arg->begin; i <= maxn*maxn; i += arg->step){ int row = i / maxn; if(i % maxn == 0){ row--; } int column = ((i % maxn) + maxn - 1) % maxn; for(k = 0; k < maxn; k++){ tmp[row][column] += res[row][k] * A[k][column]; } } return NULL; } void *cal1(void *_arg) { long i; Args *arg = (Args *)_arg; for(i = arg->begin; i <= maxn*maxn; i += arg->step){ int row = i / maxn; if(i % maxn == 0){ row--; } int column = ((i % maxn) + maxn - 1) % maxn; for(k = 0; k < maxn; k++){ tmp[row][column] += A[row][k] * A[k][column]; } } return NULL; }
1、标准c中的结构体内部是不可以定义函数的,c++可以
2、实验平台下不允许变量的重复定义