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

PDC Exp-2

The document provides a C source code implementation of matrix-vector multiplication using OpenMP for parallelization. It includes functions to allocate memory for a matrix and a vector, initialize them with sample values, perform the multiplication, and print the resulting vector. Memory allocated for the matrix, vector, and result is also freed at the end of the program.

Uploaded by

sebsatq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

PDC Exp-2

The document provides a C source code implementation of matrix-vector multiplication using OpenMP for parallelization. It includes functions to allocate memory for a matrix and a vector, initialize them with sample values, perform the multiplication, and print the resulting vector. Memory allocated for the matrix, vector, and result is also freed at the end of the program.

Uploaded by

sebsatq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

AIM: Matrix-Vector Multiplication: Implement matrix-vector multiplication

using OpenMP to parallelize row-wise operations.

SOURCE CODE:

#include <stdio.h>

#include <stdlib.h>

#include <omp.h>

void matrix_vector_multiply(double** A, double* v, double* result, int m,


int n) {

// Parallelize the loop over the rows of the matrix

#pragma omp parallel for

for (int i = 0; i < m; i++) {

result[i] = 0.0;

for (int j = 0; j < n; j++) {

result[i] += A[i][j] * v[j];

int main() {

int m = 4; // Number of rows of matrix A

int n = 3; // Number of columns of matrix A (and size of vector v)

// Allocate memory for matrix A and vector v

double** A = (double**)malloc(m * sizeof(double*));

for (int i = 0; i < m; i++) {

A[i] = (double*)malloc(n * sizeof(double));

double* v = (double*)malloc(n * sizeof(double));


double* result = (double*)malloc(m * sizeof(double));

// Initialize matrix A

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

A[i][j] = i + j + 1.0; // Sample values

// Initialize vector v

for (int i = 0; i < n; i++) {

v[i] = i + 1.0; // Sample values

// Perform matrix-vector multiplication

matrix_vector_multiply(A, v, result, m, n);

// Print the result

printf("Resulting vector:\n");

for (int i = 0; i < m; i++) {

printf("result[%d] = %f\n", i, result[i]);

// Free allocated memory

for (int i = 0; i < m; i++) {

free(A[i]);

free(A);

free(v);
free(result);

return 0;

output:

You might also like