并行计算上机代码
Posted solvit
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了并行计算上机代码相关的知识,希望对你有一定的参考价值。
多线程求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 ", 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 2 typedef struct Args { long n; long begin; long step; } Args; typedef struct Matrix{ double a[maxn][maxn]; /* void init(){ for(int i=0;i<maxn;i++){ for(int j=0;j<maxn;j++){ a[i][j]=0.0; } } for(int i=0;i<maxn;i++){ a[i][i] = 1.0; } }*/ } Matrix; Matrix res, A; void *cal(void *arg); void output(Matrix a); Matrix mul(Matrix a, Matrix b); Matrix qpow(Matrix a, int n); int main(int argc, char *argv[]) { long i; long n = atol(argv[1]); int np = atoi(argv[2]); Args *arg; pthread_t *pid; pid = (pthread_t *)malloc(np * sizeof(pthread_t)); //res.init(); for(int i=0;i<maxn;i++){ for(int j=0;j<maxn;j++){ res.a[i][j]=0.0; } } for(int i=0;i<maxn;i++){ res.a[i][i] = 1.0; } // for(int i=0;i<maxn;i++){ for(int j=0;j<maxn;j++){ scanf("%lf", &A.a[i][j]); } } 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); } output(res); free(pid); return 0; } void *cal(void *_arg) { long i; Matrix val; Args *arg = (Args *)_arg; //val.init(); for(int i=0;i<maxn;i++){ for(int j=0;j<maxn;j++){ val.a[i][j]=0.0; } } for(int i=0;i<maxn;i++){ val.a[i][i] = 1.0; } for (i = arg->begin; i <= arg->n; i += arg->step) { val = mul(val, A); } res = mul(res, val); return NULL; } Matrix mul(Matrix a, Matrix b){ Matrix ans; for(int i=0;i<maxn;i++){ for(int j=0;j<maxn;j++){ ans.a[i][j] = 0; for(int k=0;k<maxn;k++){ ans.a[i][j] += a.a[i][k] * b.a[k][j]; } } } return ans; } Matrix qpow(Matrix a, int n){ Matrix ans; //ans.init(); for(int i=0;i<maxn;i++){ for(int j=0;j<maxn;j++){ ans.a[i][j]=0.0; } } for(int i=0;i<maxn;i++){ ans.a[i][i] = 1.0; } while(n > 0){ if(n&1) ans = mul(ans, a); a = mul(a, a); n >>= 1; } return ans; } void output(Matrix a){ for(int i=0;i<maxn;++i){ for(int j=0;j<maxn;++j){ printf("%lf ", a.a[i][j]); } puts(""); } }
以上是关于并行计算上机代码的主要内容,如果未能解决你的问题,请参考以下文章
[工作积累] UE4 并行渲染的同步 - Sync between FParallelCommandListSet & FRHICommandListImmediate calls(代码片段