Assignment 2: Building A Task Execution Library from the Ground Up

https://github.com/stanford-cs149/asst2/tree/master
在这里插入图片描述

sudo apt install gcc-arm-linux-gnueabi

python3 ../tests/run_test_harness.py

由于我c++代码能力弱,这作业做了三天三夜,但是主要难点是没接触过这种在头文件中定义,需要同步更新cpp和h两个文件的代码

结果

在这里插入图片描述

在这里插入图片描述

代码

part_a/tasksys.cpp

#include "tasksys.h"
#include <mutex>

IRunnable::~IRunnable() {
   
   }

ITaskSystem::ITaskSystem(int num_threads) {
   
   }
ITaskSystem::~ITaskSystem() {
   
   }

/*
 * ================================================================
 * Serial task system implementation
 * ================================================================
 */

const char* TaskSystemSerial::name() {
   
   
    return "Serial";
}

TaskSystemSerial::TaskSystemSerial(int num_threads): ITaskSystem(num_threads) {
   
   
}

TaskSystemSerial::~TaskSystemSerial() {
   
   }

void TaskSystemSerial::run(IRunnable* runnable, int num_total_tasks) {
   
   
    for (int i = 0; i < num_total_tasks; i++) {
   
   
        runnable->runTask(i, num_total_tasks);
    }
}

TaskID TaskSystemSerial::runAsyncWithDeps(IRunnable* runnable, int num_total_tasks,
                                          const std::vector<TaskID>& deps) {
   
   
    // You do not need to implement this method.
    return 0;
}

void TaskSystemSerial::sync() {
   
   
    // You do not need to implement this method.
    return;
}

/*
 * ================================================================
 * Parallel Task System Implementation
 * ================================================================
 */

const char* TaskSystemParallelSpawn::name() {
   
   
    return "Parallel + Always Spawn";
}

TaskSystemParallelSpawn::TaskSystemParallelSpawn(int num_threads): ITaskSystem(num_threads) {
   
   
    //
    // TODO: CS149 student implementations may decide to perform setup
    // operations (such as thread pool construction) here.
    // Implementations are free to add new class member variables
    // (requiring changes to tasksys.h).
    //
    this -> num_threads_ = num_threads;
    threads_pool_ = new std::thread[num_threads];
}

TaskSystemParallelSpawn::~TaskSystemParallelSpawn() {
   
   
    delete[] threads_pool_;
}

void TaskSystemParallelSpawn::threadRun(IRunnable* runnable, int num_total_tasks, std::mutex* mtx, int* curr_task){
   
   
    int rem_task = -1;
    while(rem_task < num_total_tasks){
   
   
        mtx -> lock();
        int rem_task = *curr_task;
        (*curr_task)++;
        mtx -> unlock();
        if(rem_task >= num_total_tasks){
   
   
            break;
        }
        runnable->runTask(rem_task, num_total_tasks);
    }
}

void TaskSystemParallelSpawn::run(IRunnable* runnable, int num_total_tasks) {
   
   

    //
    // TODO: CS149 students will modify the implementation of this
    // method in Part A.  The implementation provided below runs all
    // tasks sequentially on the calling thread.
    //
    // for (int i = 0; i < num_total_tasks; i++) {
   
   
    //     runnable->runTask(i, num_total_tasks);
    // }
    std::mutex* mtx = new std::mutex();
    int* curr_task = new int;
    *curr_task = 0;
    for (int i = 0; i < num_threads_; i++){
   
   
        threads_pool_[i] = std::thread(&TaskSystemParallelSpawn::threadRun, this, runnable, num_total_tasks, mtx, curr_task);
    }
    for (int i = 0; i < num_threads_; i++){
   
   
        threads_pool_[i].join();
    }
    delete mtx;
    delete curr_task;
}

TaskID TaskSystemParallelSpawn::runAsyncWithDeps(IRunnable* runnable, int num_total_tasks,
                                                 const std::vector<TaskID>& deps) {
   
   
    // You do not need to implement this method.
    return 0;
}

void TaskSystemParallelSpawn::sync() {
   
   
    // You do not need to implement this method.
    return;
}

/*
 * ================================================================
 * Parallel Thread Pool Spinning Task System Implementation
 * ================================================================
 */

TaskState::TaskState(){
   
   
    mutex_ = new std::mutex();
    finished_ = new std::condition_variable();
    finished_mutex_ = new std::mutex();
    runnable_ = nullptr;
    finished_tasks_ = -1;
    left_tasks_ = -1;
    num_total_tasks_ = -1;
}

TaskState::~TaskState(){
   
   
    delete mutex_;
    delete finished_;
    delete finished_mutex_;
}

const char* TaskSystemParallelThreadPoolSpinning::name() {
   
   
    return "Parallel + Thread Pool + Spin";
}

TaskSystemParallelThreadPoolSpinning::TaskSystemParallelThreadPoolSpinning(int num_threads): ITaskSystem(num_threads) {
   
   
    //
    // TODO: CS149 student implementations may decide to perform setup
    // operations (such as thread pool construction) here.
    // Implementations are free to add new class member variables
    // (requiring changes to tasksys.h).
    //
    state_ = new TaskState;
    killed = false;
    threads_pool_ = new std::thread[num_threads];
    num_threads_ = num_threads;
    for(int i = 0; i < num_threads; i++){
   
   
        threads_pool_[i] = std::thread(&TaskSystemParallelThreadPoolSpinning::spinningThread, this);
    }
}

TaskSystemParallelThreadPoolSpinning::~TaskSystemParallelThreadPoolSpinning() {
   
   
    killed = true;
    for(int i = 0; i < num_threads_; i++){
   
   
        threads_pool_[i].join();
    }
    delete[] threads_pool_;
    delete state_;
}

void TaskSystemParallelThreadPoolSpinning::spinningThread(){
   
   
    int id;
    int total;
    while(true){
   
   
        if(killed) break;

        state_ -> mutex_ -> lock();
        total = state_ -> num_total_tasks_;
        id = state_ -> num_total_tasks_ - state_ -> left_tasks_;
        if (id < total)
            state_ -> left_tasks_ --;
        state_ 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值