📄 matrix.c
字号:
/*
This is modified code from the internet
After adjusting the memory allocation, I was able to run it under whale
OpenMP implementation of matrix multiplication.
*/
#include <omp.h>
#include <stdio.h>
#include <stdlib.h>
#define DEBUG 0
#define NRA 10 /* number of rows in matrix A */
#define NCA 10 /* number of columns in matrix A */
#define NCB 10 /* number of columns in matrix B */
int main (int argc, char *argv[])
{
int tid, nthreads, i, j, k;
double **a, **b, **c;
double *a_block, *b_block, *c_block;
double **res;
double *res_block;
double starttime, stoptime;
a = (double **) malloc(NRA*sizeof(double *)); /* matrix a to be multiplied */
b = (double **) malloc(NCA*sizeof(double *)); /* matrix b to be multiplied */
c = (double **) malloc(NRA*sizeof(double *)); /* result matrix c */
a_block = (double *) malloc(NRA*NCA*sizeof(double)); /* Storage for matrix a */
b_block = (double *) malloc(NCA*NCB*sizeof(double));
c_block = (double *) malloc(NRA*NCB*sizeof(double));
res = (double **) malloc(NRA*sizeof(double *));
res_block = (double *) malloc(NRA*NCB*sizeof(double));
for (i=0; i<NRA; i++) /* Initialize pointers to a */
a[i] = a_block+i*NRA;
for (i=0; i<NCA; i++) /* Initialize pointers to b */
b[i] = b_block+i*NCA;
for (i=0; i<NRA; i++) /* Initialize pointers to c */
c[i] = c_block+i*NRA;
for (i=0; i<NRA; i++) /* Initialize pointers to c */
res[i] = res_block+i*NRA;
/* A static allocation of the matrices would be done like this */
/* double a[NRA][NCA], b[NCA][NCB], c[NRA][NCB]; */
/*** Spawn a parallel region explicitly scoping all variables ***/
#pragma omp parallel shared(a,b,c,nthreads) private(tid,i,j,k)
{
tid = omp_get_thread_num();
if (tid == 0) {
nthreads = omp_get_num_threads();
printf("Starting matrix multiplication with %d threads\n",nthreads);
printf("Initializing matrices...\n");
}
/*** Initialize matrices ***/
#pragma omp for nowait
for (i=0; i<NRA; i++)
for (j=0; j<NCA; j++)
a[i][j]= (double) (i+j);
#pragma omp for nowait
for (i=0; i<NCA; i++)
for (j=0; j<NCB; j++)
b[i][j]= (double) (i*j);
#pragma omp for
for (i=0; i<NRA; i++)
for (j=0; j<NCB; j++)
c[i][j]= 0.0;
if (tid == 0)
starttime = omp_get_wtime(); /* Master thread measures the execution time */
/* Do matrix multiply sharing iterations on outer loop */
/* If DEBUG is TRUE display who does which iterations */
printf("Thread %d starting matrix multiply...\n",tid);
#pragma omp for nowait
for (i=0; i<NRA; i++) {
if (DEBUG) printf("Thread=%d did row=%d\n",tid,i);
for(j=0; j<NCB; j++) {
for (k=0; k<NCA; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
if (tid == 0) {
stoptime = omp_get_wtime();
printf("Time for parallel matrix multiplication: %f\n", stoptime-starttime);
}
} /*** End of parallel region ***/
printf ("Done.\n");
exit(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -