diff --git a/Concurrency/portfolio b/Concurrency/portfolio new file mode 100644 index 0000000..597beb1 Binary files /dev/null and b/Concurrency/portfolio differ diff --git a/Concurrency/portfolioSubmission.cpp b/Concurrency/portfolioSubmission.cpp new file mode 100644 index 0000000..c0bd53c --- /dev/null +++ b/Concurrency/portfolioSubmission.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int pos = 0; + +std::mutex mutx; +std::condition_variable cv; +bool ready = false; + +void printArray(std::vector array, int pos){ + + std::unique_lock lck (mutx);//critical section + while(!ready) cv.wait(lck); + + std::cout << array.at(pos) << std::endl; + std::this_thread::sleep_for(std::chrono::seconds(2)); + +} +void increasePos(){ + pos++; +} + +void start(){ + std::unique_lock lck (mutx);//critical section + ready = true; + cv.notify_all(); +} +int main(){ + + std::string line; + std::vector poemLines = {}; + std::ifstream poem("ritmo_rhythm.txt"); + std::thread t1, t2; + + + while(getline(poem, line)){ + poemLines.emplace_back(line); + } + start(); + while(pos < poemLines.size()){ + t2 = std::thread(printArray, poemLines, pos); + t2.join();//prevents unpredictable behaviour + t1 = std::thread(increasePos); + t1.join(); + } + + return 0; +} diff --git a/Concurrency/ritmo_rhythm.txt b/Concurrency/ritmo_rhythm.txt new file mode 100644 index 0000000..9f36f8f --- /dev/null +++ b/Concurrency/ritmo_rhythm.txt @@ -0,0 +1,37 @@ +Mad has decided to catch a vulture, +the biggest bird she can find. + +She is so determined, and so inventive, +that by stringing together a rickety trap +of ropes and sticks, she creates +a puzzling structure that just might +be clever enough to trick a buzzard, +once the trap’s baited with leftover pork +from supper. + +Mad and I used to do everything together, +but now I need a project all my own, +so I roam the green fields, +finding bones. + +The skull of a wild boar. +The jawbone of a mule. + +Older cousins show me +how to shake the mule’s quijada, +to make the blunt teeth +rattle. + +Guitars. +Drums. +Gourds. +Sticks. + +A cow bell. +A washboard. +Pretty soon, we have +a whole orchestra. + +On Cuban farms, even death +can turn into +music. \ No newline at end of file diff --git a/Distributed/arith b/Distributed/arith new file mode 100644 index 0000000..56db27b Binary files /dev/null and b/Distributed/arith differ diff --git a/Distributed/data b/Distributed/data new file mode 100644 index 0000000..ebaef14 Binary files /dev/null and b/Distributed/data differ diff --git a/Distributed/distribArith.cpp b/Distributed/distribArith.cpp new file mode 100644 index 0000000..dc22d9d --- /dev/null +++ b/Distributed/distribArith.cpp @@ -0,0 +1,77 @@ +#include "mpi.h" +#include +#include +#include +#include +#include + +std::list unavailableNodes = {}; +int current_val; +std::string lastUsedOp = ""; + +bool isNodeAvailable(int node){ + if (node != unavailableNodes.front() && node != unavailableNodes.back())return true; + return false; +} + +void removeNode(int node){ + unavailableNodes.pop_front(); +} +void addNode(int node){ + unavailableNodes.push_back(node); +} +int determineArith(int randNum, int numPassed){ + if (randNum < 3 && lastUsedOp != "+"){ + lastUsedOp = "+"; + current_val += numPassed; + return current_val + numPassed; + } + if (randNum < 6 && lastUsedOp != "-"){ + lastUsedOp = "-"; + current_val -= numPassed; + return current_val - numPassed; + } + if (randNum < 9 && lastUsedOp != "*"){ + lastUsedOp = "*"; + current_val *= numPassed; + return current_val * numPassed; + } +} +int determineNextNode(int currentNode, int mpiSize){ + int destination; + do{ + destination = rand()%mpiSize; + }while(!isNodeAvailable(destination)); + return destination; +} +int main(){ + + srand(time(NULL)); + MPI_Init(NULL, NULL); + char node_name[MPI_MAX_PROCESSOR_NAME]; + int rank,size, namelen; + MPI_Comm comm; + comm = MPI_COMM_WORLD; + int passNum = 6; + int current_node = 0; + + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Get_processor_name(node_name, &namelen); + + while(true){ + if(rank == 0){ + addNode(current_node); + int nextNode = determineNextNode(current_node, size); + addNode(nextNode); + int operatorDet = rand()%10; + int updatedVal = determineArith(operatorDet, passNum); + std::cout << "New value is: " << updatedVal << " on node " << nextNode << std::endl; + removeNode(current_node); + current_node = nextNode; + std::this_thread::sleep_for(std::chrono::seconds(2)); + } + } + + MPI_Finalize(); +} \ No newline at end of file diff --git a/Distributed/distribData.cpp b/Distributed/distribData.cpp new file mode 100644 index 0000000..1eeb368 --- /dev/null +++ b/Distributed/distribData.cpp @@ -0,0 +1,65 @@ +#include +#include "mpi.h" +#include +#include +#include +#include + +std::vector make_sentence(std::string s){ + std::string word = ""; + std::vector sentence; + for(int i = 0; i poemLines = {}; + std::vector jumbledUpPoem = {}; + std::ifstream poem("ritmo_rhythm.txt"); + + while(getline(poem, line)){ + poemLines.emplace_back(line); + } + + jumbledUpPoem = poemLines; + if (rank == 0){ + std::vector originalPoem = poemLines; + } + #pragma omp parallel for num_threads(1) + for (int i = 0; i < poemLines.size(); i++){ + if (rank == i){ + std::string j_line = jumbledUpPoem[i]; + //std::cout << j_line << std::endl; + std::vector sentence = make_sentence(j_line); + std::random_shuffle(sentence.begin(), sentence.end()); + for(auto word : sentence){ + std::cout << word << " "; + } + std::cout << std::endl; + } + } + MPI_Finalize(); +} \ No newline at end of file diff --git a/Distributed/distribStatus.cpp b/Distributed/distribStatus.cpp new file mode 100644 index 0000000..dfd0676 --- /dev/null +++ b/Distributed/distribStatus.cpp @@ -0,0 +1,48 @@ +#include +#include "mpi.h" +#include +#include +#include + +int main(int argc, char** argv) { + + // Initialize the MPI environment + MPI_Init(NULL, NULL); + char node_name[MPI_MAX_PROCESSOR_NAME]; + int rank,size, namelen,*buf; + MPI_File fh; + MPI_Comm comm; + MPI_Status status; + comm = MPI_COMM_WORLD; + + + MPI_Comm_rank(MPI_COMM_WORLD, &rank); + MPI_Comm_size(MPI_COMM_WORLD, &size); + MPI_Get_processor_name(node_name, &namelen); + + FILE *pPipeProc, *pPipeClock, *pPipeRAM; + pPipeProc = popen("grep 'cpu cores' /proc/cpuinfo | head -n 1", "r"); + pPipeClock = popen("grep 'cpu MHz' /proc/cpuinfo | tail -n 1", "r"); + pPipeRAM = popen("free -m | grep 'Mem'", "r"); + char bufCore[1024],bufClock[1024], bufRAM[1024]; + + fgets(bufCore, 1024, pPipeProc); + fgets(bufClock, 1024, pPipeClock); + fgets(bufRAM, 1024, pPipeRAM); + + std::cout << bufCore << std::endl; + std::cout << bufClock << std::endl; + std::cout << "RAM for node_name " << node_name << " = " << bufRAM << std::endl; + + if (rank==0) { + std::cout << "Node Count = " << size << std::endl; + + std::string sCore(bufCore); + int cores_per = sCore[12] - '0'; + + std::cout << "total cores = " << cores_per* size << std::endl; //implies single digit core number, since stoi does not work + + + } + MPI_Finalize(); +} \ No newline at end of file diff --git a/Distributed/machines b/Distributed/machines new file mode 100644 index 0000000..0551ed3 --- /dev/null +++ b/Distributed/machines @@ -0,0 +1,10 @@ +node2 +node3 +node4 +node5 +node6 +node7 +node8 +node9 +node10 +node11 diff --git a/Distributed/ritmo_rhythm.txt b/Distributed/ritmo_rhythm.txt new file mode 100644 index 0000000..9f36f8f --- /dev/null +++ b/Distributed/ritmo_rhythm.txt @@ -0,0 +1,37 @@ +Mad has decided to catch a vulture, +the biggest bird she can find. + +She is so determined, and so inventive, +that by stringing together a rickety trap +of ropes and sticks, she creates +a puzzling structure that just might +be clever enough to trick a buzzard, +once the trap’s baited with leftover pork +from supper. + +Mad and I used to do everything together, +but now I need a project all my own, +so I roam the green fields, +finding bones. + +The skull of a wild boar. +The jawbone of a mule. + +Older cousins show me +how to shake the mule’s quijada, +to make the blunt teeth +rattle. + +Guitars. +Drums. +Gourds. +Sticks. + +A cow bell. +A washboard. +Pretty soon, we have +a whole orchestra. + +On Cuban farms, even death +can turn into +music. \ No newline at end of file diff --git a/Distributed/runJob.sh b/Distributed/runJob.sh new file mode 100644 index 0000000..6884e6a --- /dev/null +++ b/Distributed/runJob.sh @@ -0,0 +1,7 @@ +#!/bin/bash +if [".$1" == ""]; then + echo "Positional paramter 1 is empty" +else +mpirun -machinefile /home/370user30/370CT/Distributed/machines --map-by ppr:4:node $1 + +fi diff --git a/Distributed/status b/Distributed/status new file mode 100644 index 0000000..779af9b Binary files /dev/null and b/Distributed/status differ diff --git a/Parallelism/parallel_portfolio.cpp b/Parallelism/parallel_portfolio.cpp new file mode 100644 index 0000000..d0d95f7 --- /dev/null +++ b/Parallelism/parallel_portfolio.cpp @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +#include +#include + +std::string determine_sched_type(){ + std::string type; + do{ + std::cout << "What type of scheduling would you like?(auto, static, dynamic, guided) "; + std::getline(std::cin,type); + if (type == "auto") return type; + if (type == "static") return type; + if (type == "dynamic") return type; + if (type == "guided") return type; + }while(true); + return type; +} +int main(){ + + std::string type_sched = determine_sched_type(); + if (type_sched == "auto") omp_set_schedule(omp_sched_auto,0); + if (type_sched == "static") omp_set_schedule(omp_sched_static,0); + if (type_sched == "dynamic") omp_set_schedule(omp_sched_dynamic,0); + if (type_sched == "guided") omp_set_schedule(omp_sched_guided,0); + + std::vector v1 = {5,14,10}; + std::vector v2 = {7,-8,-14}; + std::vector v3 = {-2,9,8}; + std::vector v4 = {15,-6,3}; + std::vector v5 = {12,4,-5}; + std::vector v6 = {4,20,17}; + std::vector v7 = {-16,5,-1}; + std::vector v8 = {-11,3,16}; + std::vector v9 = {3,10,-19}; + std::vector v10 = {-16,7,4}; + + std::cout << "Original State of Vectors:" << std::endl; + std::cout << v1.at(0) << "," << v1.at(2) << "," << v1.at(2) << std::endl; + std::cout << v2.at(0) << "," << v2.at(2) << "," << v2.at(2) << std::endl; + std::cout << v3.at(0) << "," << v3.at(2) << "," << v3.at(2) << std::endl; + std::cout << v4.at(0) << "," << v4.at(2) << "," << v4.at(2) << std::endl; + std::cout << v5.at(0) << "," << v5.at(2) << "," << v5.at(2) << std::endl; + std::cout << v6.at(0) << "," << v6.at(2) << "," << v6.at(2) << std::endl; + std::cout << v7.at(0) << "," << v7.at(2) << "," << v7.at(2) << std::endl; + std::cout << v8.at(0) << "," << v8.at(2) << "," << v8.at(2) << std::endl; + std::cout << v9.at(0) << "," << v9.at(2) << "," << v9.at(2) << std::endl; + std::cout << v10.at(0) << "," << v10.at(2) << "," << v10.at(2) << std::endl; + srand(time(NULL)); + #pragma omp parallel for schedule(runtime) + for (int z = 0; z <10; z++) + { + const auto processor_count = std::thread::hardware_concurrency(); + std::cout << "Processor Count: " << processor_count << std::endl; + + system("cat /proc/cpuinfo | grep 'cpu MHz'"); + system("free -m | grep 'Mem'"); + system("du -sh /home"); //cannot gain access to some folders due to not having sudo access + } + #pragma omp parallel for num_threads(1) + for (int step = 0; step < 5; step++) + { + + short co_ord_rand = rand()%3; + short rand_direct = rand()%100 +1; // x>50, increment. x<=50, decerement + + //if chosen to add + if (rand_direct > 50){ + v1.at(co_ord_rand)++; + v2.at(co_ord_rand)++; + v3.at(co_ord_rand)++; + v4.at(co_ord_rand)++; + v5.at(co_ord_rand)++; + v6.at(co_ord_rand)++; + v7.at(co_ord_rand)++; + v8.at(co_ord_rand)++; + v9.at(co_ord_rand)++; + v10.at(co_ord_rand)++; + }else{ + v1.at(co_ord_rand)--; + v2.at(co_ord_rand)--; + v3.at(co_ord_rand)--; + v4.at(co_ord_rand)--; + v5.at(co_ord_rand)--; + v6.at(co_ord_rand)--; + v7.at(co_ord_rand)--; + v8.at(co_ord_rand)--; + v9.at(co_ord_rand)--; + v10.at(co_ord_rand)--; + } + //questions wants 5th and 10th steps + if (step == 4){ + std::cout << "Values of vectors on step #" << step + 1 << ": " << std::endl; + std::cout << v1.at(0) << "," << v1.at(2) << "," << v1.at(2) << std::endl; + std::cout << v2.at(0) << "," << v2.at(2) << "," << v2.at(2) << std::endl; + std::cout << v3.at(0) << "," << v3.at(2) << "," << v3.at(2) << std::endl; + std::cout << v4.at(0) << "," << v4.at(2) << "," << v4.at(2) << std::endl; + std::cout << v5.at(0) << "," << v5.at(2) << "," << v5.at(2) << std::endl; + std::cout << v6.at(0) << "," << v6.at(2) << "," << v6.at(2) << std::endl; + std::cout << v7.at(0) << "," << v7.at(2) << "," << v7.at(2) << std::endl; + std::cout << v8.at(0) << "," << v8.at(2) << "," << v8.at(2) << std::endl; + std::cout << v9.at(0) << "," << v9.at(2) << "," << v9.at(2) << std::endl; + std::cout << v10.at(0) << "," << v10.at(2) << "," << v10.at(2) << std::endl; + } + } + #pragma omp parallel num_threads(1) + for (int steppt2 = 0; steppt2 <=5; steppt2++) + { + + short co_ord_rand = rand()%3; + short rand_direct = rand()%100 +1; // x>50, increment. x<=50, decerement + + //if chosen to add + if (rand_direct > 50){ + v1.at(co_ord_rand)++; + v2.at(co_ord_rand)++; + v3.at(co_ord_rand)++; + v4.at(co_ord_rand)++; + v5.at(co_ord_rand)++; + v6.at(co_ord_rand)++; + v7.at(co_ord_rand)++; + v8.at(co_ord_rand)++; + v9.at(co_ord_rand)++; + v10.at(co_ord_rand)++; + }else{ + v1.at(co_ord_rand)--; + v2.at(co_ord_rand)--; + v3.at(co_ord_rand)--; + v4.at(co_ord_rand)--; + v5.at(co_ord_rand)--; + v6.at(co_ord_rand)--; + v7.at(co_ord_rand)--; + v8.at(co_ord_rand)--; + v9.at(co_ord_rand)--; + v10.at(co_ord_rand)--; + } + //questions wants 5th and 10th steps + if (steppt2 == 5){ + std::cout << "Values of vectors on step #10: " << std::endl; + std::cout << v1.at(0) << "," << v1.at(2) << "," << v1.at(2) << std::endl; + std::cout << v2.at(0) << "," << v2.at(2) << "," << v2.at(2) << std::endl; + std::cout << v3.at(0) << "," << v3.at(2) << "," << v3.at(2) << std::endl; + std::cout << v4.at(0) << "," << v4.at(2) << "," << v4.at(2) << std::endl; + std::cout << v5.at(0) << "," << v5.at(2) << "," << v5.at(2) << std::endl; + std::cout << v6.at(0) << "," << v6.at(2) << "," << v6.at(2) << std::endl; + std::cout << v7.at(0) << "," << v7.at(2) << "," << v7.at(2) << std::endl; + std::cout << v8.at(0) << "," << v8.at(2) << "," << v8.at(2) << std::endl; + std::cout << v9.at(0) << "," << v9.at(2) << "," << v9.at(2) << std::endl; + std::cout << v10.at(0) << "," << v10.at(2) << "," << v10.at(2) << std::endl; + } + } +} \ No newline at end of file diff --git a/Parallelism/paraport b/Parallelism/paraport new file mode 100644 index 0000000..d71d18e Binary files /dev/null and b/Parallelism/paraport differ diff --git a/README.md b/README.md new file mode 100644 index 0000000..18a99f9 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# 370CT \ No newline at end of file diff --git a/gcc commands b/gcc commands new file mode 100644 index 0000000..8ba7cc8 --- /dev/null +++ b/gcc commands @@ -0,0 +1 @@ +g++ --std=c++1y filename.cpp -o exefile -pthread \ No newline at end of file