0% found this document useful (0 votes)
30 views3 pages

Exercise 3

The document discusses matrix multiplication using OpenMP. It describes multiplying two matrices A and B to produce a result matrix M. The code defines the number of rows and columns for the input matrices, initializes the matrices with values of 1, performs the multiplication using three nested loops in parallel sections, and calculates the computation time and total execution time.

Uploaded by

Oliver Planes
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views3 pages

Exercise 3

The document discusses matrix multiplication using OpenMP. It describes multiplying two matrices A and B to produce a result matrix M. The code defines the number of rows and columns for the input matrices, initializes the matrices with values of 1, performs the multiplication using three nested loops in parallel sections, and calculates the computation time and total execution time.

Uploaded by

Oliver Planes
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Matrix multiplication with OpenMP

Oliver Planes January 2, 2012


The application calculates the result M of the multiplication of two matrices A and B. The input data are the number of rows and columns for each one of the matrices A and B. Once the sizes of the matrices are known, they are lled with 1. The parallel part of the code is the multiplication of the matrices, and the number of threads is dened. The multiplication is calculated with three loops, being private the last two of them, according to sinchronize access to the matrix M. The output of the aplication are the execution time and the calculation time (in seconds). N 1 2 4 8 16 32 64 128 256 512 1024 Execution 0.000067 0.000081 0.000076 0.000083 0.000144 0.000551 0.003750 0.029709 0.238700 2.337832 30.472405 Calculation 0.000007 0.000007 0.000009 0.000014 0.000065 0.000447 0.003553 0.029157 0.236791 2.330495 30.440672

Figure 1: Execution and calculation times for N N matrices A and B (1 thread).

Source code

#include<stdio.h> #include <math.h> #include <stdlib.h> #include <omp.h> #define P 1 int main(){ int i,j,f,c,k,NR1,NR2,NC1,NC2; double **M,**A,**B,S; double inittime,endtime,tottime,inittime2,endtime2,tottime2; printf("\nChoose number scanf("%d",&NR1); printf("\nChoose number scanf("%d",&NC1); printf("\nChoose number scanf("%d",&NR2); printf("\nChoose number scanf("%d",&NC2); of rows for matrix 1: "); of columns for matrix 1: "); of rows for matrix 2: "); of columns for matrix 2: ");

if(NC1!=NR2){ printf("\nThis multiplication cant be done\n"); exit(2);} inittime2=omp_get_wtime(); omp_set_num_threads(P); /*pointers*/ if((A=(double**)calloc(NR1, sizeof(double*)))==NULL){ printf("\n\tNo memory."); exit(1); } for(i=0;i<NR1;i++){ if(((A[i]=(double*)calloc(NC1, sizeof(double)))==NULL)){ printf("\n\tNo memory."); exit(1); } } if((B=(double**)calloc(NR2, sizeof(double*)))==NULL){ printf("\n\tNo memory."); exit(1); } for(i=0;i<NR2;i++){ if(((B[i]=(double*)calloc(NC2, sizeof(double)))==NULL)){ printf("\n\tNo memory."); exit(1); } } 2

if((M=(double**)calloc(NR1, sizeof(double*)))==NULL){ printf("\n\tNo memory."); exit(1); } for(i=0;i<NR1;i++){ if(((M[i]=(double*)calloc(NC2, sizeof(double)))==NULL)){ printf("\n\tNo memory."); exit(1); } } /*matrices definition*/ for(i=0;i<NR1;i++){ for(j=0;j<NC1;j++){ A[i][j]=1.; } } for(i=0;i<NR2;i++){ for(j=0;j<NC2;j++){ B[i][j]=1.; } } /*product*/ inittime=omp_get_wtime(); #pragma omp parallel for private(c,k) for(f=0;f<NR1;f++){ for(c=0;c<NC2;c++){ for(k=0;k<NC1;k++){M[f][c]=M[f][c]+A[f][k]*B[k][c];} } } /*time*/ endtime=omp_get_wtime(); endtime2=omp_get_wtime(); tottime=endtime-inittime; tottime2=endtime2-inittime2; /*printing result*/ printf("\n\nComputation time: %lf seconds\n",tottime); printf("Total execution time: %lf seconds\n",tottime2); return(0); }

You might also like