联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2020-11-08 08:04

DUE Before: Before the end of your lab session

Page 1 of 5

CSE-381: Systems 2

Exercise #12

Max Points: 40

Objective: The objective of this exercise is to:

• Practice for Exam #2

• Self-assess your preparedness on concepts and programming skills for Exam #2

• Build self-confidence and skills for technical part of job interviews

Submission: This exercise consists must be submitted separately:

• Programming part – submit via the Canvas CODE plug-in.

Have an exam like setting for yourself in this exercise. Only when you are really stuck, seek

help from your instructor or TA.

When working on these programming problems try to stick to information

included in CommonMethodsAndCommands.pdf on Canvas. This will help

you prepare for the exam and importantly for interviews.

Quick review

Estimated time to complete: < 15 minutes

It would be helpful for you to review the following video that

summarizes key threading concepts in this exercise and Exam #2:

https://youtu.be/kOP2wr7XSiE. Please watch this video at 1.5´ or 2´

speed.

Setup

Estimated time to complete: < 5 minutes

1. Via NetBeans, on the Linux server os1.csi.miamioh.edu, create a Miami

University C++ Project named exercise12.

2. Download the supplied files and scp them to your NetBeans project folder on the server.

Of course, you can scp all of the files in one shot as shown below –

scp main.cpp exercise12.cpp os1.csi.miamioh.edu:NetBeansProject/exercise12

3. Add source files to your NetBeans project.

4. Note: You should not be modifying main.cpp (or submitting main.cpp)

DUE Before: Before the end of your lab session

Page 2 of 5

Testing

You can test your solution for each one of the questions by setting the following command-line

arguments in NetBeans as shown below:

In the command-line arguments above, change q1, to q2, q3 or q4 for testing the other

questions.

Q1: Coordinating threads (sleep-wake up)

Estimated time to complete: 15 minutes

The threadMain method is called on many threads, with id (i.e., 0, 1, 2, …) indicating the

logical number of the thread. The method has been implemented to print data in order of id

value, as shown in the adjacent sample output. Rewrite the method to use a sleep-wakeup approach

(instead of the busy-wait) by modifying

the starter code below. [10 points]




// Assume all necessary headers are included.

using namespace std;

// Add global variables as needed.


void printInOrder(const int id, const std::string data) {

// Of course, you should be typing the solution in

// exercise1.cpp. Testing it to ensure it works correctly.

// Of course, you can always upload your complete/partial

// solutions to the CODE plug-in for extra testing.

}

id 0: data 0

id 1: data 1

id 2: data 2

void printInOrder (int id, std::string data) {

static std::atomic<int> turn;

// Wait for my turn

while (turn != id) {}

std::cout << "id " << id

<< ": " << data << std::endl;

turn++; // Next thread's turn

}

Expected output:

$ ./exercise12 q1 3

id 0: q1: 0

id 1: q1: 1

id 2: q1: 2

id 0: data 0

id 1: data 1

id 2: data 2

In a 300-level course full points are reserved for concise/precise solutions.

DUE Before: Before the end of your lab session

Page 3 of 5

Q2: Working with unordered_map

Estimated time to complete: 20 minutes

The following method printMTprocs is given a map of

thread-ids or TID (key in the map) and the corresponding

process-id or PID (value in the map). The method must

suitably process the given tidPid map and print all

processes with at least n (i.e., >= n) threads. For example,

given the adjacent map and n==2, the printMTprocs

method should generate the adjacent example output. Note:

This method does not require multithreading. [10 points]

// Assume all necessary headers are included.

using namespace std;

// Add global variables as needed

using IntIntMap = std::unordered_map<int, int>;

// Add global variables or

// helper methods as needed.

void printMTprocs(const IntIntMap& tidPid, const int n) {

// Implement this method

}

TID PID Expected output

2375 15 (order may vary):

375 5474

$ ./exercise12 q2 0

7936

15

$ ./exercise12 q2 50

15

27

38

3348 7936

3347 7936

3354 7936

2240 15

In a 300-level course full points are reserved for concise/precise solutions.

DUE Before: Before the end of your lab session

Page 4 of 5

Q3: Working with std::async

Estimated time to complete: 20 minutes

The following isGoodVaccine method uses 3 different algorithms (that do not use any shared

data) to detect if a vaccine is a match for the novel COVID-19 virus. Currently, it is running very

slowly. Suitably accelerate the isGoodVaccine method by programming it to use

multithreading (hint: async). [10 points]

// Assume all the necessary headers are included

using namespace std;

// Add global variables as needed

// Prototype declaration for the 3 complex algorithms

bool isSameEpitope(const std::string& strain);

bool isSimilarSeq(const std::string& strain);

bool isEquProtien(const std::string& strain);

// Multithread the following method

// to make it run faster

bool isGoodVaccine(const std::string& virus) {

return isSameEpitope(virus) || isSimilarSeq(virus) ||

isEquProtien(virus);

}

Expected outputs:

$ ./exercise12 q3 10

isGoodVaccine = true

$ ./exercise12 q3 11

isGoodVaccine = false

bool isGoodVaccine(const std::string& virus) {

// return isSameEpitope(virus) || isSimilarSeq(virus) ||

// isEquProtien(virus);

}

In a 300-level course full points are reserved for concise/precise solutions.

DUE Before: Before the end of your lab session

Page 5 of 5

Q4: Data parallel multithreading via reduction

Estimated time to complete: 20 minutes

Convert the adjacent sum method to run as a data

parallel multithreaded program using k threads. [10

points]

// Assume all necessary headers are included.

using namespace std;

using IntVec = std::vector<int>;

// Add global variables or

// helper methods as needed.

int sum(const IntVec& nums, int k) {


// Implement this method to run as a data parallel multithreaded

// program using k threads.

}

Submit to Canvas

Estimated time to complete: 5 minutes

This exercise must be turned-in electronically via Canvas CODE plug-in. Ensure your program

compiles (without any warnings or style errors) successfully. Ensure you have tested operations of

your program as indicated. Once you have tested your implementation, upload the following onto

Canvas:

Ø The 1 C++ source file exercise12.cpp you modified for this exercise.

Ensure you actually complete the submission by clicking the button on Canvas

CODE plug-in.

using IntVec = std::vector<int>;

int sum(const IntVec& nums, int k) {

sum = 0;

for(auto i : nums) {

sum += i;

}

return sum;

}

Expected output:

$ ./exercise12 q4 5

sum = 112492500

$ ./exercise12 q4 10

sum = 449985000

In a 300-level course full points are reserved for concise/precise solutions.


版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。 站长地图

python代写
微信客服:codinghelp