Heterogeneity in Parallel and Distributed Systems
Heterogeneity in Parallel and Distributed Systems
Systems
#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<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.).
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}};
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.
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.
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>
if (cols1 != rows2) {
std::cerr << "Error: Matrix dimensions mismatch\n";
return std::vector<std::vector<T>>();
}
return result;
}
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}};
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;
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;
}