In: Computer Science
write a C++ Program for matrix operations which include:
1. constructor in the form-- matrix::matrix(int numRows, int numColumns)
2. Implement a setter method of the form: -- void matrix::setElement(int row, int col)
3 Implement a getter method of the form: -- float matrix::getElement(int row, int col)
4. Make a threaded version of the matrix/matrix addition method.
5. Make a threaded version of the matrix/scalar multiplication method.
6. Matrix/matrix addition methods of the form: -- matrix matrix::matrixAdd(matrix inMatrix) // non-threaded version
matrix matrix::matrixAdd_threaded(matrix inMatrix) // threaded version
7. Matrix/scalar multiplication methods of the form: -- matrix matrix::scalarMult(float scalar) // non-threaded version
matrix matrix::scalarMult_threaded(float scalar) // threaded version
8. The vector reduce operation method should be of the form:
float matrix::reduceVec()
The reduce operation must use parallelization with the following algorithm. Use eight threads to compute this. Begin by splitting the vector (a 1xM matrix with one row and M columns) into 8 parts. Compute the sum of those parts and store them. Then, launch eight threads to sum the sums. Do this until you have a single number remaining.
Summary :
Implemented Threaded and Nonthreaded method functions for Addition , scalar multiplication .
For threaded version , it spawns thread = number of rows and waits for them to return , which in turn iterate over that row and calculate the values.
###################### Cpp ######################################
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
//#include <bits/stdc++.h>
//#include <boost/algorithm/string.hpp>
#include "matrices.h"
#include <thread>
using namespace std;
Matrix::Matrix(int iR, int iC) {
this->nR = iR;
nC = iC;
values = new int* [this->nR];
for (int i = 0; i < this->nR; i++) {
values[i] = new int[nC];
}
}
void Matrix::setElement(int row, int col, float iValue) {
values[row][col] = iValue;
}
float Matrix::getElement(int row, int col) {
return values[row][col];
}
Matrix* Matrix::matrixAdd(Matrix& matrx) {
Matrix* m1 = new Matrix(this->nR, this->nC);
for (int i = 0; i < this->nR; i++) {
for (int j = 0; j < this->nC;
j++) {
m1->setElement(i, j, (this->getElement(i, j) +
matrx.getElement(i, j)));
}
}
return m1;
}
void Matrix::addValues(Matrix* rM, Matrix* aM, int rVal) {
for (int k = 0; k < nC; k++) {
rM->setElement(rVal, k,
(values[rVal][k] + aM->getElement(rVal, k)));
}
return;
}
Matrix* Matrix::matrixAdd_threaded(Matrix* matrx) {
Matrix* m1 = new Matrix(this->nR,
this->nC);
vector<std::thread> th;
for (int cR = 0; cR < this->nR; cR++) {
th.push_back(std::thread([=] {
addValues(m1, matrx, cR); }));
}
for (auto it = th.begin(); it != th.end(); it++)
{
it->join();
}
return m1;
}
void Matrix::Scalar_multiply(Matrix* rM, float sVal, int cR)
{
for (int k = 0; k < this->nC; k++) {
rM->setElement(cR, k, (sVal *
(values[cR][k])));
}
return;
}
Matrix* Matrix::scalarMult_threaded(float sVal) {
Matrix* m1 = new Matrix(this->nR,
this->nC);
vector<std::thread> th;
for (int cR = 0; cR < this->nR; cR++) {
th.push_back(std::thread([=] {
Scalar_multiply(m1, sVal, cR); }));
}
for (auto it = th.begin(); it != th.end(); it++)
{
it->join();
}
return m1;
}
Matrix* Matrix::scalarMult(float x)
{
Matrix* m1 = new Matrix(this->nR, this->nC);
for (int i = 0; i < this->nR; i++) {
for (int j = 0; j < this->nC;
j++) {
m1->setElement(i, j, (this->getElement(i, j) * x));
}
}
return m1;
}
void Matrix::free_values() {
for (int i = 0; i < this->nR; i++)
delete[] values[i];
delete[] values;
}
Matrix::~Matrix() {
free_values();
}
std::ostream& operator<<(std::ostream& output,
const Matrix& M) {
output << M.nR << "X" << M.nC
<< "\n";
for (int i = 0; i < M.nR; i++) {
for (int j = 0; j < M.nC - 1;
j++) {
output <<
M.values[i][j] << "-";
}
output << M.values[i][M.nC -
1] << "\n";
}
return output;
}
std::istream& operator>> (std::istream& is,
Matrix& M) {
char c;
is >> M.nR >> c >> M.nC;
M.values = new int* [M.nR];
for (int i = 0; i < M.nR; i++) {
M.values[i] = new int[M.nC];
}
for (int i = 0; i < M.nR; i++) {
for (int j = 0; j < M.nC - 1;
j++) {
is >>
M.values[i][j] >> c;
}
is >> M.values[i][M.nC -
1];
}
return is;
}
int main() {
std::istringstream iss1("2X3\n3-4-3\n5-6-3\n");
std::istringstream iss2("2X3\n6-3-4\n5-5-6\n");
std::istream is1(iss1.rdbuf());
std::istream is2(iss2.rdbuf());
Matrix c1(2, 3), c2(2, 3);
is1 >> c1; //enter 4,2,4,2
is2 >> c2;
std::cout << " Matrix c1 : \n" <<
c1;
std::cout << " Matrix c2 : \n" << c2;
Matrix* cadd = c1.matrixAdd(c2);
std::cout << " Result matrix of c1 + c2 : \n"
<< *cadd;
Matrix* cs = c2.scalarMult(2.0);
std::cout << " After c2.calarMult(2) \n"
<< *cs;
Matrix* cadt = c1.matrixAdd_threaded(&c2);
std::cout << " Result matrixT of c1 + c2 : \n" << *cadt;
Matrix* sM =
cadt->scalarMult_threaded(3.0);
std::cout << " After Scalar multiplication with
3.0 \n" << *sM;
}
############################ End #################################
########################### Header File #########################
#include <iostream>
using namespace std;
class Matrix
{
//the private data members
private :
int **values ;
int nR , nC ;
public:
Matrix(int, int);
void setElement(int row, int col , float
iValue);
float getElement(int row , int col);
Matrix* matrixAdd( Matrix &matrx ) ;
Matrix* matrixAdd_threaded( Matrix *matrx )
;
void Scalar_multiply( Matrix *rM, float sVal,
int cR);
Matrix* scalarMult_threaded(float sVal);
void addValues(Matrix *rM , Matrix *aM, int rN
);
Matrix* scalarMult(float x ) ;
float reduceVec() ;
friend std::ostream& operator<<( std::ostream
&output, const Matrix &M ) ;
friend std::istream& operator>> (std::istream& is,
Matrix& M);
void free_values() ;
//void operator()();
~Matrix();
};
###############################################################
#########################Output #############################
##################################################################