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.

final push of code

parent 78c1712b
File added
{
"files.associations": {
"*.py": "python",
"iostream": "cpp"
}
}
\ No newline at end of file
......@@ -7,27 +7,27 @@ using namespace std;
// at https://www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/
// and parallel functionality was added
const int MAX = 2147483647;
const int ARR_SIZE = 100000;
const uint64_t MAX = 2147483647;
const uint64_t ARR_SIZE = 5000;
int MatrixChainOrder(int p[], int n)
uint64_t MatrixChainOrder(uint64_t p[], uint64_t n)
{
int m[n][n];
uint64_t m[n][n];
// dummy row and column, can be ignored
for (int i = 1; i < n; i++)
for (uint64_t 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 (uint64_t len = 2; len < n; len++)
{
for (int i = 1; i < n - len + 1; i++)
for (uint64_t i = 1; i < n - len + 1; i++)
{
int j = i + len - 1;
uint64_t j = i + len - 1;
m[i][j] = MAX;
for (int k = i; k <= j - 1; k++)
for (uint64_t k = i; k <= j - 1; k++)
{
// q = cost / scalar mult.
int q = m[i][k] + m[k + 1][j] +
uint64_t q = m[i][k] + m[k + 1][j] +
p[i - 1] * p[k] * p[j];
if (q < m[i][j])
m[i][j] = q;
......
#include "functions.hpp"
#include <iostream>
#include <stdlib.h>
#include<time.h>
#include <chrono>
#include <mpi.h>
#include <omp.h>
using namespace std;
using namespace chrono;
const int MAX = 2147483647;
const int ARR_SIZE = 100;
int main(int argc, char* argv[])
{
srand(time(0));
......@@ -10,20 +17,37 @@ int main(int argc, char* argv[])
for(int i = 0; i < ARR_SIZE; i++) {
arr[i] = (rand() % 10 + 1) * 10;
}
int size = sizeof(arr) / sizeof(arr[0]);
int size = ARR_SIZE;
cout << "Starting execution." << endl;
auto start = high_resolution_clock::now();
int n = ARR_SIZE;
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();
// int result = MatrixChainOrder(arr, size);
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++)
{
#pragma omp parallel for
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] +
arr[i - 1] * arr[k] * arr[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
int result = m[1][n-1];
cout << "Minimum number of multiplications is "
<< result
<< endl;
......
#include "functions.hpp"
#include <iostream>
#include <stdlib.h>
#include<time.h>
#include <chrono>
using namespace std;
using namespace chrono;
const int MAX = 2147483647;
const int ARR_SIZE = 100;
int main(int argc, char* argv[])
{
srand(time(0));
......@@ -9,14 +16,36 @@ int main(int argc, char* argv[])
for(int i = 0; i < ARR_SIZE; i++) {
arr[i] = (rand() % 10 + 1) * 10;
}
int size = sizeof(arr) / sizeof(arr[0]);
int size = ARR_SIZE;
cout << "Starting execution." << endl;
auto start = high_resolution_clock::now();
int result = MatrixChainOrder(arr, size);
int n = ARR_SIZE;
// int result = MatrixChainOrder(arr, size);
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] +
arr[i - 1] * arr[k] * arr[j];
if (q < m[i][j])
m[i][j] = q;
}
}
}
int result = m[1][n-1];
cout << "Minimum number of multiplications is "
<< result
<< endl;
......
No preview for this file type
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