OpenMP并行计算矩阵向量相乘

程序计算随机生成的矩阵与向量相乘
函数f():单线程计算
函数fp():多线程并行计算


#include 
#include 
#include 
#include 

const int NUM_THREADS = 20;
typedef long long ll;
int m = 10000, n = 10000;
int mat[10000][10000];
int vec[10000], ans[10000];

void makeRandomMatrix()
{
    srand(time(NULL));
    int i, j;
    for (i = 0; i < m; i++)
    {
        for (j = 0; j < n; j++)
        {
            mat[i][j] = rand() % 10 + 1;
        }
    }
}

void makeRandomVector()
{
    srand(time(NULL));
    int i;
    for (i = 0; i < n; i++)
    {
        vec[i] = rand() % 10 + 1;
    }
}

void funy(int a[], int cur)
{
    int i;
    for (i = 0; i < n; i++)
    {
        ans[cur] += a[i] * vec[i];
    }
}

void f()
{
    int i;
    for (i = 0; i < m; i++)
    {
        funy(mat[i], i);
    }
}

void fp()
{
    int i;
    #pragma omp parallel
    {
        int id = omp_get_thread_num();
        #pragma omp parallel for
        for (i = id; i < m; i += NUM_THREADS)
        {
            funy(mat[i], i);
        }
    }
}

int main()
{
    printf("Makeing matrix(10000*10000) & vector(10000*1)...\n");
    makeRandomMatrix(); 
    makeRandomVector();
    double start_time = omp_get_wtime();
    f();
    // for (int i = 0; i < m; i ++) printf("%d%c\n", ans[i], ' \n'[i==n-1]);
    double end_time = omp_get_wtime();
    printf("1 thread --- Running time=%f s\n", end_time - start_time);
    start_time = omp_get_wtime();
    fp();
    end_time = omp_get_wtime();
    // for (int i = 0; i < m; i ++) printf("%d%c\n", ans[i], ' \n'[i==n-1]);
    printf("20 threads --- Running time=%f s\n", end_time - start_time);
    return 0;
}

你可能感兴趣的:(高性能计算,openmp)