This server is intended for use for Academic Classwork related Git repositories only. Projects/repositories will generally be removed after 6 months following close of the semester. Inactive repositories from previous semester are now being archived when no activity for 365 days. They are renamed and marked as 'archived'. After 90 days in that state they will be removed from the system completely.

upload source code

parents
File added
#include <iostream>
#include <mpi.h>
#include <stdlib.h>
#include<time.h>
using namespace std;
// code for dynamic model was referenced from Akanksha Rai
// at https://www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/
// and parallel functionality was added
const int MAX = 2147483647;
const int ARR_SIZE = 1000;
int MatrixChainOrder(int p[], int n)
{
int m[n][n];
// dummy row and column, can be ignored
for (int i = 1; i < n; i++)
m[i][i] = 0;
// len is used to track length of the mult. chain
for (int len = 2; len < n; len++)
{
for (int i = 1; i < n - len + 1; i++)
{
int j = i + len - 1;
m[i][j] = MAX;
for (int k = i; k <= j - 1; k++)
{
// q = cost / scalar mult.
int q = m[i][k] + m[k + 1][j] +
p[i - 1] * p[k] * p[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
return m[1][n - 1];
}
#include "functions.hpp"
int main(int argc, char* argv[])
{
srand(time(0));
int arr[ARR_SIZE];
for(int i = 0; i < ARR_SIZE; i++) {
arr[i] = (rand() % 10 + 1) * 10;
}
int size = sizeof(arr) / sizeof(arr[0]);
int ierr = MPI_Init(&argc, &argv);
int procid, numprocs;
ierr = MPI_Comm_rank(MPI_COMM_WORLD, &procid);
ierr = MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
int result = MatrixChainOrder(arr, size);
ierr = MPI_Finalize();
cout << "Minimum number of multiplications is "
<< result
<< endl;
return 0;
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment