0% found this document useful (0 votes)
139 views

Heterogeneity in Parallel and Distributed Systems

This document discusses heterogeneity in parallel and distributed systems. It provides examples of heterogeneous systems including a C++ program that performs matrix multiplication on matrices of different data types and sizes. It also includes code to calculate the area of different shapes based on user input, demonstrating heterogeneity by handling different shape calculations within the same program.

Uploaded by

sidrakhalid1357
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
139 views

Heterogeneity in Parallel and Distributed Systems

This document discusses heterogeneity in parallel and distributed systems. It provides examples of heterogeneous systems including a C++ program that performs matrix multiplication on matrices of different data types and sizes. It also includes code to calculate the area of different shapes based on user input, demonstrating heterogeneity by handling different shape calculations within the same program.

Uploaded by

sidrakhalid1357
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Heterogeneity in Parallel and Distributed

Systems

Heterogeneity in computing refers to the presence of diverse elements within a system,


particularly in terms of hardware, software, or data types. It can manifest in various ways, such
as having multiple types of processors, operating systems, programming languages, or data
formats within a single environment.
Let's create a program that performs matrix multiplication. We'll design it to handle matrices of
different sizes and data types.
Let's break down the provided C++ code into smaller sections and explain each part:

#include <iostream>
#include <vector>
These lines include necessary header files. <iostream> is included for input-output operations,
and <vector> is included to use the std::vector container.

Template Function for Matrix Multiplication

template<typename T>
std::vector<std::vector<T>> matrix_multiply(const std::vector<std::vector<T>>& mat1,
const std::vector<std::vector<T>>& mat2) {
// Function body...
}
This part defines a template function matrix_multiply for matrix multiplication. It takes two
std::vector containers of vectors, representing matrices of type T, and returns the result of their
multiplication as a new matrix. The function is templated so that it can handle matrices of any
data type (int, double, etc.).

Function to Print Matrix

template<typename T>
void print_matrix(const std::vector<std::vector<T>>& mat) {
// Function body...
}
This is another template function print_matrix, which takes a matrix as input and prints its
elements. Like matrix_multiply, it's templated to work with matrices of any data type.

Main Function
int main() {
// Example matrices
std::vector<std::vector<int>> matrix1 = {{1, 2, 3}, {4, 5, 6}};
std::vector<std::vector<int>> matrix2 = {{7, 8}, {9, 10}, {11, 12}};

// Perform matrix multiplication


auto result = matrix_multiply(matrix1, matrix2);

// Print the result


std::cout << "Result of matrix multiplication:" << std::endl;
print_matrix(result);

return 0;
}
The main function demonstrates the usage of the matrix_multiply and print_matrix functions. It
creates two example matrices (matrix1 and matrix2) and then multiplies them using
matrix_multiply. The result is stored in result and printed using print_matrix.

Explanation of Matrix Multiplication Function


The matrix_multiply function computes the product of two matrices. It follows the standard
algorithm for matrix multiplication:

It checks if the dimensions of the input matrices are compatible for multiplication.
It initializes a new matrix to store the result.
It iterates through each element of the resulting matrix, computing the dot product of the
corresponding rows and columns of the input matrices.
It returns the resulting matrix.

Explanation of Print Matrix Function

The print_matrix function simply iterates through the elements of a given matrix and prints them
to the standard output.

Full Code
#include <iostream>
#include <vector>

// Template function for matrix multiplication


template<typename T>
std::vector<std::vector<T>> matrix_multiply(const std::vector<std::vector<T>>& mat1,
const std::vector<std::vector<T>>& mat2) {
int rows1 = mat1.size();
int cols1 = mat1[0].size();
int rows2 = mat2.size();
int cols2 = mat2[0].size();

if (cols1 != rows2) {
std::cerr << "Error: Matrix dimensions mismatch\n";
return std::vector<std::vector<T>>();
}

std::vector<std::vector<T>> result(rows1, std::vector<T>(cols2));

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


for (int j = 0; j < cols2; ++j) {
for (int k = 0; k < cols1; ++k) {
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}

return result;
}

// Function to print matrix


template<typename T>
void print_matrix(const std::vector<std::vector<T>>& mat) {
for (const auto& row : mat) {
for (const auto& elem : row) {
std::cout << elem << " ";
}
std::cout << std::endl;
}
}

int main() {
// Example matrices
std::vector<std::vector<int>> matrix1 = {{1, 2, 3}, {4, 5, 6}};
std::vector<std::vector<int>> matrix2 = {{7, 8}, {9, 10}, {11, 12}};

// Perform matrix multiplication


auto result = matrix_multiply(matrix1, matrix2);

// Print the result


std::cout << "Result of matrix multiplication:" << std::endl;
print_matrix(result);

return 0;
}

Another Example:

Let's create a simple program that calculates the area of different shapes (circle, rectangle, and
triangle) based on user input. This program will demonstrate heterogeneity by handling
calculations for different shapes within the same codebase.

#include <iostream>
#include <cmath>
Using namespace std;

// Function to calculate the area of a circle


double calculate_circle_area(double radius) {
return M_PI * radius * radius;
}

// Function to calculate the area of a rectangle


double calculate_rectangle_area(double length, double width) {
return length * width;
}

// Function to calculate the area of a triangle


double calculate_triangle_area(double base, double height) {
return 0.5 * base * height;
}

int main() {
int choice;
cout << "Choose a shape to calculate area:" << endl;
cout << "1. Circle" << endl;
cout << "2. Rectangle" << endl;
cout << "3. Triangle" << endl;
cin >> choice;
double area;
switch (choice) {
case 1:
double radius;
cout << "Enter radius of the circle: ";
cin >> radius;
area = calculate_circle_area(radius);
cout << "Area of the circle: " << area << endl;
break;
case 2:
double length, width;
cout << "Enter length and width of the rectangle: ";
cin >> length >> width;
area = calculate_rectangle_area(length, width);
cout << "Area of the rectangle: " << area << endl;
break;
case 3:
double base, height;
cout << "Enter base and height of the triangle: ";
cin >> base >> height;
area = calculate_triangle_area(base, height);
cout << "Area of the triangle: " << area << endl;
break;
default:
cout << "Invalid choice!" << endl;
break;
}

return 0;
}

You might also like